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

Dll files opened by multiple processes during dotnet test #725

Closed
svenskmand opened this issue Feb 8, 2020 · 37 comments
Closed

Dll files opened by multiple processes during dotnet test #725

svenskmand opened this issue Feb 8, 2020 · 37 comments
Labels
needs more info More details are needed

Comments

@svenskmand
Copy link

svenskmand commented Feb 8, 2020

When I run dotnet test /p:CollectCoverage=true

I get the following exceptions that some dll files built by the project are opened by multiple processes:

C:\Users\CAGK\Desktop\datahub>dotnet test /p:CollectCoverage=true
Test run for C:\Users\CAGK\Desktop\datahub\Datahub.Tests\bin\Debug\netcoreapp3.1\Datahub.Tests.dll(.NETCoreApp,Version=v3.1)
Microsoft (R) Test Execution Command Line Tool Version 16.3.0
Copyright (c) Microsoft Corporation.  All rights reserved.

Starting test execution, please wait...

A total of 1 test files matched the specified pattern.

Test Run Successful.
Total tests: 20
     Passed: 20
 Total time: 15,5588 Seconds

Calculating coverage result...
C:\Users\CAGK\.nuget\packages\coverlet.msbuild\2.8.0\build\coverlet.msbuild.targets(36,5): error : One or more errors occurred. (The process cannot access the file 'C:\Users\CAGK\Desktop\datahub\Datahub.Tests\bin\Debug\netcoreapp3.1\Datahub.dll' because it is being used by another process.) (The process cannot access the file 'C:\Users\CAGK\Desktop\datahub\Datahub.Tests\bin\Debug\netcoreapp3.1\Datahub.dll' because it is being used by another process.) (The process cannot access the file 'C:\Users\CAGK\Desktop\datahub\Datahub.Tests\bin\Debug\netcoreapp3.1\Datahub.dll' because it is being used by another process.) (The process cannot access the file 'C:\Users\CAGK\Desktop\datahub\Datahub.Tests\bin\Debug\netcoreapp3.1\Datahub.dll' because it is being used by another process.) (The process cannot access the file 'C:\Users\CAGK\Desktop\datahub\Datahub.Tests\bin\Debug\netcoreapp3.1\Datahub.dll' because it is being used by another process.) (The process cannot access the file 'C:\Users\CAGK\Desktop\datahub\Datahub.Tests\bin\Debug\netcoreapp3.1\Datahub.dll' because it is being used by another process.) (The process cannot access the file 'C:\Users\CAGK\Desktop\datahub\Datahub.Tests\bin\Debug\netcoreapp3.1\Datahub.dll' because it is being used by another process.) (The process cannot access the file 'C:\Users\CAGK\Desktop\datahub\Datahub.Tests\bin\Debug\netcoreapp3.1\Datahub.dll' because it is being used by another process.) (The process cannot access the file 'C:\Users\CAGK\Desktop\datahub\Datahub.Tests\bin\Debug\netcoreapp3.1\Datahub.dll' because it is being used by another process.) (The process cannot access the file 'C:\Users\CAGK\Desktop\datahub\Datahub.Tests\bin\Debug\netcoreapp3.1\Datahub.dll' because it is being used by another process.) [C:\Users\CAGK\Desktop\datahub\Datahub.Tests\Datahub.Tests.csproj]
C:\Users\CAGK\.nuget\packages\coverlet.msbuild\2.8.0\build\coverlet.msbuild.targets(36,5): error :    at Coverlet.Core.Helpers.RetryHelper.Do[T](Func`1 action, Func`1 backoffStrategy, Int32 maxAttemptCount) in D:\git\coverletToRelease\src\coverlet.core\Helpers\RetryHelper.cs:line 59 [C:\Users\CAGK\Desktop\datahub\Datahub.Tests\Datahub.Tests.csproj]
C:\Users\CAGK\.nuget\packages\coverlet.msbuild\2.8.0\build\coverlet.msbuild.targets(36,5): error :    at Coverlet.Core.Helpers.RetryHelper.Retry(Action action, Func`1 backoffStrategy, Int32 maxAttemptCount) in D:\git\coverletToRelease\src\coverlet.core\Helpers\RetryHelper.cs:line 28 [C:\Users\CAGK\Desktop\datahub\Datahub.Tests\Datahub.Tests.csproj]
C:\Users\CAGK\.nuget\packages\coverlet.msbuild\2.8.0\build\coverlet.msbuild.targets(36,5): error :    at Coverlet.Core.Helpers.InstrumentationHelper.RestoreOriginalModule(String module, String identifier) in D:\git\coverletToRelease\src\coverlet.core\Helpers\InstrumentationHelper.cs:line 218 [C:\Users\CAGK\Desktop\datahub\Datahub.Tests\Datahub.Tests.csproj]
C:\Users\CAGK\.nuget\packages\coverlet.msbuild\2.8.0\build\coverlet.msbuild.targets(36,5): error :    at Coverlet.Core.Coverage.GetCoverageResult() in D:\git\coverletToRelease\src\coverlet.core\Coverage.cs:line 219 [C:\Users\CAGK\Desktop\datahub\Datahub.Tests\Datahub.Tests.csproj]
C:\Users\CAGK\.nuget\packages\coverlet.msbuild\2.8.0\build\coverlet.msbuild.targets(36,5): error :    at Coverlet.MSbuild.Tasks.CoverageResultTask.Execute() in D:\git\coverletToRelease\src\coverlet.msbuild.tasks\CoverageResultTask.cs:line 109 [C:\Users\CAGK\Desktop\datahub\Datahub.Tests\Datahub.Tests.csproj]

C:\Users\CAGK\Desktop\datahub>Unhandled exception. System.AggregateException: One or more errors occurred. (The process cannot access the file 'C:\Users\CAGK\Desktop\datahub\Datahub.Tests\bin\Debug\netcoreapp3.1\Datahub.dll' because it is being used by another process.) (The process cannot access the file 'C:\Users\CAGK\Desktop\datahub\Datahub.Tests\bin\Debug\netcoreapp3.1\Datahub.dll' because it is being used by another process.) (The process cannot access the file 'C:\Users\CAGK\Desktop\datahub\Datahub.Tests\bin\Debug\netcoreapp3.1\Datahub.dll' because it is being used by another process.) (The process cannot access the file 'C:\Users\CAGK\Desktop\datahub\Datahub.Tests\bin\Debug\netcoreapp3.1\Datahub.dll' because it is being used by another process.) (The process cannot access the file 'C:\Users\CAGK\Desktop\datahub\Datahub.Tests\bin\Debug\netcoreapp3.1\Datahub.dll' because it is being used by another process.) (The process cannot access the file 'C:\Users\CAGK\Desktop\datahub\Datahub.Tests\bin\Debug\netcoreapp3.1\Datahub.dll' because it is being used by another process.) (The process cannot access the file 'C:\Users\CAGK\Desktop\datahub\Datahub.Tests\bin\Debug\netcoreapp3.1\Datahub.dll' because it is being used by another process.) (The process cannot access the file 'C:\Users\CAGK\Desktop\datahub\Datahub.Tests\bin\Debug\netcoreapp3.1\Datahub.dll' because it is being used by another process.) (The process cannot access the file 'C:\Users\CAGK\Desktop\datahub\Datahub.Tests\bin\Debug\netcoreapp3.1\Datahub.dll' because it is being used by another process.) (The process cannot access the file 'C:\Users\CAGK\Desktop\datahub\Datahub.Tests\bin\Debug\netcoreapp3.1\Datahub.dll' because it is being used by another process.)
 ---> System.IO.IOException: The process cannot access the file 'C:\Users\CAGK\Desktop\datahub\Datahub.Tests\bin\Debug\netcoreapp3.1\Datahub.dll' because it is being used by another process.
   at System.IO.FileSystem.CopyFile(String sourceFullPath, String destFullPath, Boolean overwrite)
   at System.IO.File.Copy(String sourceFileName, String destFileName, Boolean overwrite)
   at Coverlet.Core.Helpers.FileSystem.Copy(String sourceFileName, String destFileName, Boolean overwrite) in D:\git\coverletToRelease\src\coverlet.core\Helpers\FileSystem.cs:line 33
   at Coverlet.Core.Helpers.InstrumentationHelper.<>c__DisplayClass10_0.<RestoreOriginalModules>b__0() in D:\git\coverletToRelease\src\coverlet.core\Helpers\InstrumentationHelper.cs:line 242
   at Coverlet.Core.Helpers.RetryHelper.<>c__DisplayClass0_0.<Retry>b__0() in D:\git\coverletToRelease\src\coverlet.core\Helpers\RetryHelper.cs:line 26
   at Coverlet.Core.Helpers.RetryHelper.Do[T](Func`1 action, Func`1 backoffStrategy, Int32 maxAttemptCount) in D:\git\coverletToRelease\src\coverlet.core\Helpers\RetryHelper.cs:line 52
   --- End of inner exception stack trace ---
   at Coverlet.Core.Helpers.RetryHelper.Do[T](Func`1 action, Func`1 backoffStrategy, Int32 maxAttemptCount) in D:\git\coverletToRelease\src\coverlet.core\Helpers\RetryHelper.cs:line 59
   at Coverlet.Core.Helpers.RetryHelper.Retry(Action action, Func`1 backoffStrategy, Int32 maxAttemptCount) in D:\git\coverletToRelease\src\coverlet.core\Helpers\RetryHelper.cs:line 28
   at Coverlet.Core.Helpers.InstrumentationHelper.RestoreOriginalModules() in D:\git\coverletToRelease\src\coverlet.core\Helpers\InstrumentationHelper.cs:line 239
   at Coverlet.Core.Helpers.InstrumentationHelper.<.ctor>b__3_0(Object s, EventArgs e) in D:\git\coverletToRelease\src\coverlet.core\Helpers\InstrumentationHelper.cs:line 24
   at System.AppContext.OnProcessExit()
 ---> (Inner Exception #1) System.IO.IOException: The process cannot access the file 'C:\Users\CAGK\Desktop\datahub\Datahub.Tests\bin\Debug\netcoreapp3.1\Datahub.dll' because it is being used by another process.
   at System.IO.FileSystem.CopyFile(String sourceFullPath, String destFullPath, Boolean overwrite)
   at System.IO.File.Copy(String sourceFileName, String destFileName, Boolean overwrite)
   at Coverlet.Core.Helpers.FileSystem.Copy(String sourceFileName, String destFileName, Boolean overwrite) in D:\git\coverletToRelease\src\coverlet.core\Helpers\FileSystem.cs:line 33
   at Coverlet.Core.Helpers.InstrumentationHelper.<>c__DisplayClass10_0.<RestoreOriginalModules>b__0() in D:\git\coverletToRelease\src\coverlet.core\Helpers\InstrumentationHelper.cs:line 242
   at Coverlet.Core.Helpers.RetryHelper.<>c__DisplayClass0_0.<Retry>b__0() in D:\git\coverletToRelease\src\coverlet.core\Helpers\RetryHelper.cs:line 26
   at Coverlet.Core.Helpers.RetryHelper.Do[T](Func`1 action, Func`1 backoffStrategy, Int32 maxAttemptCount) in D:\git\coverletToRelease\src\coverlet.core\Helpers\RetryHelper.cs:line 52<---

 ---> (Inner Exception #2) System.IO.IOException: The process cannot access the file 'C:\Users\CAGK\Desktop\datahub\Datahub.Tests\bin\Debug\netcoreapp3.1\Datahub.dll' because it is being used by another process.
   at System.IO.FileSystem.CopyFile(String sourceFullPath, String destFullPath, Boolean overwrite)
   at System.IO.File.Copy(String sourceFileName, String destFileName, Boolean overwrite)
   at Coverlet.Core.Helpers.FileSystem.Copy(String sourceFileName, String destFileName, Boolean overwrite) in D:\git\coverletToRelease\src\coverlet.core\Helpers\FileSystem.cs:line 33
   at Coverlet.Core.Helpers.InstrumentationHelper.<>c__DisplayClass10_0.<RestoreOriginalModules>b__0() in D:\git\coverletToRelease\src\coverlet.core\Helpers\InstrumentationHelper.cs:line 242
   at Coverlet.Core.Helpers.RetryHelper.<>c__DisplayClass0_0.<Retry>b__0() in D:\git\coverletToRelease\src\coverlet.core\Helpers\RetryHelper.cs:line 26
   at Coverlet.Core.Helpers.RetryHelper.Do[T](Func`1 action, Func`1 backoffStrategy, Int32 maxAttemptCount) in D:\git\coverletToRelease\src\coverlet.core\Helpers\RetryHelper.cs:line 52<---

 ---> (Inner Exception #3) System.IO.IOException: The process cannot access the file 'C:\Users\CAGK\Desktop\datahub\Datahub.Tests\bin\Debug\netcoreapp3.1\Datahub.dll' because it is being used by another process.
   at System.IO.FileSystem.CopyFile(String sourceFullPath, String destFullPath, Boolean overwrite)
   at System.IO.File.Copy(String sourceFileName, String destFileName, Boolean overwrite)
   at Coverlet.Core.Helpers.FileSystem.Copy(String sourceFileName, String destFileName, Boolean overwrite) in D:\git\coverletToRelease\src\coverlet.core\Helpers\FileSystem.cs:line 33
   at Coverlet.Core.Helpers.InstrumentationHelper.<>c__DisplayClass10_0.<RestoreOriginalModules>b__0() in D:\git\coverletToRelease\src\coverlet.core\Helpers\InstrumentationHelper.cs:line 242
   at Coverlet.Core.Helpers.RetryHelper.<>c__DisplayClass0_0.<Retry>b__0() in D:\git\coverletToRelease\src\coverlet.core\Helpers\RetryHelper.cs:line 26
   at Coverlet.Core.Helpers.RetryHelper.Do[T](Func`1 action, Func`1 backoffStrategy, Int32 maxAttemptCount) in D:\git\coverletToRelease\src\coverlet.core\Helpers\RetryHelper.cs:line 52<---

 ---> (Inner Exception #4) System.IO.IOException: The process cannot access the file 'C:\Users\CAGK\Desktop\datahub\Datahub.Tests\bin\Debug\netcoreapp3.1\Datahub.dll' because it is being used by another process.
   at System.IO.FileSystem.CopyFile(String sourceFullPath, String destFullPath, Boolean overwrite)
   at System.IO.File.Copy(String sourceFileName, String destFileName, Boolean overwrite)
   at Coverlet.Core.Helpers.FileSystem.Copy(String sourceFileName, String destFileName, Boolean overwrite) in D:\git\coverletToRelease\src\coverlet.core\Helpers\FileSystem.cs:line 33
   at Coverlet.Core.Helpers.InstrumentationHelper.<>c__DisplayClass10_0.<RestoreOriginalModules>b__0() in D:\git\coverletToRelease\src\coverlet.core\Helpers\InstrumentationHelper.cs:line 242
   at Coverlet.Core.Helpers.RetryHelper.<>c__DisplayClass0_0.<Retry>b__0() in D:\git\coverletToRelease\src\coverlet.core\Helpers\RetryHelper.cs:line 26
   at Coverlet.Core.Helpers.RetryHelper.Do[T](Func`1 action, Func`1 backoffStrategy, Int32 maxAttemptCount) in D:\git\coverletToRelease\src\coverlet.core\Helpers\RetryHelper.cs:line 52<---

 ---> (Inner Exception #5) System.IO.IOException: The process cannot access the file 'C:\Users\CAGK\Desktop\datahub\Datahub.Tests\bin\Debug\netcoreapp3.1\Datahub.dll' because it is being used by another process.
   at System.IO.FileSystem.CopyFile(String sourceFullPath, String destFullPath, Boolean overwrite)
   at System.IO.File.Copy(String sourceFileName, String destFileName, Boolean overwrite)
   at Coverlet.Core.Helpers.FileSystem.Copy(String sourceFileName, String destFileName, Boolean overwrite) in D:\git\coverletToRelease\src\coverlet.core\Helpers\FileSystem.cs:line 33
   at Coverlet.Core.Helpers.InstrumentationHelper.<>c__DisplayClass10_0.<RestoreOriginalModules>b__0() in D:\git\coverletToRelease\src\coverlet.core\Helpers\InstrumentationHelper.cs:line 242
   at Coverlet.Core.Helpers.RetryHelper.<>c__DisplayClass0_0.<Retry>b__0() in D:\git\coverletToRelease\src\coverlet.core\Helpers\RetryHelper.cs:line 26
   at Coverlet.Core.Helpers.RetryHelper.Do[T](Func`1 action, Func`1 backoffStrategy, Int32 maxAttemptCount) in D:\git\coverletToRelease\src\coverlet.core\Helpers\RetryHelper.cs:line 52<---

 ---> (Inner Exception #6) System.IO.IOException: The process cannot access the file 'C:\Users\CAGK\Desktop\datahub\Datahub.Tests\bin\Debug\netcoreapp3.1\Datahub.dll' because it is being used by another process.
   at System.IO.FileSystem.CopyFile(String sourceFullPath, String destFullPath, Boolean overwrite)
   at System.IO.File.Copy(String sourceFileName, String destFileName, Boolean overwrite)
   at Coverlet.Core.Helpers.FileSystem.Copy(String sourceFileName, String destFileName, Boolean overwrite) in D:\git\coverletToRelease\src\coverlet.core\Helpers\FileSystem.cs:line 33
   at Coverlet.Core.Helpers.InstrumentationHelper.<>c__DisplayClass10_0.<RestoreOriginalModules>b__0() in D:\git\coverletToRelease\src\coverlet.core\Helpers\InstrumentationHelper.cs:line 242
   at Coverlet.Core.Helpers.RetryHelper.<>c__DisplayClass0_0.<Retry>b__0() in D:\git\coverletToRelease\src\coverlet.core\Helpers\RetryHelper.cs:line 26
   at Coverlet.Core.Helpers.RetryHelper.Do[T](Func`1 action, Func`1 backoffStrategy, Int32 maxAttemptCount) in D:\git\coverletToRelease\src\coverlet.core\Helpers\RetryHelper.cs:line 52<---

 ---> (Inner Exception #7) System.IO.IOException: The process cannot access the file 'C:\Users\CAGK\Desktop\datahub\Datahub.Tests\bin\Debug\netcoreapp3.1\Datahub.dll' because it is being used by another process.
   at System.IO.FileSystem.CopyFile(String sourceFullPath, String destFullPath, Boolean overwrite)
   at System.IO.File.Copy(String sourceFileName, String destFileName, Boolean overwrite)
   at Coverlet.Core.Helpers.FileSystem.Copy(String sourceFileName, String destFileName, Boolean overwrite) in D:\git\coverletToRelease\src\coverlet.core\Helpers\FileSystem.cs:line 33
   at Coverlet.Core.Helpers.InstrumentationHelper.<>c__DisplayClass10_0.<RestoreOriginalModules>b__0() in D:\git\coverletToRelease\src\coverlet.core\Helpers\InstrumentationHelper.cs:line 242
   at Coverlet.Core.Helpers.RetryHelper.<>c__DisplayClass0_0.<Retry>b__0() in D:\git\coverletToRelease\src\coverlet.core\Helpers\RetryHelper.cs:line 26
   at Coverlet.Core.Helpers.RetryHelper.Do[T](Func`1 action, Func`1 backoffStrategy, Int32 maxAttemptCount) in D:\git\coverletToRelease\src\coverlet.core\Helpers\RetryHelper.cs:line 52<---

 ---> (Inner Exception #8) System.IO.IOException: The process cannot access the file 'C:\Users\CAGK\Desktop\datahub\Datahub.Tests\bin\Debug\netcoreapp3.1\Datahub.dll' because it is being used by another process.
   at System.IO.FileSystem.CopyFile(String sourceFullPath, String destFullPath, Boolean overwrite)
   at System.IO.File.Copy(String sourceFileName, String destFileName, Boolean overwrite)
   at Coverlet.Core.Helpers.FileSystem.Copy(String sourceFileName, String destFileName, Boolean overwrite) in D:\git\coverletToRelease\src\coverlet.core\Helpers\FileSystem.cs:line 33
   at Coverlet.Core.Helpers.InstrumentationHelper.<>c__DisplayClass10_0.<RestoreOriginalModules>b__0() in D:\git\coverletToRelease\src\coverlet.core\Helpers\InstrumentationHelper.cs:line 242
   at Coverlet.Core.Helpers.RetryHelper.<>c__DisplayClass0_0.<Retry>b__0() in D:\git\coverletToRelease\src\coverlet.core\Helpers\RetryHelper.cs:line 26
   at Coverlet.Core.Helpers.RetryHelper.Do[T](Func`1 action, Func`1 backoffStrategy, Int32 maxAttemptCount) in D:\git\coverletToRelease\src\coverlet.core\Helpers\RetryHelper.cs:line 52<---

 ---> (Inner Exception #9) System.IO.IOException: The process cannot access the file 'C:\Users\CAGK\Desktop\datahub\Datahub.Tests\bin\Debug\netcoreapp3.1\Datahub.dll' because it is being used by another process.
   at System.IO.FileSystem.CopyFile(String sourceFullPath, String destFullPath, Boolean overwrite)
   at System.IO.File.Copy(String sourceFileName, String destFileName, Boolean overwrite)
   at Coverlet.Core.Helpers.FileSystem.Copy(String sourceFileName, String destFileName, Boolean overwrite) in D:\git\coverletToRelease\src\coverlet.core\Helpers\FileSystem.cs:line 33
   at Coverlet.Core.Helpers.InstrumentationHelper.<>c__DisplayClass10_0.<RestoreOriginalModules>b__0() in D:\git\coverletToRelease\src\coverlet.core\Helpers\InstrumentationHelper.cs:line 242
   at Coverlet.Core.Helpers.RetryHelper.<>c__DisplayClass0_0.<Retry>b__0() in D:\git\coverletToRelease\src\coverlet.core\Helpers\RetryHelper.cs:line 26
   at Coverlet.Core.Helpers.RetryHelper.Do[T](Func`1 action, Func`1 backoffStrategy, Int32 maxAttemptCount) in D:\git\coverletToRelease\src\coverlet.core\Helpers\RetryHelper.cs:line 52<---


C:\Users\CAGK\Desktop\datahub>

This is my dotnet --info:

C:\Users\CAGK\Desktop\datahub>dotnet --info
.NET Core SDK (reflecting any global.json):
 Version:   3.1.100
 Commit:    cd82f021f4

Runtime Environment:
 OS Name:     Windows
 OS Version:  10.0.18363
 OS Platform: Windows
 RID:         win10-x64
 Base Path:   C:\Program Files\dotnet\sdk\3.1.100\

Host (useful for support):
  Version: 3.1.0
  Commit:  65f04fb6db

.NET Core SDKs installed:
  3.1.100 [C:\Program Files\dotnet\sdk]

.NET Core runtimes installed:
  Microsoft.AspNetCore.App 3.1.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 3.1.0 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.WindowsDesktop.App 3.1.0 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

To install additional .NET Core runtimes or SDKs:
  https://aka.ms/dotnet-download

C:\Users\CAGK\Desktop\datahub>

If I remove the coverage option then the command runs fine. I do not understand why this has started happening.

@MarcoRossignoli MarcoRossignoli added the needs more info More details are needed label Feb 10, 2020
@MarcoRossignoli
Copy link
Collaborator

MarcoRossignoli commented Feb 10, 2020

Some question:

Do you have only one test project?
Are you running command on solution file?
Can you try with dotnet test /p:CollectCoverage=true /nodeReuse:false -m:1?

From exception seem that after test run when coverlet tries to "restore" old files(because coverlet to work backup file instrument and at the end restore old one without instrumentation code inside) that files are in use by someone, so restoring fail. I found this issue sometimes when lib for some reason are "re-used" for different tests(related to some build config) in parallel or when you run big solutions with a lot of test projects(by default will run in parallel) and slow tests.

@MarcoRossignoli
Copy link
Collaborator

Advice: if possible you should use vstest integration https://github.com/tonerdo/coverlet/blob/master/Documentation/VSTestIntegration.md

@svenskmand
Copy link
Author

svenskmand commented Feb 10, 2020

Hi Marco :)

I just run the tests from the command line. I do not use Visual Studio, I use Visual Studio Code as my editor, but I run the tests from the command line using dotnet test /p:CollectCoverage=true /p:CoverletOutputFormat=\"lcov\" /p:CoverletOutput=\"./Datahub/lcov.info\"

When I just run dotnet test it works, so it has something to do with the options /p:CollectCoverage=true /p:CoverletOutputFormat=\"lcov\" /p:CoverletOutput=\"./Datahub/lcov.info\", but now it works again, so the problem seems to be intermittent :S

My Solution structure is like this:

Datahub (solution)
  Datahub (project) -> Protobuf (references the ProtoBuf project)
  Datahub.Tests (project) ->Datahub (references the Datahub project)
  ProtoBuf (project) This only contains all my *.proto files and the *.cs files generated from them

@MarcoRossignoli
Copy link
Collaborator

MarcoRossignoli commented Feb 10, 2020

Intermittent nature is expected(I mean it's a problem 😄 it's not correct but expected possible bad behaviour) on parallel test/build try with this:

dotnet test /p:CollectCoverage=true /p:CoverletOutputFormat=\"lcov\" /p:CoverletOutput=\"./Datahub/lcov.info\ -nr:false -m:1 

@svenskmand
Copy link
Author

svenskmand commented Feb 10, 2020

I tried dotnet test /p:CollectCoverage=true /p:CoverletOutputFormat=\"lcov\" /p:CoverletOutput=\"./Datahub/lcov.info\" /nodeReuse:false -m:1 which works, but currently the command that did not work before, i.e. dotnet test /p:CollectCoverage=true /p:CoverletOutputFormat=\"lcov\" /p:CoverletOutput=\"./Datahub/lcov.info\" also works.

I is possible to pass the options /nodeReuse:false -m:1 as properties in the csproj file? I have done that for the other options /p:CollectCoverage=true /p:CoverletOutputFormat=\"lcov\" /p:CoverletOutput=\"./Datahub/lcov.info\" like this:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <!-- ... -->
    <CollectCoverage>true</CollectCoverage>
    <CoverletOutputFormat>lcov</CoverletOutputFormat>
    <CoverletOutput>./Datahub/lcov.info</CoverletOutput>
  </PropertyGroup>
  <!-- ... -->
</Project>

and I would like to do it for the /nodeReuse:false -m:1 options also so I can just run my tests using dotnet test.

@svenskmand
Copy link
Author

Also I googled a little bit about running tests in parallel when searching for solutions to the problem and I found this flag /p:ParallelizeTestCollections=false does that do the same as /nodeReuse:false -m:1?

@MarcoRossignoli
Copy link
Collaborator

MarcoRossignoli commented Feb 10, 2020

but currently the command that did not work before,

Yes as I said it's possible because the nature of issue is "randomic" if for some timing reason who is using that dll free the lib before restoring issue doesn't bubble up...the flag -nr:false -m:1 said to msbuild to build and run tests sequentially and to not reuse the build hosting process. ParallelizeTestCollections seem an xunit settings to serialize tests...but the issue doesn't seem related to that, I mean when tests end nobody should use that lib anymore so it's not a problem with "tests" to me.

and I would like to do it for the -nr:false -m:1 options also so I can just run my tests using dotnet test.

I don't know if it works as xml props https://docs.microsoft.com/en-us/visualstudio/msbuild/msbuild-command-line-reference?view=vs-2019 try and tell me if it works, on docs is referred as command line arg.

@svenskmand
Copy link
Author

The -m (--maxcpucount) should be the property MSBuildNodeCount according to the link and I think that nodeReuse should be the property NodeReuse according to the link.

I will try and keep these two set and see if the issue occurs again. You think the issue occurs in the dotnet test command and not in coverlet?

@MarcoRossignoli
Copy link
Collaborator

MarcoRossignoli commented Feb 10, 2020

You think the issue occurs in the dotnet test command and not in coverlet?

It occour in coverlet but it's related to how build/test run. Coverlet restore all instrumented libs at the end of tests, we use an msbuild task(in this case) so seem that also if task should start at the end of tests someone(msbuild/test process) has got that dll loaded and doesn't allow to coverlet to restore(copy) files back.

You could try with vstest integration, maybe solve the issue because the "workflow" is completely different https://github.com/tonerdo/coverlet/blob/master/Documentation/VSTestIntegration.md and there is no msbuild interaction.

@Cheesebaron
Copy link

Cheesebaron commented Feb 11, 2020

We have this issue as well. It happens intermittenly in Azure DevOps. However, when I run locally it happens every time.

I tried adding both -m:1 and /nodeReuse:false but that doesn't seem to help.

The command I am running looks something like:

dotnet test "/Users/cheesebaron/git/DrivingRangeApp/TrackMan.DrivingRangeApp.Tests/TrackMan.DrivingRangeApp.Tests.csproj" --settings "/Users/cheesebaron/git/DrivingRangeApp/CodeCoverage.runsettings" --logger "xunit;LogFilePath=/Users/cheesebaron/git/DrivingRangeApp/artifacts/Tests/TrackMan.DrivingRangeApp.Tests.xml" --configuration Labs --no-build --collect:"XPlat Code Coverage" -m:1 /nodeReuse:false

Tried with dotnet vstest but it can't seem to find the data collector, even though I specify it and give it the same CodeCoverage.runsettings file as dotnet test runs with.

dotnet --info                  
                                                                                                                                                                                     .NET Core SDK (reflecting any global.json):
 Version:   3.0.101
 Commit:    bc5f8df0f5

Runtime Environment:
 OS Name:     Mac OS X
 OS Version:  10.14
 OS Platform: Darwin
 RID:         osx.10.14-x64
 Base Path:   /usr/local/share/dotnet/sdk/3.0.101/

Host (useful for support):
  Version: 3.1.1
  Commit:  a1388f194c

.NET Core SDKs installed:
  2.1.505 [/usr/local/share/dotnet/sdk]
  2.1.700 [/usr/local/share/dotnet/sdk]
  2.1.701 [/usr/local/share/dotnet/sdk]
  3.0.100-rc1-014190 [/usr/local/share/dotnet/sdk]
  3.0.100-rc2-014271 [/usr/local/share/dotnet/sdk]
  3.0.101 [/usr/local/share/dotnet/sdk]
  3.1.100 [/usr/local/share/dotnet/sdk]
  3.1.101 [/usr/local/share/dotnet/sdk]

.NET Core runtimes installed:
  Microsoft.AspNetCore.All 2.1.9 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.All 2.1.11 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.All 2.1.12 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.App 2.1.9 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 2.1.11 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 2.1.12 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 3.0.0-rc1.19457.4 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 3.0.0 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 3.0.1 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 3.1.0 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 3.1.1 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 2.1.9 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.1.11 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.1.12 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.1.13 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.1.14 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.1.15 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
  Microsoft.NETCore.App 3.0.0-rc1-19456-20 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
  Microsoft.NETCore.App 3.0.0 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
  Microsoft.NETCore.App 3.0.1 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
  Microsoft.NETCore.App 3.1.0 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
  Microsoft.NETCore.App 3.1.1 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]

To install additional .NET Core runtimes or SDKs:
  https://aka.ms/dotnet-download

Not sure if related, but it seems to me that it started happening after I bumped mono to 6.6.0 and .NET Core to 3.1.100, but not sure.

I tried adding a global.json to try make it run with .NET Core 3.0.100 but that doesn't seem to change anything.

@MarcoRossignoli
Copy link
Collaborator

It's possible have a repro?Are one of these project OSS?I remember another user with same issue but I cannot repro in my local so it's hard to understand.

Can you try with my custom patched version? #572 (comment)

@Cheesebaron
Copy link

@MarcoRossignoli it is unfortunately not OSS

@Cheesebaron
Copy link

OK! Just tried something. Lowered the version of Microsoft.NET.Test.Sdk from 16.5.0 to 16.4.0 and now it doesn't fail every time on on my machine anymore. Will investigate a little more.

@MarcoRossignoli
Copy link
Collaborator

OK! Just tried something. Lowered the version of Microsoft.NET.Test.Sdk from 16.5.0 to 16.4.0 and now it doesn't fail every time on on my machine anymore.

I've got a suspect...can you try with my patched version? #572 (comment)

@Cheesebaron
Copy link

Cheesebaron commented Feb 11, 2020

@MarcoRossignoli yep, on it now.

Ran it with:

    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" />
    <PackageReference Include="coverlet.collector" Version="1.1.9-g42c952b491" />

and config looking like:

<?xml version="1.0" encoding="utf-8"?>
<!-- File name extension must be .runsettings -->
<RunSettings>
    <DataCollectionRunSettings>
        <DataCollectors>
            <DataCollector friendlyName="XPlat code coverage">
                <Configuration>
                    <Format>cobertura</Format>
                    <Exclude>[xunit.*]*,[*Tests]*,[System.*]*</Exclude> <!-- [Assembly-Filter]Type-Filter -->
                    <ExcludeByAttribute>Obsolete,GeneratedCodeAttribute,CompilerGeneratedAttribute</ExcludeByAttribute>
                </Configuration>
            </DataCollector>
        </DataCollectors>
    </DataCollectionRunSettings>
    <InProcDataCollectionRunSettings>
    <InProcDataCollectors>
        <InProcDataCollector
            assemblyQualifiedName="Coverlet.Collector.DataCollection.CoverletInProcDataCollector, coverlet.collector, Version=1.1.0.0, Culture=neutral, PublicKeyToken=null"
            friendlyName="XPlat Code Coverage"
            enabled="True"
            codebase="coverlet.collector.dll" />
        </InProcDataCollectors>
    </InProcDataCollectionRunSettings>
</RunSettings>

Seems to run fine.

@MarcoRossignoli
Copy link
Collaborator

MarcoRossignoli commented Feb 11, 2020

Please try removing <InProcDataCollectionRunSettings> sections from xml and add --diag:log.txt and re-run, check that on log file *host* there is some row with [coverlet] prefix, I want to understand of you're loading inproc collector correctly

https://github.com/tonerdo/coverlet/blob/master/Documentation/Troubleshooting.md#collectors-integration

@Cheesebaron
Copy link

Cheesebaron commented Feb 12, 2020

Yes, the host log file contains this, when I remove the <InProcDataCollectionRunSettings> node from the config

TpTrace Verbose: 0 : 5736, 4, 2020/02/12, 16:06:56.884, 3170021842796425, testhost.dll, [coverlet]Initialize CoverletInProcDataCollector


TpTrace Verbose: 0 : 5736, 4, 2020/02/12, 16:07:04.478, 3170029436956824, testhost.dll, [coverlet]Calling ModuleTrackerTemplate.UnloadModule for 'TrackMan.DrivingRangeApp, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'
TpTrace Verbose: 0 : 5736, 4, 2020/02/12, 16:07:04.481, 3170029440183782, testhost.dll, [coverlet]Called ModuleTrackerTemplate.UnloadModule for 'TrackMan.DrivingRangeApp, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'


TpTrace Warning: 0 : 5736, 4, 2020/02/12, 16:07:04.488, 3170029447655724, testhost.dll, [coverlet]CoverletInProcDataCollector: Failed to get Instrumentation class with error: System.Reflection.ReflectionTypeLoadException: Unable to load one or more of the requested types.
Could not load file or assembly 'Microsoft.CodeAnalysis, Version=2.6.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. The system cannot find the file specified.

Could not load file or assembly 'Microsoft.CodeAnalysis, Version=2.6.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. The system cannot find the file specified.

Could not load file or assembly 'Microsoft.CodeAnalysis.Workspaces, Version=2.6.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. The system cannot find the file specified.

Could not load file or assembly 'Microsoft.CodeAnalysis, Version=2.6.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. The system cannot find the file specified.

Could not load file or assembly 'Microsoft.CodeAnalysis, Version=2.6.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. The system cannot find the file specified.

Could not load file or assembly 'Microsoft.CodeAnalysis, Version=2.6.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. The system cannot find the file specified.

Could not load file or assembly 'Microsoft.CodeAnalysis, Version=2.6.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. The system cannot find the file specified.

Could not load file or assembly 'Microsoft.CodeAnalysis, Version=2.6.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. The system cannot find the file specified.

Could not load file or assembly 'Microsoft.CodeAnalysis.Workspaces, Version=2.6.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. The system cannot find the file specified.

Could not load file or assembly 'Microsoft.CodeAnalysis.Workspaces, Version=2.6.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. The system cannot find the file specified.

Could not load file or assembly 'Microsoft.CodeAnalysis.Workspaces, Version=2.6.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. The system cannot find the file specified.

   at System.Reflection.RuntimeModule.GetTypes(RuntimeModule module)
   at System.Reflection.RuntimeModule.GetTypes()
   at System.Reflection.Assembly.GetTypes()
   at Coverlet.Collector.DataCollection.CoverletInProcDataCollector.GetInstrumentationClass(Assembly assembly) in C:\git\coverlet\src\coverlet.collector\InProcDataCollection\CoverletInProcDataCollector.cs:line 65

@MarcoRossignoli
Copy link
Collaborator

Seem ok...the errors are logged so are not a problem, the essential part is

TpTrace Verbose: 0 : 5736, 4, 2020/02/12, 16:07:04.478, 3170029436956824, testhost.dll, [coverlet]Calling ModuleTrackerTemplate.UnloadModule for 'TrackMan.DrivingRangeApp, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'
TpTrace Verbose: 0 : 5736, 4, 2020/02/12, 16:07:04.481, 3170029440183782, testhost.dll, [coverlet]Called ModuleTrackerTemplate.UnloadModule for 'TrackMan.DrivingRangeApp, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'

Double check....if you only update to official 1.2.0 does it work or start to fail?

@MarcoRossignoli
Copy link
Collaborator

@Cheesebaron can you try with our nightly build we fixed the issue of race condition in collectors #835
This is the guide of nightly https://github.com/coverlet-coverage/coverlet/blob/master/Documentation/ConsumeNightlyBuild.md

@svenskmand does this issue solved?

@Cheesebaron
Copy link

@MarcoRossignoli yes. Will have a look tomorrow. Do you want me to test with coverlet nightly and latest version of Test.Sdk?

@MarcoRossignoli
Copy link
Collaborator

@MarcoRossignoli yes. Will have a look tomorrow. Do you want me to test with coverlet nightly and latest version of Test.Sdk?

Yes thanks

@Cheesebaron
Copy link

It seems to work. Tried a couple of runs with coverlet.collector version 1.3.0-preview.17.g3c55332cce and Microsoft.NET.Test.Sdk 16.6.1

@MarcoRossignoli
Copy link
Collaborator

Great!Thanks!

@AntiPasha
Copy link

AntiPasha commented May 20, 2020

Hi, I've got the same errors, mentioned above, with both msbuild and vstest integrations. Interesting point here, the error is observed with tests written with xUnit, with NUnit everything work just fine. Maybe it's related to xunit.runner? Tests and code itself target .NET Core 2.1, installed SDK details are below

.NET Core SDK (reflecting any global.json):
 Version:   3.1.202
 Commit:    6ea70c8dca

Runtime Environment:
 OS Name:     Windows
 OS Version:  10.0.18363
 OS Platform: Windows
 RID:         win10-x64
 Base Path:   C:\Program Files\dotnet\sdk\3.1.202\

Host (useful for support):
  Version: 3.1.4
  Commit:  0c2e69caa6

.NET Core SDKs installed:
  2.1.806 [C:\Program Files\dotnet\sdk]
  3.1.202 [C:\Program Files\dotnet\sdk]

.NET Core runtimes installed:
  Microsoft.AspNetCore.All 2.1.18 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.All 2.2.8 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.App 2.1.18 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 2.2.8 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 3.1.4 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 2.1.18 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.2.8 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 3.1.4 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.WindowsDesktop.App 3.1.4 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

The relevant packages

<PackageReference Include="coverlet.collector" Version="1.2.1">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>
    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.6.1" />
    <PackageReference Include="Moq" Version="4.14.1" />
    <PackageReference Include="xunit" Version="2.4.1" />
    <PackageReference Include="xunit.runner.visualstudio" Version="2.4.1">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>

Adding -nr:false -m:1 didn't help, unfortunately

@MarcoRossignoli
Copy link
Collaborator

Can you attach the error?

@AntiPasha
Copy link

AntiPasha commented May 20, 2020

Hi @MarcoRossignoli please have a look at logs generated by MSBuild integration (I've just removed the sensitive names) The process cannot access the file error is repeated a multiple times

C:\Users\xxx\.nuget\packages\coverlet.msbuild\2.8.1\build\coverlet.msbuild.targets(36,5): error : One or more errors occurred. (The process cannot access the file 'D:\Work\xxx\yyy\test\yyy.Tests\bin\Debug\netcoreapp2.1\zzz.Dto.dll' because it is being used by another process.)[D:\Work\xxx\yyy\test\yyy.Tests\yyy.Tests.csproj]
C:\Users\xxx\.nuget\packages\coverlet.msbuild\2.8.1\build\coverlet.msbuild.targets(36,5): error :    at Coverlet.Core.Helpers.RetryHelper.Do[T](Func`1 action, Func`1 backoffStrategy, Int32 maxAttemptCount) in c:\git\coverletToRelease\src\coverlet.core\Helpers\RetryHelper.cs:line 59 [D:\Work\xxx\yyy\test\yyy.Tests\yyy.Tests.csproj]
C:\Users\xxx\.nuget\packages\coverlet.msbuild\2.8.1\build\coverlet.msbuild.targets(36,5): error :    at Coverlet.Core.Helpers.RetryHelper.Retry(Action action, Func`1 backoffStrategy, Int32 maxAttemptCount) in c:\git\coverletToRelease\src\coverlet.core\Helpers\RetryHelper.cs:line 28 [D:\Work\xxx\yyy\test\yyy.Tests\yyy.Tests.csproj]
C:\Users\xxx\.nuget\packages\coverlet.msbuild\2.8.1\build\coverlet.msbuild.targets(36,5): error :    at Coverlet.Core.Helpers.InstrumentationHelper.RestoreOriginalModule(String module, String identifier) in c:\git\coverletToRelease\src\coverlet.core\Helpers\InstrumentationHelper.cs:line 219 [D:\Work\xxx\yyy\test\yyy.Tests\yyy.Tests.csproj]
C:\Users\xxx\.nuget\packages\coverlet.msbuild\2.8.1\build\coverlet.msbuild.targets(36,5): error :    at Coverlet.Core.Coverage.GetCoverageResult() in c:\git\coverletToRelease\src\coverlet.core\Coverage.cs:line 219 [D:\Work\xxx\yyy\test\yyy.Tests\yyy.Tests.csproj]
C:\Users\xxx\.nuget\packages\coverlet.msbuild\2.8.1\build\coverlet.msbuild.targets(36,5): error :    at Coverlet.MSbuild.Tasks.CoverageResultTask.Execute() in c:\git\coverletToRelease\src\coverlet.msbuild.tasks\CoverageResultTask.cs:line 113 [D:\Work\xxx\yyy\test\yyy.Tests\yyy.Tests.csproj]

The same error is observed with VSTest integration

 ---> (Inner Exception #1) System.IO.IOException: The process cannot access the file 'D:\Work\xxx\yyy\test\yyy.Tests\bin\Debug\netcoreapp2.1\zzz.dll' because it is being used by another process.
   at System.IO.FileSystem.CopyFile(String sourceFullPath, String destFullPath, Boolean overwrite)
   at System.IO.File.Copy(String sourceFileName, String destFileName, Boolean overwrite)
   at Coverlet.Core.Helpers.FileSystem.Copy(String sourceFileName, String destFileName, Boolean overwrite) in c:\git\coverletToRelease\src\coverlet.core\Helpers\FileSystem.cs:line 33
   at Coverlet.Core.Helpers.InstrumentationHelper.<>c__DisplayClass10_0.<RestoreOriginalModule>b__0() in c:\git\coverletToRelease\src\coverlet.core\Helpers\InstrumentationHelper.cs:line 215
   at Coverlet.Core.Helpers.RetryHelper.<>c__DisplayClass0_0.<Retry>b__0() in c:\git\coverletToRelease\src\coverlet.core\Helpers\RetryHelper.cs:line 26
   at Coverlet.Core.Helpers.RetryHelper.Do[T](Func`1 action, Func`1 backoffStrategy, Int32 maxAttemptCount) in c:\git\coverletToRelease\src\coverlet.core\Helpers\RetryHelper.cs:line 52<---

Thank you!

@MarcoRossignoli
Copy link
Collaborator

If you run for only one csproj does this error happen?I mean run something like dotnet test ....TestProject.csproj /p:CollectCoverage=true

@AntiPasha
Copy link

Yes, error the same. I have only one test project here. I've tried to run using different ways, by specifying csproj, by running dotnet test at solution and project folders. Interesting part here is that it fails even I specify --no-build switch as well (in one of linked issues I found that instrumentation will not run in this case, maybe I'm wrong)

@MarcoRossignoli
Copy link
Collaborator

--no-build is not an issue...the error I see is the rollback of instrumentation, when tests end coverlet get a file with hits(from tmp) and calculate coverage, after that rollback dll to old ones(moved before IL instrumentation to another tmp folder so we can run more than one instrumentation and also we maintain dll in a original state), and seem that someone is locking files in your bin/Debug folder.
I don't think it's related to coverlet if it fails also with single project(tests was to understand if that dll was loaded more than one time in different tests, out of coverlet control but maybe could be the culprit), you could try to understand who is blocking that files(antivirus?), you can start with this guide https://support.qlik.com/articles/000005295
I cannot repro in my local, in past we had some issue with hits file but was related to our bug with VSTest integration fixed on master(we'll release asap) #835 but no with dll.

@AntiPasha
Copy link

AntiPasha commented May 21, 2020

Thank you, I'll have a look. Can xunit.runner or testhost lock these files? Because I have a bunch of nunit tests in different repo, they work fine. Output folder for nunit tests doesn't contain testhost.exe and dll, but for xunit does

@MarcoRossignoli
Copy link
Collaborator

MarcoRossignoli commented May 21, 2020

Can xunit.runner or testhost lock these files?

I know a bit testhost(worked on vstest repo) and never seen that issue, I don't know nothing about xunit runner...btw it's hard to guess...could be everything, we need to analyze with some tecnique above.

@AntiPasha
Copy link

AntiPasha commented May 21, 2020

I've collected some events using Process Monitor, there no processes, except dotnet.exe and testhost.exe accessing the file. During usual test run (without coverage) there is only file open, query information and locking file with only readers by testhost.exe.
When coverage is run, there are a file reading, writing and locking file with only readers by both processes. After file is locked by testhost.exe, dotnet.exe is returning a sharing violation result when attempt to use a file for generic read and write. It seems that both processes have something like a race condition, when file is locked for reading by one process and second process attempts to access it with read/write access.

Attached two files with events, first one for run with /p:CollectCoverage=true, second one with regular dotnet test. Attempt to run with --collect:"XPlat Code Coverage" shows the same behavior with msbuild integration
coverlet_msbuild.xlsx
dotnet test.xlsx

@MarcoRossignoli
Copy link
Collaborator

MarcoRossignoli commented May 23, 2020

@AntiPasha two things, first one, can you file a new issue and attach your first and last comment?This issue is old aged stale and solved in part, and I want a clean history for your one.
Second, is the behaviour the same with version 2.8.1 or 1.2.0?I mean it's first time you use coverlet or it's a version regression?

@AntiPasha
Copy link

@MarcoRossignoli Thank you, I've created new 857 issue fro that. It's first time, when I try to use coverlet

@hippieZhou
Copy link

Some question:

Do you have only one test project? Are you running command on solution file? Can you try with dotnet test /p:CollectCoverage=true /nodeReuse:false -m:1?

From exception seem that after test run when coverlet tries to "restore" old files(because coverlet to work backup file instrument and at the end restore old one without instrumentation code inside) that files are in use by someone, so restoring fail. I found this issue sometimes when lib for some reason are "re-used" for different tests(related to some build config) in parallel or when you run big solutions with a lot of test projects(by default will run in parallel) and slow tests.

I also have this problem in my multiple test projects, when I use dotnet test -m:1 by CLI, all my test cases can passed.

@svenskmand
Copy link
Author

@Cheesebaron can you try with our nightly build we fixed the issue of race condition in collectors #835 This is the guide of nightly https://github.com/coverlet-coverage/coverlet/blob/master/Documentation/ConsumeNightlyBuild.md

@svenskmand does this issue solved?

I have not had the issue in a long time, I am currently using version 3.2.0 of both coverlet.collector and coverlet.msbuild. So it might have been resolved :S

Thanks for all your help and time :)

@Cheesebaron
Copy link

Yeah, I haven't seen it forever after updating, so I think all is good. Still running coverlet so should be fine to keep this closed.

Thanks for following up :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs more info More details are needed
Projects
None yet
Development

No branches or pull requests

5 participants