Add runtime mismatch validation when Runtime is explicitly specified in custom task#12642
Merged
YuliiaKovalova merged 10 commits intovs18.0from Oct 21, 2025
Merged
Conversation
Contributor
There was a problem hiding this comment.
Pull Request Overview
Adds validation to ensure custom tasks execute out-of-process when a Runtime parameter (e.g., "Net") is explicitly specified but doesn't match the current process runtime. Previously, this required explicit TaskFactory="TaskHostFactory" in the UsingTask declaration.
Key Changes
- Modified type loading logic to check if task host parameters match the current process
- Introduced
isTaskHostParamsMatchCurrentProcparameter to control metadata load context usage - Added end-to-end test validating out-of-process execution with explicit Runtime specification
Reviewed Changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| src/Shared/TypeLoader.cs | Added isTaskHostParamsMatchCurrentProc parameter throughout type loading chain to enable runtime mismatch detection |
| src/Build/Instance/TaskFactories/AssemblyTaskFactory.cs | Initialized runtime mismatch detection by comparing task host parameters with current process |
| src/Build.UnitTests/TestAssets/ExampleNetTask/TestNetTaskWithImplicitParams/global.json | Added SDK configuration for test asset to prevent SDK path resolution issues |
| src/Build.UnitTests/TestAssets/ExampleNetTask/TestNetTaskWithImplicitParams/TestNetTaskWithImplicitParams.csproj | Created test project with Runtime="NET" to validate out-of-process execution |
| src/Build.UnitTests/NetTaskHost_E2E_Tests.cs | Added test verifying task executes out-of-process when Runtime is specified without TaskHostFactory |
| src/Build.UnitTests/Microsoft.Build.Engine.UnitTests.csproj | Added reference to new test asset's global.json file |
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
rainersigwald
approved these changes
Oct 20, 2025
baronfel
reviewed
Oct 21, 2025
surayya-MS
approved these changes
Oct 21, 2025
This was referenced Oct 21, 2025
YuliiaKovalova
added a commit
that referenced
this pull request
Dec 10, 2025
…ks (CLR35 and CLR2) (#12823) ### Context The recent change #12642 helped to uncover a long-term bug when a custom task was eventually executed out-of-proc, but due to lack of architecture/runtime match check BEFORE the TypeLoad, it was lazily loaded in-proc and pushed out-of-proc later. https://github.com/dotnet/msbuild/blob/ee6457a6b06e66a5e4037549c84d8282ecd0df92/src/Build/Instance/TaskFactories/AssemblyTaskFactory.cs#L335 look at `!TaskHostParametersMatchCurrentProcess` ### Changes Made - Fixed assembly enumeration: Previously only `.dll` files were added to MetadataLoadContext's resolver. Now `.exe` files are also included, since executables can be valid managed assemblies and dependencies. - Added legacy .NET Framework support (.NET Framework only): Added assembly discovery for v2.0 and v3.5 framework directories to support tasks that depend on legacy Microsoft.Build.Utilities.dll (as opposed to the modern Microsoft.Build.Utilities.Core.dll). ### Test Locally tested with custom tasks using both modern and legacy MSBuild assemblies + current tests confirm there are no regression. --------- 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.
Context
Currently, in order to have Runtime="Net" executed out-of-proc, you must explicitly specify TaskFactory="TaskHostFactory" in the UsingTask element.
Changes Made
Added runtime mismatch validation when Runtime is explicitly specified to omit TaskFactory specification in UsingTask.
Regression
No
Risks
Low — validation logic only affects cases where Runtime is explicitly set.
Testing
Added a dedicated test.