dotnet/roslyn-sdk

Have a built-in way to add `Assembly` to analyzer/generator testing

Youssef1313 opened this issue · 2 comments

Currently, there is ReferenceAssemblies which provides base reference assemblies for various TFMs, with the ability to add NuGet packages. This is often good enough, but in some cases we want to add a reference to an assembly built by the solution.

I found that aspnetcore needed this as well, and wrote this: https://github.com/dotnet/aspnetcore/blob/f107566c2ef1c9dd648b04509249838d656ac72b/src/Shared/AnalyzerTesting/TestReferences.cs#L18

I wonder if this could be part of the testing library?

The behavior in aspnetcore doesn't match the behavior of NuGet/MSBuild for compile time, so I wouldn't want to add that in its current state. The intent of ReferenceAssemblies (and any other similar feature added directly to the SDK) would be to match the true build. As such, my expectation for implementation would be for the feature to always produce a collection of MetadataReference instances which is the same set of references one would get if they referenced the output of the project in question. It should not include any assemblies that the project brought in through NuGet packages, since these would also be brought in through the packages functionality of ReferenceAssemblies.

I might suggest here that we use MSBuild to help generate what we need. Perhaps we could have something like:

<ItemGroup>
  <MetaProjectReference Include="../MyProject/MyProject.csproj" />
</ItemGroup>

The thought here is that this can act similar to a ProjectReference in that it will ensure that the referenced project is built before the current project, however without actually adding any references to the output assemblies. The key would however be to Generate a class as part of the build that would let developers get the required references like:

var references = MetaProjectReference.MyProject.Net80;

The goal there should be that all of the resolved references for that project are included and that it can be done for any TargetFrameworks that may be included as part of MyProject