Repository navigation
Anchor MSBuildTask0003 code fix on the flagged call and withhold it where TaskEnvironment cannot bind - #14810
Conversation
|
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. |
Co-authored-by: ViktorHofer <7412651+ViktorHofer@users.noreply.github.com>
Co-authored-by: ViktorHofer <7412651+ViktorHofer@users.noreply.github.com>
Co-authored-by: ViktorHofer <7412651+ViktorHofer@users.noreply.github.com>
Co-authored-by: ViktorHofer <7412651+ViktorHofer@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
This pull request updates the TaskAnalyzer code fix provider for MSBuildTask0002/MSBuildTask0003 so fixes are applied to the exact flagged call/argument and are withheld in contexts where TaskEnvironment cannot be referenced, and expands the test suite and documentation to cover these scenarios.
Changes:
- Anchor the code fix on the analyzer-reported syntax node (
FindNode(..., getInnermostNodeForTie: true)) and semantically select the correct “path” argument to wrap. - Add guarding to withhold MSBuildTask0002/0003 fixes when referencing the instance
TaskEnvironmentmember would not compile. - Add targeted code-fix tests and update TaskAnalyzer README guidance (including FixAll caveats).
Show a summary per file
| File | Description |
|---|---|
| src/TaskAnalyzer/MultiThreadableTaskCodeFixProvider.cs | Re-anchors and refines MSBuildTask0002/0003 code-fix selection and adds TaskEnvironment reference guards. |
| src/TaskAnalyzer.Tests/MultiThreadableTaskCodeFixProviderTests.cs | Adds new coverage for nested calls, named arguments, static contexts, missing TaskEnvironment, and FixAll behavior. |
| src/TaskAnalyzer/README.md | Documents the anchoring behavior, fix withholding rules, and dotnet format analyzers FixAll limitations/workarounds. |
Review details
💡 Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.
- Files reviewed: 3/3 changed files
- Comments generated: 1
- Review effort level: Lite
Co-authored-by: ViktorHofer <7412651+ViktorHofer@users.noreply.github.com>
There was a problem hiding this comment.
Review details
Suppressed comments (1)
Previously missed (1) — in code that hasn't changed since the last review.
src/TaskAnalyzer/MultiThreadableTaskCodeFixProvider.cs:186
- Checking only for any field/property with this name does not prove that the generated receiver will bind or have the required API. For an attributed helper, an inaccessible inherited
TaskEnvironment, a write-only/wrongly typed member, or a local/parameter that shadows a valid member makes the offered fix emit non-compiling code. Resolve the exact receiver atnode.SpanStart(or generatethis.TaskEnvironment) and require an accessible readable member of the frameworkTaskEnvironmenttype before registering the fix.
foreach (var member in current.GetMembers("TaskEnvironment"))
{
if (member is IPropertySymbol or IFieldSymbol)
{
return true;
- Files reviewed: 3/3 changed files
- Comments generated: 1
- Review effort level: Balanced
Co-authored-by: ViktorHofer <7412651+ViktorHofer@users.noreply.github.com>
Context
MultiThreadableTaskCodeFixProvideremitted non-compiling code for the MSBuildTask0003 fix ("Wrap withTaskEnvironment.GetAbsolutePath()") in two shapes: nested calls got the outer call's argument wrapped (CS1503), and static contexts got a reference to the instanceTaskEnvironmentproperty (CS0120).The root cause of the first is span aliasing. An
ArgumentSyntaxand its child expression share aTextSpan, soroot.FindNode(span)returns the enclosing argument when the flagged call is nested;FindContainingCall's ancestor walk then climbed to the enclosing call:This matters at scale — the provider registers
BatchFixAllProvider, so bulk application viadotnet format analyzersis the intended workflow, and breakage only surfaces on the next compile.Changes Made
FindNode(span, getInnermostNodeForTie: true)and drop the ancestor walk. The analyzer always reports oncontext.Operation.Syntax, so the fix now rewrites exactly the call that was flagged.FindContainingCallis deleted. The MSBuildTask0002 path had the same defect (AncestorsAndSelf().OfType<InvocationExpressionSyntax>()) and is corrected the same way.FindPathArgumentresolves the flagged argument through the semantic model, reusing the analyzer's ownIsPathParameterName/IsWrappedSafelyhelpers so fixer and analyzer cannot disagree. This also corrects named-argument ordering (Directory.GetFiles(searchPattern:, path:)no longer wraps the pattern). A syntactic fallback preserves prior behavior when no operation is available.CanReferenceTaskEnvironmentguard covers every way the generated reference could fail to bind, applied to both the 0002 and 0003 fixers since both emit it.Two adjacent instances of the same defect surfaced while building that guard and are covered:
thisisn't usable there (CS0236), including inside a lambda declared in the initializer.TaskEnvironmentmember — the defaultallscope analyzes everyITask, butMicrosoft.Build.Utilities.Taskimplements onlyITask; the member lives onIMultiThreadableTask/ToolTask. The fix previously emitted CS0103 on ordinary tasks, likely the most common broken shape at scale. Mirrors the existingPreferTypedParameterCodeFixProvider.HasAccessibleTaskEnvironmentprecedent.Testing
16 new code-fix tests covering nested object creation and invocation, implicit object creation, named non-path arguments, static method / local function / lambda, lambda in an instance method (still fixed), property initializer, plain task without the member, MSBuildTask0002 in a static method, and a mixed FixAll test mirroring the issue's repro.
Microsoft.CodeAnalysis.Testingvalidates compiler diagnostics on the fixed state, so these assert the emitted code compiles.End-to-end against the built analyzer,
dotnet format analyzers --diagnostics MSBuildTask0003on the issue's repro now produces its "Expected output" and the rebuild reports 0 errors, down from 3× CS1503 + CS0120.Notes
Worth knowing before bulk-migrating:
dotnet format analyzersderives its FixAll equivalence key from the first reported diagnostic, and Roslyn'sBatchFixAllProvideronly matches actions whose key is non-null and equal to it. If that first occurrence is one where no fix is offered, the tool logsUnable to fix MSBuildTask0003…and applies nothing. Withholding still seems right — it is what the issue asks for, andPreferTypedParameterCodeFixProvideris already conservative by design — but the caveat and its workaround are now insrc/TaskAnalyzer/README.md.A consequence worth reviewer attention: the fix is no longer offered inside
[MSBuildMultiThreadableTaskAnalyzed]helper classes unless the helper itself declares aTaskEnvironmentmember. That is strictly better than emitting CS0103, but it narrows what that attribute buys you, so the README wording there was adjusted.