andrewabest / Conventional

A suite of convention specifications for enforcing type and style conventions in your codebase
Microsoft Public License
97 stars 29 forks source link

Assembly (project) conventions should support Directory.Build.Props & Directory.Build.Targets #88

Open flakey-bit opened 9 months ago

flakey-bit commented 9 months ago

MSBuild allows templating / "meta-programming" of csproj files via Directory.Build.Props & Directory.Build.Targets - see this page for more background but basically

At least that's my understanding - haven't used Directory.Build.Targets myself.

Existing conventions like MustNotReferenceDllsFromTransientOrSdkDirectoriesConventionSpecification that parse the project XML are unaware of this functionality and are arguably broken as a result.

For example, a Directory.Build.Props file could add an (assembly) Reference w/ a HintPath to all projects in the directory, which MustNotReferenceDllsFromTransientOrSdkDirectoriesConventionSpecification would be unaware of when examing the project XML, leading to false-negative.

flakey-bit commented 9 months ago

Ironically, Directory.Build.Props & Directory.Build.Targets files make keeping things consistent easier - however they make checking things are consistent harder 🤔

andrewabest commented 9 months ago

Good spotting @flakey-bit. Fixing this would be reasonably straight-forward. I'll either get to it eventually, or someone can pick it up, I've labelled it a good first issue if someone happens to come by and wants to contribute.

flakey-bit commented 8 months ago

Fixing this would be reasonably straight-forward

@andrewabest do you have some thoughts in mind as to how we could address this?

andrewabest commented 8 months ago

I was thinking you would essentially have to replicate what MSBuild does - for any project, locate the directory.build.props file closest to it, and consider its values in applying the convention (docs)

It can get a bit complex, but we could support the simpler version of it: don't support multiple props files, just support the nearest one per project.

flakey-bit commented 8 months ago

I think you're suggesting that we should fully evaluate the project XML and pass the "composed" csproj (i.e. post-application of Directory.Build.Props & Directory.Build.Targets) to Conventional conventions (in other words, treating it as a templating system).

If so, I agree 👍

It would be neat if we could do so by leveraging MSBuild, rather than reimplementing it. If we can find a way to do that I guess we'd get full support (multiple props files etc) "for free".

andrewabest commented 8 months ago

Yup, strong agree there. I know dotnet-affected uses MsBuild internally (example), so it is very possible that that is a doable thing.