dotnet / roslyn

The Roslyn .NET compiler provides C# and Visual Basic languages with rich code analysis APIs.
https://docs.microsoft.com/dotnet/csharp/roslyn-sdk/
MIT License
19.06k stars 4.04k forks source link

Source Generator - Could not load file or assembly Microsoft.CodeAnalysis on Azure Devops build agent #59945

Open tattan opened 2 years ago

tattan commented 2 years ago

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:

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!

tattan commented 2 years ago

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 commented 2 years ago

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 commented 2 years ago

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 commented 2 years ago

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 commented 2 years ago

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