Roslyn: SourceGenerator from nuget package ends up in output

Created on 21 Sep 2020  路  4Comments  路  Source: dotnet/roslyn

Version Used:
5.0.100-rc.1.20452.10 on linux with vscode

Steps to Reproduce:

  1. Reference a nuget package of a Source Generator for example
    dotnet add package JsonSrcGen --version 0.1.0.6-alpha

  2. 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.

Most helpful comment

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.

All 4 comments

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

AdamSpeight2008 picture AdamSpeight2008  路  3Comments

joshua-mng picture joshua-mng  路  3Comments

MadsTorgersen picture MadsTorgersen  路  3Comments

JesperTreetop picture JesperTreetop  路  3Comments

asvishnyakov picture asvishnyakov  路  3Comments