Refactor: use TaskEnvironment public API#13462
Merged
AR-May merged 2 commits intodotnet:mainfrom Mar 30, 2026
Merged
Conversation
Contributor
There was a problem hiding this comment.
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)withTaskEnvironment.Fallbackacross built-in tasks and test helpers. - Update engine and unit tests to use
TaskEnvironment.CreateWithProjectDirectoryAndEnvironment(...)instead of directly new’ingMultiThreadedTaskEnvironmentDriver. - 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
multithreadedEnvironmentis created viaTaskEnvironment.CreateWithProjectDirectoryAndEnvironment(...), which wraps aMultiThreadedTaskEnvironmentDriverthat 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 createdTaskEnvironment(e.g., callDisposeTaskEnvironment(multithreadedEnvironment)in thefinally, or use ausingpattern 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);
}
rainersigwald
approved these changes
Mar 30, 2026
JanProvaznik
approved these changes
Mar 30, 2026
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Member
|
@SimaTian do this for migrated sdk tasks please |
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
No description provided.