Given a .Net C# project I want to check if the build contains warnings. When running the command
dotnet build -consoleloggerparameters:"Summary;Verbosity=normal" -m -p:"WarnLevel=5;EnforceCodeStyleInBuild=true" -t:"clean,build"
I get the expected output
Build succeeded.
2 Warning(s)
0 Error(s)
But the exit code is 0, so how do I know if that build contains warnings?
I created a shell script to play around
#!/bin/bash
dotnet build -consoleloggerparameters:"Summary;Verbosity=normal" -m -p:"WarnLevel=5;EnforceCodeStyleInBuild=true" -t:"clean,build"
if [ $? -eq 0 ]
then
echo ">>> Success"
else
echo ">>> Contains warnings or errors"
fi
read
but how can I extend the script to check for a successful build with warnings? The configured CI pipelines should pass but I want to mark them as unstable if warnings occured.
I would like to avoid Regex magic if possible.
Edit:
I don't want to add the flag -warnaserror because then CI pipelines will fail and you won't get to know if build errors occured or the code style is just bad.
What you can do is simply add /WarnAsError to your dotnet build.
Example code:
using System;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
int x = 0;
}
}
}
Output of dotnet build:
dotnet build
Microsoft (R) Build Engine version 16.10.0+4242f381a for .NET
Copyright (C) Microsoft Corporation. All rights reserved.
Determining projects to restore...
All projects are up-to-date for restore.
Program.cs(10,17): warning CS0219: The variable 'x' is assigned but its value is never used [ConsoleApp1.csproj]
ConsoleApp1 -> ConsoleApp1.dll
Build succeeded.
\Program.cs(10,17): warning CS0219: The variable 'x' is assigned but its value is never used [ConsoleApp1.csproj]
1 Warning(s)
0 Error(s)
Time Elapsed 00:00:00.79
Output of dotnet build /WarnAsError:
dotnet build /WarnAsError
Microsoft (R) Build Engine version 16.10.0+4242f381a for .NET
Copyright (C) Microsoft Corporation. All rights reserved.
Determining projects to restore...
Restored ConsoleApp1.csproj (in 60 ms).
Program.cs(10,17): error CS0219: The variable 'x' is assigned but its value is never used [ConsoleApp1.csproj]
ConsoleApp1 -> ConsoleApp1.dll
Build FAILED.
Program.cs(10,17): error CS0219: The variable 'x' is assigned but its value is never used [ConsoleApp1.csproj]
0 Warning(s)
1 Error(s)
Time Elapsed 00:00:01.00
EDIT: OP requested to not fail the build upon warning.
Something you can try, is to have the warnings be written to a file and afterwards count the number of lines in that file.
Exmample:
I took the code above and added several variables:
int a = 0;
int b = 0;
int c = 0;
int d = 0;
int e = 0;
Now when I compile, I get 5 warnings. Now I added this to my dotnet build:
$warningsFile = "SomeFile.log"
dotnet build -fl1 "/flp1:logFile=$warningsFile;warningsonly"
Afterwards I can simply count the number of lines in that file. Using powershell:
gc $warningsFile | Measure-Object -Line
Results in:
Lines Words Characters Property
----- ----- ---------- --------
5
I'm not sure whether this will work in all cases, but at least it will tell you whether you had warnings or not. Hope this helps you