Add MSBuildTask0012: diagnose tasks constructed without TaskEnvironment propagation - #14813
Open
ViktorHofer with Copilot wants to merge 5 commits into
Open
Add MSBuildTask0012: diagnose tasks constructed without TaskEnvironment propagation#14813ViktorHofer with Copilot wants to merge 5 commits into
ViktorHofer with Copilot wants to merge 5 commits into
Conversation
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. |
…ropagation Co-authored-by: ViktorHofer <7412651+ViktorHofer@users.noreply.github.com>
…eation sites Co-authored-by: ViktorHofer <7412651+ViktorHofer@users.noreply.github.com>
Co-authored-by: ViktorHofer <7412651+ViktorHofer@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Fix TaskAnalyzer to propagate TaskEnvironment to inner tasks
Add MSBuildTask0012: diagnose tasks constructed without TaskEnvironment propagation
Aug 24, 2026
ViktorHofer
marked this pull request as ready for review
August 24, 2026 12:53
Contributor
There was a problem hiding this comment.
Pull request overview
Adds a new TaskAnalyzer diagnostic (MSBuildTask0012) to catch multithreadable tasks that construct inner ITask instances without propagating TaskEnvironment, addressing #14792 and preventing inner tasks from silently using TaskEnvironment.Fallback.
Changes:
- Introduces
TaskEnvironmentPropagationAnalyzer(MSBuildTask0012) to detect missing propagation via ctor args, object initializers, or later assignments. - Adds
TaskEnvironmentPropagationCodeFixProviderto auto-insertTaskEnvironment = <source>into object initializers when an instance source is available. - Updates analyzer documentation/spec text and adds new unit tests covering common construction/configuration patterns.
Show a summary per file
| File | Description |
|---|---|
| src/TaskAnalyzer/TaskEnvironmentPropagationCodeFixProvider.cs | New code fix provider for MSBuildTask0012 that adds a TaskEnvironment initializer entry when possible. |
| src/TaskAnalyzer/TaskEnvironmentPropagationAnalyzer.cs | New analyzer implementing MSBuildTask0012 detection and suppression via later assignment tracking. |
| src/TaskAnalyzer/README.md | Documents MSBuildTask0012 and updates rule/scope/code-fix tables. |
| src/TaskAnalyzer/DiagnosticIds.cs | Adds the MSBuildTask0012 diagnostic ID constant. |
| src/TaskAnalyzer/DiagnosticDescriptors.cs | Adds the MSBuildTask0012 descriptor and includes it in the descriptor list. |
| src/TaskAnalyzer/AnalyzerReleases.Unshipped.md | Records MSBuildTask0012 in the unshipped analyzer releases list. |
| src/TaskAnalyzer.Tests/TestHelpers.cs | Updates framework stubs to model ToolTask as IMultiThreadableTask with TaskEnvironment. |
| src/TaskAnalyzer.Tests/TaskEnvironmentPropagationCodeFixProviderTests.cs | New tests validating the code fix behavior (including static-context no-fix). |
| src/TaskAnalyzer.Tests/TaskEnvironmentPropagationAnalyzerTests.cs | New tests validating detection/suppression scenarios for MSBuildTask0012. |
| documentation/specs/multithreading/thread-safe-tasks.md | Adds guidance on propagating TaskEnvironment when a task constructs other tasks. |
Review details
💡 Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.
- Files reviewed: 10/10 changed files
- Comments generated: 1
- Review effort level: Lite
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Context
MSBuild assigns
TaskEnvironmentonly to the task instances it creates itself (TaskExecutionHost.cs:545, guarded byTaskInstance is IMultiThreadableTask). A task that constructs anotherITaskdirectly — the arcadeExecWithRetries→Execpattern — leaves the inner task onTaskEnvironment.Fallback, resolving paths and environment variables against the shared node state.No existing rule catches this: the outer task uses
TaskEnvironmentcorrectly throughout its own body and passes the analyzer cleanly, while handing the real work to a task with no environment.Changes Made
New rule MSBuildTask0012 (Warning) —
TaskEnvironmentPropagationAnalyzerreports an object creation when:IMultiThreadableTaskor[MSBuildMultiThreadableTask]) and holds a readableTaskEnvironmentmember of its own — a task with nothing to propagate is not reported;ITask;TaskEnvironmentproperty (as onToolTask, where it ispublic virtual) or a constructor parameter — keeping the diagnostic actionable;Suppression via later assignment spans the whole type: candidates are collected in a symbol-start action and reported in the symbol-end action, so a field configured in a helper method is recognized. Members are matched by type rather than by name, so any
TaskEnvironment-typed field or property counts as the source.Code fix —
TaskEnvironmentPropagationCodeFixProvideradds the missing entry, creating the object initializer when absent, and is skipped in static contexts where no instance member is reachable:Docs — rule section plus scope/severity/code-fix table rows in
src/TaskAnalyzer/README.md, a "Tasks That Construct Other Tasks" note indocumentation/specs/multithreading/thread-safe-tasks.md, and a row inAnalyzerReleases.Unshipped.md.Testing
20 new tests covering the arcade
ToolTaskshape, constructor injection, cross-method configuration, field initializers, implicitnew(), and static contexts; theTaskAnalyzer.Testssuite is at 268 passing.Verified end-to-end against the real built
Microsoft.Build.Framework/Microsoft.Build.Utilities.Core: the rule fires on a reproduction of the arcade pattern,dotnet format analyzers --diagnostics MSBuildTask0012applies the fix, and the rebuilt project is warning-free.Notes
src/Tasksbuilt with-p:BuildAnalyzer=trueproduces zero MSBuildTask0012 occurrences, so the rule was deliberately left out of that project'sWarningsNotAsErrorslist — if it ever fires there, the build should fail rather than warn.Known trade-off: a factory-style method returning
new SomeTask()is flagged, consistent with the issue's near-zero false-positive expectation. Diagnostics inside static members are reported but offer no fix, since there is no instance environment to reference.