Skip to content

Document safe temporary file creation for tasks - #15091

Merged
VolPlita merged 2 commits into
dotnet:mainfrom
VolPlita:document-temp-file-replacement
Nov 2, 2026
Merged

VolPlita merged 2 commits into
dotnet:mainfrom
VolPlita:document-temp-file-replacement

Conversation

@VolPlita

Copy link
Copy Markdown
Contributor

Fixes #15088

Context

TaskAnalyzer reports MSBuildTask0002 for Path.GetTempFileName() because it resolves the temporary directory from process-wide environment variables.

#14729 added TaskEnvironment.GetTempPath(). A task can now create a temporary file in its task-local temporary directory without a new TaskEnvironment.GetTempFileName() API.

The current diagnostic says that suppression is the only option. The README also says that no workaround exists and links to the completed #14583.

Changes

  • Update the Path.GetTempFileName() diagnostic guidance.
  • Document a complete temporary-file helper that:
    • gets the directory from TaskEnvironment.GetTempPath();
    • generates a name with Path.GetRandomFileName();
    • creates the file atomically with FileMode.CreateNew;
    • retries name collisions;
    • preserves the final I/O exception;
    • returns an AbsolutePath.
  • Explain that the caller owns and deletes the created file.
  • Clarify that MSBuild does not provide a TaskEnvironment.GetTempFileName() convenience API.
  • Remove the stale Add TaskEnvironment.GetTempPath() #14583 links.
  • Clarify that Directory.CreateTempSubdirectory() still has no supported task-local equivalent.
  • Add an exact diagnostic-message assertion.
  • Add coverage that compiles and analyzes the documented pattern without diagnostics.

Compatibility

This PR changes guidance text for an existing diagnostic. It does not add a diagnostic, change its severity, or expand its scope. No ChangeWave is required.

Validation

dotnet test src\TaskAnalyzer.Tests\TaskAnalyzer.Tests.csproj -c Release

  • 369 passed
  • 0 failed
  • 0 skipped

Copilot AI lite review requested due to automatic review settings September 21, 2026 13:49

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.

Copilot review overview

🔵 Needs a closer look

The test must explicitly verify that the documented helper compiles without errors.

Review effort: Lite
Findings: None

What changed in this PR

Updates TaskAnalyzer guidance for safe, task-local temporary-file creation.

Changes:

  • Documents atomic, collision-resistant temporary-file creation.
  • Updates MSBuildTask0002 guidance and removes stale references.
  • Adds diagnostic assertions and analyzer coverage.
File Description
src/​TaskAnalyzer/​README.md Documents the temporary-file helper and cleanup ownership.
src/​TaskAnalyzer/​BannedApiDefinitions.cs Updates diagnostic guidance.
src/​TaskAnalyzer.Tests/​MultiThreadableTaskAnalyzerTests.cs Verifies diagnostic text and the documented pattern.

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

@VolPlita VolPlita left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

# Dimension Verdict
4 Test Coverage & Completeness 🟠 1 MAJOR

✅ 23/24 dimensions clean.

  • Test Coverage & Completeness — the documented-pattern test checks only analyzer diagnostics and therefore passes even though its source does not compile against the test stubs.

No production correctness, compatibility, concurrency, security, or ChangeWave issue was confirmed.

@github-actions github-actions Bot 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.

# Dimension Verdict
22 Correctness 🟡 1 MODERATE

✅ 23/24 dimensions clean.

  • Correctness — retry only confirmed filename collisions. In src/TaskAnalyzer/README.md around line 176, the documented helper catches and retries every IOException. Failures unrelated to a generated-name collision (for example, the task-local temporary directory becoming unavailable) are retried ten times rather than propagated immediately. Restrict retries to failures that can be identified as FileMode.CreateNew collisions and preserve immediate failure for other I/O errors.

Warning

Firewall blocked 2 domains

The following domains were blocked by the firewall during workflow execution:

  • api.github.com
  • github.com

[!TIP]
api.github.com is blocked because GitHub API access uses the built-in GitHub tools by default. Instead of adding api.github.com to network.allowed, use tools.github.mode: gh-proxy for direct pre-authenticated GitHub CLI access without requiring network access to api.github.com:

tools:
  github:
    mode: gh-proxy

See GitHub Tools for more information on gh-proxy mode.

To allow these domains, add them to the network.allowed list in your workflow frontmatter:

network:
  allowed:
    - defaults
    - "api.github.com"
    - "github.com"

See Network Configuration for more information.

Generated by Expert Code Review (on open) for #15091 · copilot · gpt56 · 359.5 AIC · ⌖ 5.61 AIC · ⊞ 17.8K

@VolPlita
VolPlita enabled auto-merge (squash) September 21, 2026 14:37
@VolPlita
VolPlita force-pushed the document-temp-file-replacement branch from eb75223 to e9eafd3 Compare September 29, 2026 07:29
Replace the stale Path.GetTempFileName guidance with an atomic file-creation pattern based on TaskEnvironment.GetTempPath. Add exact diagnostic and documented-pattern coverage.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot-Session: b7562379-021b-44cf-8a49-4df05a7c3b13
Compile the documented pattern against complete framework stubs and retry only confirmed filename collisions.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot-Session: b7562379-021b-44cf-8a49-4df05a7c3b13
@VolPlita
VolPlita force-pushed the document-temp-file-replacement branch from e9eafd3 to a6ea2d3 Compare November 2, 2026 14:54
@VolPlita
VolPlita merged commit e88a5df into dotnet:main Nov 2, 2026
13 checks passed
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.

Document the safe replacement for Path.GetTempFileName in multithreaded tasks

3 participants