Skip to content

Allow passing environment variables to the test process from the InvokeTestingPlatform target - #10405

Merged
Viktor Hofer (ViktorHofer) merged 2 commits into
mainfrom
dev/vihofer/fix-issue-10342
Aug 3, 2026
Merged

Viktor Hofer (ViktorHofer) merged 2 commits into
mainfrom
dev/vihofer/fix-issue-10342

Conversation

@ViktorHofer

@ViktorHofer Viktor Hofer (ViktorHofer) commented Aug 3, 2026 •

Copy link
Copy Markdown
Member

Fixes #10342

Summary

InvokeTestingPlatformTask derives from ToolTask and therefore already inherits a functional EnvironmentVariables parameter, but the InvokeTestingPlatform target never plumbed anything into it — so there was no supported way to set an environment variable for the test process from MSBuild.

This matters because MTP launches the apphost directly and an apphost ignores PATH: it resolves a runtime through DOTNET_ROOT, then falls back to the machine-wide install. Repos that build against a repo-local SDK (.dotnet/) and run tests through /t:Test therefore silently run on the wrong runtime, or fail outright on a machine with no .NET installed.

Shape

<ItemGroup>
  <TestingPlatformEnvironmentVariable Include="DOTNET_ROOT" Value="$(DotNetRoot)" />
</ItemGroup>

An item, not a property: item metadata is not subject to MSBuild's ; splitting, so a value containing a ; reaches the test process exactly as authored. (A string[] property would bind MY_PATHS=/a;/b as ["MY_PATHS=/a", "/b"].)

No task code is needed. The item is transformed into the NAME=VALUE shape the inherited ToolTask.EnvironmentVariables parameter expects, directly in the target:

EnvironmentVariables="@(TestingPlatformEnvironmentVariable->'%(Identity)=%(Value)')"

Verified that the transform is not re-split on ;: it produces exactly one item per declaration, with MY_PATHS=/a;/b intact.

ToolTask applies these on top of the environment inherited from the MSBuild process, and declaring no item leaves that environment untouched — so the change is fully backwards compatible.

Scope

This is only the passthrough half of #10342. The separate proposal to have the task set DOTNET_ROOT_<ARCH> itself (mirroring what the SDK does for dotnet test) is intentionally not included here — it is a behavior change that needs its own opt-out and tests.

Testing

New acceptance test InvokeTestingPlatform_Target_Should_Pass_EnvironmentVariables_To_TestProcess runs build -t:Test with two declared variables and asserts on what the test process actually observed, including a value containing a ;.

Verified end-to-end locally against the packed Microsoft.Testing.Platform.MSBuild package:

[env] MTP_ENV_SIMPLE=simple-value
[env] MTP_ENV_WITH_SEMICOLON=first;second
Tests succeeded

and, with no item declared, the ambient value still flows through by inheritance and the build stays warning-free:

[env] MTP_ENV_SIMPLE=inherited-value
[env] MTP_ENV_WITH_SEMICOLON=
Tests succeeded
    0 Warning(s)

Note: a full -pack could not run on this machine (the uap10.0.16299 projects require desktop MSBuild), so the acceptance test itself is validated in CI.

Copilot AI review requested due to automatic review settings August 3, 2026 12:48

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

Adds MSBuild support for passing environment variables to MTP test processes.

Changes:

  • Adds item- and property-based environment-variable configuration.
  • Projects configured values onto ToolTask.EnvironmentVariables.
  • Adds unit, acceptance, and changelog coverage.
Show a summary per file
File Description
InvokeTestingPlatformTaskTests.cs Tests environment-variable projection and edge cases.
MSBuildTests.Test.cs Verifies variables reach the test process.
InvokeTestingPlatformTask.Execution.cs Converts and applies environment-variable items.
InvokeTestingPlatformTask.cs Adds the task input parameter and metadata definition.
Microsoft.Testing.Platform.MSBuild.targets Exposes item and property configuration to MSBuild users.
Changelog-Platform.md Documents the new capability.

Review details

  • Files reviewed: 6/6 changed files
  • Comments generated: 0
  • Review effort level: Balanced

@github-actions github-actions Bot 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.

Note

🤖 Automated review by GitHub Copilot. Generated by the Expert Code Review workflow. To request a follow-up action, reply by tagging @copilot directly.

Summary

Clean, well-documented change with good test coverage. One blocking typo prevents the property-based sugar from working at all.

Critical Issue

Typo in .targets file — Include="$(TestingPlatformEnvironmentVariable)" (singular) should be Include="$(TestingPlatformEnvironmentVariables)" (plural). The condition checks the plural property but the Include reads a different (empty) symbol, so the property sugar path is dead code. See inline comment.

Dimension Verdict

# Dimension Verdict
1 Algorithmic Correctness ❌ Typo breaks property→item folding
2 Threading & Concurrency ✅ N/A
3 Security & IPC ✅ OK
4 Public API & Binary Compat ✅ New property, additive only
5 Performance ✅ Negligible allocations at task startup
6 Cross-TFM ✅ IndexOf(char) / Substring are available everywhere
7 Resource / IDisposable ✅ N/A
8 Defensive Coding ✅ Null/empty guards present
9 Localization ✅ N/A (no user-facing strings)
10 Test Isolation ✅ Unit tests use in-memory items
11 Test Quality ✅ Good edge-case coverage (semicolons, whitespace, empty)
12 Error Handling ✅ Graceful null return for blank names
13 Naming ✅ Consistent with existing conventions
14 Code Style ✅ Follows repo conventions
15 Documentation ✅ XML docs and inline comments clear
16 Changelog ✅ Entry added
17 MSBuild Targets ❌ Singular/plural typo (see above)
18 Test Asset Coverage ✅ Both item and property paths exercised
19 Backward Compat ✅ Null EnvironmentVariables preserved when nothing set
20 IPC Contract ✅ N/A
21 Memory Safety ✅ N/A
22 Platform-Specific ⚠️ Unit test note about backslash normalization is good

Please fix the singular→plural typo and the integration test should then pass the property path end-to-end.

@github-actions

This comment has been minimized.

@github-actions github-actions Bot 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.

🤖 Automated content by GitHub Copilot. Generated by the Grade Tests on PR (on open / sync) workflow. · sonnet46 66.4 AIC · ⌖ 4.7 AIC · ⊞ 11.8K · ◷

…stingPlatform

`InvokeTestingPlatformTask` derives from `ToolTask` and therefore already inherits a
functional `EnvironmentVariables` parameter, but the `InvokeTestingPlatform` target
never plumbed anything into it. There was no supported way to set an environment
variable for the test process from MSBuild, which matters because MTP launches the
apphost directly and an apphost ignores `PATH`: repos using a repo-local SDK had no
way to point the test run at it.

Bind the inherited parameter from a new `TestingPlatformEnvironmentVariable` item:

    <TestingPlatformEnvironmentVariable Include="DOTNET_ROOT" Value="$(DotNetRoot)" />

An item rather than a property, because item metadata is not subject to MSBuild's
`;` splitting, so a value containing a `;` reaches the test process exactly as
authored. `ToolTask` applies the variables on top of the environment inherited from
the MSBuild process, and declaring no item leaves that environment untouched, so the
change is fully backwards compatible.

No task code is needed: the item is transformed into the `NAME=VALUE` shape
`ToolTask.EnvironmentVariables` expects directly in the target.

Fixes #10342

Co-authored-by: Copilot <[email protected]>
Copilot-Session: 3369b9ec-a09b-4371-882d-7a2e67ebbf17
Copilot AI review requested due to automatic review settings August 3, 2026 13:47

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.

Review details

  • Files reviewed: 3/3 changed files
  • Comments generated: 0 new
  • Review effort level: Balanced

@microsoft-github-policy-service microsoft-github-policy-service Bot added needs/attention Needs maintainer attention. and removed needs/author-feedback Waiting on the original author. needs/attention Needs maintainer attention. labels Aug 3, 2026
@github-actions

This comment has been minimized.

@github-actions github-actions Bot 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.

🤖 Automated content by GitHub Copilot. Generated by the Grade Tests on PR (on open / sync) workflow. · sonnet46 82.3 AIC · ⌖ 4.19 AIC · ⊞ 11.8K · ◷

@Evangelink Amaury Levé (Evangelink) left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I reviewed the current simplified implementation at 4fe932d. The item transform preserves semicolon-containing values and binds directly to ToolTask.EnvironmentVariables; inherited-environment behavior, cross-platform behavior, and backward compatibility look sound. The acceptance test validates values in the actual test process, and the changelog matches the supported contract. Approving with one non-blocking suggestion to add inheritance and override coverage.

…ronment

The acceptance test only proved that declared TestingPlatformEnvironmentVariable
items reach the test process. It did not pin the load-bearing half of the feature:
that ToolTask layers EnvironmentVariables on top of the environment inherited from
MSBuild rather than replacing it. Under replace semantics, declaring a single
variable would strip PATH / DOTNET_ROOT / ... from every test process.

Set a variable on the MSBuild process that is deliberately not declared as an item
and assert it still reaches the test process alongside the declared values.

Co-authored-by: Copilot <[email protected]>
Copilot-Session: 3369b9ec-a09b-4371-882d-7a2e67ebbf17
Copilot AI review requested due to automatic review settings August 3, 2026 14:06

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.

Review details

  • Files reviewed: 3/3 changed files
  • Comments generated: 0 new
  • Review effort level: Balanced

@github-actions

github-actions Bot commented Aug 3, 2026

Copy link
Copy Markdown
Contributor

🧪 Test quality grade — PR #10405

GradeTestMutationNotesHow to improve
A (90–100) new MSBuildTests.
InvokeTestingPlatform_
Target_
Should_
Pass_
EnvironmentVariables_
To_
TestProcess
3/3 killed Clear AAA; three distinct assertions cover declared value, semicolon metadata, and inherited-env passthrough. —

This advisory comment was generated automatically. Grades are heuristic
and informational — they do not block merging. Re-run with
/grade-tests.

🤖 Automated content by GitHub Copilot. Generated by the Grade Tests on PR (on open / sync) workflow. · sonnet46 54 AIC · ⌖ 6.91 AIC · ⊞ 11.8K · [◷]( · ◷)

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.

Allow passing environment variables to the test process from the InvokeTestingPlatform target

3 participants