coverlet-coverage / coverlet

Cross platform code coverage for .NET
MIT License
2.94k stars 385 forks source link

The process cannot access the file, because it is being used by another process" error during attempt to collect coverage #1471

Open tepatelcmc opened 1 year ago

tepatelcmc commented 1 year ago

I can see previous issue being fixed in an unofficial version - did it ever make it to stable release?

The issue still exist with running test in parallel with vstest code coverage (XPlat code coverage)

daveMueller commented 1 year ago

Are you refering to #1155? The fix for this wasn't released yet, but it will be in the next release. You can try out the fix by consuming our nightly build (https://github.com/coverlet-coverage/coverlet/blob/master/Documentation/ConsumeNightlyBuild.md).

mathijs-dumon commented 1 year ago

Don't think he means that one, but this one: https://github.com/coverlet-coverage/coverlet/issues/857

I'm facing this issue again when using coverlet.console v6.0.0 Since we didn't have the issue before my guess was a nugget, but it seems like it's actually the dotnet global tool (coverlet.console) which is causing the problem.

Just did some more testing with different versions of the tool: I don't have the issue using v3.1.x or v3.2.0, but I do with v6.0

With v6 I get this kind of exception:

---> System.IO.IOException: The process cannot access the file '(real path left out for nda reasons).pdb' because it is being used by another process. at System.IO.FileSystem.CopyFile(String sourceFullPath, String destFullPath, Boolean overwrite) at Coverlet.Core.Helpers.InstrumentationHelper.<>cDisplayClass17_0.b_0() in //src/coverlet.core/Helpers/InstrumentationHelper.cs:line 295 at Coverlet.Core.Helpers.RetryHelper.<>cDisplayClass0_0.b_0() in //src/coverlet.core/Helpers/RetryHelper.cs:line 29 at Coverlet.Core.Helpers.RetryHelper.Do[T](Func1 action, Func1 backoffStrategy, Int32 maxAttemptCount) in /_/src/coverlet.core/Helpers/RetryHelper.cs:line 55

mareklinka commented 1 year ago

I just started encountering this same after installing the newly-released C# Dev Kit for VSCode. It seems the processes started by VSCode keep a handle open on the assemblies:

