-
Notifications
You must be signed in to change notification settings - Fork 4.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Source Generator] Source Generator works with dotnet cli but fails with TypeLoadException
in VS
#108164
Comments
Questions for source generator belong to roslyn repo. |
@huoyaoyuan Thanks for reply, I can transfer this issue to roslyn repo. Curious to ask though, why the behavior is different when using source generator in dotnet cli versus using in VS. In the example above, the source generator can successfully generate code when I compile from command line using dotnet cli. |
Dotnet cli executes almost everything with the same .NET version of the SDK, while VS executes roslyn with .NET Framework because itself is still on .NET Framework. Source generators or analyzers should target the exact same lowest framework version of roslyn, to ensure any process that's capable loading roslyn can load the analyzer/generator. It also should reference the lowest roslyn version it need to support. |
@jaredpar can you help with the transfer to roslyn? |
Analyzers and generators are plugins to the compiler and they need to operate wherever the compiler operates. That includes running on .NET Framework and NET Core (potentially multiple versions). That is why they are strongly recommended to target |
@jaredpar Thanks for the explanation, can I understand it in the way that if my project targets to |
Correct. It will fail for any API that is |
The original message hints that its dependency (AutoSDK) attempts to load net8.0 api. |
I don't see anything meaningful in the message except the version number. Another idea is that it might have something to do with using MinVer, there was a recent update to version 6.0.0 that might have broken something. |
Think there was a bit of confusion here. My initial reading was that the generator targeted Here is the build.rsp that I was using for the repro project. I removed a few red herrings like Microsoft.OpenApi.dll being passed twice as well as the net analyzers to get it down to the core problem. From there I was just running it against roslyn using effectively the following command line: > dotnet exec C:\users\jaredpar\code\roslyn\artifacts\bin\csc\Debug\net7.0\csc.dll `@build.rsp What we observed is the following:
ETW entry:
This appears to be a runtime issue but thus far I can only reproduce it in @agocke, @elinor-fung FYI |
Tagging subscribers to this area: @vitek-karas, @agocke, @VSadov |
I think assembly loading / With the repro project, I could repro the
In .NET 7, we are detecting a recursive type load and throwing: runtime/src/coreclr/vm/clsload.cpp Lines 3448 to 3452 in 0fb6ac5
This looks like #6924, which @davidwrighton fixed in .NET 8 with #83995.
I expect the VS build is hitting the same problem - assuming this is a long-standing issue that has always existed in .NET Framework. And building from the command line is fine because it is using .NET 8. @davidwrighton does that sound right? |
Yes @elinor-fung appears to have the correct analysis. This is a bug that has been fixed in the runtime, but the fix is far too risky to backport to the .NET Framework. |
I think this goes back to @jaredpar's comment at #108164 (comment) then:
In this case, even though the generator is targeting |
Description
The source generator can successfully generate source code when I compile using dotnet cli. It also successfully generates source code in VS UI when I expand the
analyzer
node. But it raises aTypeLoadException
when I compile and run my code in VS by hittingcontrol + F5
.Here is the link to the source code of source generator: https://github.com/tryAGI/AutoSDK/tree/main/src/libs/AutoSDK.SourceGenerators
Here is the
dotnet --list-sdks
Here is the error log in VS:
Here is the screenshot of which VS I am using:

Finally, the link to a minimal reproducable example for this bug.
Reproduction Steps
See above
Expected behavior
Source generator successfully generate source in VS
Actual behavior
It raise a TypeLoadException
Regression?
No response
Known Workarounds
No response
Configuration
No response
Other information
No response
The text was updated successfully, but these errors were encountered: