Repository navigation
Description
Summary
InvokeTestingPlatformTask derives from ToolTask, so it already inherits a fully functional EnvironmentVariables parameter. However, the InvokeTestingPlatform target never plumbs it through, so there is no supported way to set environment variables for the test process from MSBuild.
I'd like to propose exposing it via a TestingPlatformEnvironmentVariables property.
Why this matters: repo-local .NET SDKs
This is not hypothetical — it silently changes which runtime your tests execute on.
Many repos (all of the .NET product repos, and anything Arcade-based) install a private SDK into a repo-local folder such as .dotnet/ and build against it, without exporting DOTNET_ROOT for the whole build session. That works for the build because everything goes through the dotnet muxer that was invoked by path.
It does not work for the test run. Test projects using xunit.v3 must build an app host (xunit.v3.core.mtp-v2.props forces UseAppHost=true, and its targets hard-error otherwise), and MTP launches that .exe directly rather than via the muxer. An app host ignores PATH — it resolves a runtime through DOTNET_ROOT, then falls back to the machine-wide install.
The consequences:
- On a machine with a different .NET installed, tests silently run on the wrong runtime. In our repo the local SDK is
11.0.0-preview.6while the machine has11.0.0-preview.7installed, and the tests ran on preview.7 without any warning — verified viaTESTINGPLATFORM_DIAGNOSTIC:no DOTNET_ROOT -> Runtime information: win-x64 - .NET 11.0.0-preview.7.26377.110 # machine-wide with DOTNET_ROOT -> Runtime information: win-x64 - .NET 11.0.0-preview.6.26359.118 # repo-local - On a clean CI machine with no .NET installed at all, the app host finds no runtime and the test run fails outright.
For reference, this is exactly why Arcade's own xunit runner target sets it explicitly when it invokes the runner (XUnitV3.Runner.targets):
EnvironmentVariables="DOTNET_ROOT=$(TestDotNetRoot);DOTNET_ROOT_X86=$(TestDotNetRoot)x86"Repos that use MTP's own Test/InvokeTestingPlatform target instead of Arcade's runner lose that, and there is currently no supported replacement.
DotnetHostPath does not help here: it only selects the muxer used when there is no app host. With UseAppHost=true it is ignored (verified), and xunit.v3 does not allow turning the app host off.
Proposed fix
The task side already works — nothing to change there. Only the target needs a passthrough:
<PropertyGroup>
<TestingPlatformShowTestsFailure Condition=" '$(TestingPlatformShowTestsFailure)' == '' ">false</TestingPlatformShowTestsFailure>
<TestingPlatformCaptureOutput Condition=" '$(TestingPlatformCaptureOutput)' == '' " >true</TestingPlatformCaptureOutput>
</PropertyGroup>
<Target Name="InvokeTestingPlatform" >
<CallTarget Targets="_TestRunStart" />
<InvokeTestingPlatformTask TargetPath="$(TargetPath)"
...
TestingPlatformCaptureOutput="$(TestingPlatformCaptureOutput)"
+ EnvironmentVariables="$(TestingPlatformEnvironmentVariables)"
DotnetHostPath="$(DOTNET_HOST_PATH)" />
</Target>Consumers could then write:
<PropertyGroup>
<TestingPlatformEnvironmentVariables>DOTNET_ROOT=$(DotNetRoot)</TestingPlatformEnvironmentVariables>
</PropertyGroup>ToolTask.EnvironmentVariables is string[], so an MSBuild property containing A=1;B=2 binds naturally, and an item-based @(TestingPlatformEnvironmentVariable) form would work equally well if that fits the existing conventions better. Since the parameter is unset by default, the change is fully backwards compatible (ToolTask keeps inheriting the parent process environment).
I'm happy to send a PR if you're on board with the shape.
Verification
I confirmed the task side already works end-to-end by calling it directly with the inherited parameter:
<InvokeTestingPlatformTask TargetPath="$(TargetPath)"
...
EnvironmentVariables="DOTNET_ROOT=$(DotNetRoot.TrimEnd('\/'))" />MSBuild accepted the parameter (no MSB4064) and the test host switched from the machine-wide runtime to the repo-local one. So this really is only about the target not exposing it.
Current workaround
For anyone hitting this before it's fixed: ToolTask passes the MSBuild process environment on to the child, so the variable can be set on the MSBuild process itself. System.Environment::SetEnvironmentVariable is blocked as an MSBuild property function (MSB4185), so it needs a small inline task:
<UsingTask TaskName="SetEnvironmentVariable"
TaskFactory="RoslynCodeTaskFactory"
AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.Core.dll">
<ParameterGroup>
<Name ParameterType="System.String" Required="true" />
<Value ParameterType="System.String" Required="true" />
</ParameterGroup>
<Task>
<Code Type="Fragment" Language="cs">System.Environment.SetEnvironmentVariable(Name, Value);</Code>
</Task>
</UsingTask>
<Target Name="SetDotNetRootForTestRun" BeforeTargets="InvokeTestingPlatform">
<SetEnvironmentVariable Name="DOTNET_ROOT" Value="$(DotNetRoot.TrimEnd('\/'))" />
</Target>That works, but it reaches around the public surface of the targets and is the kind of thing that ideally shouldn't be necessary.
Environment
Microsoft.Testing.Platform/Microsoft.Testing.Platform.MSBuild2.2.3 (behaviour is the same onmain)xunit.v34.0.0-pre.81 (MTP v2 runner)- .NET SDK 11.0.100-preview.6, Windows x64
Activity
Migrate VMR test projects to xunit.v3 and Microsoft.Testing.Platform
Youssef1313 commentedon Jul 30, 2026
This task is rarely used. I'm curious how you're running the tests?
Youssef1313 commentedon Jul 30, 2026
You linked XUnitV3.Runner.targets from Arcade, but the
Execcall there should be done using the apphost. So there should be no interaction with InvokeTestingPlatform target in this scenario at all?ViktorHofer commentedon Jul 30, 2026
See dotnet/dotnet#8060. Is there a better way to use xunit + MTP? I'm explicitly not using the Arcade Test infrastructure. Both my local tests (using dotnet test or invoking the /t:Test target) invoked that target.
Youssef1313 commentedon Jul 30, 2026
dotnet testfor MTP isn't supposed to use this target. The target is only called by the VSTest-baseddotnet test(which predates the MTP-baseddotnet test).Did you opt-in to the MTP-based
dotnet testviaglobal.json? This one doesn't rely on this target at all.Youssef1313 commentedon Jul 30, 2026
Also note that running MTP using the VSTest-based
dotnet testis no longer supported in MTP v2.Youssef1313 commentedon Jul 30, 2026
Looking at that PR, if you run
dotnet testfrom a working directory which is undersrc, the emptyglobal.jsonwill be picked, and it will default to using the VSTest-baseddotnet test.ViktorHofer commentedon Jul 30, 2026
The empty global.json under src is intentional. We don't enable MTP anywhere there. That's the business of the individual inner repo. Copilot is telling me the following about one of your statements:
So the
InvokeTestingPlatformtarget is still invoked when you invoke the/t:Testtarget, without any VSTest in the play BUT you were right that in thedotnet testexample it doesn't get invoked. Sorry for my confusion there.Youssef1313 commentedon Jul 30, 2026
Yup,
/t:Teststill uses InvokeTestingPlatform target. That was the only reason we couldn't delete the target altogether actually.But then this doesn't affect
XUnitV3.Runner.targetsright?It should be fine to introduce a new
TestingPlatformEnvironmentVariables, I think. I just wanted to make sure the exact scenario and use case is well understood.ViktorHofer commentedon Jul 30, 2026
We don't use Arcade's testing infrastructure. It's completely disabled. With MTP there is virtually no reason to still rely on it. It's more harmful than beneficial.
OK. Keep supporting an MSBuild entry point is very important IMO. Most of the time customers are already inside an msbuild environment when they want to execute tests.
OK will send a PR, the addition is really much just a one-liner.
Are there any behavioral differences between the MSBuild and the dotnet test entrypoints?
Youssef1313 commentedon Jul 30, 2026
The user experience is different (what we print, etc). Some options are considered "global" options when using
dotnet teston a solution (because the command knows about the whole solution) and that can't be achieved with/t:Testwhich runs on each project in isolation, etc. Nothing should hopefully be "surprising" though 😄Sounds good to me if Amaury Levé (@Evangelink) doesn't have additional concerns :)
Evangelink commentedon Jul 30, 2026
Thanks for the very thorough write-up Viktor Hofer (@ViktorHofer), and for pushing through the back-and-forth on the entry points. I'm on board with the passthrough. Two things on top of the diff, one small and one that I think is actually the more interesting half of this issue.
1. On the exact shape of the diff
ToolTask.EnvironmentVariablesisstring[], so binding$(TestingPlatformEnvironmentVariables)means MSBuild splits the property on;. That's fine forDOTNET_ROOT=..., but it silently corrupts any value that contains a semicolon:binds as
["MY_PATHS=/a", "/b"], and the second entry is malformed. It's escapable (/a%3B/b), but that's an obscure footgun to bake into a brand-new public property.You already floated the item form in the issue, and I'd lean that way as the primary surface:
Item metadata isn't subject to the
;split, so values are safe as authored. Implementation-wise this stays cheap — take anITaskItem[]parameter and project it into the inheritedEnvironmentVariablesat the top ofExecute()beforebase.Execute()runs. (Worth notingToolTask.EnvironmentOverrideis not the right hook here — it's[Obsolete]precisely because aDictionarycan't be set from a project file.)Happy to also keep
TestingPlatformEnvironmentVariablesas sugar for the simpleNAME=VALUEcase if you'd rather have both; I just wouldn't make thestring[]property the only way in.2. The
DOTNET_ROOThalf probably shouldn't need configuration at allRe-reading your repro, I think the escape hatch and the bug you hit are two separate things, and we're at risk of shipping only the former.
The reason
dotnet testresolved your repo-local preview.6 and/t:Testdidn't isn't that one of them reads a property you haven't set. It's that the SDK explicitly setsDOTNET_ROOT_<ARCH>for you before launching the apphost and we don't. SeeTestApplication.CreateProcessStartInfoin dotnet/sdk:with
EnvironmentVariableNames.TryGetDotNetRootArchVariableNamereturningnullwhen the target architecture differs from the SDK's, so it only fires when it's safe to.InvokeTestingPlatformTaskhas everything needed to do the same. On the apphost path,TryGetRunCommand()returns the.exeandGenerateFullPathToToolreturns early — soDotnetHostPathis never consulted at all, which is exactly the "DotnetHostPathdoes not help here" behaviour you observed. We already have$(DOTNET_HOST_PATH)flowing into the task and anIsCurrentProcessArchitectureCompatible()check that mirrors the SDK's arch guard.There's precedent one layer further down too: MSBuild does this for its own apphost-launched child processes in
DotnetHostEnvironmentHelper.CreateDotnetRootEnvironmentOverrides, derivingDOTNET_ROOTfromDOTNET_HOST_PATHand nulling out the arch-specific variants, since those take precedence over plainDOTNET_ROOT. Notably it also hasClearBootstrapDotnetRootEnvironment, whose comment says the bootstrap value "should not leak to tools those processes execute" — i.e. we can't count on inheriting a usableDOTNET_ROOTfrom the MSBuild process even when MSBuild itself had one.If we do this, your original scenario works with no configuration on a clean CI machine, and the "same repo, two entry points, different runtime" divergence goes away. The env-var passthrough then stops being the fix for a runtime-resolution bug and becomes what it should be: a general escape hatch for everything else. It's a behaviour change, so it'd want an opt-out property and a note in the changelog.
FWIW this repo works around the same gap today — we pin
DOTNET_ROOTineng/pipelines/variables/test-env-vars.yml, and the/t:Testacceptance test sets it by hand.3. > Are there any behavioral differences between the MSBuild and the dotnet test entrypoints?
Yes, and this is one of them. Off the top of my head,
/t:Test→InvokeTestingPlatform:ToolTaskper project/TFM, coordinating over a named pipe, with results written per-TFM underTestResults; there's no cross-project aggregation or summary$(TestingPlatformCommandLineArguments), not CLI parsinglaunchSettings.json, and has no equivalent of--environmentDOTNET_ROOT*(this issue)DispatchToInnerBuildsWithMTPTestTarget, gated on$(TestTfmsInParallel)/$(BuildInParallel)MTP
dotnet testorchestrates in the CLI instead: it aggregates across projects, owns the terminal output, honourslaunchSettings.jsonand--environment, and sets theDOTNET_ROOTarch variable as above.Agreed with your point that keeping an MSBuild entry point matters — most people are already inside an MSBuild environment when they want to run tests. Which is why I'd rather close the resolution gap properly than leave it as something every repo has to know to configure.
If you're up for it, I'd suggest two PRs: the passthrough (yours, small), and the
DOTNET_ROOTfix separately since it needs its own tests and an opt-out. Happy to take the second one if you'd prefer.Allow passing environment variables to the test process from InvokeTe…
ViktorHofer commentedon Aug 3, 2026
Heads-up: this auto-closed as "completed" when #10405 merged (my commit said
Fixes #10342), but only the first half shipped — theTestingPlatformEnvironmentVariablepassthrough.Amaury Levé (@Evangelink)'s second point, having the task set
DOTNET_ROOT_<ARCH>for the apphost itself so the common case needs no configuration, is still open. I've split it out into #10408 so it isn't lost, with your analysis carried over. Leaving it unassigned since you offered to take it in this comment — happy either way.