Skip to content
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 load failure is unclear when it references a newer version of the compiler than the build uses #54710

Closed
alexrosenfeld10 opened this issue Jul 9, 2021 · 26 comments
Labels
Area-Compilers Feature - Source Generators Source Generators Need More Info The issue needs more information to proceed. Resolution-Duplicate The described behavior is tracked in another issue
Milestone

Comments

@alexrosenfeld10
Copy link

alexrosenfeld10 commented Jul 9, 2021

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:

  1. 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>
  1. 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
@dotnet-issue-labeler dotnet-issue-labeler bot added Area-Compilers untriaged Issues and PRs which have not yet been triaged by a lead labels Jul 9, 2021
@jaredpar jaredpar added Need More Info The issue needs more information to proceed. Feature - Source Generators Source Generators and removed untriaged Issues and PRs which have not yet been triaged by a lead labels Jul 9, 2021
@jaredpar
Copy link
Member

jaredpar commented Jul 9, 2021

Can you grab binary logs for both the builds that are succeeding and failing? Comparing them should give us an idea on what is going on here. To get a binary log just add the /bl option to the dotnet build command.

@chsienki

@jaredpar jaredpar added this to the 17.0 milestone Jul 9, 2021
@alexrosenfeld10
Copy link
Author

@jaredpar certainly.. thanks for the quick reply!! Coming right up.

@alexrosenfeld10
Copy link
Author

@jaredpar GitHub doesn't allow me to attach that file type here. Is there some other channel I can share it with you on? Happy to open a support ticket if need be.

I poked around the log myself and didn't find tons, but you will probably find more than I will.

I would imagine you'll also ask for the logs of the succeeding build on Linux, which I am also happy to provide.

@sharwell
Copy link
Member

sharwell commented Jul 9, 2021

@alexrosenfeld10 Keep in mind the binlog will contain all environment variables and a bunch of other data specific to your machine. I recommend submitting a feedback ticket through the Report a Problem tool (covered by Microsoft data privacy policy) instead of posting it here (public).

@alexrosenfeld10
Copy link
Author

Thanks @sharwell . I did check that I don't have any env vars I'm worried about.

I'll open something up there, thanks for linking! You updated it literally while I was about to ask.

@alexrosenfeld10
Copy link
Author

Thing is, this isn't a problem with VS. It's a general problem with source generators, and can be replicated outside of the IDE in a CLI setting alone.

@sharwell
Copy link
Member

sharwell commented Jul 9, 2021

That's fine. We can link the internal report to this issue.

@alexrosenfeld10
Copy link
Author

Ok. @sharwell I don't have Visual Studio, I use Rider / CLI... So, instead I opened one on https://my.visualstudio.com/GetHelp - the case number is 2107090040003568

@alexrosenfeld10
Copy link
Author

unfortunately there was no way to attach a file on that case. Let me know how best to proceed here!

@jaredpar
Copy link
Member

jaredpar commented Jul 9, 2021

You can try opening a feedback issue directly and uploading there https://developercommunity.visualstudio.com/report?entry=suggestion&space=8

@alexrosenfeld10
Copy link
Author

I tried signing in there and I get the following:

Screen Shot 2021-07-09 at 12 38 45 PM

@alexrosenfeld10
Copy link
Author

After a few tries it started working. Submitting one now...

@alexrosenfeld10
Copy link
Author

Here's the link:

https://developercommunity.visualstudio.com/t/C-Source-Generator-cannot-be-created/1471907?entry=myfeedback&space=8

Is this private? My colleague is able to view the link without even signing in. I would just like to share these logs in a secure channel...

@sharwell
Copy link
Member

sharwell commented Jul 9, 2021

The issue is public, but the attachment(s) are not. When you add a reply comment to attach the file(s), it will ask you to select the visibility.

@alexrosenfeld10
Copy link
Author

got it. Uploaded as private to microsoft. Thanks. That's the failing build btw, let me know if you need the successful linux build

