Skip to content

Add implicit architecture/runtime detection for custom tasks#12630

Merged
YuliiaKovalova merged 33 commits intomainfrom
dev/ykovalova_respect_task_arch_runtime
Nov 27, 2025
Merged

Add implicit architecture/runtime detection for custom tasks#12630
YuliiaKovalova merged 33 commits intomainfrom
dev/ykovalova_respect_task_arch_runtime

Conversation

@YuliiaKovalova
Copy link
Member

@YuliiaKovalova YuliiaKovalova commented Oct 10, 2025

Fixes #12511 and #6357

Context

MSBuild currently doesn't support implicit TaskHost resolution. When customers run custom tasks without explicit Runtime/Architecture specifications in their project files, MSBuild fails with an exception if the task's requirements don't match the current process. This forces users to manually specify Runtime and Architecture attributes, creating a poor developer experience and limiting cross-platform compatibility.

Example failure scenario:

Developer builds a project on x64 with a custom task compiled for x86
Without explicit <UsingTask Architecture="x86">, MSBuild throws an exception
The build fails even though MSBuild could automatically run the task in the correct host

Changes Made

Dynamic Task Analysis: When loading custom tasks, MSBuild now inspects assembly metadata to determine:

Target runtime (e.g., .NET Framework, NET)
Target architecture (x86, x64, etc.)
Intelligent Execution Routing:

  • If task requirements match the current process → execute in-process (fast path)
  • If task requirements don't match → automatically route to appropriate out-of-process TaskHost
  • Falls back to metadata inspection if in-process load fails due to architecture/runtime mismatch
{A542F30E-608A-4803-AD03-36A81449736F}

Testing

Existing tests + added coverage
for implicit architecture (x86. x64) and implicit runtime (NET)

@YuliiaKovalova YuliiaKovalova changed the title Add implicit architecture/runtime support Add implicit architecture/runtime detection from custom tasks Oct 10, 2025
@YuliiaKovalova YuliiaKovalova changed the title Add implicit architecture/runtime detection from custom tasks Add implicit architecture/runtime detection for custom tasks Oct 13, 2025
@YuliiaKovalova YuliiaKovalova self-assigned this Oct 13, 2025
@YuliiaKovalova YuliiaKovalova force-pushed the dev/ykovalova_respect_task_arch_runtime branch from 5d55c8e to adc7541 Compare October 13, 2025 15:42
@YuliiaKovalova YuliiaKovalova marked this pull request as ready for review October 13, 2025 15:52
Copilot AI review requested due to automatic review settings October 13, 2025 15:52
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR implements implicit architecture/runtime detection for custom tasks in MSBuild, allowing automatic TaskHost resolution without requiring explicit Runtime/Architecture attributes in project files. When custom tasks don't match the current process requirements, MSBuild now automatically detects the task's target runtime and architecture from assembly metadata and routes execution to the appropriate out-of-process TaskHost.

Key changes include:

  • Dynamic task analysis through assembly metadata inspection to determine target runtime (.NET Framework vs .NET) and architecture (x86, x64, etc.)
  • Intelligent execution routing that uses in-process execution when possible and falls back to out-of-process TaskHost when needed
  • Enhanced LoadedType class to carry architecture and runtime information extracted from PE headers and assembly attributes

Reviewed Changes

Copilot reviewed 20 out of 21 changed files in this pull request and generated 9 comments.

Show a summary per file
File Description
src/Shared/TypeUtilities.cs Simplified attribute detection methods by removing exception handling and using expression-bodied members
src/Shared/TypeLoader.cs Added assembly metadata analysis for runtime/architecture detection and fallback loading logic
src/Shared/LoadedType.cs Extended to store architecture/runtime information and handle MetadataLoadContext exceptions
src/MSBuildTaskHost/TypeLoader.cs Updated method signature to match new parameters
src/MSBuild/OutOfProcTaskAppDomainWrapperBase.cs Updated to use new TypeLoader parameter signature
src/Framework/MSBuildEventSource.cs Added event logging for fallback assembly loading
src/Build/Instance/TaskRegistry.cs Added logic to track when TaskHostFactory is explicitly requested
src/Build/Instance/TaskFactories/AssemblyTaskFactory.cs Refactored task factory logic to use detected runtime/architecture and handle MetadataLoadContext scenarios
src/Build.UnitTests/TestAssets/ExampleNetTask/TestNetTaskWithImplicitParams/* New test assets for .NET task with implicit parameters
src/Build.UnitTests/TestAssets/ExampleFrameworkTask/* New test assets for .NET Framework tasks with different architectures
src/Build.UnitTests/NetTaskHost_E2E_Tests.cs Added end-to-end test for implicit host parameter detection
src/Build.UnitTests/Microsoft.Build.Engine.UnitTests.csproj Updated project references and warnings for new test assets
src/Build.UnitTests/BackEnd/TaskHostFactory_Tests.cs Updated tests to use AssemblyFile instead of AssemblyName and improved assertions
src/Build.UnitTests/BackEnd/AssemblyTaskFactory_Tests.cs Modified test conditions and assembly loading logic
src/Build.UnitTests/BackEnd/AssemblyTaskFactory_E2E_Tests.cs New end-to-end tests for architecture mismatch handling

@YuliiaKovalova YuliiaKovalova force-pushed the dev/ykovalova_respect_task_arch_runtime branch from f016f6c to 3490ac9 Compare October 14, 2025 18:42
rainersigwald pushed a commit that referenced this pull request Oct 20, 2025
…odes (#12644)

### Context
When a path to dotnet.exe or MSBuild.dll contains spaces, it's
incorrectly escaped and causes errors like this:
Could not execute because the specified command or file was not found.
* You intended to execute a .NET program, but dotnet-C:\Program does not
exist.
The path gets truncated at the space character, causing the process
launch to fail.

The important notice – it’s a critical scenario for the feature
dogfooding , since dotnet.exe usually is placed in “Program Files” (path
with spaces).

It wasn’t caught during the development because the change required
coordination with SDK and the default private SDK was in a path
without spaces, unlike `C:\Program Files`.

### Changes Made
Added proper escaping for the msbuild.dll path to ensure it works
correctly even when the path contains spaces.

### Regression
No, it’s a bug in the new feature

### Risks
No risks, minimal well-boxed change

### Testing
UT + manual

### Notes
This logic is taken from #12630
@YuliiaKovalova YuliiaKovalova force-pushed the dev/ykovalova_respect_task_arch_runtime branch from 759daa9 to 14d3483 Compare November 4, 2025 13:52
@YuliiaKovalova YuliiaKovalova marked this pull request as ready for review November 4, 2025 14:04
@YuliiaKovalova YuliiaKovalova requested a review from a team as a code owner November 4, 2025 14:04
Copy link
Member

@rainersigwald rainersigwald left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Many nits but lgtm overall.

@YuliiaKovalova YuliiaKovalova force-pushed the dev/ykovalova_respect_task_arch_runtime branch from d88736e to ecac848 Compare November 25, 2025 16:29
@YuliiaKovalova YuliiaKovalova merged commit 2b17fa5 into main Nov 27, 2025
10 checks passed
@YuliiaKovalova YuliiaKovalova deleted the dev/ykovalova_respect_task_arch_runtime branch November 27, 2025 11:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

MSBuild should be able to detect and automatically use .NET TaskHost when a .NET Task dll is provided

3 participants