Skip to content

Refactor: use TaskEnvironment public API#13462

Merged
AR-May merged 2 commits intodotnet:mainfrom
AR-May:use-fallback-task-environment
Mar 30, 2026
Merged

Refactor: use TaskEnvironment public API#13462
AR-May merged 2 commits intodotnet:mainfrom
AR-May:use-fallback-task-environment

Conversation

@AR-May
Copy link
Copy Markdown
Member

@AR-May AR-May commented Mar 30, 2026

No description provided.

@AR-May AR-May requested a review from a team as a code owner March 30, 2026 15:16
Copilot AI review requested due to automatic review settings March 30, 2026 15:16
@AR-May AR-May self-assigned this Mar 30, 2026
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

Refactors TaskEnvironment initialization sites to use the newer TaskEnvironment public surface area (primarily TaskEnvironment.Fallback and TaskEnvironment.CreateWithProjectDirectoryAndEnvironment(...)) instead of directly constructing TaskEnvironment with driver implementations.

Changes:

  • Replace per-task default initialization new TaskEnvironment(MultiProcessTaskEnvironmentDriver.Instance) with TaskEnvironment.Fallback across built-in tasks and test helpers.
  • Update engine and unit tests to use TaskEnvironment.CreateWithProjectDirectoryAndEnvironment(...) instead of directly new’ing MultiThreadedTaskEnvironmentDriver.
  • Refresh multithreaded task migration docs/examples to reference TaskEnvironment.Fallback.

Reviewed changes

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

Show a summary per file
File Description
src/UnitTests.Shared/TaskEnvironmentHelper.cs Switch test helper to return TaskEnvironment.Fallback.
src/Tasks/AssignTargetPath.cs Default TaskEnvironment now uses TaskEnvironment.Fallback.
src/Tasks/Copy.cs Default TaskEnvironment now uses TaskEnvironment.Fallback.
src/Tasks/Delete.cs Default TaskEnvironment now uses TaskEnvironment.Fallback.
src/Tasks/DownloadFile.cs Default TaskEnvironment now uses TaskEnvironment.Fallback.
src/Tasks/FileIO/GetFileHash.cs Default TaskEnvironment now uses TaskEnvironment.Fallback.
src/Tasks/FileIO/ReadLinesFromFile.cs Default TaskEnvironment now uses TaskEnvironment.Fallback.
src/Tasks/FileIO/VerifyFileHash.cs Default TaskEnvironment now uses TaskEnvironment.Fallback.
src/Tasks/FileIO/WriteLinesToFile.cs Default TaskEnvironment now uses TaskEnvironment.Fallback.
src/Tasks/ListOperators/FindUnderPath.cs Default TaskEnvironment now uses TaskEnvironment.Fallback.
src/Tasks/MakeDir.cs Default TaskEnvironment now uses TaskEnvironment.Fallback.
src/Tasks/Move.cs Default TaskEnvironment now uses TaskEnvironment.Fallback.
src/Tasks/RemoveDir.cs Default TaskEnvironment now uses TaskEnvironment.Fallback.
src/Tasks/Touch.cs Default TaskEnvironment now uses TaskEnvironment.Fallback.
src/Tasks/Unzip.cs Default TaskEnvironment now uses TaskEnvironment.Fallback.
src/Tasks/WriteCodeFragment.cs Default TaskEnvironment now uses TaskEnvironment.Fallback.
src/Tasks/XmlPeek.cs Default TaskEnvironment now uses TaskEnvironment.Fallback.
src/Tasks/XmlPoke.cs Default TaskEnvironment now uses TaskEnvironment.Fallback.
src/Tasks/XslTransformation.cs Default TaskEnvironment now uses TaskEnvironment.Fallback.
src/Tasks/ZipDirectory.cs Default TaskEnvironment now uses TaskEnvironment.Fallback.
src/Build/BackEnd/Components/BuildRequestEngine/BuildRequestEngine.cs Use CreateWithProjectDirectoryAndEnvironment(...) for multithreaded task environments; use Fallback otherwise.
src/Build.UnitTests/BackEnd/TaskEnvironment_Tests.cs Update tests to use Fallback / CreateWithProjectDirectoryAndEnvironment(...).
documentation/specs/multithreading/thread-safe-tasks.md Update docs snippet to use TaskEnvironment.Fallback.
.github/skills/multithreaded-task-migration/SKILL.md Update guidance/snippets/checklist to use TaskEnvironment.Fallback.
Comments suppressed due to low confidence (1)

src/Build.UnitTests/BackEnd/TaskEnvironment_Tests.cs:359

  • multithreadedEnvironment is created via TaskEnvironment.CreateWithProjectDirectoryAndEnvironment(...), which wraps a MultiThreadedTaskEnvironmentDriver that should be disposed to clear thread-local state (e.g., FileUtilities.CurrentThreadWorkingDirectory). This test currently never disposes it, which can leak state across tests and cause flakiness. Please dispose the created TaskEnvironment (e.g., call DisposeTaskEnvironment(multithreadedEnvironment) in the finally, or use a using pattern if available).
            var multithreadedEnvironment = TaskEnvironment.CreateWithProjectDirectoryAndEnvironment(
                GetResolvedTempPath(),
                new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase));

            try
            {
                // Verify system and environement doesn't have the test variable initially
                Environment.GetEnvironmentVariable(testVarName).ShouldBeNull();
                multithreadedEnvironment.GetEnvironmentVariable(testVarName).ShouldBeNull();

                // Set variable in multithreaded environment
                multithreadedEnvironment.SetEnvironmentVariable(testVarName, testVarValue);

                // Multithreaded should have the value but system should not
                multithreadedEnvironment.GetEnvironmentVariable(testVarName).ShouldBe(testVarValue);
                Environment.GetEnvironmentVariable(testVarName).ShouldBeNull();
            }
            finally
            {
                Environment.SetEnvironmentVariable(testVarName, null);
            }

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@JanProvaznik
Copy link
Copy Markdown
Member

@SimaTian do this for migrated sdk tasks please

@AR-May AR-May enabled auto-merge (squash) March 30, 2026 15:24
@AR-May AR-May merged commit 4fde89a into dotnet:main Mar 30, 2026
10 checks passed
dfederm pushed a commit to dfederm/msbuild that referenced this pull request Apr 9, 2026
Co-authored-by: Jan Provazník <janpro@janpro.dev>
Co-authored-by: Copilot <175728472+Copilot@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.

4 participants