Skip to content

TaskAnalyzer: report MT migration diagnostics as suggestions for non-MT tasks - #14992

Draft
VolPlita with Copilot wants to merge 3 commits into
mainfrom
copilot/taskanalyzer-report-mt-suggestions
Draft

VolPlita with Copilot wants to merge 3 commits into
mainfrom
copilot/taskanalyzer-report-mt-suggestions

Conversation

Copilot AI commented Sep 10, 2026

Copy link
Copy Markdown
Contributor

Related issue(s):

Context

MT-related TaskAnalyzer diagnostics are useful migration guidance for every task, but reporting them as warnings on tasks that are not adopting multithreaded execution disrupts those builds. Previously the analyzer resolved this by suppressing MSBuildTask0002/0003 (and the MSBuildTask0005 findings carrying them) entirely outside MT-scoped code, so authors got no guidance at all unless they opted into full enforcement.

These diagnostics are now reported for all tasks, at a severity chosen from the task context: Info/Suggestion for non-MT tasks, unchanged Warning for MT-scoped code.

Changes Made

  • Contextual severity mechanism (SharedAnalyzerHelpers): CreateWithContextualSeverity reports a diagnostic at descriptor.DefaultSeverity for MT-scoped code and at MigrationGuidanceSeverity (Info) elsewhere. The diagnostic ID is unchanged, and the compiler applies dotnet_diagnostic.<ID>.severity after the analyzer reports, so an explicit setting still wins in both directions.
  • MSBuildTask0002 / MSBuildTask0003 (MultiThreadableTaskAnalyzer): the scope check ShouldReportEnvironmentRules became ShouldEnforceEnvironmentRules — it now selects severity instead of gating the report.
  • MSBuildTask0005 (TransitiveCallChainAnalyzer): transitive MT migration violations use the same contextual severity. IsReportedByDirectAnalyzer became GetDirectAnalyzerCoverage, returning None / Guidance / Full, so a call the direct analyzer already surfaces as a suggestion is not repeated as MSBuildTask0005 on the same call site, while an MT caller still gets the full-severity chain diagnostic through the same helper.
  • run_mt_analyzers_on_all_tasks interaction: unchanged as the opt-in for full enforcement — when true, all tasks report at Warning; when absent or false, non-MT tasks report at Info.
  • Rules outside 0002/0003/0005 keep their existing behavior and defaults; MSBuildTask0013 remains disabled by default.
  • README rule matrix and analysis-scope sections updated.
// Non-MT task, default configuration:
//   info MSBuildTask0002 — migration guidance, does not fail the build
public class PlainTask : Microsoft.Build.Utilities.Task
{
    public override bool Execute() => Environment.GetEnvironmentVariable("KEY") is not null;
}

// MT-scoped task: same ID, still a warning
[MSBuildMultiThreadableTask]
public class MtTask : Microsoft.Build.Utilities.Task, IMultiThreadableTask
{
    public TaskEnvironment TaskEnvironment { get; set; }

    // warning MSBuildTask0002
    public override bool Execute() => Environment.GetEnvironmentVariable("KEY") is not null;
}

Compatibility

Non-MT tasks that previously saw no MSBuildTask0002/0003/0005 migration diagnostics now see them as Info. This is visible in IDE and -v:detailed output but does not affect build success, including under /WarnAsError. MT-scoped severity, run_mt_analyzers_on_all_tasks semantics, and explicit dotnet_diagnostic.<ID>.severity configuration are unchanged. The MSBuildTask0002/0003 code fixes already withhold themselves where a TaskEnvironment reference would not compile, so they remain safe on tasks that now receive suggestions.

Testing

dotnet test src/TaskAnalyzer.Tests/TaskAnalyzer.Tests.csproj — 365 passing. Existing tests that asserted absence for non-MT tasks were converted to assert Info severity; new tests cover MT-scoped Warning, run_mt_analyzers_on_all_tasks = true raising non-MT tasks to Warning, dotnet_diagnostic.MSBuildTask0002.severity = error raising a non-MT suggestion, = suggestion lowering an MT warning, = none suppressing both 0002 and 0003, and an explicit override on transitive MSBuildTask0005.

dotnet msbuild src/Tasks/Microsoft.Build.Tasks.csproj -p:BuildAnalyzer=true -t:Rebuild produces the same diagnostics as before — src/Tasks/TaskAnalyzer.globalconfig sets run_mt_analyzers_on_all_tasks = true, so that build stays on full enforcement.

Dependencies and Follow-up

Open design point for reviewers: whether any diagnostics beyond MSBuildTask0002/0003 and the MSBuildTask0005 findings that carry them should also use contextual severity.

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Hello @copilot, I noticed that you’re changing an .swr file or any file under src/Package/MSBuild.VSSetup.. Please make sure to validate this change by an experimental VS insertion. This is accomplished by pushing to an exp/* branch, which requires write permissions to this repo.

Copilot AI and others added 2 commits September 10, 2026 10:42
…MT tasks

Co-authored-by: VolPlita <95765929+VolPlita@users.noreply.github.com>
Co-authored-by: VolPlita <95765929+VolPlita@users.noreply.github.com>
Copilot AI changed the title [WIP] Update TaskAnalyzer to report MT diagnostics as suggestions for non-MT tasks TaskAnalyzer: report MT migration diagnostics as suggestions for non-MT tasks Sep 10, 2026
Copilot AI requested a review from VolPlita September 10, 2026 10:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

TaskAnalyzer: report MT diagnostics as suggestions for non-MT tasks

2 participants