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

[BUG] "Unable to instrument module" caused by const field #1732

Open
santisq opened this issue Jan 20, 2025 · 0 comments
Open

[BUG] "Unable to instrument module" caused by const field #1732

santisq opened this issue Jan 20, 2025 · 0 comments
Labels
untriaged To be investigated

Comments

@santisq
Copy link

santisq commented Jan 20, 2025

Describe the bug

Having a const field on a PowerShell cmdlet causes the warning:

Unable to instrument module: ...../....MyModule.dll
Coverlet.Core.Exceptions.CecilAssemblyResolutionException: AssemblyResolutionException for 'System.Management.Automation, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. Try to add true to test projects or pass '/p:CopyLocalLockFileAssemblies=true' option to the 'dotnet test' command-line

---> Mono.Cecil.AssemblyResolutionException: Failed to resolve assembly: 'System.Management.Automation, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'

The field in question:

const WildcardOptions options = WildcardOptions.Compiled
    | WildcardOptions.CultureInvariant
    | WildcardOptions.IgnoreCase;

And, in turn, this also cause the coverage result to be all 0%:

Calculating coverage result...
  Generating report '/home/runner/work/PSCompression/PSCompression/output/TestResults/Coverage.xml'

+--------+------+--------+--------+
| Module | Line | Branch | Method |
+--------+------+--------+--------+

+---------+------+--------+--------+
|         | Line | Branch | Method |
+---------+------+--------+--------+
| Total   | 0%   | 0%     | 0%     |
+---------+------+--------+--------+
| Average | 0%   | 0%     | 0%     |
+---------+------+--------+--------+

To Reproduce

Moving the const in https://github.com/santisq/PSCompression/blob/main/src/PSCompression/Commands/GetZipEntryCommand.cs#L50-L52 as a field causes the warning. If changing it to a static readonly also solves the problem.

Steps to reproduce:

  1. git clone 'https://github.com/santisq/PSCompression.git'
  2. Open src\PSCompression\Commands\GetZipEntryCommand.cs and move the const WildcardOptions options from BeginProcessing method to a class field
  3. Run .\build.ps1 -Task Build, Test

Expected behavior

No warnings when testing the project.

Actual behavior

Unable to instrument module: /home/runner/work/PSCompression/PSCompression/output/PSCompression/2.1.0/bin/netstandard2.0/PSCompression.dll
Coverlet.Core.Exceptions.CecilAssemblyResolutionException: AssemblyResolutionException for 'System.Management.Automation, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. Try to add <PreserveCompilationContext>true</PreserveCompilationContext> to test projects </PropertyGroup> or pass '/p:CopyLocalLockFileAssemblies=true' option to the 'dotnet test' command-line
 ---> Mono.Cecil.AssemblyResolutionException: Failed to resolve assembly: 'System.Management.Automation, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'
   --- End of inner exception stack trace ---
   at Coverlet.Core.Instrumentation.NetstandardAwareAssemblyResolver.TryWithCustomResolverOnDotNetCore(AssemblyNameReference name) in /_/src/coverlet.core/Instrumentation/CecilAssemblyResolver.cs:line 217
   at Coverlet.Core.Instrumentation.NetstandardAwareAssemblyResolver.Resolve(AssemblyNameReference name) in /_/src/coverlet.core/Instrumentation/CecilAssemblyResolver.cs:line 1[29](https://github.com/santisq/PSCompression/actions/runs/12713283173/job/35440961122#step:6:30)
   at Mono.Cecil.MetadataResolver.Resolve(TypeReference type)
   at Mono.Cecil.ModuleDefinition.Resolve(TypeReference type)
   at Mono.Cecil.TypeReference.Resolve()
   at Mono.Cecil.Mixin.CheckedResolve(TypeReference self)
   at Mono.Cecil.MetadataBuilder.GetConstantType(TypeReference constant_type, Object constant)
   at Mono.Cecil.MetadataBuilder.AddConstant(IConstantProvider owner, TypeReference type)
   at Mono.Cecil.MetadataBuilder.AddField(FieldDefinition field)
   at Mono.Cecil.MetadataBuilder.AddFields(TypeDefinition type)
   at Mono.Cecil.MetadataBuilder.AddType(TypeDefinition type)
   at Mono.Cecil.MetadataBuilder.AddTypes()
   at Mono.Cecil.MetadataBuilder.BuildTypes()
   at Mono.Cecil.MetadataBuilder.BuildModule()
   at Mono.Cecil.MetadataBuilder.BuildMetadata()
   at Mono.Cecil.ModuleWriter.<>c.<BuildMetadata>b__2_0(MetadataBuilder builder, MetadataReader _)
   at Mono.Cecil.ModuleDefinition.Read[TItem,TRet](TItem item, Func`3 read)
   at Mono.Cecil.ModuleWriter.BuildMetadata(ModuleDefinition module, MetadataBuilder metadata)
   at Mono.Cecil.ModuleWriter.Write(ModuleDefinition module, Disposable`1 stream, WriterParameters parameters)
   at Mono.Cecil.ModuleWriter.WriteModule(ModuleDefinition module, Disposable`1 stream, WriterParameters parameters)
   at Mono.Cecil.ModuleDefinition.Write(Stream stream, WriterParameters parameters)
   at Coverlet.Core.Instrumentation.Instrumenter.InstrumentModule() in /_/src/coverlet.core/Instrumentation/Instrumenter.cs:line [32](https://github.com/santisq/PSCompression/actions/runs/12713283173/job/35440961122#step:6:33)5
   at Coverlet.Core.Instrumentation.Instrumenter.Instrument() in /_/src/coverlet.core/Instrumentation/Instrumenter.cs:line 148
   at Coverlet.Core.Coverage.PrepareModules() in /_/src/coverlet.core/Coverage.cs:line 134

Configuration (please complete the following information):
Please provide more information on your .NET configuration:

  • Which coverlet package and version was used? 6.0.3 - This issue also happened when testing different versions too.
  • Which version of .NET is the code running on? I'm not sure if this question relates to the target framework I'm compiling for (netstandard2.0) or what .NET version PowerShell is running on (PowerShell 7 runs on .NET 8 and PowerShell 5.1 runs on .NET Framework 4)
  • What OS and version, and what distro if applicable? This happened to me locally on Windows 10, and also in GitHub workers using windows-latest and ubuntu-latest.
  • What is the architecture (x64, x86, ARM, ARM64)? x64
  • Do you know whether it is specific to that configuration? Not sure

Additional context

As you can see this issue happens on the 3 workers (Linux PowerShell 7 / Windows PowerShell 7 / Windows PowerShell 5.1) when running tests: https://github.com/santisq/PSCompression/actions/runs/12713283173/job/35440961122 (Expand "Run Tests - ....").

❗ Please also read Known Issues

@github-actions github-actions bot added the untriaged To be investigated label Jan 20, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
untriaged To be investigated
Projects
None yet
Development

No branches or pull requests

1 participant