I've created a small project and added the StyleCop.Analyzers package (based on Roslyn). When I build in Visual Studio, I can see some warnings, which is what I expect.
However, when I build it using the dotnet CLI (dotnet build
), I get no warnings. Just to be sure, I also tried using msbuild
, which didn't produce any warnings either:
So that has me wondering: what is the difference and how can I get the same result in both approaches?
This is because warnings do not stop the build from generating an assembly. Visual Studio already built the assembly so when you call dotnet build
or msbuild
both see that there have been no changes and do not perform a build. If you run msbuild /t:rebuild
(run the rebuild target) you will see the warning on the commandline.
Below is what happens on my machine with Visual Studio 2017.6 and 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