roslyn: Source Generators: design-time completion/intellisense and Target Framework not work

Version Used: image

Steps to Reproduce:

  1. Setup the project image
  2. Use generated code image
  3. Build image

Expected Behavior: design-time completion/intellisense and My TestProject->Dependencies->Analyzers->Generator not generated, mostly VS not catch it as i see it’s generated in the path $(ProjectDir)Generated\Generator\Generator.MySourceGenerator\HelloWorldGenerator.cs

And only after restart (not change any setting) image

Actual Behavior: Nothing of Expected Behavior happens 😄 , Also if i change my generator generated namespace to HelloWorldGeneratedxxx and rebuild image intellisense will work if restart VS(without change any setting)

Side Problem As i read here StackOverflow it should my generator work with any Target Framework, but when targeting net5.0 it’s never works even after restart Visual Studio it’s not completion/intellisense problem as project never build image image

Rebuild started...
1>------ Rebuild All started: Project: Generator, Configuration: Debug Any CPU ------
Restored C:\Users\CorrM\source\repos\SourceGenTest\SourceGenTest\SourceGenTest.csproj (in 5 ms).
Restored C:\Users\CorrM\source\repos\SourceGenTest\Generator\Generator.csproj (in 31 ms).
1>Generator -> C:\Users\CorrM\source\repos\SourceGenTest\Generator\bin\Debug\net5.0\Generator.dll
2>------ Rebuild All started: Project: SourceGenTest, Configuration: Debug Any CPU ------
2>CSC : warning CS8032: An instance of analyzer Generator.MySourceGenerator cannot be created from C:\Users\CorrM\source\repos\SourceGenTest\Generator\bin\Debug\net5.0\Generator.dll : Could not load file or assembly 'System.Runtime, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified..
2>C:\Users\CorrM\source\repos\SourceGenTest\SourceGenTest\Program.cs(9,4,9,23): error CS0103: The name 'HelloWorldGenerated' does not exist in the current context
2>Done building project "SourceGenTest.csproj" -- FAILED.
========== Rebuild All: 1 succeeded, 1 failed, 0 skipped ==========

When targeting netstandard2.0(without change any other setting) it’s working!

Build started...
1>------ Build started: Project: Generator, Configuration: Debug Any CPU ------
1>Generator -> C:\Users\CorrM\source\repos\SourceGenTest\Generator\bin\Debug\netstandard2.0\Generator.dll
2>------ Build started: Project: SourceGenTest, Configuration: Debug Any CPU ------
2>SourceGenTest -> C:\Users\CorrM\source\repos\SourceGenTest\SourceGenTest\bin\Debug\net5.0\SourceGenTest.dll
========== Build: 2 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========
Sorry for all this images but i think images is the best to explain that problem

@sharwell @chsienki

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 19 (13 by maintainers)

Most upvoted comments

Going through this conversation I think there’s three issues that were raised:

  1. Generators need to be netstandard2.0 (which has been addressed)
  2. The classification issue, which @JoeRobich linked to the tracking bug there.
  3. The issue around generators needing an unload/reload after a change, which we’re tracking at #48083.

I’m closing this since I don’t think there’s anything else unique in this bug, but if I missed something do raise it!

Any plans on supporting this scenario for Visual Studio?

We have source generators for internal use and put the generator and the unit tests in the same solution.

Restarting VS everytime we want to run the unit tests (after modifying the source generator) is a poor experience.

Any plans on supporting this scenario for Visual Studio?

We are investigating ways to fix this, but we’re not yet certain it’s possible. The .NET Framework assembly loader generally doesn’t like loading multiple versions of the same assembly. It also isn’t possible to unload the assembly after it’s loaded, so each new build will add a new assembly in memory until you eventually restart the IDE.

As i read here StackOverflow it should my generator work with any Target Framework, but when targeting net5.0 it’s never works even after restart Visual Studio it’s not completion/intellisense problem as project never build

The flexibility described on Stack Overflow applies only to consumers of source generators (in this case, SourceGenTest.csproj). The source generator itself (Generator.csproj) must target exactly netstandard2.0.