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

Cannot create analyzer during build. #150

Open
belav opened this issue Aug 23, 2022 · 6 comments
Open

Cannot create analyzer during build. #150

belav opened this issue Aug 23, 2022 · 6 comments

Comments

@belav
Copy link

belav commented Aug 23, 2022

I migrated over to using Moq.AutoMocker from a homebrew version using Unity.
Building locally in debug and release works.

Using our docker based release build comes up with the following error. This may just require a binding redirect, I didn't dig into it too much because the docker based build takes forever and is a bit hard to debug.

CSC : error CS8032: An instance of analyzer Moq.AutoMocker.TestGenerator.UnitTestSourceGenerator cannot be created from
C:\Users\ContainerAdministrator\.nuget\packages\moq.automock\3.4.0\analyzers\dotnet\cs\Moq.AutoMocker.TestGenerator.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.

I'm not using the TestGenerator, and tried to exclude it from the build using the following

<PackageReference Include="Moq.AutoMock" Version="3.4.0">
    <ExcludeAssets>analyzers</ExcludeAssets>
</PackageReference>

It sounds like this doesn't actually work though - dotnet/sdk#1212

I was able to downgrade to 3.3.0 to get past the issue, but maybe it makes sense to split the SourceGenerator into a separate nuget package.

@Keboo
Copy link
Collaborator

Keboo commented Aug 23, 2022

Hi @belav thank you for the report. Are you able to provide a little more information on your build process?
Which version of the dotnet SDK are you running?
How are you running the build/tests (just dotnet build/test) or some CI tasks?
Are you able to manually include the Microsoft.CodeAnalysis package?

@belav
Copy link
Author

belav commented Aug 23, 2022

We are targeting .net48, and using the mcr.microsoft.com/dotnet/framework/sdk:4.8-windowsservercore-ltsc2019 docker image. We do also install dotnet-5.0-sdk

The error happens during the build process, the solution is being built using msbuild version 16.10.2 It doesn't get to the test execution.

I did try including Microsoft.CodeAnalysis version 4.1.0 directly, but that caused issues, if I remember right it was a version mismatch. It takes like 20 minutes to get to the failure, so it can be kind of painful to test.

The latest issue I am running into is another assembly mismatch now with Castle.Core. Running all of our tests locally works, but they are failing on the docker build. Within this same PR I removed all the app.config binding redirects and moved to auto generated ones. So maybe this problem has nothing to do with Moq.AutoMocker, and it just happened to be the first thing in our build that ran into the problem. However there were no binding redirects for Microsoft.CodeAnalysis or Castle.Core

I found source generators to be a bit finicky in a side project I am using them on, so this was mostly a proposal to potentially split the Moq.AutoMocker ones out. But if no one else is having issues with them there probably isn't a need to do that.

@Keboo
Copy link
Collaborator

Keboo commented Aug 29, 2022

@belav just ran into a very similar looking issue on another project. The key failure ended up being the presence of the 6.0.3* SDK. Either downgrading 6.0.2* (NB: downgrading requires removing 6.0.3* and reinstalling 6.0.2*) or upgrading to 6.0.4* fixed the issue. Are you able to check if that SDK is present on your builds?

@zplan
Copy link

zplan commented Sep 29, 2022

Same issue here as well.
Updated AutoMock from 3.3.0 to 3.4.0 --> this results in a compile error when building with .NET SDK version 6.0.100:
error CS8032: An instance of analyzer Moq.AutoMocker.TestGenerator.UnitTestSourceGenerator cannot be created from C:\Users\abc\.nuget\packages\moq.automock\3.4.0\analyzers\dotnet\cs\Moq.AutoMocker.TestGenerator.dll : Could not load file or assembly 'Microsoft.CodeAnalysis, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. The system cannot find the file specified.. [Z:\w\25\s\Tests\MyProject.Tests.csproj]

@Keboo
Copy link
Collaborator

Keboo commented Sep 29, 2022

This is an issue with Microsoft.CodeAnalysis.CSharp which is related to the version of the SDK that you are running. Are you able to update the version of the SDK that you are using? I think this is fixed in 6.0.4*

See the following issues:
dotnet/roslyn#63919
and related linked issue: dotnet/roslyn#63780

@Ian1971
Copy link
Contributor

Ian1971 commented Apr 26, 2024

I had the same and fixed it by removing this from the csproj

  <ItemGroup>
    <PackageReference Include="Microsoft.Net.Compilers.Toolset" Version="4.2.0">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers;</IncludeAssets>
    </PackageReference>
  </ItemGroup>

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

4 participants