Skip to content

Analyzer warnings are not logged as warnings when DiagnosticId does not follow common pattern #1559

@taiscrummaster

Description

@taiscrummaster

Usage Information

NUKE Version: 9.0.4, SDK: 9.0.305, Target Framework: net9.0, Operating System: Win 11

Description

We use custom DiagnosticAnalyzer to analyze our code, for example to identify and warn of remaining Todo's. These analyzers use a generic DiagnosticID like 'TodoCommentAnalyzer'. These warnings are handled as such within the IDE's and also when execute dotnet build via the command line.
When executing the build with Nuke, these warnings are logged as debug entry and hence are not shown in the Errors & Warnings summary. Interestingly, Nuke still gets them as warnings since when treating warnings as errors, the build is halted with the correct exception.

Reproduction Steps

1. Create a custom DiagnosticAnalyzer with a generic DiagnosticId (overrice DiagnosticAnalyzer):

[DiagnosticAnalyzer(LanguageNames.CSharp)]
public class TodoCommentAnalyzer : DiagnosticAnalyzer
{
    private const string DiagnosticId = "TodoCommentAnalyzer";
    private const string Category = "DocumentationRules";
    private static readonly LocalizableString Title = new LocalizableResourceString(nameof(Resources.TodoAnalyzerTitle), Resources.ResourceManager, typeof(Resources));
    private static readonly LocalizableString MessageFormat = new LocalizableResourceString(nameof(Resources.TodoAnalyzerMessageFormat), Resources.ResourceManager, typeof(Resources));
    private static readonly LocalizableString Description = new LocalizableResourceString(nameof(Resources.TodoAnalyzerDescription), Resources.ResourceManager, typeof(Resources));

    internal static readonly DiagnosticDescriptor Rule = new(
        DiagnosticId,
        Title,
        MessageFormat,
        Category,
        DiagnosticSeverity.Warning,
        true,
        Description);

// Implementation of the DiagnosticAnalyzer

2. Reference this custom DiagnosticAnalyzer in any project where the analyzer should analyze the code

<ItemGroup>
        <ProjectReference Include="..\CustomAnalyzers\CustomAnalyzers.csproj"
                          OutputItemType="Analyzer"
                          ReferenceOutputAssembly="false" />
    </ItemGroup>

3. Build the project via Nuke's DotNetBuild

Target Integrate => x => x
        .Executes(
            () =>
            {
                return DotNetBuild(s => s
                        .SetProjectFile(this.Solution)
                        .SetConfiguration(Configuration.Release)
                        .EnableNoRestore()
                        .SetNoIncremental(true)
                );
            });

=> The analyzer is triggered, but the build does not show the analyzer warning as warning

Expected Behavior

The analyzer warning is shown as such in the build log and hence is included in the Errors & Warnings summary:
[WRN] C:\Work\Erowa.LedStorageAPI\source\Erowa.LedStorageApi.Foundation\FoundationModule.cs(18,9): warning TodoCommentAnalyzer: Todo comment found: 'Todo: LED' [C:\Work\Erowa.LedStorageAPI\source\Erowa.LedStorageApi.Foundation\Erowa.LedStorageApi.Foundation.csproj]

Actual Behavior

The analyzer warning is shown as debug log in the build log and hence is not included in the Errors & Warnings summary:
[DBG] C:\Work\Erowa.LedStorageAPI\source\Erowa.LedStorageApi.Foundation\FoundationModule.cs(18,9): warning TodoCommentAnalyzer: Todo comment found: 'Todo: LED' [C:\Work\Erowa.LedStorageAPI\source\Erowa.LedStorageApi.Foundation\Erowa.LedStorageApi.Foundation.csproj]

Regression?

n/a

Known Workarounds

Do not use generic DiagnosticId like 'TodoCommentAnalyzer', instead follow the common analyzer pattern like 'TD0001'.

Could you help with a pull-request?

No

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions