-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Add default fallback TaskEnvironment to all IMultiThreadableTask implementations #13415
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
JanProvaznik
merged 7 commits into
dotnet:main
from
JanProvaznik:dev/janprovaznik/fallback-multiprocess-task-env
Mar 19, 2026
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
3c15385
Add default fallback TaskEnvironment to all IMultiThreadableTask impl…
JanProvaznik 210980d
Update docs and skills to reflect default TaskEnvironment on built-in…
JanProvaznik 186c6a9
Merge branch 'main' into dev/janprovaznik/fallback-multiprocess-task-env
JanProvaznik 5f78ece
Update .github/skills/multithreaded-task-migration/SKILL.md
JanProvaznik 56f4149
Update documentation/specs/multithreading/thread-safe-tasks.md
JanProvaznik 9057318
Update documentation/specs/multithreading/multithreaded-msbuild.md
JanProvaznik 110063d
Update documentation/specs/multithreading/multithreaded-msbuild.md
JanProvaznik File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
122 changes: 122 additions & 0 deletions
122
src/Build.UnitTests/BackEnd/TaskHost_MultiThreadableTask_Tests.cs
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,122 @@ | ||
| // 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.IO; | ||
| using System.Reflection; | ||
| using Microsoft.Build.Execution; | ||
| using Microsoft.Build.Framework; | ||
| using Microsoft.Build.Tasks; | ||
| using Microsoft.Build.UnitTests; | ||
| using Microsoft.Build.Utilities; | ||
| using Shouldly; | ||
| using Xunit; | ||
| using Xunit.Abstractions; | ||
|
|
||
| namespace Microsoft.Build.Engine.UnitTests.BackEnd | ||
| { | ||
| /// <summary> | ||
| /// Tests that IMultiThreadableTask implementations always have a usable TaskEnvironment, | ||
| /// even when explicitly instantiated or run in the out-of-proc task host. | ||
| /// </summary> | ||
| public class TaskHost_MultiThreadableTask_Tests : IDisposable | ||
| { | ||
| private readonly ITestOutputHelper _output; | ||
| private readonly TestEnvironment _env; | ||
| private readonly string _testProjectsDir; | ||
|
|
||
| public TaskHost_MultiThreadableTask_Tests(ITestOutputHelper output) | ||
| { | ||
| _output = output; | ||
| _env = TestEnvironment.Create(output); | ||
| _testProjectsDir = _env.CreateFolder().Path; | ||
| } | ||
|
|
||
| public void Dispose() | ||
| { | ||
| _env.Dispose(); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// A subclass of a built-in IMultiThreadableTask (MakeDir) should inherit the | ||
| /// non-null default TaskEnvironment. This covers the scenario where someone | ||
| /// derives from a built-in task and explicitly instantiates it. | ||
| /// </summary> | ||
| [Fact] | ||
| public void ExplicitlyInstantiated_InheritedTask_HasNonNullTaskEnvironment() | ||
| { | ||
| var task = new DerivedMakeDirTask(); | ||
| IMultiThreadableTask multiThreadable = task; | ||
|
|
||
| multiThreadable.TaskEnvironment.ShouldNotBeNull(); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// When a task that inherits from a built-in IMultiThreadableTask (MakeDir) runs in | ||
| /// the out-of-proc task host (via TaskHostFactory), the inherited default TaskEnvironment | ||
| /// should be usable. The task accesses TaskEnvironment.ProjectDirectory in Execute() — | ||
| /// without the default it would NRE. | ||
| /// </summary> | ||
| [Fact] | ||
| public void InheritedTask_InTaskHost_HasUsableTaskEnvironment() | ||
| { | ||
| string projectContent = $""" | ||
| <Project> | ||
| <UsingTask TaskName="DerivedMakeDirTask" | ||
| AssemblyFile="{Assembly.GetExecutingAssembly().Location}" | ||
| TaskFactory="TaskHostFactory" /> | ||
|
|
||
| <Target Name="TestTarget"> | ||
| <DerivedMakeDirTask Directories="does-not-matter" /> | ||
| </Target> | ||
| </Project> | ||
| """; | ||
|
|
||
| string projectFile = Path.Combine(_testProjectsDir, "TaskEnvTest.proj"); | ||
| File.WriteAllText(projectFile, projectContent); | ||
|
|
||
| var logger = new MockLogger(_output); | ||
| var buildParameters = new BuildParameters | ||
| { | ||
| Loggers = [logger], | ||
| DisableInProcNode = false, | ||
| EnableNodeReuse = false, | ||
| }; | ||
|
|
||
| var buildRequestData = new BuildRequestData( | ||
| projectFile, | ||
| new Dictionary<string, string?>(), | ||
| null, | ||
| ["TestTarget"], | ||
| null); | ||
|
|
||
| var result = BuildManager.DefaultBuildManager.Build(buildParameters, buildRequestData); | ||
|
|
||
| _output.WriteLine(logger.FullLog); | ||
|
|
||
| result.OverallResult.ShouldBe(BuildResultCode.Success); | ||
|
|
||
| // Verify the task actually ran in the task host | ||
| TaskRouterTestHelper.AssertTaskUsedTaskHost(logger, "DerivedMakeDirTask"); | ||
|
|
||
| // Verify the task was able to read ProjectDirectory without NRE | ||
| logger.FullLog.ShouldContain("TaskEnvironment.ProjectDirectory="); | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Task that inherits from the built-in MakeDir (which implements IMultiThreadableTask). | ||
| /// Does NOT declare its own TaskEnvironment — it relies on the inherited default from MakeDir. | ||
| /// Overrides Execute() to log TaskEnvironment.ProjectDirectory, proving the default works. | ||
| /// </summary> | ||
| public class DerivedMakeDirTask : MakeDir | ||
| { | ||
| public override bool Execute() | ||
| { | ||
| string projectDir = TaskEnvironment.ProjectDirectory; | ||
| Log.LogMessage(MessageImportance.High, $"TaskEnvironment.ProjectDirectory={projectDir}"); | ||
| return true; | ||
| } | ||
| } | ||
| } | ||
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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.
Uh oh!
There was an error while loading. Please reload this page.