我创建了一个小项目并添加了StyleCop.Analyzers包(基于Roslyn)。当我在Visual Studio中构建时,我可以看到一些警告,这就是我所期望的。
但是,当我使用dotnet CLI(dotnet build
)构建它时,我没有收到任何警告。可以肯定的是,我也尝试使用msbuild
,它没有产生任何警告:
所以我想知道:有什么区别,我怎样才能在两种方法中得到相同的结果?
答案 0 :(得分:5)
这是因为警告不会阻止构建生成程序集。 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