TpTrace Warning: 0 : 20944, 1, 2023/06/07, 15:33:20.113, 4930720815067, datacollector.dll, [coverlet]Unable to instrument module: C:\Source\Personal\fourth-pharos\backend\FourthPharos.Domain.Tests\bin\Debug\net7.0\FourthPharos.Domain.dll
System.IO.IOException: The process cannot access the file 'C:\Source\Personal\fourth-pharos\backend\FourthPharos.Domain.Tests\bin\Debug\net7.0\FourthPharos.Domain.dll' because it is being used by another process.
   at Microsoft.Win32.SafeHandles.SafeFileHandle.CreateFile(String fullPath, FileMode mode, FileAccess access, FileShare share, FileOptions options)
   at Microsoft.Win32.SafeHandles.SafeFileHandle.Open(String fullPath, FileMode mode, FileAccess access, FileShare share, FileOptions options, Int64 preallocationSize, Nullable`1 unixCreateMode)
   at System.IO.Strategies.OSFileStreamStrategy..ctor(String path, FileMode mode, FileAccess access, FileShare share, FileOptions options, Int64 preallocationSize, Nullable`1 unixCreateMode)
   at System.IO.Strategies.FileStreamHelpers.ChooseStrategyCore(String path, FileMode mode, FileAccess access, FileShare share, FileOptions options, Int64 preallocationSize, Nullable`1 unixCreateMode)
   at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access)
   at Coverlet.Core.Helpers.FileSystem.NewFileStream(String path, FileMode mode, FileAccess access) in /_/src/coverlet.core/Helpers/FileSystem.cs:line 52
   at Coverlet.Core.Instrumentation.Instrumenter.InstrumentModule() in /_/src/coverlet.core/Instrumentation/Instrumenter.cs:line 207
   at Coverlet.Core.Instrumentation.Instrumenter.Instrument() in /_/src/coverlet.core/Instrumentation/Instrumenter.cs:line 149
   at Coverlet.Core.Coverage.PrepareModules() in /_/src/coverlet.core/Coverage.cs:line 135

Once I close VSCode and run dotnet test again, instrumentation works as expected. Probably not much Coverlet can do about it, just wanted to mention it. I'll open an issue in the Dev Kit repo, once it goes public.

github-actions[bot] commented 10 months ago

This issue is stale because it has been open for 3 months with no activity.

roryprimrose commented 8 months ago

This has been a problem for my team as we have minimum code coverage being applied to changed code as a policy in pull requests. This bug is preventing us from being able to complete pull requests.

I have found that with this bug in 6.0.0, we get this error in the dotnet test build log and 0% coverage reported for that project. I tried downgrading to 3.2.0. That version didn't throw the error but also reported 0% coverage. I have downgraded to 3.1.2 which:

github-actions[bot] commented 5 months ago

This issue is stale because it has been open for 3 months with no activity.

Bertk commented 5 months ago

Issue should be closed because the related coverlet nuget package is not mentioned and comments for all packages are available. The new issue template for bugs should improve this.

github-actions[bot] commented 1 month ago

This issue is stale because it has been open for 3 months with no activity.

tripleacoder commented 1 month ago

I get the same error on one of my .NET 4.8 projects.

I am using the coverlet console tool as part of an Azure Pipeline.

Tool version is 6.0.2

The exit code from coverlet is 255, which is undocumented.

Command:

coverlet C:\agents\A1\_work\64/RAT.IntTest.dll --target dotnet --targetargs test C:\agents\A1\_work\64/RAT.IntTest.dll --no-build --logger trx --diag:log.txt --format cobertura --source-mapping-file C:\agents\A1\_work\64/SourceMapping.txt --use-source-link --output C:\agents\A1\_work\64/coverage/coverage.cobertura.xml --verbosity detailed

Output:

(...) BadImageFormatException during MetadataReaderProvider.FromPortablePdbStream in InstrumentationHelper.PortablePdbHasLocalSource, unable to check if module has got local source. TryWithCustomResolverOnDotNetCore for PresentationFramework, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35 Unable to instrument module: C:\agents\A1_work\64\RAT.exe Coverlet.Core.Exceptions.CecilAssemblyResolutionException: AssemblyResolutionException for 'PresentationFramework, Version=4.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: 'PresentationFramework, Version=4.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 129 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.AddParameter(UInt16 sequence, ParameterDefinition parameter, ParamTable table) at Mono.Cecil.MetadataBuilder.AddParameters(MethodDefinition method) at Mono.Cecil.MetadataBuilder.AddMethod(MethodDefinition method) at Mono.Cecil.MetadataBuilder.AddMethods(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.b__20(MetadataBuilder builder, MetadataReader ) at Mono.Cecil.ModuleDefinition.Read[TItem,TRet](TItem item, Func3 read) at Mono.Cecil.ModuleWriter.BuildMetadata(ModuleDefinition module, MetadataBuilder metadata) at Mono.Cecil.ModuleWriter.Write(ModuleDefinition module, Disposable1 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 325 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 135 One or more errors occurred. (The process cannot access the file 'C:\agents\A1_work\64\RAT.pdb' because it is being used by another process.) (The process cannot access the file 'C:\agents\A1_work\64\RAT.pdb' because it is being used by another process.) (The process cannot access the file 'C:\agents\A1_work\64\RAT.pdb' because it is being used by another process.) (The process cannot access the file 'C:\agents\A1_work\64\RAT.pdb' because it is being used by another process.) (The process cannot access the file 'C:\agents\A1_work\64\RAT.pdb' because it is being used by another process.) (The process cannot access the file 'C:\agents\A1_work\64\RAT.pdb' because it is being used by another process.) (The process cannot access the file 'C:\agents\A1_work\64\RAT.pdb' because it is being used by another process.) (The process cannot access the file 'C:\agents\A1_work\64\RAT.pdb' because it is being used by another process.) (The process cannot access the file 'C:\agents\A1_work\64\RAT.pdb' because it is being used by another process.) (The process cannot access the file 'C:\agents\A1_work\64\RAT.pdb' because it is being used by another process.) (The process cannot access the file 'C:\agents\A1_work\64\RAT.pdb' because it is being used by another process.) (The process cannot access the file 'C:\agents\A1_work\64\RAT.pdb' because it is being used by another process.) Unhandled exception. System.AggregateException: One or more errors occurred. (The process cannot access the file 'C:\agents\A1_work\64\RAT.pdb' because it is being used by another process.) (The process cannot access the file 'C:\agents\A1_work\64\RAT.pdb' because it is being used by another process.) (The process cannot access the file 'C:\agents\A1_work\64\RAT.pdb' because it is being used by another process.) (The process cannot access the file 'C:\agents\A1_work\64\RAT.pdb' because it is being used by another process.) (The process cannot access the file 'C:\agents\A1_work\64\RAT.pdb' because it is being used by another process.) (The process cannot access the file 'C:\agents\A1_work\64\RAT.pdb' because it is being used by another process.) (The process cannot access the file 'C:\agents\A1_work\64\RAT.pdb' because it is being used by another process.) (The process cannot access the file 'C:\agents\A1_work\64\RAT.pdb' because it is being used by another process.) (The process cannot access the file 'C:\agents\A1_work\64\RAT.pdb' because it is being used by another process.) (The process cannot access the file 'C:\agents\A1_work\64\RAT.pdb' because it is being used by another process.) (The process cannot access the file 'C:\agents\A1_work\64\RAT.pdb' because it is being used by another process.) (The process cannot access the file 'C:\agents\A1_work\64\RAT.pdb' because it is being used by another process.) ---> System.IO.IOException: The process cannot access the file 'C:\agents\A1_work\64\RAT.pdb' because it is being used by another process.

BadImageFormatException during MetadataReaderProvider.FromPortablePdbStream in InstrumentationHelper.PortablePdbHasLocalSource, unable to check if module has got local source. TryWithCustomResolverOnDotNetCore for PresentationFramework, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35 Unable to instrument module: C:\agents\A1_work\64\RAT.exe Coverlet.Core.Exceptions.CecilAssemblyResolutionException: AssemblyResolutionException for 'PresentationFramework, Version=4.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: 'PresentationFramework, Version=4.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 129 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.AddParameter(UInt16 sequence, ParameterDefinition parameter, ParamTable table) at Mono.Cecil.MetadataBuilder.AddParameters(MethodDefinition method) at Mono.Cecil.MetadataBuilder.AddMethod(MethodDefinition method) at Mono.Cecil.MetadataBuilder.AddMethods(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.b__20(MetadataBuilder builder, MetadataReader ) at Mono.Cecil.ModuleDefinition.Read[TItem,TRet](TItem item, Func3 read) at Mono.Cecil.ModuleWriter.BuildMetadata(ModuleDefinition module, MetadataBuilder metadata) at Mono.Cecil.ModuleWriter.Write(ModuleDefinition module, Disposable1 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 325 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 135 One or more errors occurred. (The process cannot access the file 'C:\agents\A1_work\64\RAT.pdb' because it is being used by another process.) (The process cannot access the file 'C:\agents\A1_work\64\RAT.pdb' because it is being used by another process.) (The process cannot access the file 'C:\agents\A1_work\64\RAT.pdb' because it is being used by another process.) (The process cannot access the file 'C:\agents\A1_work\64\RAT.pdb' because it is being used by another process.) (The process cannot access the file 'C:\agents\A1_work\64\RAT.pdb' because it is being used by another process.) (The process cannot access the file 'C:\agents\A1_work\64\RAT.pdb' because it is being used by another process.) (The process cannot access the file 'C:\agents\A1_work\64\RAT.pdb' because it is being used by another process.) (The process cannot access the file 'C:\agents\A1_work\64\RAT.pdb' because it is being used by another process.) (The process cannot access the file 'C:\agents\A1_work\64\RAT.pdb' because it is being used by another process.) (The process cannot access the file 'C:\agents\A1_work\64\RAT.pdb' because it is being used by another process.) (The process cannot access the file 'C:\agents\A1_work\64\RAT.pdb' because it is being used by another process.) (The process cannot access the file 'C:\agents\A1_work\64\RAT.pdb' because it is being used by another process.) Unhandled exception. System.AggregateException: One or more errors occurred. (The process cannot access the file 'C:\agents\A1_work\64\RAT.pdb' because it is being used by another process.) (The process cannot access the file 'C:\agents\A1_work\64\RAT.pdb' because it is being used by another process.) (The process cannot access the file 'C:\agents\A1_work\64\RAT.pdb' because it is being used by another process.) (The process cannot access the file 'C:\agents\A1_work\64\RAT.pdb' because it is being used by another process.) (The process cannot access the file 'C:\agents\A1_work\64\RAT.pdb' because it is being used by another process.) (The process cannot access the file 'C:\agents\A1_work\64\RAT.pdb' because it is being used by another process.) (The process cannot access the file 'C:\agents\A1_work\64\RAT.pdb' because it is being used by another process.) (The process cannot access the file 'C:\agents\A1_work\64\RAT.pdb' because it is being used by another process.) (The process cannot access the file 'C:\agents\A1_work\64\RAT.pdb' because it is being used by another process.) (The process cannot access the file 'C:\agents\A1_work\64\RAT.pdb' because it is being used by another process.) (The process cannot access the file 'C:\agents\A1_work\64\RAT.pdb' because it is being used by another process.) (The process cannot access the file 'C:\agents\A1_work\64\RAT.pdb' because it is being used by another process.) ---> System.IO.IOException: The process cannot access the file 'C:\agents\A1_work\64\RAT.pdb' because it is being used by another process.

roryprimrose commented 1 month ago

We still have this issue on 6.0.0, 6.0.1 and 6.0.2. Version 3.2.0 still works fine.

aya-bjoseph commented 6 days ago

We still have this issue on 6.0.0, 6.0.1 and 6.0.2. Version 3.2.0 still works fine.

Same here. v6.0.2 has the "because it is being used by another process." error but v3.2.0 works without issue.