moq / Moq.AutoMocker

An auto-mocking IoC container for Moq
MIT License
393 stars 53 forks source link

Cannot create analyzer during build. #150

Open belav opened 2 years ago

belav commented 2 years ago

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 - https://github.com/dotnet/sdk/issues/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 commented 2 years ago

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

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

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

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

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: https://github.com/dotnet/roslyn/issues/63919 and related linked issue: https://github.com/dotnet/roslyn/issues/63780

Ian1971 commented 7 months ago

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>