Verify.SourceGenerators icon indicating copy to clipboard operation
Verify.SourceGenerators copied to clipboard

Project which uses latest Microsoft.CodeAnalysis causes error Could not load file or assembly 'Microsoft.CodeAnalysis, Version=4.7.0.0

Open AdaskoTheBeAsT opened this issue 1 year ago • 7 comments

Describe the bug

When I try to launch debug on source generator and invoke

"""csharp [ModuleInitializer] public static void Init() { VerifySourceGenerators.Initialize(); } """ I receive error

"""cmd The type initializer for '<Module>' threw an exception. at System.RuntimeTypeHandle.GetActivationInfo(RuntimeType rt, & pfnAllocator, Void*& vAllocatorFirstArg, & pfnCtor, Boolean& ctorIsPublic) at System.RuntimeType.ActivatorCache..ctor(RuntimeType rt) at System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean wrapExceptions) at System.RuntimeType.CreateInstanceImpl(BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture) at System.Activator.CreateInstance(Type type, Object[] args) at ReflectionAbstractionExtensions.<>c__DisplayClass0_0.<CreateTestClass>b__0() in //src/xunit.execution/Extensions/ReflectionAbstractionExtensions.cs:line 42 at Xunit.Sdk.ExecutionTimer.Aggregate(Action action) in //src/xunit.execution/Sdk/Frameworks/ExecutionTimer.cs:line 31 at ReflectionAbstractionExtensions.CreateTestClass(ITest test, Type testClassType, Object[] constructorArguments, IMessageBus messageBus, ExecutionTimer timer, CancellationTokenSource cancellationTokenSource) in //src/xunit.execution/Extensions/ReflectionAbstractionExtensions.cs:line 42 at Xunit.Sdk.TestInvoker1.CreateTestClass() in /_/src/xunit.execution/Sdk/Frameworks/Runners/TestInvoker.cs:line 122 at Xunit.Sdk.TestInvoker1.<RunAsync>b__45_0() in //src/xunit.execution/Sdk/Frameworks/Runners/TestInvoker.cs:line 161 at Xunit.Sdk.ExceptionAggregator.RunAsync[T](Func`1 code) in /_/src/xunit.core/Sdk/ExceptionAggregator.cs:line 107

System.IO.FileNotFoundException Could not load file or assembly 'Microsoft.CodeAnalysis, Version=4.7.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. Nie można odnaleźć określonego pliku. at VerifyTests.VerifySourceGenerators.Initialize() at AdaskoTheBeAsT.Identity.Dapper.SqlServer.Test.ModuleInitializer.Init() in C:\GitHub\AdaskoTheBeAsT.Identity.Dapper\test\unit\AdaskoTheBeAsT.Identity.Dapper.SqlServer.Test\ModuleInitializer.cs:line 10 at .cctor() """

It looks like this project references old version of metapackage - could it be updated to last version please?

Minimal Repro

It is available here

Submit a PR that fixes the bug

Submit a Pull Request (PR) that fixes the bug. Include in this PR a test that verifies the fix. If you were not able to fix the bug, a PR that illustrates your partial progress will suffice.

AdaskoTheBeAsT avatar Jul 27 '24 21:07 AdaskoTheBeAsT

can you share a Minimal Repro. ie the smallest possible codebase that reproduces the problem

SimonCropp avatar Jul 28 '24 00:07 SimonCropp

can u try version 2.3.0

and what versionf of VS are you using.

SimonCropp avatar Jul 28 '24 01:07 SimonCropp

Hi,

I am using VS 2022 Pro v.17.10.5

unfortunately on nuget there is only version 2.2.0 of Verify.SourceGenerators

image

Error which I receive

image

Link to repro https://github.com/AdaskoTheBeAsT/AdaskoTheBeAsT.Identity.Dapper

AdaskoTheBeAsT avatar Jul 28 '24 05:07 AdaskoTheBeAsT

can u try nuget again

SimonCropp avatar Jul 28 '24 06:07 SimonCropp

no - code analysis on version 4.9 also does not work

image

AdaskoTheBeAsT avatar Jul 29 '24 15:07 AdaskoTheBeAsT

in that case i dont know.

any progress on a minimal repro? and pointing to you entire codebase is not minimal

SimonCropp avatar Jul 30 '24 00:07 SimonCropp

I think that problem is here but is here - I will prepare some minimal repro - anyway it does not change the fact that lib is dependant on Microsoft analysis 4.9.0

image

AdaskoTheBeAsT avatar Jul 30 '24 07:07 AdaskoTheBeAsT

hi

minimal sample available here https://github.com/AdaskoTheBeAsT/VerifySourceGeneratorCodeAnalysisIssue

when I use latest analyzers build just fails

when i switch off analyzers and downgrade to 4.9.2 as it is in branch

https://github.com/AdaskoTheBeAsT/VerifySourceGeneratorCodeAnalysisIssue/blob/feature/without-analyzers/SampleGenerator/SampleGenerator.csproj

all works

I think this is due to verify using older version of Microsoft.CodeAnalysis.CSharp

https://github.com/VerifyTests/Verify.SourceGenerators/blob/17b88972262956ce7dd8d02e9a360a3107f51067/src/Directory.Packages.props#L9

AdaskoTheBeAsT avatar Aug 21 '24 17:08 AdaskoTheBeAsT

try removing the PrivateAssets="all" ExcludeAssets="runtime" from the following in your dir props


    <PackageReference Include="Microsoft.CodeAnalysis" Version="4.11.0"  />
    <PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.4" />
    <PackageReference Include="Microsoft.CodeAnalysis.Common" Version="4.11.0" />
    <PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.11.0" />

SimonCropp avatar Aug 22 '24 10:08 SimonCropp

The package AdaskoTheBeAsT.Puma.Security.Rules.2022 is also forcing a reference to Microsoft.CodeAnalysis

image

distantcam avatar Aug 22 '24 11:08 distantcam

the problem is you have PrivateAssets="all" ExcludeAssets="runtime" for the CodeAnalysis packages. which means they dont get copied to the output directory of the tests

SimonCropp avatar Aug 22 '24 12:08 SimonCropp

ok I see - thanks a lot!

AdaskoTheBeAsT avatar Aug 22 '24 12:08 AdaskoTheBeAsT

and fwiw, a repro that has 40+ package references is not minimal

SimonCropp avatar Aug 22 '24 12:08 SimonCropp

y I know - anyway I am obsessed with usage of source code analyzers

AdaskoTheBeAsT avatar Aug 22 '24 12:08 AdaskoTheBeAsT