为什么使用 Sonar Scanner 会阻止 C# 编译器创建 Razor Views 程序集?

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

我们有一个小型 Asp.Net 核心应用程序,其中包含一些 cshtml 文件。构建它运行 C# 编译器两次:

  1. 一次是为应用程序本身生成程序集,例如JavascriptClient.dll
  2. 另一次是制作 Razor Views 程序集,例如JavascriptClient.Views.dll

当我们激活声纳扫描仪时发生了奇怪的事情 - JavascriptClient.Views.dll 不再产生并且没有声纳警告输出!

我追踪到了 C# 编译器的 /errorlog 命令行开关。以下带注释的简短记录记录了问题:

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

所以 Views 程序集最初并不存在。

C:\src\JavascriptClient\JavascriptClient> fc.exe C:\temp.txt c:\temp.txt
Comparing files C:\TEMP.txt and C:\TEMP.TXT
***** C:\TEMP.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.TXT
/out:obj\Debug\netcoreapp2.2\JavascriptClient.Views.dll
/target:library
*****

这两个文件 c:\temp.txtc:\temp.txt 是相同的,除了对于单行差异 - 前者指定 /errorlog 开关。

C:\src\JavascriptClient\JavascriptClient> dotnet "C:\Program Files\dotnet\sdk.2.110\Roslyn\bincore\csc.dll" '@c:\temp.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

运行 参数来自c:\temp.txt的C#编译器成功,但是没有生成Views程序集!

C:\src\JavascriptClient\JavascriptClient> dotnet "C:\Program Files\dotnet\sdk.2.110\Roslyn\bincore\csc.dll" '@c:\temp.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

运行 没有 /errorlog 开关的相同命令行 会生成视图程序集!

请在下面找到 c:\temp.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.0\analyzers\dotnet\cs\Microsoft.AspNetCore.Mvc.Analyzers.dll"
/analyzer:C:\Users\me\.nuget\packages\microsoft.codeanalysis.analyzers.1.0\analyzers\dotnet\cs\Microsoft.CodeAnalysis.Analyzers.dll
/analyzer:C:\Users\me\.nuget\packages\microsoft.codeanalysis.analyzers.1.0\analyzers\dotnet\cs\Microsoft.CodeAnalysis.CSharp.Analyzers.dll
/analyzer:"C:\Program Files\dotnet\sdk\NuGetFallbackFolder\microsoft.entityframeworkcore.analyzers.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

注意,我已经从参数中删除了所有声纳分析器!

/errorlog 参数的文档在这里 - https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/compiler-options/listed-alphabetically 那里什么都没有。

所以,我迷路了。我该如何解决这个难题?我的意思是我想要 Sonar 和 Views 组件。

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 以获取更多信息。

尝试升级到更新版本的构建工具(v16.1 或更高版本)。