Repository navigation
Correct MT routing docs and add analyzer rules for mismatched declarations - #14809
Merged
Merged
Conversation
…tions `[MSBuildMultiThreadableTask]` and `IMultiThreadableTask` do different jobs, but the spec and `TaskRouter`'s own doc comment both implied they were interchangeable alternatives. The attribute is the routing signal read by `TaskRouter.NeedsTaskHostInMultiThreadedMode`; the interface is the injection signal read by `TaskExecutionHost`. The interface can never be a routing signal, because `ToolTask` implements it -- routing on it would opt in every `ToolTask`-derived task in the ecosystem, unaudited. Docs: - Rewrite the spec's "Thread-Safe Capability Indicators" section around the routing/injection split, with a comparison table and both silent failure modes. - Drop "customers using newer MSBuild versions should prefer the Interface-Based declaration" -- the attribute is required either way. - Note that deriving from `MultiThreadableTask` supplies the interface but not the attribute. - Fix the attribute snippet, which showed `internal` and omitted `Inherited = false`, contradicting the surrounding text. - Rewrite the `TaskRouter` class comment to say why the interface is not consulted. Analyzer: - MSBuildTask0012 (Warning, on by default): the task carries the attribute and declares a `TaskEnvironment` property but not the interface, so the engine never assigns the property and it silently stays `TaskEnvironment.Fallback` while the task runs in-process. Attribute-only without the property is a supported compatibility-bridge state and is not reported. A task declaring a public single-`TaskEnvironment` constructor is also not reported: the engine selects that constructor by signature, independently of the interface. - MSBuildTask0013 (Info, off by default): the task declares the interface but lacks the attribute, so it still pays for a TaskHost. Only a type declaring the interface in its own base list is reported -- inheriting it from `ToolTask` says nothing about the derived task's intent. Fixes dotnet#14779 Fixes dotnet#14790 Co-authored-by: Copilot <[email protected]> Copilot-Session: be19d794-fe4f-43fe-9e6e-f6e6b13ec6e7
ViktorHofer
temporarily deployed
to
copilot-pat-pool
August 24, 2026 06:05 — with
GitHub Actions
Inactive
ViktorHofer
temporarily deployed
to
copilot-pat-pool
August 24, 2026 06:06 — with
GitHub Actions
Inactive
Contributor
There was a problem hiding this comment.
Pull request overview
This PR clarifies the multithreaded-task contract in MSBuild by documenting the distinct roles of [MSBuildMultiThreadableTask] (routing) vs IMultiThreadableTask (TaskEnvironment injection), and adds analyzer diagnostics to catch the two common “half-migrated” shapes that previously failed silently.
Changes:
- Updates multithreading docs (spec +
TaskRoutercomment) to state that the attribute is the only routing signal, and explains why the interface cannot be used for routing (ToolTask inheritance). - Adds two new TaskAnalyzer rules: MSBuildTask0012 (enabled Warning) and MSBuildTask0013 (disabled-by-default Info) plus unshipped analyzer release notes.
- Adds a new analyzer implementation and a dedicated test suite covering positive/negative cases (including ToolTask-like inheritance).
Show a summary per file
| File | Description |
|---|---|
| src/TaskAnalyzer/README.md | Documents the new MSBuildTask0012/0013 rules and their intended migration guidance. |
| src/TaskAnalyzer/MultiThreadableTaskDeclarationAnalyzer.cs | Implements the new analyzer that detects mismatches between routing and injection declarations. |
| src/TaskAnalyzer/DiagnosticIds.cs | Adds IDs MSBuildTask0012 and MSBuildTask0013. |
| src/TaskAnalyzer/DiagnosticDescriptors.cs | Adds descriptors (severity/default-enabled behavior) for the new diagnostics. |
| src/TaskAnalyzer/AnalyzerReleases.Unshipped.md | Records the new analyzer rules for release tracking. |
| src/TaskAnalyzer.Tests/MultiThreadableTaskDeclarationAnalyzerTests.cs | Adds coverage for both diagnostics, including the ToolTask-like inheritance exclusion. |
| src/Build/BackEnd/Components/RequestBuilder/TaskRouter.cs | Fixes the class-level comment to match the attribute-only routing behavior and rationale. |
| documentation/specs/multithreading/thread-safe-tasks.md | Rewrites the “capability indicators” section around the routing/injection split and documents silent failure modes. |
Review details
💡 Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.
Suppressed comments (1)
src/TaskAnalyzer/DiagnosticDescriptors.cs:122
- The message/description currently assert the property "stays TaskEnvironment.Fallback". That is not guaranteed: the task may initialize TaskEnvironment to a different value, but MSBuild still won’t inject the proper environment unless the interface (or a TaskEnvironment constructor) is used. Rewording to "MSBuild never assigns it" + "retains its default" would be more accurate.
messageFormat: "Task '{0}' declares a TaskEnvironment property but does not implement IMultiThreadableTask, so the engine never assigns it and it stays TaskEnvironment.Fallback; implement IMultiThreadableTask or add a public constructor taking a TaskEnvironment",
category: "MSBuild.TaskAuthoring",
defaultSeverity: DiagnosticSeverity.Warning,
isEnabledByDefault: true,
description: "The MSBuild engine assigns TaskEnvironment only to tasks that implement IMultiThreadableTask, or through a public constructor that takes a single TaskEnvironment. A task that declares the property without either receives no environment: the property silently remains TaskEnvironment.Fallback and every path is resolved against the shared process working directory rather than the project directory. Because the task carries [MSBuildMultiThreadableTask] it runs in-process, which is exactly where that resolution is wrong.");
- Files reviewed: 8/8 changed files
- Comments generated: 4
- Review effort level: Lite
…nt both fixes The PR text claimed an unassigned `TaskEnvironment` property "stays `TaskEnvironment.Fallback`". MSBuild only guarantees that it never assigns the property; the value that survives is whatever the task itself initialized it to, which is `null` when there is no initializer. Asserting a specific default was exactly the kind of imprecision this PR set out to fix. Reworded the diagnostic title, message, description, release-tracking note, spec bullet and README example accordingly. The README also offered only one fix. The analyzer treats a public single-`TaskEnvironment` constructor as equally valid, and the diagnostic message already mentioned it, so document both. Verified while writing it that the constructor body must assign the property itself: `TaskExecutionHost` gates its post-construction assignment on `is IMultiThreadableTask` (TaskExecutionHost.cs:545), so a task without the interface gets no second chance. `LoadedType` picks the constructor purely by parameter type name (LoadedType.cs:341-346), independent of the interface, which is what makes the exclusion correct in the first place. Also fixed a wrong cross-reference anchor to the MSBuildTask0011 section. Co-authored-by: Copilot <[email protected]> Copilot-Session: be19d794-fe4f-43fe-9e6e-f6e6b13ec6e7
AR-May
approved these changes
Aug 25, 2026
AR-May
left a comment
Member
There was a problem hiding this comment.
Overall new analyzer looks good to me. Have some comments concerning docs update.
…SBuildTask0014 Spec: - The attribute alone is the capability declaration; the interface is an additional mechanism for TaskEnvironment access. A task needing only the attribute is fully migrated. - Correct the "interface only" bullet: the out-of-proc host does not assign the property. It supplies TaskEnvironment.Fallback to a TaskEnvironment constructor when one is declared, and otherwise leaves the property at the task's own default, which is correct because Fallback is backed by MultiProcessTaskEnvironmentDriver and the host process is dedicated. - Remove the MultiThreadableTask abstract class section; that type was never added. - Move the ToolTask routing rationale to the existing Alternatives appendix and drop a repeated sentence from the attribute section. Analyzer: - Add MSBuildTask0014, a warning for [MSBuildMultiThreadableTask] applied to a type that does not implement ITask, where nothing reads it. The common cause is the attribute landing on a helper type beside the real task, leaving the task unmarked and still routed to a TaskHost. Co-authored-by: Copilot <[email protected]> Copilot-Session: be19d794-fe4f-43fe-9e6e-f6e6b13ec6e7
TaskRouter reads [MSBuildMultiThreadableTask] with inherit: false, off the concrete type the engine has just instantiated as a task. The attribute therefore only takes effect on a non-abstract class that implements ITask. The rule previously covered only one of the two inert shapes. An abstract task carrying the attribute is equally inert, and quieter: the engine never instantiates that type, and because the attribute is not inherited every concrete subclass is still routed to an out-of-proc TaskHost while looking fully migrated. Both shapes usually mean the attribute landed on the wrong class -- a helper type beside the real task, or a shared base instead of each task deriving from it. Rule is now 'attribute has no effect on this type', with the reason carried in the message. Both shapes remain zero-false-positive. Co-authored-by: Copilot <[email protected]> Copilot-Session: be19d794-fe4f-43fe-9e6e-f6e6b13ec6e7
AR-May
approved these changes
Aug 25, 2026
This was referenced Aug 25, 2026
ViktorHofer
added a commit
that referenced
this pull request
Aug 26, 2026
#14809 merged first and claimed MSBuildTask0012 through MSBuildTask0014, so the rule added here moves from MSBuildTask0012 to MSBuildTask0015. Conflicts were confined to the four shared registration points -- DiagnosticIds, DiagnosticDescriptors, AnalyzerReleases.Unshipped.md and README.md -- where both sides appended to the same lists; both sets of rules are kept, ordered by ID. MSBuildTask0015 covers every concrete task type, so it subsumes the merged MSBuildTask0013, which reports the same missing attribute on the narrower set of tasks that declare IMultiThreadableTask. That relationship is now called out in the descriptor and the README so the two opt-ins are not enabled redundantly. Co-authored-by: Copilot <[email protected]> Copilot-Session: be19d794-fe4f-43fe-9e6e-f6e6b13ec6e7
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.
Fixes #14779
Fixes #14790
The problem
[MSBuildMultiThreadableTask]andIMultiThreadableTaskdo different jobs, but both the spec andTaskRouter's own doc comment described them as interchangeable alternatives:[MSBuildMultiThreadableTask]TaskRouter.NeedsTaskHostInMultiThreadedModeIMultiThreadableTaskTaskEnvironmentTaskExecutionHostThe spec went further and told authors on newer MSBuild versions to prefer the interface. Following that advice produces a task that resolves paths correctly and then runs in a TaskHost anyway, gaining nothing. This tripped up a real migration of ~150 tasks across
dotnet/arcade,dotnet/source-build-assetsand the VMR.The interface cannot be promoted to a routing signal, which was the original suggestion in #14779.
ToolTaskimplementsIMultiThreadableTask(src/Utilities/ToolTask.cs), so routing on it would silently opt in everyToolTask-derived task in the ecosystem, none of which have been reviewed for thread safety. The attribute stays the routing signal; the docs and the analyzer are what need to change.Documentation (#14790)
TaskEnvironmentaccess, and a task that needs only the attribute is fully migrated.TaskEnvironment.Fallbackto aTaskEnvironmentconstructor when one is declared, and otherwise leaves the property at the task's own default. That is correct there, becauseFallbackis backed byMultiProcessTaskEnvironmentDriverand the host process is dedicated to a single task. Existing coverage:TaskHost_MultiThreadableTask_Tests.InheritedTask_InTaskHost_HasUsableTaskEnvironment.MultiThreadableTaskabstract base class section — that type was never added.internal classand omittedInherited = false— contradicting the text two lines below it and the actual declaration insrc/Framework/MSBuildMultiThreadableTaskAttribute.cs.TaskRouterclass-level doc comment to state that the attribute is the only routing signal, and why the interface is deliberately not consulted.Analyzer (#14779)
Three new rules turn these mismatches into build diagnostics instead of silent runtime behaviour.
MSBuildTask0012 —
TaskEnvironmentproperty is never assigned (Warning, on by default)Fires when a task has the attribute, declares a settable
TaskEnvironmentproperty, and does not implement the interface. The engine never assigns the property, so it retains the task's own default and resolves every path against the shared process working directory — while the attribute has put the task in-process, which is exactly where that is wrong.Two shapes are deliberately not reported:
TaskEnvironmentproperty. This is the supported compatibility-bridge state and is correct for a task that does not resolve relative paths or read environment variables. 26 Arcade tasks intentionally sit here.TaskEnvironmentconstructor, without the interface.LoadedTypeselects that constructor by signature alone, independently of the interface, so the task does receive an environment. Flagging it would have been a false positive.MSBuildTask0013 — Missing
[MSBuildMultiThreadableTask](Info, off by default)Fires when a task declares the interface but lacks the attribute, so it still pays for a TaskHost. Off by default because that is a valid intermediate migration state — path handling done, thread-safety review not yet complete.
Only a type that declares the interface in its own base list is reported. Because
ToolTaskimplementsIMultiThreadableTask, keying onAllInterfaceswould have fired on everyToolTasksubclass in the ecosystem — the same trap as the routing bug this PR documents.MSBuildTask0014 — Attribute has no effect on this type (Warning, on by default)
TaskRouterreads[MSBuildMultiThreadableTask]withinherit: false, off the concrete type the engine has just instantiated as a task. The attribute therefore only takes effect on a non-abstract class that implementsITask. Two shapes are reported.A concrete task MSBuild cannot construct — no public parameterless constructor and no public single-
TaskEnvironmentconstructor — is a third inert shape, deliberately not reported:Task.RegisterTask(string, Func<TaskEnvironment, ITask>)lets a host supply an arbitrary factory, so such a task may be perfectly reachable.Validation
src/TaskAnalyzerandsrc/Buildbuild clean (0 warnings, 0 errors).MultiThreadableTaskDeclarationAnalyzerTests, including negative cases for both MSBuildTask0012 exclusions above, forToolTask-like bases, for a non-task type without the attribute, and for an abstract base without the attribute. TheToolTaskand0013tests run with the rule explicitly enabled, so they cannot pass trivially by virtue of the rule being off by default.TaskAnalyzer.Testssuite: 266/266.