Skip to content

Set host path for direct MSBuild builds - #55581

Merged
jonathanpeppers merged 3 commits into
mainfrom
jonathanpeppers-fix-dotnet-host-path
Aug 4, 2026
Merged

jonathanpeppers merged 3 commits into
mainfrom
jonathanpeppers-fix-dotnet-host-path

Conversation

@jonathanpeppers

Copy link
Copy Markdown
Member

dotnet test MTP and device-aware dotnet run invoke some MSBuild targets in-process through ProjectInstance.Build, bypassing the forwarding path that sets DOTNET_HOST_PATH. This can cause runtime bootstrap tasks, including Android ILLink targets, to fail with MSB4221.

Use the existing MSBuild forwarding environment-variable helper as a scoped operation around project evaluation and each direct build. The scope restores the prior environment value immediately after each operation, so it does not leak into launched applications.

Add target-time device test assertions for DOTNET_HOST_PATH. The MTP regression removes the variable from the test process, proving the direct build path supplies it; existing run device scenarios exercise the corresponding run targets.

Tests:

  • GivenDotnetRunSelectsDevice (23 tests)
  • GivenDotnetTestSelectsDevice (31 tests)
  • src\Cli\dotnet\dotnet.csproj Debug build

Co-authored-by: Copilot App <[email protected]>

Copilot-Session: 452f1417-5b58-4aa0-9182-e8c75f219a12
Copilot AI review requested due to automatic review settings August 3, 2026 19:03
@jonathanpeppers
jonathanpeppers requested review from a team as code owners August 3, 2026 19:03
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 1 pipeline(s).
2 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

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 addresses failures in device-aware dotnet test (MTP) and dotnet run paths that invoke MSBuild in-process (via ProjectInstance.Build / ProjectInstance.FromFile) without the usual forwarding setup that provides DOTNET_HOST_PATH. It factors the MSBuild-required environment setup into a disposable scope and uses it around direct evaluation/build operations, plus adds regression assertions in device test assets.

Changes:

  • Added MSBuildForwardingAppWithoutLogging.SetMSBuildRequiredEnvironmentVariables() to scope MSBuild-required env vars (including DOTNET_HOST_PATH) and restore them on dispose.
  • Wrapped direct MSBuild evaluation/build calls in dotnet run device-selection flows and MTP device flows with that environment scope.
  • Added test-asset MSBuild targets to assert $(DOTNET_HOST_PATH) is set during device-target execution, and added a dotnet test regression test that removes DOTNET_HOST_PATH from the test process environment.

Reviewed changes

Copilot reviewed 10 out of 10 changed files in this pull request and generated no comments.

Show a summary per file
File Description
test/TestAssets/TestProjects/DotnetTestDevices/DotnetTestDevices.csproj Adds a target-time assertion that $(DOTNET_HOST_PATH) is set for device-related targets.
test/TestAssets/TestProjects/DotnetRunDevices/DotnetRunDevices.csproj Adds the same DOTNET_HOST_PATH assertion for run-device target execution.
test/dotnet.Tests/CommandTests/Test/GivenDotnetTestSelectsDevice.cs Adds a regression test ensuring direct device targets set DOTNET_HOST_PATH even if removed from the parent process environment.
src/Cli/Microsoft.DotNet.Cli.Utils/MSBuildForwardingAppWithoutLogging.cs Refactors env-var setup/restore into a reusable disposable scope and uses it for in-proc MSBuild execution.
src/Cli/dotnet/Commands/Test/MTP/SolutionAndProjectUtility.cs Applies the environment scope around direct in-proc device deploy/run-argument MSBuild targets.
src/Cli/dotnet/Commands/Test/MTP/MSBuildUtility.cs Applies the environment scope around MTP solution/project MSBuild operations and evaluation.
src/Cli/dotnet/Commands/Test/MTP/MicrosoftTestingPlatformTestCommand.cs Applies the environment scope when evaluating projects to resolve target frameworks for --device.
src/Cli/dotnet/Commands/Run/VirtualProjectBuildingCommand.cs Switches to the shared env-var scope instead of ad-hoc save/restore.
src/Cli/dotnet/Commands/Run/RunCommandSelector.cs Applies the environment scope around direct project evaluation/restore/build target invocations for device selection.
src/Cli/dotnet/Commands/Run/RunCommand.cs Applies the environment scope around project evaluation + ComputeRunArguments execution.
Suppressed comments (1)

