Version Used:
5.0.100-rc.1.20452.10 on linux with vscode
Steps to Reproduce:
Reference a nuget package of a Source Generator for example
dotnet add package JsonSrcGen --version 0.1.0.6-alpha
Build the consuming project
'dotnet build -c Release`
Expected Behavior:
The generator generates code but is not required at runtime and so does not appear in the output, and the consuming code does not reference the generator.
Actual Behavior:
The generator end up in the bin/Release folder, or if there is conflicting code fails to build, in my case both the generator and the generated code have the same type and so this is marked as a conflict even though the consuming code should not be referencing the generator at all. I ended up working around this by marking the type as internal.
If the generator is used as a project reference instead of a nuget package then these issues do not occur.
You can see this problem in my github project:
https://github.com/trampster/JsonSrcGen
Here is the csproj of the generator including the nuget package settings:
https://github.com/trampster/JsonSrcGen/blob/master/JsonSrcGen/JsonSrcGen.csproj
Here is the csproj of the project consuming the generator from it's nuget package:
https://github.com/trampster/JsonSrcGen/blob/master/Benchmarks/Benchmarks.csproj
If you build the Benchmarks project and then look in the output folder you will see JsonSrcGen.dll in the output, but it shouldn't be there.
I believe you need to mark your NuGet package as a development dependency. IE:
<DevelopmentDependency>true</DevelopmentDependency>
You also need to include these properties:
<IncludeBuildOutput>false</IncludeBuildOutput>
<SuppressDependenciesWhenPacking>true</SuppressDependenciesWhenPacking>
I believe you need to mark your NuGet package as a development dependency
This will may prevent the package from appearing as a declared dependency of downstream packages, but it won't otherwise change the compilation behavior of a direct consumer.
This has fixed it for me.
Is this documented anywhere?
The properties @sharwell mentioned are part of the default analyzer template from Visual Studio. (The properties themselves are documented, but their use with analyzers is not as I can tell.)
I erroneously thought DevelopmentDependency was the main thing you needed based on its usage in Roslynator, but as @sharwell pointed out it doesn't really do what you wanted (and might not even be applicable to all types of analyzers) so it probably shouldn't be documented specifically for analyzers.
Most helpful comment
You also need to include these properties:
This
willmay prevent the package from appearing as a declared dependency of downstream packages, but it won't otherwise change the compilation behavior of a direct consumer.