roslyn: Source Generator load failure is unclear when it references a newer version of the compiler than the build uses
Please note
This does not happen when running on Linux. The same exact project compiles and runs normally in any linux environment, but fails on Windows and macOS. This problem started seemingly spontaneously, and has been replicated on various dotnet
SDK versions (including 5.0.204
, 5.0.203
, and 5.0.202
).
I have also checked out old commits in my repo that are known to have compiled on macOS and Windows in the past, and they now show the same issue. This suggests something that has changed in the NuGet packages themselves, or some other external factor.
I have also posted about this here: https://stackoverflow.com/questions/68273070/c-sharp-source-generator-cannot-be-created
Version Used:
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="3.10.0" PrivateAssets="all" />
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.2" PrivateAssets="all" />
Steps to Reproduce:
- Reference a source generation package like so:
<ItemGroup>
<ProjectReference Include="..\My.Company.ReportingModel.Generators\My.Company.ReportingModel.Generators.csproj">
<ReferenceOutputAssembly>false</ReferenceOutputAssembly>
<OutputItemType>Analyzer</OutputItemType>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<AdditionalFiles Include="../../models/*.yaml;../../models/*.yml" />
</ItemGroup>
- Run
dotnet build
.
Expected Behavior:
The solution builds normally.
Actual Behavior:
Build FAILED
CSC : warning CS8032: An instance of analyzer My.Company.Generators.ReportingV2Generator cannot be created from /Users/work/dev/git/reporting-model/src/My.Company.Generators/bin/Debug/netstandard2.1/My.Company.Generators.dll : Could not load file or assembly 'Microsoft.CodeAnalysis, Version=3.10.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. The system cannot find the file specified.. [/Users/work/dev/git/reporting-model/src/My.Team.Models.ReportingV2/My.Team.Models.ReportingV2.csproj]
/Users/work/dev/git/reporting-model/src/My.Company.Api/SmokeTests/Tests/ContentAskExpertSmokeTest.cs(7,20): error CS0234: The type or namespace name 'Models' does not exist in the namespace 'My.Team' (are you missing an assembly reference?) [/Users/work/dev/git/reporting-model/src/My.Company.Api/My.Company.Api.csproj]
... Many more of the above "type or namespace" errors
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Comments: 26 (10 by maintainers)
Commits related to this issue
- Downgrade the version of Microsoft.CodeAnalaysis.CSharp So that it works in lower versions of the SDK e.g. see https://github.com/dotnet/roslyn/issues/54710#issuecomment-879258612 — committed to andrewlock/StronglyTypedId by andrewlock 3 years ago
- Downgrade the version of Microsoft.CodeAnalaysis.CSharp So that it works in lower versions of the SDK e.g. see https://github.com/dotnet/roslyn/issues/54710#issuecomment-879258612 — committed to ProActive-Engineer/StronglyID by deleted user 3 years ago
- Downgrade the version of Microsoft.CodeAnalaysis.CSharp So that it works in lower versions of the SDK e.g. see https://github.com/dotnet/roslyn/issues/54710#issuecomment-879258612 — committed to ProActive-Engineer/StronglyID by ProActive-Engineer 3 years ago
@alexrosenfeld10 thanks for the bin logs, super helpful.
The error on the failure case is that the generator is dependent on
Microsoft.CodeAnalysis.dll
version3.10
, and3.10
only shipped in5.0.300
. It appears that your success case is using SDK5.0.300
but the failure case is using5.0.204
.You have two options to fix this:
3.10
for source generators, you can lower theMicrosoft.CodeAnalysis
nuget package target to3.9
5.0.300
version of the SDKLet me know if that fixes the build for you.
Still getting bit by this, and this issue was not the most straightforward to find.
Could this be annotated somewhere close to https://docs.microsoft.com/en-us/dotnet/csharp/roslyn-sdk/source-generators-overview ?
Same Problem But I’m Using visual studio
@sharwell @jaredpar Someone from Visual Studio support wrote to me in email about the ticket, indicating that it’s opened in the wrong place. I took a guess at your Microsoft email addresses and sent on a reply. Thanks for continuing to investigate here!