From 347a5fdf7553738038535e2a0a4c8436a5a3cd43 Mon Sep 17 00:00:00 2001 From: AR-May <67507805+AR-May@users.noreply.github.com> Date: Fri, 13 Mar 2026 16:21:55 +0100 Subject: [PATCH 1/8] Expose public constructor for TaskEnvironment. --- src/Framework/TaskEnvironment.cs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/Framework/TaskEnvironment.cs b/src/Framework/TaskEnvironment.cs index ec18be1d817..41afc837ecb 100644 --- a/src/Framework/TaskEnvironment.cs +++ b/src/Framework/TaskEnvironment.cs @@ -21,6 +21,13 @@ internal TaskEnvironment(ITaskEnvironmentDriver driver) { _driver = driver; } + + /// + /// Initializes a new instance of the TaskEnvironment class using the default multi-process execution mode. + /// This mode directly accesses the system environment variables and current working directory. + /// + public TaskEnvironment() : this(MultiProcessTaskEnvironmentDriver.Instance) + { } /// /// Gets or sets the project directory for the task execution. From 53b9b0ee280ae955f8525f3e0ece0d5f37cfe71b Mon Sep 17 00:00:00 2001 From: AR-May <67507805+AR-May@users.noreply.github.com> Date: Tue, 24 Mar 2026 10:19:28 +0100 Subject: [PATCH 2/8] Expose both environments. Address comments. --- src/Framework/TaskEnvironment.cs | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/src/Framework/TaskEnvironment.cs b/src/Framework/TaskEnvironment.cs index 41afc837ecb..1128a295d91 100644 --- a/src/Framework/TaskEnvironment.cs +++ b/src/Framework/TaskEnvironment.cs @@ -21,13 +21,31 @@ internal TaskEnvironment(ITaskEnvironmentDriver driver) { _driver = driver; } - + + /// + /// Gets the default task environment that directly accesses the system environment variables and current working directory. + /// + /// + /// Suitable for multi-process execution mode. Tasks running with this environment will see the same environment variables and working directory as the MSBuild process. + /// + public static TaskEnvironment Default { get; } = new(MultiProcessTaskEnvironmentDriver.Instance); + /// - /// Initializes a new instance of the TaskEnvironment class using the default multi-process execution mode. - /// This mode directly accesses the system environment variables and current working directory. + /// Creates a new with isolated environment variables and working directory. /// - public TaskEnvironment() : this(MultiProcessTaskEnvironmentDriver.Instance) - { } + /// + /// Suitable for multithreaded execution mode where each task needs its own environment state + /// that does not interfere with other concurrently executing tasks. + /// + /// The initial working directory for the task. + /// A dictionary of environment variables to use, or to use the current process environment variables. + /// A new with isolated environment state. + public static TaskEnvironment CreateMultithreaded(string projectDirectory, IDictionary? environmentVariables = null) + { + return environmentVariables is null + ? new TaskEnvironment(new MultiThreadedTaskEnvironmentDriver(projectDirectory)) + : new TaskEnvironment(new MultiThreadedTaskEnvironmentDriver(projectDirectory, environmentVariables)); + } /// /// Gets or sets the project directory for the task execution. From bc86395bd35e6ab8990304cf87c700f033b8a10c Mon Sep 17 00:00:00 2001 From: AR-May <67507805+AR-May@users.noreply.github.com> Date: Tue, 24 Mar 2026 12:10:05 +0100 Subject: [PATCH 3/8] And null checks. Add tests. --- .../BackEnd/TaskEnvironment_Tests.cs | 115 ++++++++++++++++++ src/Framework/TaskEnvironment.cs | 7 +- 2 files changed, 121 insertions(+), 1 deletion(-) diff --git a/src/Build.UnitTests/BackEnd/TaskEnvironment_Tests.cs b/src/Build.UnitTests/BackEnd/TaskEnvironment_Tests.cs index 4a9d6126ced..df9c19e9953 100644 --- a/src/Build.UnitTests/BackEnd/TaskEnvironment_Tests.cs +++ b/src/Build.UnitTests/BackEnd/TaskEnvironment_Tests.cs @@ -360,6 +360,121 @@ public void TaskEnvironment_MultithreadedEnvironment_ShouldBeIsolatedFromSystem( } } + [Fact] + public void TaskEnvironment_Default_ReturnsSingleton() + { + TaskEnvironment first = TaskEnvironment.Default; + TaskEnvironment second = TaskEnvironment.Default; + + first.ShouldNotBeNull(); + first.ShouldBeSameAs(second); + } + + [Fact] + public void TaskEnvironment_Default_ReadsProcessEnvironment() + { + string testVarName = $"MSBUILD_DEFAULT_ENV_TEST_{Guid.NewGuid():N}"; + string testVarValue = "default_env_test_value"; + + try + { + Environment.SetEnvironmentVariable(testVarName, testVarValue); + + TaskEnvironment.Default.GetEnvironmentVariable(testVarName).ShouldBe(testVarValue); + } + finally + { + Environment.SetEnvironmentVariable(testVarName, null); + } + } + + [Fact] + public void TaskEnvironment_CreateMultithreaded_SnapshotsCurrentEnvironment() + { + string testVarName = $"MSBUILD_CREATE_MT_TEST_{Guid.NewGuid():N}"; + string testVarValue = "snapshot_test_value"; + string projectDir = GetResolvedTempPath(); + + try + { + Environment.SetEnvironmentVariable(testVarName, testVarValue); + + TaskEnvironment env = TaskEnvironment.CreateMultithreaded(projectDir); + + env.ShouldNotBeNull(); + env.GetEnvironmentVariable(testVarName).ShouldBe(testVarValue); + env.ProjectDirectory.Value.ShouldBe(projectDir); + + // Changing the process env var after snapshot should not affect the isolated environment. + Environment.SetEnvironmentVariable(testVarName, "changed_after_snapshot"); + env.GetEnvironmentVariable(testVarName).ShouldBe(testVarValue); + } + finally + { + Environment.SetEnvironmentVariable(testVarName, null); + } + } + + [Fact] + public void TaskEnvironment_CreateMultithreaded_WithCustomEnvironment_UsesProvidedDictionary() + { + string excludedVarName = $"MSBUILD_EXCLUDED_VAR_{Guid.NewGuid():N}"; + string projectDir = GetResolvedTempPath(); + + try + { + // Set a process-level env var that should NOT appear in the custom environment. + Environment.SetEnvironmentVariable(excludedVarName, "process_level_value"); + + var customEnv = new Dictionary(StringComparer.OrdinalIgnoreCase) + { + ["MY_CUSTOM_VAR"] = "custom_value", + ["ANOTHER_VAR"] = "another_value" + }; + + TaskEnvironment env = TaskEnvironment.CreateMultithreaded(projectDir, customEnv); + + env.ShouldNotBeNull(); + env.GetEnvironmentVariable("MY_CUSTOM_VAR").ShouldBe("custom_value"); + env.GetEnvironmentVariable("ANOTHER_VAR").ShouldBe("another_value"); + env.GetEnvironmentVariable(excludedVarName).ShouldBeNull(); + env.ProjectDirectory.Value.ShouldBe(projectDir); + } + finally + { + Environment.SetEnvironmentVariable(excludedVarName, null); + } + } + + [Fact] + public void TaskEnvironment_CreateMultithreaded_ReturnsIsolatedInstances() + { + string projectDir = GetResolvedTempPath(); + + TaskEnvironment env1 = TaskEnvironment.CreateMultithreaded(projectDir); + TaskEnvironment env2 = TaskEnvironment.CreateMultithreaded(projectDir); + + env1.ShouldNotBeSameAs(env2); + + string testVarName = $"MSBUILD_ISOLATION_TEST_{Guid.NewGuid():N}"; + env1.SetEnvironmentVariable(testVarName, "only_in_env1"); + + env1.GetEnvironmentVariable(testVarName).ShouldBe("only_in_env1"); + env2.GetEnvironmentVariable(testVarName).ShouldNotBe("only_in_env1"); + } + + [Fact] + public void TaskEnvironment_CreateMultithreaded_NullProjectDirectory_Throws() + { + Should.Throw(() => TaskEnvironment.CreateMultithreaded(null!)); + } + + [Fact] + public void TaskEnvironment_CreateMultithreaded_EmptyProjectDirectory_Throws() + { + Should.Throw(() => TaskEnvironment.CreateMultithreaded(string.Empty)); + } + [Theory] [MemberData(nameof(EnvironmentTypes))] public void TaskEnvironment_GetAbsolutePath_WithInvalidPathChars_ShouldNotThrow(string environmentType) diff --git a/src/Framework/TaskEnvironment.cs b/src/Framework/TaskEnvironment.cs index 1128a295d91..85ca3df5341 100644 --- a/src/Framework/TaskEnvironment.cs +++ b/src/Framework/TaskEnvironment.cs @@ -1,6 +1,7 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. +using System; using System.Collections.Generic; using System.Diagnostics; @@ -23,7 +24,7 @@ internal TaskEnvironment(ITaskEnvironmentDriver driver) } /// - /// Gets the default task environment that directly accesses the system environment variables and current working directory. + /// Gets the default task environment that directly accesses the system environment variables and current working directory. /// /// /// Suitable for multi-process execution mode. Tasks running with this environment will see the same environment variables and working directory as the MSBuild process. @@ -40,8 +41,12 @@ internal TaskEnvironment(ITaskEnvironmentDriver driver) /// The initial working directory for the task. /// A dictionary of environment variables to use, or to use the current process environment variables. /// A new with isolated environment state. + /// is . + /// is empty. public static TaskEnvironment CreateMultithreaded(string projectDirectory, IDictionary? environmentVariables = null) { + ArgumentException.ThrowIfNullOrEmpty(projectDirectory); + return environmentVariables is null ? new TaskEnvironment(new MultiThreadedTaskEnvironmentDriver(projectDirectory)) : new TaskEnvironment(new MultiThreadedTaskEnvironmentDriver(projectDirectory, environmentVariables)); From 4af43ee654184d73d0138d963433695120ccd219 Mon Sep 17 00:00:00 2001 From: AR-May <67507805+AR-May@users.noreply.github.com> Date: Wed, 25 Mar 2026 16:00:24 +0100 Subject: [PATCH 4/8] Rename function - keep the naming homogenous in the repo. --- .../BackEnd/TaskEnvironment_Tests.cs | 22 +++++++++---------- src/Framework/TaskEnvironment.cs | 2 +- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/Build.UnitTests/BackEnd/TaskEnvironment_Tests.cs b/src/Build.UnitTests/BackEnd/TaskEnvironment_Tests.cs index df9c19e9953..7169e4a70af 100644 --- a/src/Build.UnitTests/BackEnd/TaskEnvironment_Tests.cs +++ b/src/Build.UnitTests/BackEnd/TaskEnvironment_Tests.cs @@ -389,7 +389,7 @@ public void TaskEnvironment_Default_ReadsProcessEnvironment() } [Fact] - public void TaskEnvironment_CreateMultithreaded_SnapshotsCurrentEnvironment() + public void TaskEnvironment_CreateMultiThreaded_SnapshotsCurrentEnvironment() { string testVarName = $"MSBUILD_CREATE_MT_TEST_{Guid.NewGuid():N}"; string testVarValue = "snapshot_test_value"; @@ -399,7 +399,7 @@ public void TaskEnvironment_CreateMultithreaded_SnapshotsCurrentEnvironment() { Environment.SetEnvironmentVariable(testVarName, testVarValue); - TaskEnvironment env = TaskEnvironment.CreateMultithreaded(projectDir); + TaskEnvironment env = TaskEnvironment.CreateMultiThreaded(projectDir); env.ShouldNotBeNull(); env.GetEnvironmentVariable(testVarName).ShouldBe(testVarValue); @@ -416,7 +416,7 @@ public void TaskEnvironment_CreateMultithreaded_SnapshotsCurrentEnvironment() } [Fact] - public void TaskEnvironment_CreateMultithreaded_WithCustomEnvironment_UsesProvidedDictionary() + public void TaskEnvironment_CreateMultiThreaded_WithCustomEnvironment_UsesProvidedDictionary() { string excludedVarName = $"MSBUILD_EXCLUDED_VAR_{Guid.NewGuid():N}"; string projectDir = GetResolvedTempPath(); @@ -432,7 +432,7 @@ public void TaskEnvironment_CreateMultithreaded_WithCustomEnvironment_UsesProvid ["ANOTHER_VAR"] = "another_value" }; - TaskEnvironment env = TaskEnvironment.CreateMultithreaded(projectDir, customEnv); + TaskEnvironment env = TaskEnvironment.CreateMultiThreaded(projectDir, customEnv); env.ShouldNotBeNull(); env.GetEnvironmentVariable("MY_CUSTOM_VAR").ShouldBe("custom_value"); @@ -447,12 +447,12 @@ public void TaskEnvironment_CreateMultithreaded_WithCustomEnvironment_UsesProvid } [Fact] - public void TaskEnvironment_CreateMultithreaded_ReturnsIsolatedInstances() + public void TaskEnvironment_CreateMultiThreaded_ReturnsIsolatedInstances() { string projectDir = GetResolvedTempPath(); - TaskEnvironment env1 = TaskEnvironment.CreateMultithreaded(projectDir); - TaskEnvironment env2 = TaskEnvironment.CreateMultithreaded(projectDir); + TaskEnvironment env1 = TaskEnvironment.CreateMultiThreaded(projectDir); + TaskEnvironment env2 = TaskEnvironment.CreateMultiThreaded(projectDir); env1.ShouldNotBeSameAs(env2); @@ -464,15 +464,15 @@ public void TaskEnvironment_CreateMultithreaded_ReturnsIsolatedInstances() } [Fact] - public void TaskEnvironment_CreateMultithreaded_NullProjectDirectory_Throws() + public void TaskEnvironment_CreateMultiThreaded_NullProjectDirectory_Throws() { - Should.Throw(() => TaskEnvironment.CreateMultithreaded(null!)); + Should.Throw(() => TaskEnvironment.CreateMultiThreaded(null!)); } [Fact] - public void TaskEnvironment_CreateMultithreaded_EmptyProjectDirectory_Throws() + public void TaskEnvironment_CreateMultiThreaded_EmptyProjectDirectory_Throws() { - Should.Throw(() => TaskEnvironment.CreateMultithreaded(string.Empty)); + Should.Throw(() => TaskEnvironment.CreateMultiThreaded(string.Empty)); } [Theory] diff --git a/src/Framework/TaskEnvironment.cs b/src/Framework/TaskEnvironment.cs index 85ca3df5341..681cafbb472 100644 --- a/src/Framework/TaskEnvironment.cs +++ b/src/Framework/TaskEnvironment.cs @@ -43,7 +43,7 @@ internal TaskEnvironment(ITaskEnvironmentDriver driver) /// A new with isolated environment state. /// is . /// is empty. - public static TaskEnvironment CreateMultithreaded(string projectDirectory, IDictionary? environmentVariables = null) + public static TaskEnvironment CreateMultiThreaded(string projectDirectory, IDictionary? environmentVariables = null) { ArgumentException.ThrowIfNullOrEmpty(projectDirectory); From f8cc43888a877199d457ab1f7483deb7156d9fd3 Mon Sep 17 00:00:00 2001 From: AR-May <67507805+AR-May@users.noreply.github.com> Date: Thu, 26 Mar 2026 10:42:32 +0100 Subject: [PATCH 5/8] Address PR comments. --- .../BackEnd/TaskEnvironment_Tests.cs | 22 +++++++++---------- src/Framework/TaskEnvironment.cs | 15 +++++++------ 2 files changed, 19 insertions(+), 18 deletions(-) diff --git a/src/Build.UnitTests/BackEnd/TaskEnvironment_Tests.cs b/src/Build.UnitTests/BackEnd/TaskEnvironment_Tests.cs index 7169e4a70af..0e3d3b2e5ff 100644 --- a/src/Build.UnitTests/BackEnd/TaskEnvironment_Tests.cs +++ b/src/Build.UnitTests/BackEnd/TaskEnvironment_Tests.cs @@ -389,7 +389,7 @@ public void TaskEnvironment_Default_ReadsProcessEnvironment() } [Fact] - public void TaskEnvironment_CreateMultiThreaded_SnapshotsCurrentEnvironment() + public void TaskEnvironment_CreateWithProjectDirectoryAndEnvironment_SnapshotsCurrentEnvironment() { string testVarName = $"MSBUILD_CREATE_MT_TEST_{Guid.NewGuid():N}"; string testVarValue = "snapshot_test_value"; @@ -399,7 +399,7 @@ public void TaskEnvironment_CreateMultiThreaded_SnapshotsCurrentEnvironment() { Environment.SetEnvironmentVariable(testVarName, testVarValue); - TaskEnvironment env = TaskEnvironment.CreateMultiThreaded(projectDir); + TaskEnvironment env = TaskEnvironment.CreateWithProjectDirectoryAndEnvironment(projectDir); env.ShouldNotBeNull(); env.GetEnvironmentVariable(testVarName).ShouldBe(testVarValue); @@ -416,7 +416,7 @@ public void TaskEnvironment_CreateMultiThreaded_SnapshotsCurrentEnvironment() } [Fact] - public void TaskEnvironment_CreateMultiThreaded_WithCustomEnvironment_UsesProvidedDictionary() + public void TaskEnvironment_CreateWithProjectDirectoryAndEnvironment_WithCustomEnvironment_UsesProvidedDictionary() { string excludedVarName = $"MSBUILD_EXCLUDED_VAR_{Guid.NewGuid():N}"; string projectDir = GetResolvedTempPath(); @@ -432,7 +432,7 @@ public void TaskEnvironment_CreateMultiThreaded_WithCustomEnvironment_UsesProvid ["ANOTHER_VAR"] = "another_value" }; - TaskEnvironment env = TaskEnvironment.CreateMultiThreaded(projectDir, customEnv); + TaskEnvironment env = TaskEnvironment.CreateWithProjectDirectoryAndEnvironment(projectDir, customEnv); env.ShouldNotBeNull(); env.GetEnvironmentVariable("MY_CUSTOM_VAR").ShouldBe("custom_value"); @@ -447,12 +447,12 @@ public void TaskEnvironment_CreateMultiThreaded_WithCustomEnvironment_UsesProvid } [Fact] - public void TaskEnvironment_CreateMultiThreaded_ReturnsIsolatedInstances() + public void TaskEnvironment_CreateWithProjectDirectoryAndEnvironment_ReturnsIsolatedInstances() { string projectDir = GetResolvedTempPath(); - TaskEnvironment env1 = TaskEnvironment.CreateMultiThreaded(projectDir); - TaskEnvironment env2 = TaskEnvironment.CreateMultiThreaded(projectDir); + TaskEnvironment env1 = TaskEnvironment.CreateWithProjectDirectoryAndEnvironment(projectDir); + TaskEnvironment env2 = TaskEnvironment.CreateWithProjectDirectoryAndEnvironment(projectDir); env1.ShouldNotBeSameAs(env2); @@ -464,15 +464,15 @@ public void TaskEnvironment_CreateMultiThreaded_ReturnsIsolatedInstances() } [Fact] - public void TaskEnvironment_CreateMultiThreaded_NullProjectDirectory_Throws() + public void TaskEnvironment_CreateWithProjectDirectoryAndEnvironment_NullProjectDirectory_Throws() { - Should.Throw(() => TaskEnvironment.CreateMultiThreaded(null!)); + Should.Throw(() => TaskEnvironment.CreateWithProjectDirectoryAndEnvironment(null!)); } [Fact] - public void TaskEnvironment_CreateMultiThreaded_EmptyProjectDirectory_Throws() + public void TaskEnvironment_CreateWithProjectDirectoryAndEnvironment_EmptyProjectDirectory_Throws() { - Should.Throw(() => TaskEnvironment.CreateMultiThreaded(string.Empty)); + Should.Throw(() => TaskEnvironment.CreateWithProjectDirectoryAndEnvironment(string.Empty)); } [Theory] diff --git a/src/Framework/TaskEnvironment.cs b/src/Framework/TaskEnvironment.cs index 681cafbb472..d40005dbe39 100644 --- a/src/Framework/TaskEnvironment.cs +++ b/src/Framework/TaskEnvironment.cs @@ -24,26 +24,27 @@ internal TaskEnvironment(ITaskEnvironmentDriver driver) } /// - /// Gets the default task environment that directly accesses the system environment variables and current working directory. + /// Gets the default task environment that directly accesses the system environment variables and working directory of the current process. /// /// - /// Suitable for multi-process execution mode. Tasks running with this environment will see the same environment variables and working directory as the MSBuild process. + /// This environment is provided to tasks by MSBuild engine in multi-process execution mode. /// - public static TaskEnvironment Default { get; } = new(MultiProcessTaskEnvironmentDriver.Instance); + public static TaskEnvironment CurrentProcess { get; } = new(MultiProcessTaskEnvironmentDriver.Instance); /// - /// Creates a new with isolated environment variables and working directory. + /// Creates a new with isolated working directory and environment variables. /// /// - /// Suitable for multithreaded execution mode where each task needs its own environment state - /// that does not interfere with other concurrently executing tasks. + /// This method is primarily intended for testing scenarios. In normal MSBuild operation, the correct task environment is provided by the MSBuild engine. + /// The created TaskEnvironment provides isolated environment state similar to what tasks receive in multithreaded execution mode, enabling testing of task isolation behavior. /// /// The initial working directory for the task. /// A dictionary of environment variables to use, or to use the current process environment variables. /// A new with isolated environment state. /// is . /// is empty. - public static TaskEnvironment CreateMultiThreaded(string projectDirectory, IDictionary? environmentVariables = null) + [System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)] + public static TaskEnvironment CreateWithProjectDirectoryAndEnvironment(string projectDirectory, IDictionary? environmentVariables = null) { ArgumentException.ThrowIfNullOrEmpty(projectDirectory); From 5efe04ebce00b5c828072350f6df0956ba81bdd4 Mon Sep 17 00:00:00 2001 From: AR-May <67507805+AR-May@users.noreply.github.com> Date: Thu, 26 Mar 2026 17:33:56 +0100 Subject: [PATCH 6/8] Fix tests. --- src/Build.UnitTests/BackEnd/TaskEnvironment_Tests.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Build.UnitTests/BackEnd/TaskEnvironment_Tests.cs b/src/Build.UnitTests/BackEnd/TaskEnvironment_Tests.cs index 0e3d3b2e5ff..3098bc3fab0 100644 --- a/src/Build.UnitTests/BackEnd/TaskEnvironment_Tests.cs +++ b/src/Build.UnitTests/BackEnd/TaskEnvironment_Tests.cs @@ -363,8 +363,8 @@ public void TaskEnvironment_MultithreadedEnvironment_ShouldBeIsolatedFromSystem( [Fact] public void TaskEnvironment_Default_ReturnsSingleton() { - TaskEnvironment first = TaskEnvironment.Default; - TaskEnvironment second = TaskEnvironment.Default; + TaskEnvironment first = TaskEnvironment.CurrentProcess; + TaskEnvironment second = TaskEnvironment.CurrentProcess; first.ShouldNotBeNull(); first.ShouldBeSameAs(second); @@ -380,7 +380,7 @@ public void TaskEnvironment_Default_ReadsProcessEnvironment() { Environment.SetEnvironmentVariable(testVarName, testVarValue); - TaskEnvironment.Default.GetEnvironmentVariable(testVarName).ShouldBe(testVarValue); + TaskEnvironment.CurrentProcess.GetEnvironmentVariable(testVarName).ShouldBe(testVarValue); } finally { From de7beacfb843f80ad8c23f4cb5584598a24294d6 Mon Sep 17 00:00:00 2001 From: AR-May <67507805+AR-May@users.noreply.github.com> Date: Fri, 27 Mar 2026 17:29:12 +0100 Subject: [PATCH 7/8] Rename the field. --- src/Build.UnitTests/BackEnd/TaskEnvironment_Tests.cs | 10 +++++----- src/Framework/TaskEnvironment.cs | 8 +++++--- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/src/Build.UnitTests/BackEnd/TaskEnvironment_Tests.cs b/src/Build.UnitTests/BackEnd/TaskEnvironment_Tests.cs index 3098bc3fab0..e445357d96c 100644 --- a/src/Build.UnitTests/BackEnd/TaskEnvironment_Tests.cs +++ b/src/Build.UnitTests/BackEnd/TaskEnvironment_Tests.cs @@ -361,17 +361,17 @@ public void TaskEnvironment_MultithreadedEnvironment_ShouldBeIsolatedFromSystem( } [Fact] - public void TaskEnvironment_Default_ReturnsSingleton() + public void TaskEnvironment_Fallback_ReturnsSingleton() { - TaskEnvironment first = TaskEnvironment.CurrentProcess; - TaskEnvironment second = TaskEnvironment.CurrentProcess; + TaskEnvironment first = TaskEnvironment.Fallback; + TaskEnvironment second = TaskEnvironment.Fallback; first.ShouldNotBeNull(); first.ShouldBeSameAs(second); } [Fact] - public void TaskEnvironment_Default_ReadsProcessEnvironment() + public void TaskEnvironment_Fallback_ReadsProcessEnvironment() { string testVarName = $"MSBUILD_DEFAULT_ENV_TEST_{Guid.NewGuid():N}"; string testVarValue = "default_env_test_value"; @@ -380,7 +380,7 @@ public void TaskEnvironment_Default_ReadsProcessEnvironment() { Environment.SetEnvironmentVariable(testVarName, testVarValue); - TaskEnvironment.CurrentProcess.GetEnvironmentVariable(testVarName).ShouldBe(testVarValue); + TaskEnvironment.Fallback.GetEnvironmentVariable(testVarName).ShouldBe(testVarValue); } finally { diff --git a/src/Framework/TaskEnvironment.cs b/src/Framework/TaskEnvironment.cs index d40005dbe39..43f3c92315e 100644 --- a/src/Framework/TaskEnvironment.cs +++ b/src/Framework/TaskEnvironment.cs @@ -24,12 +24,14 @@ internal TaskEnvironment(ITaskEnvironmentDriver driver) } /// - /// Gets the default task environment that directly accesses the system environment variables and working directory of the current process. + /// Gets the fallback task environment that directly accesses the system environment variables + /// and working directory of the current process. /// /// - /// This environment is provided to tasks by MSBuild engine in multi-process execution mode. + /// This is the environment provided to tasks by the MSBuild engine in multi-process execution mode, + /// where each task runs in its own process and process-level state is inherently isolated. /// - public static TaskEnvironment CurrentProcess { get; } = new(MultiProcessTaskEnvironmentDriver.Instance); + public static TaskEnvironment Fallback { get; } = new(MultiProcessTaskEnvironmentDriver.Instance); /// /// Creates a new with isolated working directory and environment variables. From 8a051b19535ae26103e44403af9e7517e2541c68 Mon Sep 17 00:00:00 2001 From: Alina Mayorova <67507805+AR-May@users.noreply.github.com> Date: Mon, 30 Mar 2026 09:13:12 +0000 Subject: [PATCH 8/8] Remove test. --- src/Build.UnitTests/BackEnd/TaskEnvironment_Tests.cs | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/src/Build.UnitTests/BackEnd/TaskEnvironment_Tests.cs b/src/Build.UnitTests/BackEnd/TaskEnvironment_Tests.cs index e445357d96c..43b7910f1c0 100644 --- a/src/Build.UnitTests/BackEnd/TaskEnvironment_Tests.cs +++ b/src/Build.UnitTests/BackEnd/TaskEnvironment_Tests.cs @@ -360,16 +360,6 @@ public void TaskEnvironment_MultithreadedEnvironment_ShouldBeIsolatedFromSystem( } } - [Fact] - public void TaskEnvironment_Fallback_ReturnsSingleton() - { - TaskEnvironment first = TaskEnvironment.Fallback; - TaskEnvironment second = TaskEnvironment.Fallback; - - first.ShouldNotBeNull(); - first.ShouldBeSameAs(second); - } - [Fact] public void TaskEnvironment_Fallback_ReadsProcessEnvironment() {