Skip to content

Detect environment-dependent temp helpers in TaskAnalyzer - #14975

Merged
ViktorHofer merged 4 commits into
mainfrom
copilot/fix-banned-api-directory-create-temp-subdirectory
Sep 8, 2026
Merged

ViktorHofer merged 4 commits into
mainfrom
copilot/fix-banned-api-directory-create-temp-subdirectory

Conversation

Copilot AI commented Sep 8, 2026 •

Copy link
Copy Markdown
Contributor

Context

Directory.CreateTempSubdirectory() depends on the same process-global temp environment as Path.GetTempPath(), but escaped both direct and transitive diagnostics.

Changes Made

  • Detection: Ban Directory.CreateTempSubdirectory(string) and parameterless TempFileCollection(), identified by the BCL audit.
  • Guidance: Align temp-helper diagnostics and documentation on suppression with justification until a TaskEnvironment alternative exists.

Testing

Regression coverage for direct/transitive calls, omitted/null/empty/named prefixes, and permitted GetRandomFileName() and explicit-directory TempFileCollection usage.

Notes

TempFileCollection(tempDir) overloads remain allowed. Their null/empty-directory fallback still escapes detection; this limitation is documented.

@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.

Co-authored-by: ViktorHofer <7412651+ViktorHofer@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix banned API to include Directory.CreateTempSubdirectory Detect environment-dependent temp helpers in TaskAnalyzer Sep 8, 2026
Copilot AI requested a review from ViktorHofer September 8, 2026 08:04
@ViktorHofer
ViktorHofer marked this pull request as ready for review September 8, 2026 08:06
Copilot AI lite review requested due to automatic review settings September 8, 2026 08:06

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.

🟢 Approval recommended

The behavioral change is well-scoped to analyzer detection and is covered by new direct/transitive regression tests, with only minor test-harness consistency nits noted.

Pull request overview

This PR closes a detection gap in the TaskAnalyzer by flagging additional “temp helper” APIs that depend on process-global TMP/TEMP state, and updates analyzer guidance/docs to recommend suppression-with-justification until a TaskEnvironment alternative exists.

Changes:

  • Adds Directory.CreateTempSubdirectory(string) and TempFileCollection() (parameterless ctor) to the banned API list under ApiCategory.TaskEnvironment.
  • Updates TaskAnalyzer README to document the new temp-helper bans and clarify suppression guidance/limitations.
  • Extends direct + transitive analyzer tests to cover the newly banned APIs and ensure Path.GetRandomFileName() and explicit-directory TempFileCollection usage remain allowed.
File summaries
File Description
src/TaskAnalyzer/README.md Documents additional temp-helper bans and clarifies suppression guidance/limitations.
src/TaskAnalyzer/BannedApiDefinitions.cs Adds new banned API entries and aligns temp-helper messaging with “suppress with justification” guidance.
src/TaskAnalyzer.Tests/TransitiveCallChainAnalyzerTests.cs Adds transitive-call coverage for the new banned temp helpers.
src/TaskAnalyzer.Tests/TestHelpers.cs Adds System.CodeDom reference for test compilations so TempFileCollection can be exercised.
src/TaskAnalyzer.Tests/MultiThreadableTaskAnalyzerTests.cs Adds direct analyzer coverage for CreateTempSubdirectory and TempFileCollection() patterns, plus permitted cases.
Review details

Suppressed comments (1)

src/TaskAnalyzer.Tests/MultiThreadableTaskAnalyzerTests.cs:1330

  • This test is scoped to the direct analyzer (the code under test is entirely inside the task type). Using GetAllDiagnosticsAsync adds the transitive analyzer without increasing coverage, and it makes failures harder to interpret. Prefer GetDiagnosticsAsync here; keep GetAllDiagnosticsAsync for tests that intentionally exercise helper-call analysis.
    {
        var diags = await GetAllDiagnosticsAsync($$"""
            using System.CodeDom.Compiler;
            using Microsoft.Build.Framework;
            public class MyTask : Microsoft.Build.Utilities.Task, IMultiThreadableTask
  • Files reviewed: 5/5 changed files
  • Comments generated: 1
  • Review effort level: Lite

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

The CreateTempSubdirectory and TempFileCollection tests exercise code
entirely inside the task type, so running the transitive analyzer adds
no coverage and makes failures harder to interpret.

PathGetRandomFileName_DirectAndTransitive_NoDiagnostics keeps
GetAllDiagnosticsAsync since it intentionally covers a helper call chain.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: be19d794-fe4f-43fe-9e6e-f6e6b13ec6e7

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.

🟢 Approval recommended

The changes are consistent across analyzer definitions, documentation, and test coverage, and I didn’t find correctness or maintainability issues in the updated logic.

Review details
  • Files reviewed: 5/5 changed files
  • Comments generated: 0 new
  • Review effort level: Lite

@ViktorHofer
ViktorHofer enabled auto-merge (squash) September 8, 2026 10:06

@VolPlita VolPlita 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.

I wanted to take time to understand how it relates to this issue I created a while ago, but approving so it can get in

@ViktorHofer
ViktorHofer merged commit 0ff3bda into main Sep 8, 2026
14 checks passed
@ViktorHofer
ViktorHofer deleted the copilot/fix-banned-api-directory-create-temp-subdirectory branch September 8, 2026 20:45
This was referenced Nov 16, 2026
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: Directory.CreateTempSubdirectory is not banned, so the recommended replacement for Path.GetTempPath escapes MSBuildTask0002

4 participants