dotnet build 不显示 Visual Studio 显示的 StyleCop 警告
dotnet build not showing StyleCop warnings that Visual Stuio does
我创建了一个小项目并添加了 StyleCop.Analyzers 包(基于 Roslyn)。当我在 Visual Studio 中构建时,我可以看到一些警告,这正是我所期望的。
但是,当我使用 dotnet CLI (dotnet build
) 构建它时,我没有收到任何警告。可以肯定的是,我也尝试使用 msbuild
,它也没有产生任何警告:
这让我想知道:这两种方法有什么区别,我怎样才能得到相同的结果?
这是因为警告不会阻止构建生成程序集。 Visual Studio 已经构建了程序集,因此当您调用 dotnet build
或 msbuild
时都看到没有任何更改,因此不会执行构建。如果您 运行 msbuild /t:rebuild
(运行 重建目标),您将在命令行上看到警告。
以下是我的机器上使用 Visual Studio 2017.6 和 dotnet core 2.0 时发生的情况:
C:\>dotnet new console -n stylcoptest
The template "Console Application" was created successfully.
Processing post-creation actions...
Running 'dotnet restore' on stylcoptest\stylcoptest.csproj...
Restoring packages for C:\stylcoptest\stylcoptest.csproj...
Generating MSBuild file C:\stylcoptest\obj\stylcoptest.csproj.nuget.g.props.
Generating MSBuild file C:\stylcoptest\obj\stylcoptest.csproj.nuget.g.targets.
Restore completed in 192.26 ms for C:\stylcoptest\stylcoptest.csproj.
Restore succeeded.
C:\>cd stylcoptest
>dotnet add package StyleCop.Analyzers --version 1.1.0-beta006
info : Adding PackageReference for package 'StyleCop.Analyzers' into project 'C:\stylcoptest\stylcoptest.csproj'
log : Restoring packages for C:\stylcoptest\stylcoptest.csproj...
log : Installing StyleCop.Analyzers 1.1.0-beta006.
info : Package 'StyleCop.Analyzers' is compatible with all the specified frameworks in project 'C:\stylcoptest\stylcoptest.csproj'
info : PackageReference for package 'StyleCop.Analyzers' version '1.1.0-beta006' added to file 'C:\stylcoptest\stylcoptest.csproj'
C:\stylcoptest> dotnet build
Microsoft (R) Build Engine version 15.7.163.20755 for .NET Core
Copyright (C) Microsoft Corporation. All rights reserved.
Restore completed in 41.27 ms for C:\stylcoptest\stylcoptest.csproj.
Program.cs(1,1): warning SA1633: The file header is missing or not located at the top of the file. [C:\stylcoptest\stylcoptest.csproj]
Program.cs(3,11): warning SA1300: Element 'stylcoptest' should begin with an uppercase letter [C:\stylcoptest\stylcoptest.csproj]
Program.cs(1,1): warning SA1200: Using directive should appear within a namespace declaration [C:\stylcoptest\stylcoptest.csproj]
Program.cs(5,11): warning SA1400: Element 'Program' should declare an access modifier [C:\stylcoptest\stylcoptest.csproj]
Program.cs(7,21): warning SA1400: Element 'Main' should declare an access modifier [C:\stylcoptest\stylcoptest.csproj]
CSC : warning SA0001: XML comment analysis is disabled due to project configuration [C:\stylcoptest\stylcoptest.csproj]
stylcoptest -> C:\Users\marol\Downloads\stylcoptest\bin\Debug\netcoreapp2.0\stylcoptest.dll
Build succeeded.
Program.cs(1,1): warning SA1633: The file header is missing or not located at the top of the file. [C:\stylcoptest\stylcoptest.csproj]
Program.cs(3,11): warning SA1300: Element 'stylcoptest' should begin with an uppercase letter [C:\stylcoptest\stylcoptest.csproj]
Program.cs(1,1): warning SA1200: Using directive should appear within a namespace declaration [C:\stylcoptest\stylcoptest.csproj]
Program.cs(5,11): warning SA1400: Element 'Program' should declare an access modifier [C:\stylcoptest\stylcoptest.csproj]
Program.cs(7,21): warning SA1400: Element 'Main' should declare an access modifier [C:\stylcoptest\stylcoptest.csproj]
CSC : warning SA0001: XML comment analysis is disabled due to project configuration [C:\stylcoptest\stylcoptest.csproj]
6 Warning(s)
0 Error(s)
Time Elapsed 00:00:03.84
C:\stylcoptest>msbuild /m /v:m
Microsoft (R) Build Engine version 15.6.85.37198 for .NET Framework
Copyright (C) Microsoft Corporation. All rights reserved.
stylcoptest -> C:\stylcoptest\bin\Debug\netcoreapp2.0\stylcoptest.dl
C:\stylcoptest>msbuild /m /v:m /t:rebuild
Microsoft (R) Build Engine version 15.6.85.37198 for .NET Framework
Copyright (C) Microsoft Corporation. All rights reserved.
Program.cs(1,1): warning SA1633: The file header is missing or not located at the top of the file. [C:\stylcoptest\stylcoptest.csproj]
Program.cs(1,1): warning SA1200: Using directive should appear within a namespace declaration [C:\stylcoptest\stylcoptest.csproj]
Program.cs(5,11): warning SA1400: Element 'Program' should declare an access modifier [C:\stylcoptest\stylcoptest.csproj]
Program.cs(3,11): warning SA1300: Element 'stylcoptest' should begin with an uppercase letter [C:\stylcoptest\stylcoptest.csproj]
Program.cs(7,21): warning SA1400: Element 'Main' should declare an access modifier [C:\stylcoptest\stylcoptest.csproj]
CSC : warning SA0001: XML comment analysis is disabled due to project configuration [C:\stylcoptest\stylcoptest.csproj]
stylcoptest -> C:\stylcoptest\bin\Debug\netcoreapp2.0\stylcoptest.dll
我创建了一个小项目并添加了 StyleCop.Analyzers 包(基于 Roslyn)。当我在 Visual Studio 中构建时,我可以看到一些警告,这正是我所期望的。
但是,当我使用 dotnet CLI (dotnet build
) 构建它时,我没有收到任何警告。可以肯定的是,我也尝试使用 msbuild
,它也没有产生任何警告:
这让我想知道:这两种方法有什么区别,我怎样才能得到相同的结果?
这是因为警告不会阻止构建生成程序集。 Visual Studio 已经构建了程序集,因此当您调用 dotnet build
或 msbuild
时都看到没有任何更改,因此不会执行构建。如果您 运行 msbuild /t:rebuild
(运行 重建目标),您将在命令行上看到警告。
以下是我的机器上使用 Visual Studio 2017.6 和 dotnet core 2.0 时发生的情况:
C:\>dotnet new console -n stylcoptest
The template "Console Application" was created successfully.
Processing post-creation actions...
Running 'dotnet restore' on stylcoptest\stylcoptest.csproj...
Restoring packages for C:\stylcoptest\stylcoptest.csproj...
Generating MSBuild file C:\stylcoptest\obj\stylcoptest.csproj.nuget.g.props.
Generating MSBuild file C:\stylcoptest\obj\stylcoptest.csproj.nuget.g.targets.
Restore completed in 192.26 ms for C:\stylcoptest\stylcoptest.csproj.
Restore succeeded.
C:\>cd stylcoptest
>dotnet add package StyleCop.Analyzers --version 1.1.0-beta006
info : Adding PackageReference for package 'StyleCop.Analyzers' into project 'C:\stylcoptest\stylcoptest.csproj'
log : Restoring packages for C:\stylcoptest\stylcoptest.csproj...
log : Installing StyleCop.Analyzers 1.1.0-beta006.
info : Package 'StyleCop.Analyzers' is compatible with all the specified frameworks in project 'C:\stylcoptest\stylcoptest.csproj'
info : PackageReference for package 'StyleCop.Analyzers' version '1.1.0-beta006' added to file 'C:\stylcoptest\stylcoptest.csproj'
C:\stylcoptest> dotnet build
Microsoft (R) Build Engine version 15.7.163.20755 for .NET Core
Copyright (C) Microsoft Corporation. All rights reserved.
Restore completed in 41.27 ms for C:\stylcoptest\stylcoptest.csproj.
Program.cs(1,1): warning SA1633: The file header is missing or not located at the top of the file. [C:\stylcoptest\stylcoptest.csproj]
Program.cs(3,11): warning SA1300: Element 'stylcoptest' should begin with an uppercase letter [C:\stylcoptest\stylcoptest.csproj]
Program.cs(1,1): warning SA1200: Using directive should appear within a namespace declaration [C:\stylcoptest\stylcoptest.csproj]
Program.cs(5,11): warning SA1400: Element 'Program' should declare an access modifier [C:\stylcoptest\stylcoptest.csproj]
Program.cs(7,21): warning SA1400: Element 'Main' should declare an access modifier [C:\stylcoptest\stylcoptest.csproj]
CSC : warning SA0001: XML comment analysis is disabled due to project configuration [C:\stylcoptest\stylcoptest.csproj]
stylcoptest -> C:\Users\marol\Downloads\stylcoptest\bin\Debug\netcoreapp2.0\stylcoptest.dll
Build succeeded.
Program.cs(1,1): warning SA1633: The file header is missing or not located at the top of the file. [C:\stylcoptest\stylcoptest.csproj]
Program.cs(3,11): warning SA1300: Element 'stylcoptest' should begin with an uppercase letter [C:\stylcoptest\stylcoptest.csproj]
Program.cs(1,1): warning SA1200: Using directive should appear within a namespace declaration [C:\stylcoptest\stylcoptest.csproj]
Program.cs(5,11): warning SA1400: Element 'Program' should declare an access modifier [C:\stylcoptest\stylcoptest.csproj]
Program.cs(7,21): warning SA1400: Element 'Main' should declare an access modifier [C:\stylcoptest\stylcoptest.csproj]
CSC : warning SA0001: XML comment analysis is disabled due to project configuration [C:\stylcoptest\stylcoptest.csproj]
6 Warning(s)
0 Error(s)
Time Elapsed 00:00:03.84
C:\stylcoptest>msbuild /m /v:m
Microsoft (R) Build Engine version 15.6.85.37198 for .NET Framework
Copyright (C) Microsoft Corporation. All rights reserved.
stylcoptest -> C:\stylcoptest\bin\Debug\netcoreapp2.0\stylcoptest.dl
C:\stylcoptest>msbuild /m /v:m /t:rebuild
Microsoft (R) Build Engine version 15.6.85.37198 for .NET Framework
Copyright (C) Microsoft Corporation. All rights reserved.
Program.cs(1,1): warning SA1633: The file header is missing or not located at the top of the file. [C:\stylcoptest\stylcoptest.csproj]
Program.cs(1,1): warning SA1200: Using directive should appear within a namespace declaration [C:\stylcoptest\stylcoptest.csproj]
Program.cs(5,11): warning SA1400: Element 'Program' should declare an access modifier [C:\stylcoptest\stylcoptest.csproj]
Program.cs(3,11): warning SA1300: Element 'stylcoptest' should begin with an uppercase letter [C:\stylcoptest\stylcoptest.csproj]
Program.cs(7,21): warning SA1400: Element 'Main' should declare an access modifier [C:\stylcoptest\stylcoptest.csproj]
CSC : warning SA0001: XML comment analysis is disabled due to project configuration [C:\stylcoptest\stylcoptest.csproj]
stylcoptest -> C:\stylcoptest\bin\Debug\netcoreapp2.0\stylcoptest.dll