Skip to content

Fix MSBuildTask0002 analyzer warnings in already-migrated tasks#13466

Open
JanProvaznik wants to merge 1 commit intodotnet:mainfrom
JanProvaznik:fix/mt-task-warnings-pr1
Open

Fix MSBuildTask0002 analyzer warnings in already-migrated tasks#13466
JanProvaznik wants to merge 1 commit intodotnet:mainfrom
JanProvaznik:fix/mt-task-warnings-pr1

Conversation

@JanProvaznik
Copy link
Copy Markdown
Member

Replace direct Environment.GetEnvironmentVariable and Path.GetFullPath calls in tasks that already implement IMultiThreadableTask, addressing analyzer warnings in the 'Linux Core Multithreaded Mode' CI leg.

Changes:

  • Copy.cs: Convert static env var reads to instance fields initialized from TaskEnvironment.GetEnvironmentVariable() at the start of Execute(). This is MT-safe since each task instance gets its own TaskEnvironment. Constants remain on Copy for test access. RefreshInternalEnvironmentValues() is now a no-op (env vars are read fresh per Execute call).
  • GetFileHash.cs: Change ComputeHash parameter from string to AbsolutePath to preserve type safety through to File.OpenRead call.
  • Unzip.cs: Replace Path.GetFullPath with TaskEnvironment.GetAbsolutePath().GetCanonicalForm() using AbsolutePath type. GetCanonicalForm resolves '..' for zip slip protection.
  • FindUnderPath.cs: Pragma-suppress MSBuildTask0002 in pre-ChangeWave branches where Path.GetFullPath is called on already-absolute paths from TaskEnvironment.GetAbsolutePath (only canonicalizes, CWD not consulted).
  • AssignTargetPath.cs: Same pragma-suppress pattern as FindUnderPath.

Copilot AI review requested due to automatic review settings March 31, 2026 11:50
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

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 addresses MSBuildTask0002 analyzer warnings by switching already-IMultiThreadableTask tasks to use TaskEnvironment-based APIs (and AbsolutePath) instead of direct Environment.GetEnvironmentVariable / Path.GetFullPath, improving correctness in multithreaded execution modes.

Changes:

  • Update path canonicalization and path-type plumbing to use TaskEnvironment.GetAbsolutePath(...).GetCanonicalForm() and AbsolutePath where appropriate.
  • Refactor Copy to read relevant environment variables via TaskEnvironment per-task-execution (instead of static reads) and adjust related unit tests.
  • Add targeted #pragma suppressions in pre-ChangeWave branches where Path.GetFullPath is still required for behavior compatibility.

Reviewed changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
src/Tasks/Unzip.cs Use TaskEnvironment + AbsolutePath canonicalization for zip extraction destination validation.
src/Tasks/NativeMethods.cs Change MoveFileEx wrapper to use AbsolutePath parameters.
src/Tasks/ListOperators/FindUnderPath.cs Add MSBuildTask0002 suppression in pre-ChangeWave branch around Path.GetFullPath.
src/Tasks/FileIO/GetFileHash.cs Strengthen ComputeHash signature to accept AbsolutePath.
src/Tasks/Copy.cs Replace static environment-variable reads with per-Execute() TaskEnvironment reads; adjust symlink retry flags accordingly.
src/Tasks/AssignTargetPath.cs Add MSBuildTask0002 suppression in pre-ChangeWave branch around Path.GetFullPath.
src/Tasks.UnitTests/Copy_Tests.cs Remove reliance on Copy.RefreshInternalEnvironmentValues() after env-var handling refactor.

Replace direct Environment.GetEnvironmentVariable and Path.GetFullPath calls
in tasks that already implement IMultiThreadableTask, addressing analyzer
warnings in the 'Linux Core Multithreaded Mode' CI leg.

Changes:
- Copy.cs: Convert static env var reads to instance fields initialized from
  TaskEnvironment.GetEnvironmentVariable() at the start of Execute(). This is
  MT-safe since each task instance gets its own TaskEnvironment. Constants
  remain on Copy for test access. RefreshInternalEnvironmentValues() is now
  a no-op (env vars are read fresh per Execute call).
- GetFileHash.cs: Change ComputeHash parameter from string to AbsolutePath to
  preserve type safety through to File.OpenRead call.
- Unzip.cs: Replace Path.GetFullPath with TaskEnvironment.GetAbsolutePath().GetCanonicalForm()
  using AbsolutePath type. GetCanonicalForm resolves '..' for zip slip protection.
- FindUnderPath.cs: Pragma-suppress MSBuildTask0002 in pre-ChangeWave branches
  where Path.GetFullPath is called on already-absolute paths from
  TaskEnvironment.GetAbsolutePath (only canonicalizes, CWD not consulted).
- AssignTargetPath.cs: Same pragma-suppress pattern as FindUnderPath.

Remaining MSBuildTask0005 (transitive) warnings in Copy/Move should be addressed
by improving the ThreadSafeTaskAnalyzer's AbsolutePath recognition in transitive
call chains.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@JanProvaznik JanProvaznik force-pushed the fix/mt-task-warnings-pr1 branch from b1e1236 to aabceed Compare March 31, 2026 12:15
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.

2 participants