Search code examples
c#asp.net-coresonarqube

Why using Sonar Scanner prevents C# compiler from creating Razor Views assembly?


We have a small Asp.Net Core application that has a few cshtml files. Building it runs the C# compiler twice:

  1. Once is to produce the assembly for the application itself, e.g. JavascriptClient.dll
  2. The other time is to produce the Razor Views assembly, e.g. JavascriptClient.Views.dll

Strange thing happened when we activated the Sonar Scanner - JavascriptClient.Views.dll is no longer produced and no Sonar warnings are output!

I tracked it down to the /errorlog command line switch of the C# compiler. The following short annotated transcript captures the problem:

C:\\src\JavascriptClient\JavascriptClient> Test-Path .\obj\Debug\netcoreapp2.2\JavascriptClient.Views.dll
False

So the Views assembly does not exist initially.

C:\\src\JavascriptClient\JavascriptClient> fc.exe C:\temp\1.txt c:\temp\2.txt
Comparing files C:\TEMP\1.txt and C:\TEMP\2.TXT
***** C:\TEMP\1.txt
/out:obj\Debug\netcoreapp2.2\JavascriptClient.Views.dll
/errorlog:C:\\src\JavascriptClient\JavascriptClient\bin\Debug\netcoreapp2.2\JavascriptClient.Views.dll.RoslynCA
.json
/target:library
***** C:\TEMP\2.TXT
/out:obj\Debug\netcoreapp2.2\JavascriptClient.Views.dll
/target:library
*****

The two files c:\temp\1.txt and c:\temp\2.txt are identical except for a single line difference - the former specifies the /errorlog switch.

C:\\src\JavascriptClient\JavascriptClient> dotnet "C:\Program Files\dotnet\sdk\2.2.110\Roslyn\bincore\csc.dll" '@c:\temp\1.txt'
Microsoft (R) Visual C# Compiler version 2.10.0.0 (45b37117)
Copyright (C) Microsoft Corporation. All rights reserved.

C:\\src\JavascriptClient\JavascriptClient> Test-Path .\obj\Debug\netcoreapp2.2\JavascriptClient.Views.dll
False

Running the C# compiler with the parameters from c:\temp\1.txt succeeds, but the Views assembly is not generated!

C:\\src\JavascriptClient\JavascriptClient> dotnet "C:\Program Files\dotnet\sdk\2.2.110\Roslyn\bincore\csc.dll" '@c:\temp\2.txt'
Microsoft (R) Visual C# Compiler version 2.10.0.0 (45b37117)
Copyright (C) Microsoft Corporation. All rights reserved.

C:\\src\JavascriptClient\JavascriptClient> Test-Path .\obj\Debug\netcoreapp2.2\JavascriptClient.Views.dll

True

Running the same command line without the /errorlog switch does generate the Views assembly!

Please, find below the abridged version of c:\temp\1.txt:

/unsafe-
/checked-
/nowarn:NU5105,1701,1702,1701,1702
/nostdlib+
/errorreport:prompt
/warn:4
/define:TRACE;DEBUG;NETCOREAPP;NETCOREAPP2_2
/reference:...
...
/reference:...
/debug+
/debug:full
/filealign:512
/optimize-
/out:obj\Debug\netcoreapp2.2\JavascriptClient.Views.dll
/errorlog:C:\src\JavascriptClient\JavascriptClient\bin\Debug\netcoreapp2.2\JavascriptClient.Views.dll.RoslynCA.json
/target:library
/warnaserror+
/utf8output
/deterministic+
/analyzer:"C:\Program Files\dotnet\sdk\NuGetFallbackFolder\microsoft.aspnetcore.mvc.analyzers\2.2.0\analyzers\dotnet\cs\Microsoft.AspNetCore.Mvc.Analyzers.dll"
/analyzer:C:\Users\me\.nuget\packages\microsoft.codeanalysis.analyzers\1.1.0\analyzers\dotnet\cs\Microsoft.CodeAnalysis.Analyzers.dll
/analyzer:C:\Users\me\.nuget\packages\microsoft.codeanalysis.analyzers\1.1.0\analyzers\dotnet\cs\Microsoft.CodeAnalysis.CSharp.Analyzers.dll
/analyzer:"C:\Program Files\dotnet\sdk\NuGetFallbackFolder\microsoft.entityframeworkcore.analyzers\2.2.0\analyzers\dotnet\cs\Microsoft.EntityFrameworkCore.Analyzers.dll"
obj\Debug\netcoreapp2.2\Razor\Pages\Error.g.cshtml.cs obj\Debug\netcoreapp2.2\Razor\Pages\Index.g.cshtml.cs ...
/warnaserror+:NU1605

Notice, I have removed all the Sonar analyzers from the parameters!

The documentation for the /errorlog parameter is here - https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/compiler-options/listed-alphabetically There is nothing there really.

So, here I am lost. How do I resolve this conundrum? I mean I want both Sonar and Views assembly.


Solution

  • There was a bug in the csc compiler that prevented the output assembly being produced in some circumstances when the /errorlog switch is passed to MSBuild. This can show up when using the SonarScanner for MSBuild as it sets this build property. See this post for more information.

    Try upgrading to a newer version of the build tools (v16.1 or later).