Project which uses latest Microsoft.CodeAnalysis causes error Could not load file or assembly 'Microsoft.CodeAnalysis, Version=4.7.0.0
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.
can you share a Minimal Repro. ie the smallest possible codebase that reproduces the problem
can u try version 2.3.0
and what versionf of VS are you using.
Hi,
I am using VS 2022 Pro v.17.10.5
unfortunately on nuget there is only version 2.2.0 of Verify.SourceGenerators
Error which I receive
Link to repro https://github.com/AdaskoTheBeAsT/AdaskoTheBeAsT.Identity.Dapper
can u try nuget again
no - code analysis on version 4.9 also does not work
in that case i dont know.
any progress on a minimal repro? and pointing to you entire codebase is not minimal
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
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
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" />
The package AdaskoTheBeAsT.Puma.Security.Rules.2022 is also forcing a reference to Microsoft.CodeAnalysis
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
ok I see - thanks a lot!
and fwiw, a repro that has 40+ package references is not minimal
y I know - anyway I am obsessed with usage of source code analyzers