【问题标题】:How to fetch warnings from the dotnet build command如何从 dotnet build 命令获取警告
【发布时间】:2021-07-16 10:26:07
【问题描述】:

给定一个 .Net C# 项目,我想检查构建是否包含警告。运行命令时

dotnet build -consoleloggerparameters:"Summary;Verbosity=normal" -m -p:"WarnLevel=5;EnforceCodeStyleInBuild=true" -t:"clean,build"

我得到了预期的输出

Build succeeded.
    2 Warning(s)
    0 Error(s)

但是退出代码是 0,那么我怎么知道该构建是否包含警告?

我创建了一个 shell 脚本来玩

#!/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

但是如何扩展脚本以检查是否成功构建并带有警告?配置的 CI 管道应该可以通过,但如果出现警告,我想将它们标记为不稳定。

如果可能,我想避免使用正则表达式。


编辑:

我不想添加标志 -warnaserror,因为这样 CI 管道将失败,您将无法知道是否发生了构建错误或代码样式不好。

【问题讨论】:

    标签: c# .net shell exit-code


    【解决方案1】:

    您只需将 /WarnAsError 添加到您的 dotnet 构建中即可。

    示例代码:

    using System;
    
    namespace ConsoleApp1
    {
       class Program
        {
            static void Main(string[] args)
            {
               Console.WriteLine("Hello World!");
               int x = 0;
            }
        }
    }
    

    dotnet 构建的输出:

    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
    

    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
    

    编辑: OP 要求在警告时不要使构建失败。

    您可以尝试将警告写入文件,然后计算该文件中的行数。

    示例:

    我把上面的代码加了几个变量:

    int a = 0;
    int b = 0;
    int c = 0;
    int d = 0;
    int e = 0;
    

    现在当我编译时,我收到 5 个警告。现在我将它添加到我的 dotnet 构建中:

    $warningsFile = "SomeFile.log"
    dotnet build -fl1 "/flp1:logFile=$warningsFile;warningsonly"
    

    之后我可以简单地计算该文件中的行数。 使用 powershell:

    gc $warningsFile | Measure-Object -Line
    

    结果:

    Lines Words Characters Property
    ----- ----- ---------- --------
        5
    

    我不确定这是否适用于所有情况,但至少它会告诉您是否有警告。 希望对你有帮助

    【讨论】:

    • 嘿,谢谢你的帮助。不幸的是,我不想使用该标志,因为该阶段被视为失败
    • 嗯,是的,我认为捕获它们的最佳方法是将整个输出写入变量,回显它们并对变量执行正则表达式。
    • 是的,但问题是正则表达式也可能获取其他单词:S 所以我不确定这个解决方案github.com/microsoft/azure-pipelines-tasks/issues/…
    • 更新了我的答案
    • 是的,你是对的,有人会这么认为。显然,这不是我们需要的全部命令。您需要做的是在之前添加 logFile,所以整个调用将是:dotnet build -consoleloggerparameters:"Summary;Verbosity=normal" -m -p:"WarnLevel=5;EnforceCodeStyleInBuild=true" -t:"clean,build" -fl1 "/flp1:logFile=$BUILD_WARNINGS_LOG_FILE;warningsonly"
    猜你喜欢
    • 2020-04-03
    • 2018-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-19
    • 2021-03-04
    • 1970-01-01
    • 2018-12-17
    相关资源
    最近更新 更多