Building SonarAnalyzer.sln fails in DEBUG mode
Description
When building in DEBUG Mode, I (consistently) get the following error:
error CS2012: Cannot open 'C:\code\sonar-dotnet\analyzers\src\SonarAnalyzer.SourceGenerators\obj\Debug\netstandard2.0\Internal.SonarAnalyzer.SourceGenerators.dll' for writing -- The process cannot access the file 'C:\code\sonar-dotnet\analyzers\src\SonarAnalyzer.SourceGenerators\obj\Debug\netstandard2.0\Internal.SonarAnalyzer.SourceGenerators.dll' because it is being used by another process. [C:\code\sonar-dotnet\analyzers\src\SonarAnalyzer.SourceGenerators\SonarAnalyzer.SourceGenerators.csproj]
When I build in RELEASE mode, this file lock does not appear. Note, that the behavior is similar for building from the command line and within Visual Studio. I assume the same applies to Visual Studio Code too, but I did not test that.
Reproducer
dotnet build -c=DEBUG
Results in:
Determining projects to restore...
C:\code\sonar-dotnet\analyzers\src\SonarAnalyzer.SourceGenerators\SonarAnalyzer.SourceGenerators.csproj : warning NU1601: Dependency specified was Microsoft.CodeAnalysis.Analyzers (>= 4.7.0) but ended up with Microsoft.CodeAnalysis.Analyzers 4.14.0. [C:\code\sonar-dotnet\analyzers\SonarAnalyzer.sln]
C:\code\sonar-dotnet\analyzers\src\SonarAnalyzer.Core\SonarAnalyzer.Core.csproj : warning NU1903: Package 'Google.Protobuf' 3.6.1 has a known high severity vulnerability, https://github.com/advisories/GHSA-77rm-9x9h-xj3g [C:\code\sonar-dotnet\analyzers\SonarAnalyzer.sln]
C:\code\sonar-dotnet\analyzers\src\SonarAnalyzer.VisualBasic.Core\SonarAnalyzer.VisualBasic.Core.csproj : warning NU1701: Package 'Microsoft.Composition 1.0.27' was restored using '.NETFramework,Version=v4.6.1, .NETFramework,Version=v4.6.2, .NETFramework,Version=v4.7, .NETFramework,Version=v4.7.1, .NETFramework,Version=v4.7.2, .NETFramework,Version=v4.8, .NETFramework,Version=v4.8.1' instead of the project target framework '.NETStandard,Version=v2.0'. This package may not be fully compatible with your project. [C:\code\sonar-dotnet\analyzers\SonarAnalyzer.sln]
C:\code\sonar-dotnet\analyzers\src\SonarAnalyzer.CSharp\SonarAnalyzer.CSharp.csproj : warning NU1701: Package 'Microsoft.Composition 1.0.27' was restored using '.NETFramework,Version=v4.6.1, .NETFramework,Version=v4.6.2, .NETFramework,Version=v4.7, .NETFramework,Version=v4.7.1, .NETFramework,Version=v4.7.2, .NETFramework,Version=v4.8, .NETFramework,Version=v4.8.1' instead of the project target framework '.NETStandard,Version=v2.0'. This package may not be fully compatible with your project. [C:\code\sonar-dotnet\analyzers\SonarAnalyzer.sln]
C:\code\sonar-dotnet\analyzers\src\RuleDescriptorGenerator\RuleDescriptorGenerator.csproj : warning NU1701: Package 'Microsoft.Composition 1.0.27' was restored using '.NETFramework,Version=v4.6.1, .NETFramework,Version=v4.6.2, .NETFramework,Version=v4.7, .NETFramework,Version=v4.7.1, .NETFramework,Version=v4.7.2, .NETFramework,Version=v4.8, .NETFramework,Version=v4.8.1' instead of the project target framework 'net8.0'. This package may not be fully compatible with your project. [C:\code\sonar-dotnet\analyzers\SonarAnalyzer.sln]
C:\code\sonar-dotnet\analyzers\src\SonarAnalyzer.VisualBasic\SonarAnalyzer.VisualBasic.csproj : warning NU1701: Package 'Microsoft.Composition 1.0.27' was restored using '.NETFramework,Version=v4.6.1, .NETFramework,Version=v4.6.2, .NETFramework,Version=v4.7, .NETFramework,Version=v4.7.1, .NETFramework,Version=v4.7.2, .NETFramework,Version=v4.8, .NETFramework,Version=v4.8.1' instead of the project target framework '.NETStandard,Version=v2.0'. This package may not be fully compatible with your project. [C:\code\sonar-dotnet\analyzers\SonarAnalyzer.sln]
All projects are up-to-date for restore.
...
CSC : error CS2012: Cannot open 'C:\code\sonar-dotnet\analyzers\src\SonarAnalyzer.SourceGenerators\obj\Debug\netstandard2.0\Internal.SonarAnalyzer.SourceGenerators.dll' for writing -- The process cannot access the file 'C:\code\sonar-dotnet\analyzers\src\SonarAnalyzer.SourceGenerators\obj\Debug\netstandard2.0\Internal.SonarAnalyzer.SourceGenerators.dll' because it is being used by another process. [C:\code\sonar-dotnet\analyzers\src\SonarAnalyzer.SourceGenerators\SonarAnalyzer.SourceGenerators.csproj]
SonarAnalyzer.ShimLayer.CodeGeneration -> C:\code\sonar-dotnet\analyzers\src\SonarAnalyzer.ShimLayer.CodeGeneration\bin\Debug\netstandard2.0\Internal.SonarAnalyzer.ShimLayer.CodeGeneration.dll
...
CSC : error CS2012: Cannot open 'C:\code\sonar-dotnet\analyzers\src\SonarAnalyzer.SourceGenerators\obj\Debug\netstandard2.0\Internal.SonarAnalyzer.SourceGenerators.dll' for writing -- The process cannot access the file 'C:\code\sonar-dotnet\analyzers\src\SonarAnalyzer.SourceGenerators\obj\Debug\netstandard2.0\Internal.SonarAnalyzer.SourceGenerators.dll' because it is being used by another process. [C:\code\sonar-dotnet\analyzers\src\SonarAnalyzer.SourceGenerators\SonarAnalyzer.SourceGenerators.csproj]
190 Warning(s)
1 Error(s)
Time Elapsed 00:00:09.35
Product and Version
Commit Hash: 0b180f6af524fc4a58bf73095aad9897e1e42371
Hello @Corniel,
Do you get this error consistently?
We experienced this in the past, we investigated it but could not find why and, at the time, we decided to stop the investigation as we couldn't reproduce it consistently and the impact was low.
Our experiences would be something like this:
- First build fails
- Any subsequent build succeed (even a full Rebuild)
I can only reproduce it if I shutdown the build server with dotnet build-server shutdown.
For reference, this was the internal investigation ticket: https://trello.com/c/NQ15cwlI/914-understand-what-is-causing-the-intermittent-error-cs2012-during-our-sonaranalyzersourcegeneratorsdll-build
I could reproduce it multiple times. But at some point I succeeded. I try remember when I faced a generator based issue before (but I can not) Anyhow, some retries (including) a command line build, seems eventually solves the issue, but it is quite annoying.
I've got new findings, I'll report them here.