src/Cli/dotnet/Commands/Run/RunCommand.cs:596

  • The try block that scopes SetMSBuildRequiredEnvironmentVariables() is not closed before the finally that follows, which makes this method syntactically invalid (missing } before finally). This should be simplified to a using (...) { ... } scope (or remove the try/finally entirely) so the environment scope is disposed correctly without an empty finally block.
                using var _ = MSBuildForwardingAppWithoutLogging.SetMSBuildRequiredEnvironmentVariables();
                project = EvaluateProject(ProjectFileFullPath, projectFactory, MSBuildArgs, logger);
                ValidatePreconditions(project);
                hasRuntimeEnvironmentVariableSupport = InvokeRunArgumentsTarget(project, NoBuild, logger, MSBuildArgs, EnvironmentVariables);
            }

Co-authored-by: Copilot App <[email protected]>

Copilot-Session: ea84fd72-34f1-4444-a407-ee5651625c9c
@jonathanpeppers
jonathanpeppers enabled auto-merge (squash) August 4, 2026 17:21
@jonathanpeppers
jonathanpeppers merged commit fd07760 into main Aug 4, 2026
21 checks passed
@jonathanpeppers
jonathanpeppers deleted the jonathanpeppers-fix-dotnet-host-path branch August 4, 2026 17:41
jonathanpeppers added a commit to dotnet/android that referenced this pull request Aug 4, 2026
- **Dependency Updates**:
  - From [11.0.0-beta.26365.101 to 11.0.0-beta.26401.101][3]
     - Microsoft.DotNet.Arcade.Sdk
     - Microsoft.DotNet.Build.Tasks.Feed
  - From [0.11.5-preview.26365.101 to 0.11.5-preview.26401.101][3]
     - Microsoft.DotNet.Cecil
  - From [11.0.0-preview.7.26365.101 to 11.0.0-rc.1.26401.101][3]
     - Microsoft.NET.ILLink
     - Microsoft.NETCore.App.Ref
  - From [11.0.100-preview.7.26365.101 to 11.0.100-rc.1.26401.101][3]
     - Microsoft.NET.Sdk
     - Microsoft.TemplateEngine.Authoring.Tasks

[3]: dotnet/dotnet@cb8306a...5da2de2

## Additional changes

- Run APK instrumentation through `dotnet test` and set `DOTNET_HOST_PATH` as a workaround pending dotnet/sdk#55581.
- Ensure nested MSBuild invocations include bound Java source JARs on the `javac` classpath.
- Temporarily skip the Mono APK smoke test due to dotnet/runtime#131760.

Co-authored-by: Jonathan Peppers <[email protected]>
Co-authored-by: Copilot App <[email protected]>
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 11.0-rc1 milestone Aug 5, 2026
Xiaoy312 added a commit to unoplatform/Uno.microsoft-ui-reactor that referenced this pull request Oct 29, 2026
`dotnet run -f net10.0-android` failed with MSB4221 "DOTNET_HOST_PATH is
not set" when the app has a ProjectReference to an Uno library, as the
in-repo samples have to Reactor.Uno: dotnet run builds DeployToDevice in
its own process without the variable, DeployToDevice rebuilds the
references, and Uno's EmbeddedResourceInjectorTask runs in a .NET task
host that needs it.

MSBuild takes DOTNET_HOST_PATH for a task host from each project's
properties, so the package's targets and Reactor.Uno.csproj now set it
from NetCoreRoot when the build doesn't have it. That covers Reactor.Uno
and any library that references the package; an Uno library that doesn't
reference Reactor still hits the SDK bug. Temporary, until the SDKs we
support carry dotnet/sdk#55581 (in the .NET 11 SDK, not in 10.0.4xx).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
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.

3 participants