@alexrosenfeld10
Copy link
Author

@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!

@chsienki
Copy link
Contributor

@alexrosenfeld10 thanks for the bin logs, super helpful.

The error on the failure case is that the generator is dependent on Microsoft.CodeAnalysis.dll version 3.10, and 3.10 only shipped in 5.0.300. It appears that your success case is using SDK 5.0.300 but the failure case is using 5.0.204.

You have two options to fix this:

  • If you're not using any of the features introduced in 3.10 for source generators, you can lower the Microsoft.CodeAnalysis nuget package target to 3.9
  • Ensure that all builds are using at least 5.0.300 version of the SDK

Let me know if that fixes the build for you.

@alexrosenfeld10
Copy link
Author

@chsienki @jaredpar @sharwell thanks a bunch! That's super helpful. This worked for me, I'm back up and running.

I use https://github.com/isen-ng/homebrew-dotnet-sdk-versions and I brew upgrade frequently, it seems like that tap hasn't published the newer .net 5 versions in a while. I use it to manage multiple versions (installing newer 5's uninstalls my 3's). Seems like I'll have to just deal with that for now.

You've all been helpful, thanks again.

@alexrosenfeld10
Copy link
Author

alexrosenfeld10 commented Jul 13, 2021

Just curious, perhaps there's some release note I missed. Seems a bit tricky to change what binaries get shipped like this. Regardless, I'm all set now so I'll close this out.

@jaredpar
Copy link
Member

Situation we could possibly make better when we implement #54108

andrewlock added a commit to andrewlock/StronglyTypedId that referenced this issue Aug 1, 2021
So that it works in lower versions of the SDK
e.g. see dotnet/roslyn#54710 (comment)
@hammypants
Copy link

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 ?

@wisamidris7
Copy link

Same Problem But I'm Using visual studio

@sharwell
Copy link
Member

@alexrosenfeld10 The following line from the original post is concerning as well:

An instance of analyzer My.Company.Generators.ReportingV2Generator cannot be created from [...]/netstandard2.1/My.Company.Generators.dll

Source generators are required to target netstandard2.0. Generators targeting netstandard2.1 are not supported and are likely to not work.

@sharwell
Copy link
Member

sharwell commented May 16, 2023

@hammypants @wisamidris7 This issue was fixed by #54108 (support declaring required Roslyn versions in analyzer packages) and #61252 (provide a specific error message if the analyzer is newer than the compiler). If you continue having problems, it would be best to file a new issue.

@sharwell
Copy link
Member

Duplicate of #61252

@sharwell sharwell marked this as a duplicate of #61252 May 16, 2023
@sharwell sharwell closed this as not planned Won't fix, can't repro, duplicate, stale May 16, 2023
@sharwell sharwell added the Resolution-Duplicate The described behavior is tracked in another issue label May 16, 2023
@sharwell sharwell changed the title Source Generator cannot be created Source Generator load failure is unclear when it references a newer version of the compiler than the build uses May 16, 2023
@alexrosenfeld10
Copy link
Author

alexrosenfeld10 commented May 16, 2023

@sharwell Yeah I was tinkering around with what works / doesn't work for source generators at the time. In most cases, they did work on 2.1, actually. I understand that's not a guarantee though.

Glad to hear the error messages and fine tuning of versions is in place!

ProActive-Engineer pushed a commit to ProActive-Engineer/StronglyID that referenced this issue Aug 19, 2023
So that it works in lower versions of the SDK
e.g. see dotnet/roslyn#54710 (comment)
ProActive-Engineer added a commit to ProActive-Engineer/StronglyID that referenced this issue Aug 19, 2023
So that it works in lower versions of the SDK
e.g. see dotnet/roslyn#54710 (comment)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Compilers Feature - Source Generators Source Generators Need More Info The issue needs more information to proceed. Resolution-Duplicate The described behavior is tracked in another issue
Projects
None yet
Development

No branches or pull requests

6 participants