Skip to content

Report MSBuildTask0005 at the unsafe call site so it can be suppressed there - #14829

Merged
ViktorHofer merged 4 commits into
mainfrom
copilot/fix-msbuildtask0005-suppression
Aug 26, 2026
Merged

ViktorHofer merged 4 commits into
mainfrom
copilot/fix-msbuildtask0005-suppression

Conversation

Copilot AI commented Aug 25, 2026 •

Copy link
Copy Markdown
Contributor

Context

Roslyn applies #pragma warning disable and [SuppressMessage] based on a diagnostic's primary Location. TransitiveCallChainAnalyzer reported MSBuildTask0005 at taskMethod.Locations[0] — the task's Execute method — so a pragma around the actual offending call sat in a different file entirely and did nothing. The only way to silence one reviewed call was to suppress on Execute, which then hid every transitive violation for that task, including future regressions.

Changes Made

src/TaskAnalyzer/TransitiveCallChainAnalyzer.cs

  • ViolationInfo now carries the Location of the unsafe operation, captured in phase 1 from context.Operation.Syntax.GetLocation() — the same anchor MSBuildTask0001–0004 already use.
  • ReportTransitiveViolation reports at that call site, with the task entry point as an AdditionalLocation. The message is unchanged and still names the task and full call chain. Falls back to the old task-method location if a call site has no source location.
  • Deduplication moved from per-(API) to per-(API, call site). Without this, suppressing one reviewed call would swallow a second, unreviewed call to the same API — the same over-suppression problem, just narrower.

This covers all three requests in the issue without new public API: (1) directly; (2) via standard [SuppressMessage("MSBuild.TaskAuthoring", "MSBuildTask0005", Justification = "…")], which now works because the location falls inside the helper member; (3) because the offending line is the reported location.

src/TaskAnalyzer/README.md — new MSBuildTask0005 section covering call-site reporting and how to scope a suppression; corrected the now-stale claim that the rule "reports only at the task entry point".

Real csc run against the issue's repro, helper and task in separate files:

before: CreateSourceArtifact.cs(6,26): ... via: … → ProcessService.Unreviewed → Process.Kill(bool)
after:  ProcessService.cs(23,9):      ... via: … → ProcessService.Unreviewed → Process.Kill(bool)

The #pragma-wrapped process.Kill(entireProcessTree: true) in the sibling helper is suppressed after the change and was reported before it, while the unreviewed call still warns.

Testing

Six new tests in TransitiveCallChainAnalyzerTests cover the reporting location and additional location, pragma suppression, [SuppressMessage] suppression, and the three "suppressing one thing doesn't hide another" cases.

Notes

Worth a reviewer's judgement: total MSBuildTask0005 volume increases — 144 → 190 unique messages in Microsoft.Build.Tasks, redistributed from 42 Execute methods onto 194 real call sites — because distinct call sites of the same API are no longer collapsed. That's the trade for making each call individually reviewable, and it can't break /WarnAsError consumers since the analyzer is opt-in, unshipped, and already listed in that project's WarningsNotAsErrors. Keeping per-API dedup would restore the lower volume at the cost of re-introducing the over-suppression the issue reports.

Suppressing on Execute no longer works, by design; suppressions must move to the call site. The issue explicitly offers this ("in addition to (or instead of)").

