Source generator project compiles using `dotnet build` but not in Visual Studio?

111 Views Asked by At

Here is a very simple example for Source generator: https://github.com/nkw/SourceGeneratorIssue/tree/master/SourceGenerators

Using dotnet build the two projects compile successfully.

> cd ..\SourceGenerators\
> dotnet build
MSBuild version 17.8.3+195e7f5a3 for .NET
  Determining projects to restore...
  All projects are up-to-date for restore.
  SourceGenerators -> C:\source\repos\SourceGenerators\SourceGenerators\bin\Debug\netstandard2.1\SourceGe
  nerators.dll

Build succeeded.
    0 Warning(s)
    0 Error(s)

Time Elapsed 00:00:00.72

> cd ..\ClassLibrary1\
> dotnet build
MSBuild version 17.8.3+195e7f5a3 for .NET
  Determining projects to restore...
  All projects are up-to-date for restore.
  SourceGenerators -> C:\source\repos\SourceGenerators\SourceGenerators\bin\Debug\netstandard2.1\SourceGe
  nerators.dll
  ClassLibrary1 -> C:\source\repos\SourceGenerators\ClassLibrary1\bin\Debug\netstandard2.1\ClassLibrary1.
  dll

Build succeeded.
    0 Warning(s)
    0 Error(s)

Time Elapsed 00:00:01.06

However, in Visual Studio, it got the following build error after Clean Solution?

Build started at 3:35 PM...
1>------ Build started: Project: SourceGenerators, Configuration: Debug Any CPU ------
Restored C:\source\repos\SourceGenerators\ClassLibrary1\ClassLibrary1.csproj (in 9 ms).
Restored C:\source\repos\SourceGenerators\SourceGenerators\SourceGenerators.csproj (in 20 ms).
1>C:\source\repos\SourceGenerators\SourceGenerators\Class1.cs(7,14,7,29): warning RS1036: 'ClassLibrary1.SourceGenerator': A project containing analyzers or source generators should specify the property '<EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules>'
1>SourceGenerators -> C:\source\repos\SourceGenerators\SourceGenerators\bin\Debug\netstandard2.1\SourceGenerators.dll
1>Done building project "SourceGenerators.csproj".
2>------ Build started: Project: ClassLibrary1, Configuration: Debug Any CPU ------
2>CSC : warning CS8032: An instance of analyzer ClassLibrary1.SourceGenerator cannot be created from C:\source\repos\SourceGenerators\SourceGenerators\bin\Debug\netstandard2.1\SourceGenerators.dll : Could not load file or assembly 'netstandard, Version=2.1.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' or one of its dependencies. The system cannot find the file specified..
2>C:\source\repos\SourceGenerators\ClassLibrary1\Class1.cs(14,36,14,41): error CS8795: Partial method 'Class1.GetS2(string)' must have an implementation part because it has accessibility modifiers.
2>Done building project "ClassLibrary1.csproj" -- FAILED.
========== Build: 1 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
1

There are 1 best solutions below

0
Guru Stron On BEST ANSWER

According to the docs the source generator library must target netstandard2.0:

The source generator project needs to target the netstandard2.0 TFM, otherwise it will not work.

Interestingly enough I was able to successfully build app using source generator via CLI and Rider, so maybe this restriction is actually applicable only in Visual Studio or when building via .NET Framework tooling.

Also see this comment @github issue about support for .netstandard 2.1 for source generators:

.NET Standard 2.1 can be used under the following conditions:

  • The generator will work during builds with .NET tooling (e.g. dotnet build or dotnet msbuild), but will not work for builds with .NET Framework tooling (e.g. msbuild)
  • The generator will not work for IDE scenarios, which includes builds within Visual Studio and all IntelliSense functionality The second limitation in particular seems sufficiently problematic to say that the scenario is not recommended.