dotnet / roslyn

The Roslyn .NET compiler provides C# and Visual Basic languages with rich code analysis APIs.
https://docs.microsoft.com/dotnet/csharp/roslyn-sdk/
MIT License
18.96k stars 4.02k forks source link

Feature Request: Runtime only dependencies for source generators #49641

Open trampster opened 3 years ago

trampster commented 3 years ago

Version Used: dotnet version 5.0.100

I have a source generator that is .net standard 2.0, but it generates .net standard 2.1 code.

I would like my generated code and thus the consumers of my Source Generator to have a dependency on a .net standard 2.1 nuget package.

The way to do this according to https://github.com/dotnet/roslyn/blob/master/docs/features/source-generators.cookbook.md is to take a public dependency on the nuget package, via a Package Reference in the Source Generator project.

I can't do that because a .net standard 2.0 project cannot reference a .net standard 2.1 package.

Currently you can do the following:

I'm proposing a third option:

This Package Reference would not need to be checked against the Source Generators version or project because the Source Generator doesn't use it, only the consumer of the Source Generator needs it.

davidfowl commented 3 years ago

I had this exact same problem with my .NET 5 runtime dependency

joshlang commented 3 years ago

I have the same issue. I've got this open stack overflow question, which is a bit wandering, but I suspect everything would be fixed if the 2.0 to 2.1 mismatch worked.

nvmkpk commented 7 months ago

I have the same issue, my source generator generates code that takes dependency on a nuget package. Currently, the consumer will need to add that package as well. Too bad this is still not fixed.

With ProjectReference we have the option to not reference the output assembly but we don't have similar option with PackageReference.

CyrusNajmabadi commented 7 months ago

SourceGenerators just generate source. And, specifically, teh exact same source that a user could manually add. If you want the user to add a package reference, tell them to do so :)