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 - Could not load file or assembly Microsoft.CodeAnalysis on Azure Devops build agent #59945

Open
tattan opened this issue Mar 4, 2022 · 5 comments
Assignees
Milestone

Comments

@tattan
Copy link

tattan commented Mar 4, 2022

Version Used:

Steps to Reproduce:
In the attached repro case there is a source generator and a test project that depends on the generator.

On an Azure Devops VM (latest agent version) using an azure pipeline:

  • Run nuget restore on the solution
  • Build the attached solution

Expected Behavior:
The build of both projects are successful both locally and on remote

Actual Behavior:
The build works in Visual Studio locally, the build fails on the Aazure Devops worker.

The generator project build succeeds, but then the project that depends on the generator fails with the warning:

##[warning]CSC(0,0): Warning CS8032: An instance of analyzer UITestsSourceGenerator.TestGenerator cannot be created from C:\...\UITestsSourceGenerator\bin\x64\Debug\netstandard2.0\UITestsSourceGenerator.dll : Could not load file or assembly 'Microsoft.CodeAnalysis, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified..
CSC : warning CS8032: An instance of analyzer UITestsSourceGenerator.TestGenerator cannot be created from C:\...\UITestsSourceGenerator\bin\x64\Debug\netstandard2.0\UITestsSourceGenerator.dll : Could not load file or assembly 'Microsoft.CodeAnalysis, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified.. [C:\...\simple-repro\simple-repro.csproj]

I scrubbed where it has '...'

Steps I've tried:
I tried the suggested fixes from the following resources:

It appears as though I'm not the only one having an issue with source generation in either a VM or container.
https://docs.microsoft.com/en-us/answers/questions/670252/could-not-load-file-or-assembly-39microsoftcodeana.html

I do think this should be an error for code generators as it's likely more breaking than a warning leads you to believe.

simple-repro.zip

Any help or steps to try would be greatly appreciated, and please let me know if more information is required. Thanks!

@dotnet-issue-labeler dotnet-issue-labeler bot added Area-Compilers untriaged Issues and PRs which have not yet been triaged by a lead labels Mar 4, 2022
@jcouv jcouv added the Question label Mar 11, 2022
@jcouv jcouv added this to the 17.2 milestone Mar 11, 2022
@jcouv jcouv removed the untriaged Issues and PRs which have not yet been triaged by a lead label Mar 11, 2022
@tattan
Copy link
Author

tattan commented Mar 14, 2022

I'm trying to get around this currently but am having no luck.

I've tried separating the source generator into its own solution, generating a nuget package from it (super easy!), and referencing the nuget package. I see the same behavior of the build succeeding locally and failing with the same error under Azure Devops.

Is there any eta on when this might be looked at or fixed? I'm contemplating copy-pasting the generated files' contents into actual files and removing the generator from my main solution entirely...

@boyvanduuren
Copy link

boyvanduuren commented Apr 6, 2022

I was running into this exact issue, and it turns out I was missing a dependency in my Roslyn generator project.
After adding it, the pipeline finally worked.

<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="4.0.1" />

Hopefully this works for you as well @tattan.

edit: Actually, never mind. For some reason my pipeline passed once on a pre-release version of my code generator, but haven't been able to get it working since.

@boyvanduuren
Copy link

boyvanduuren commented Apr 7, 2022

Did a bit more digging. All my colleagues can build (even the one colleague running OSX) without issues. Just to be absolutely sure I tried building in a fresh Windows and that was successful as well. Must be something in the Azure Devops build agent I guess.

Edit: Seem to have fixed it by making sure .NET 6 SDK is installed on the build agent. That also explains why I had one pipeline run that was successful yesterday, probably landed on an agent that already had it installed. Doesn't explain why it worked on a fresh Windows with only .NET 3.1 installed though, but I'll leave that mystery unsolved.

@tattan: Try adding the following task to your pipeline:

    - task: UseDotNet@2
      displayName: Use .NET SDK 6.x
      inputs:
        packageType: sdk
        version: 6.x

@UnleashMayhem
Copy link

I had this exact same issue (built fine locally but not on a DevOps agent).

Updating to .NET 6 did fix our issue, so I can confirm the .NET SDK does make a difference.

@tattan
Copy link
Author

tattan commented Apr 8, 2022

@boyvanduuren thanks for sharing that fix with me! It was indeed what I needed.

As far as this issue goes I think a better error message is definitely in order. Other than that I believe this issue is closed.

@jinujoseph jinujoseph modified the milestones: 17.2, 17.3 May 5, 2022
@arunchndr arunchndr modified the milestones: 17.3, Backlog Sep 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants