Skip to content

Inject runsettings environment variables on the MTP execution path - #16283

Merged
Azat Mukhametshin (azat-msft) merged 3 commits into
microsoft:mainfrom
azat-msft:fix/mtp-runsettings-envvars
Jul 14, 2026
Merged

Inject runsettings environment variables on the MTP execution path#16283
Azat Mukhametshin (azat-msft) merged 3 commits into
microsoft:mainfrom
azat-msft:fix/mtp-runsettings-envvars

Conversation

@azat-msft

Copy link
Copy Markdown
Member

Problem

On the vstest.console path that runs a Microsoft.Testing.Platform (MTP) app as its own testhost (#16201), environment variables declared in a .runsettings RunConfiguration/EnvironmentVariables block were not passed to the MTP test process. Inherited process env vars reached the app, but the documented runsettings-driven injection was silently dropped.

Root cause

On the classic path ProxyOperationManager reads the runsettings env vars and merges them into the testhost ProcessStartInfo. The MTP application is its own host, so that code is bypassed and MtpProxyExecutionManager only ever populated the launch environment with datacollector-provided profiler variables.

Fix

MtpProxyExecutionManager now reads the runsettings env vars via InferRunSettingsHelper.GetEnvironmentVariables and merges them into the launch environment before BeforeTestRun, so datacollector profiler variables still win on collision (matching the classic ordering).

Test

New acceptance test RunMtpApplicationInjectsRunSettingsEnvironmentVariables runs the MTP app with a runsettings file that injects MTP_FROM_RUNSETTINGS and a guarded RunSettingsEnvironmentVariableIsInjected asset test (opted in via a CHECK_RUNSETTINGS_VAR process env var inherited by the host). The guarded test passes only when the runsettings value actually reaches the host, so a broken injection turns the 3/1/1 summary into 2/2/1. Existing MTP summary counts are updated for the added asset test.

Validation

  • RunMtpApplicationInjectsRunSettingsEnvironmentVariables and the count-updated RunMtpApplicationExecutesTestsOverMtpProtocol: 4/4 matrix cases pass.
  • Release build of Microsoft.TestPlatform.CrossPlatEngine and the acceptance project: clean.
  • Verified end-to-end against a real TUnit MTP app (wrong-value runsettings makes the guarded assert fail, proving the value was injected).

On the MTP-as-testhost path the MTP application is its own host, so the classic
testhost code that applies runsettings RunConfiguration/EnvironmentVariables was
bypassed and those variables never reached the test process (only inherited
process env vars did). MtpProxyExecutionManager now reads them via
InferRunSettingsHelper.GetEnvironmentVariables and merges them into the launch
environment before BeforeTestRun, so datacollector-provided profiler variables
still win on collision (matching the classic ordering).

Adds acceptance test RunMtpApplicationInjectsRunSettingsEnvironmentVariables and a
guarded RunSettingsEnvironmentVariableIsInjected test in the MTP asset; existing
MTP summary counts updated for the added asset test.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: fb7de7a6-b8a1-4ebb-8266-5c5a948f245c
Copilot AI review requested due to automatic review settings July 14, 2026 10:30

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 fixes a gap in the Microsoft.Testing.Platform (MTP) execution path where .runsettings RunConfiguration/EnvironmentVariables were not being applied to the self-hosted MTP process launched by vstest.console, aligning behavior with the classic testhost-based path.

Changes:

  • Inject runsettings environment variables into the MTP application launch via InferRunSettingsHelper.GetEnvironmentVariables, before datacollector initialization so profiler vars still take precedence on collisions.
  • Add an acceptance test that validates runsettings env var injection end-to-end using a guarded asset test.
  • Update existing MTP acceptance test expected summary counts to reflect the added asset test.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
test/TestAssets/MtpMSTestProject/UnitTests.cs Adds a guarded test that asserts the runsettings-injected env var is visible inside the MTP process.
test/Microsoft.TestPlatform.Acceptance.IntegrationTests/MtpUnderVstestTests.cs Adds acceptance coverage for runsettings env var injection and updates expected result counts.
src/Microsoft.TestPlatform.CrossPlatEngine/Client/MTP/MtpProxyExecutionManager.cs Implements runsettings env var injection for the MTP launch path prior to datacollector env var merging.

Comment thread src/Microsoft.TestPlatform.CrossPlatEngine/Client/MTP/MtpProxyExecutionManager.cs Outdated
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings July 14, 2026 10:49

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

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

Comment thread src/Microsoft.TestPlatform.CrossPlatEngine/Client/MTP/MtpProxyExecutionManager.cs Outdated
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings July 14, 2026 10:58

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

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

@azat-msft
Azat Mukhametshin (azat-msft) merged commit 4294cde into microsoft:main Jul 14, 2026
20 checks passed
Azat Mukhametshin (azat-msft) added a commit to azat-msft/vstest that referenced this pull request Jul 14, 2026
- RunMtpApplicationSurfacesPerTestStandardOutput asserted the old 4-test
  summary (2,1,1); the shared MtpMSTestProject asset gained a 5th test
  (RunSettingsEnvironmentVariableIsInjected), so the run is (3,1,1).
  This is a semantic collision between microsoft#16283 and microsoft#16284 that only
  surfaces when both are on main. Update the expected summary.
- Address review feedback: extract the near-identical MTP discovery
  routine from MtpProxyDiscoveryManager and MtpProxyExecutionManager into
  a single MtpClientHelpers.DiscoverSourceTests helper so the two paths
  cannot drift (timeouts, cancellation, sentinel drain, env-var policy).

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 61d9a1c3-3c9f-4dab-8ba3-5baba8d55ed6
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