Search code examples
dotnet-cli

Show error summary when build fails using dotnet run/watch


When using dotnet run or dotnet watch the errors from the build phase are intermingled with warnings. This makes it hard to spot the build error and often requires a lot of scrolling of the terminal buffer. Take for example this output:

/some/file/name.razor(19,44): warning CS8602: Dereference of a possibly null reference. [/some/project/path.csproj]
/some/file/name.razor(55,31): warning CS8602: Dereference of a possibly null reference. [/some/project/path.csproj]
/some/file/name.razor(61,39): error CS0023: Operator '?' cannot be applied to operand of type 'EventCallback' [/some/project/path.csproj]
/some/file/name.razor(103,63): warning CS8602: Dereference of a possibly null reference. [/some/project/path.csproj]
/some/file/name.razor(104,73): warning CS8602: Dereference of a possibly null reference. [/some/project/path.csproj]
(... numerous warnings ...)
dotnet watch ⏳ Waiting for a file to change before restarting dotnet...

The error is burried deep amongst numerous warnings. Trying to figure out why the build fails is way more difficult than it should be.

Whereas dotnet build provides a nice build summary with the error:

(... numerous warnings ...)
/some/file/name.razor(103,63): warning CS8602: Dereference of a possibly null reference. [/some/project/path.csproj]
/some/file/name.razor(104,73): warning CS8602: Dereference of a possibly null reference. [/some/project/path.csproj]
/some/file/name.razor(61,39): error CS0023: Operator '?' cannot be applied to operand of type 'EventCallback' [/some/project/path.csrpoj]
    212 Warning(s)
    1 Error(s)

Time Elapsed 00:00:26.13

How can I get a similar build summary when using dotnet run and dotnet watch to run my project?


Solution

  • I've conditionally disabled analyzers in my build when running under dotnet watch. Add the following to Directory.Build.props and run as DISABLE_ANALYZERS=1 dotnet watch. As an added benefit the build will be faster as well:

    <Target Name="DisableAnalyzers" BeforeTargets="CoreCompile"
            Condition="'$(DISABLE_ANALYZERS)' == '1'">
        <PropertyGroup>
            <RunAnalyzersDuringBuild>false</RunAnalyzersDuringBuild>
            <Nullable>annotations</Nullable>
            <WarningLevel>0</WarningLevel>
        </PropertyGroup>
    </Target>