@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 changed the title [WIP] Fix MSBuildTask0005 suppression issue at unsafe call sites Report MSBuildTask0005 at the unsafe call site so it can be suppressed there Aug 25, 2026
Copilot AI requested a review from ViktorHofer August 25, 2026 14:08
Removed tests section from README.md
@ViktorHofer
ViktorHofer marked this pull request as ready for review August 26, 2026 08:14
@ViktorHofer
ViktorHofer requested review from AR-May and VolPlita and a lite review from Copilot August 26, 2026 08:14

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR updates the TaskAnalyzer’s MSBuildTask0005 (“Transitive unsafe API usage”) reporting so the diagnostic’s primary location is the unsafe call site (enabling narrow #pragma / [SuppressMessage] suppressions at the reviewed call) while still preserving task context via the call chain text and an additional location pointing back to the task entry member.

Changes:

  • Capture and carry the unsafe operation Location through ViolationInfo, and report MSBuildTask0005 at that call site with the task member as an additional location.
  • Change deduplication from per-API to per-(API, call site) so suppressing one reviewed call doesn’t hide other call sites of the same API.
  • Update TaskAnalyzer README docs and add tests covering call-site reporting and suppression behavior.
Show a summary per file
File Description
src/TaskAnalyzer/TransitiveCallChainAnalyzer.cs Report MSBuildTask0005 at the unsafe call site (with additional location), and adjust deduplication granularity.
src/TaskAnalyzer/README.md Document call-site reporting and suppression guidance for MSBuildTask0005; update related guidance text.
src/TaskAnalyzer.Tests/TransitiveCallChainAnalyzerTests.cs Add tests validating primary/additional locations and suppression behavior at the call site.

Review details

💡 Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.

Suppressed comments (2)

src/TaskAnalyzer/TransitiveCallChainAnalyzer.cs:197

  • context.Operation.Syntax can be null for implicit operations; calling GetLocation() unconditionally risks an analyzer crash (which would suppress all diagnostics). Use a null-safe location fallback (e.g., Location.None).
                    var violation = new ViolationInfo("CriticalError", displayName, message, context.Operation.Syntax.GetLocation());

src/TaskAnalyzer/TransitiveCallChainAnalyzer.cs:214

  • context.Operation.Syntax can be null for implicit operations; calling GetLocation() unconditionally risks an analyzer crash (which would suppress all diagnostics). Use a null-safe location fallback (e.g., Location.None).
                        var violation = new ViolationInfo("FilePathRequiresAbsolute", displayName,
                            "may resolve relative paths against the process working directory", context.Operation.Syntax.GetLocation());
                        directViolations.GetOrAdd(callerKey, _ => new ConcurrentBag<ViolationInfo>()).Add(violation);
  • Files reviewed: 3/3 changed files
  • Comments generated: 4
  • Review effort level: Lite

Comment thread src/TaskAnalyzer/README.md Resolved
Comment thread src/TaskAnalyzer/README.md Resolved
Deduplicate on the location the diagnostic is actually reported at rather
than on the raw call-site location, so the fall back to the task entry
point cannot collapse two violations that land on different task members.

Qualify the MSBuildTask0005 intro, which claimed MSBuildTask0001-0004
only look inside a task class and so contradicted the later
[MSBuildMultiThreadableTaskAnalyzed] section.

Restore the README's Tests section, without the stale test count, using
the build.cmd invocation that actually works from a clean worktree.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review details

  • Files reviewed: 3/3 changed files
  • Comments generated: 0 new
  • Review effort level: Balanced

@ViktorHofer
ViktorHofer enabled auto-merge (squash) August 26, 2026 09:28
This was referenced Nov 22, 2026
flxjk pushed a commit to flxjk/typespec that referenced this pull request Nov 25, 2026
Updated [Microsoft.Build](https://github.com/dotnet/msbuild) from
18.11.0 to 18.12.2.

<details>
<summary>Release notes</summary>

_Sourced from [Microsoft.Build's
releases](https://github.com/dotnet/msbuild/releases)._

## 18.12.2

## What's Changed
* [TaskAnalyzer] Align diagnostic severities and ignore open generic
ITaskItem<T> by @​VolPlita in
dotnet/msbuild#14811
* Add multi-threading option to build response file by @​ViktorHofer in
dotnet/msbuild#14837
* Anchor MSBuildTask0003 code fix on the flagged call and withhold it
where TaskEnvironment cannot bind by @​ViktorHofer with @​Copilot in
dotnet/msbuild#14810
* Bump main to 18.12 after the vs18.11 branch snap by @​ViktorHofer in
dotnet/msbuild#14841
* Report MSBuildTask0005 at the unsafe call site so it can be suppressed
there by @​ViktorHofer with @​Copilot in
dotnet/msbuild#14829
* Revert "Add multi-threading option to build response file" by
@​ViktorHofer in dotnet/msbuild#14851
* [main] Source code updates from dotnet/dotnet by @​dotnet-maestro[bot]
in dotnet/msbuild#14853
* [main] Update dependencies from dotnet/arcade by @​dotnet-maestro[bot]
in dotnet/msbuild#14854
* Update MicrosoftBuildVersion in analyzer template by
@​github-actions[bot] in dotnet/msbuild#14565
* Revert "Revert "Add multi-threading option to build response file"" by
@​ViktorHofer in dotnet/msbuild#14855
* Fix stale gh-aw-actions/setup pin causing "activation" job failure by
@​JanProvaznik with @​Copilot in
dotnet/msbuild#14842
* Make isolated graph builds fail deterministically on undeclared
cross-project targets by @​AlesProkop with @​Copilot in
dotnet/msbuild#14280
* Revise temp path handling in README by @​baronfel in
dotnet/msbuild#14834
* Fix BuildCheck rule configuration and task name casing bug by @​AR-May
in dotnet/msbuild#14778
* Remove MSBuildImportedProject item from the XSD by @​drewnoakes in
dotnet/msbuild#14659
* [main] Source code updates from dotnet/dotnet by @​dotnet-maestro[bot]
in dotnet/msbuild#14866
* Require ready-for-training tag for OptProf by @​AlesProkop in
dotnet/msbuild#14867
* Ignore stale MSBuild server console writes by @​JanProvaznik in
dotnet/msbuild#14850
* Regenerate Expert Code Review workflow lock by @​JanProvaznik with
@​Copilot in dotnet/msbuild#14870
* [main] Update dependencies from dotnet/arcade by @​dotnet-maestro[bot]
in dotnet/msbuild#14896
* [main] Update dependencies from dotnet/roslyn by @​dotnet-maestro[bot]
in dotnet/msbuild#14900
* [main] Update dependencies from nuget/nuget.client by
@​dotnet-maestro[bot] in dotnet/msbuild#14669
* [main] Source code updates from dotnet/dotnet by @​dotnet-maestro[bot]
in dotnet/msbuild#14886
* Expose AbsolutePath.GetCanonicalForm() by @​JanProvaznik with
@​Copilot in dotnet/msbuild#14873
* Make release checklist step 1.3 a full branch-retirement audit by
@​ViktorHofer in dotnet/msbuild#14911
* Resolve PathLike MatchOnMetadata against the thread working directory
by @​JanProvaznik in dotnet/msbuild#14916
* Fix typo in project dependency explanation by @​DanexisIbrahimAbtula
in dotnet/msbuild#14885
* Preserve events routed through the TaskHostTask by @​AR-May in
dotnet/msbuild#14876
* Fix NormalizePath and other intrinsic functions in multithreaded mode
by @​AR-May in dotnet/msbuild#14875
* Bump actions/checkout from 7.0.0 to 7.0.1 by @​dependabot[bot] in
dotnet/msbuild#14887
* Bump github/gh-aw-actions/setup-cli from 0.87.2 to 0.87.9 by
@​dependabot[bot] in dotnet/msbuild#14888
* Bump github/gh-aw-actions/setup from 0.82.9 to 0.87.9 by
@​dependabot[bot] in dotnet/msbuild#14889
* Upgrade gh-aw to v0.88.2 and recompile agentic workflow lock files by
@​AR-May in dotnet/msbuild#14935
* Preserve error detail when ContinueOnError downgrades an error to a
warning by @​AR-May in dotnet/msbuild#14918
* Fix Dependabot ignore rule to match gh-aw-actions sub-path actions by
@​AR-May in dotnet/msbuild#14938
* [Flaky Test] Quarantine 1 flaky test (TaskEnvironment) by
@​github-actions[bot] in dotnet/msbuild#14662
* Fix template path in quarantine pipeline by @​AR-May in
dotnet/msbuild#14937
* Run expert code review when a draft PR is marked ready for review by
@​AR-May in dotnet/msbuild#14952
* [Dreaming] dreaming: clarify RenderImmediateMessage as the correct
path for immediate TerminalLogger output by @​github-actions[bot] in
dotnet/msbuild#14381
* [Dreaming] Clarify: don't hardcode backslash path separators in test
XML by @​github-actions[bot] in
dotnet/msbuild#14973
* Regenerate review-on-open lock file by @​AR-May in
dotnet/msbuild#14977
* Recognize AbsolutePath.Value canonicalization in task analyzers by
@​JanProvaznik in dotnet/msbuild#14970
* improve issue and pr templates based on history by @​JanProvaznik in
dotnet/msbuild#14982
* Detect environment-dependent temp helpers in TaskAnalyzer by
@​ViktorHofer with @​Copilot in
dotnet/msbuild#14975
* Document registered task object semantics in multithreaded builds by
@​JanProvaznik in dotnet/msbuild#14919
* Add server-only MSBUILDDEBUGONSTART modes by @​JanProvaznik in
dotnet/msbuild#14988
* Preserve manifest input streams when writing manifests by @​dtivel in
dotnet/msbuild#14883
* Scope MT-specific TaskAnalyzer diagnostics and support .editorconfig
by @​VolPlita in dotnet/msbuild#14775
* [main] Update dependencies from dotnet/arcade by @​dotnet-maestro[bot]
in dotnet/msbuild#14948
 ... (truncated)

Commits viewable in [compare
view](dotnet/msbuild@v18.11.0...v18.12.2).
</details>

[![Dependabot compatibility
score](https://dependabot-badges.githubapp.com/badges/compatibility_score?dependency-name=Microsoft.Build&package-manager=nuget&previous-version=18.11.0&new-version=18.12.2)](https://docs.github.com/en/github/managing-security-vulnerabilities/about-dependabot-security-updates#about-compatibility-scores)

Dependabot will resolve any conflicts with this PR as long as you don't
alter it yourself. You can also trigger a rebase manually by commenting
`@dependabot rebase`.

[//]: # (dependabot-automerge-start)
[//]: # (dependabot-automerge-end)

---

<details>
<summary>Dependabot commands and options</summary>
<br />

You can trigger Dependabot actions by commenting on this PR:
- `@dependabot rebase` will rebase this PR
- `@dependabot recreate` will recreate this PR, overwriting any edits
that have been made to it
- `@dependabot show <dependency name> ignore conditions` will show all
of the ignore conditions of the specified dependency
- `@dependabot ignore this major version` will close this PR and stop
Dependabot creating any more for this major version (unless you reopen
the PR or upgrade to it yourself)
- `@dependabot ignore this minor version` will close this PR and stop
Dependabot creating any more for this minor version (unless you reopen
the PR or upgrade to it yourself)
- `@dependabot ignore this dependency` will close this PR and stop
Dependabot creating any more for this dependency (unless you reopen the
PR or upgrade to it yourself)


</details>

Signed-off-by: dependabot[bot] <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
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.

MSBuildTask0005 cannot be suppressed at the unsafe call site, only on the task's Execute method

4 participants