-
Notifications
You must be signed in to change notification settings - Fork 1.4k
[vs18.3] Fix the lifecycle of task host factory #13022
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
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
63306eb
Fix the lifecycle of task host factory when using Runtime=NET
ViktorHofer 320678b
Add documentation
ViktorHofer 6b22123
Update task execution matrix formatting in documentation
ViktorHofer aea772b
Add the test
ViktorHofer 573e1c6
Merge branch 'vs18.3' into FixAssemblyTaskFactoryLifecycleVS183
ViktorHofer d450acd
Fix tasks for NET
ViktorHofer 11246e6
Fix test
ViktorHofer 668e73f
Fix when there's already a long running build
ViktorHofer 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
120 changes: 120 additions & 0 deletions
120
src/Build.UnitTests/TaskHostFactoryLifecycle_E2E_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,120 @@ | ||
| // 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.IO; | ||
| using System.Runtime.InteropServices; | ||
| using Microsoft.Build.UnitTests; | ||
| using Microsoft.Build.UnitTests.Shared; | ||
| using Shouldly; | ||
| using Xunit; | ||
| using Xunit.Abstractions; | ||
|
|
||
| namespace Microsoft.Build.Engine.UnitTests | ||
| { | ||
| /// <summary> | ||
| /// End-to-end tests for task host factory lifecycle behavior. | ||
| /// | ||
| /// Tests validate the behavior based on whether the TaskHost runtime matches | ||
| /// the executing MSBuild runtime and whether TaskHostFactory is explicitly requested. | ||
| /// | ||
| /// This is a regression test for https://github.com/dotnet/msbuild/issues/13013 | ||
| /// </summary> | ||
| public class TaskHostFactoryLifecycle_E2E_Tests | ||
| { | ||
| private static string AssemblyLocation { get; } = Path.Combine(Path.GetDirectoryName(typeof(TaskHostFactoryLifecycle_E2E_Tests).Assembly.Location) ?? System.AppContext.BaseDirectory); | ||
|
|
||
| private static string TestAssetsRootPath { get; } = Path.Combine(AssemblyLocation, "TestAssets", "TaskHostLifecycle"); | ||
|
|
||
| private readonly ITestOutputHelper _output; | ||
|
|
||
| public TaskHostFactoryLifecycle_E2E_Tests(ITestOutputHelper output) | ||
| { | ||
| _output = output; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Validates task host lifecycle behavior for all scenarios. | ||
| /// | ||
| /// Test scenarios: | ||
| /// 1. Runtime matches + TaskHostFactory requested → short-lived out of proc (nodereuse:False) | ||
| /// 2. Runtime matches + TaskHostFactory NOT requested → in-proc execution | ||
| /// 3. Runtime doesn't match + TaskHostFactory requested → short-lived out of proc (nodereuse:False) | ||
| /// 4. Runtime doesn't match + TaskHostFactory NOT requested → long-lived sidecar out of proc (nodereuse:True) | ||
| /// </summary> | ||
| /// <param name="runtimeToUse">The runtime to use for the task (CurrentRuntime or NET)</param> | ||
| /// <param name="taskFactoryToUse">The task factory to use (TaskHostFactory or AssemblyTaskFactory)</param> | ||
| [Theory] | ||
| #if NET | ||
| [InlineData("CurrentRuntime", "AssemblyTaskFactory")] // Match + No Explicit → in-proc | ||
| [InlineData("CurrentRuntime", "TaskHostFactory")] // Match + Explicit → short-lived out-of-proc | ||
| #endif | ||
| [InlineData("NET", "AssemblyTaskFactory")] // No Match + No Explicit → long-lived sidecar out-of-proc | ||
| [InlineData("NET", "TaskHostFactory")] // No Match + Explicit → short-lived out-of-proc | ||
| public void TaskHostLifecycle_ValidatesAllScenarios( | ||
| string runtimeToUse, | ||
| string taskFactoryToUse) | ||
| { | ||
| bool? expectedNodeReuse; | ||
|
|
||
| // TaskHostFactory is always short lived and out-of-proc | ||
| if (taskFactoryToUse == "TaskHostFactory") | ||
| { | ||
| expectedNodeReuse = false; | ||
| } | ||
| // AssemblyTaskFactory behavior depends on runtime | ||
| else if (taskFactoryToUse == "AssemblyTaskFactory") | ||
| { | ||
| if (runtimeToUse == "CurrentRuntime") | ||
| { | ||
| // in-proc | ||
| expectedNodeReuse = null; | ||
| } | ||
| else if (runtimeToUse == "NET") | ||
| { | ||
| // When running on .NET Framework: out-of-proc, otherwise on .NET in-proc. | ||
| expectedNodeReuse = RuntimeInformation.FrameworkDescription.StartsWith(".NET Framework", StringComparison.OrdinalIgnoreCase) ? true : null; | ||
| } | ||
| else | ||
| { | ||
| throw new ArgumentOutOfRangeException(nameof(runtimeToUse), "Unknown runtime to use: " + runtimeToUse); | ||
| } | ||
| } | ||
| else | ||
| { | ||
| throw new ArgumentOutOfRangeException(nameof(taskFactoryToUse), "Unknown task factory to use: " + taskFactoryToUse); | ||
| } | ||
|
|
||
| using TestEnvironment env = TestEnvironment.Create(_output); | ||
| string testProjectPath = Path.Combine(TestAssetsRootPath, "TaskHostLifecycleTestApp.csproj"); | ||
|
|
||
| string testTaskOutput = RunnerUtilities.ExecBootstrapedMSBuild( | ||
| $"{testProjectPath} -v:n -restore /p:RuntimeToUse={runtimeToUse} /p:TaskFactoryToUse={taskFactoryToUse}", | ||
| out bool successTestTask, | ||
| outputHelper: _output); | ||
|
|
||
| successTestTask.ShouldBeTrue(); | ||
|
|
||
| // Verify execution mode (out-of-proc vs in-proc) and node reuse behavior | ||
| if (expectedNodeReuse.HasValue) | ||
| { | ||
| // For out-of-proc scenarios, validate the task runs in a separate process | ||
| // by checking for the presence of command-line arguments that indicate task host execution | ||
| testTaskOutput.ShouldContain("/nodemode:", | ||
| customMessage: "Task should run out-of-proc and have /nodemode: in its command-line arguments"); | ||
|
|
||
| // Validate the nodereuse flag in the task's command-line arguments | ||
| string expectedFlag = expectedNodeReuse.Value ? "/nodereuse:True" : "/nodereuse:False"; | ||
| testTaskOutput.ShouldContain(expectedFlag, | ||
| customMessage: $"Task should have {expectedFlag} in its command-line arguments"); | ||
| } | ||
| else | ||
| { | ||
| // For in-proc scenarios, validate the task does NOT run in a task host | ||
| // by ensuring task host specific command-line flags are not present | ||
| testTaskOutput.ShouldNotContain("/nodemode:", | ||
| customMessage: "Task should run in-proc and not have task host command-line arguments like /nodemode:"); | ||
| } | ||
| } | ||
| } | ||
| } |
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
19 changes: 19 additions & 0 deletions
19
src/Build.UnitTests/TestAssets/TaskHostLifecycle/TaskHostLifecycleTestApp.csproj
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,19 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <TargetFramework>net10.0</TargetFramework> | ||
| <TestProjectFolder>$([System.IO.Path]::GetFullPath('$([System.IO.Path]::Combine('$(AssemblyLocation)', '..'))'))</TestProjectFolder> | ||
| <ExampleTaskPath>$([System.IO.Path]::Combine('$(TestProjectFolder)', '$(TargetFramework)', 'ExampleTask.dll'))</ExampleTaskPath> | ||
| </PropertyGroup> | ||
|
|
||
| <UsingTask | ||
| TaskName="ExampleTask" | ||
| AssemblyFile="$(ExampleTaskPath)" | ||
| TaskFactory="$(TaskFactoryToUse)" | ||
| Runtime="$(RuntimeToUse)"/> | ||
|
|
||
| <Target Name="TestTask" BeforeTargets="Build"> | ||
| <ExampleTask /> | ||
| </Target> | ||
|
|
||
| </Project> |
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.