Skip to content

[rel/4.5] Preserve discovery JSON in headless Windows runs (#11896) - #11904

Merged
Amaury Levé (Evangelink) merged 1 commit into
rel/4.5from
dev/amauryleve/rel-4-5-backport-11896
Oct 12, 2026
Merged

Amaury Levé (Evangelink) merged 1 commit into
rel/4.5from
dev/amauryleve/rel-4-5-backport-11896

Conversation

@Evangelink

Copy link
Copy Markdown
Member

Summary

Backport #11896 independently to rel/4.5 for MTP 2.5.2: preserve lossless Unicode discovery JSON in headless Windows processes, keep runner startup/process diagnostics on stderr, resolve the desktop XML fixture from the application base directory, and load required security assemblies before AppContainer impersonation.

Source commit: f355efcf9c59bc28b274fde1593f18274cad4adc.
Split head: 6c903802eb0f25eadf718fbdfa8aa0534c80788a.
Release base: 620a5e3101d34ba5d3802efab80b62b0252eda9b.

Replaces this portion of closed combined PR #11897. Exactly one release-base commit with the nine files from the original fix, so it can receive its own squash merge. No localization, AI diagnostic URL or release-note backports. The release SDK, package versions, API baselines, scheduling and disabled affected-test rollout remain unchanged.

Contract and demonstration

The maintained headless collection contract is included without main's newer SDK/rollout changes. Schema version 1 and decoded UIDs/metadata are unchanged. Escaping non-ASCII UTF-16 units prevents a legacy console encoding from injecting raw JSON control characters. Direct headless discovery returns child exit 0 with parseable JSON; server-mode discovery still streams through the SDK pipe and keeps stdout clean. Existing launch, cancellation and cleanup ownership are unchanged.

Text evidence from the release-local comparison is more useful than screenshots:

Without only the JsonStringWriter production patch, with tests retained:
  Serialize_NonAsciiCharacters_ProducesLosslessAsciiJson: failed exact JSON equality
  ListTestsJson_HeadlessProcess_PreservesUnicode: JsonReaderException, invalid raw 0x09
  Unit selection: 13 executed, 12 passed, 1 failed; exit 2
  Packed acceptance selection: 2 executed, 1 passed, 1 failed; exit 2

With the production patch:
  The same unit selection: 13 passed, no skips; exit 0
  The same packed selection: 2 passed, no skips; exit 0

Validation and evidence provenance

These local runs were executed on the combined release head 4393b893485b0af35e790874a2f1780b3f90189d in #11897, not freshly on this split head. Stable patch-ID comparison proves this split's runtime/test/support changes are unchanged; the other excluded backports only change translations, a separate AI attribute or release notes. All seven splits recombine to the exact tested tree 666599e52aafb3813e42d2d10b4af923e6efdc6b. CI will validate this exact independent head.

Windows x64; pinned SDK 12.0.100-alpha.1.26480.103; Release; MSTest 4.5.2-dev / MTP 2.5.2-dev; unchanged CodeCoverage 18.12.0.

.\build.cmd -c Release -pack -bl
# Passed: full combined build/pack, zero warnings/errors.

.\.dotnet\dotnet.exe artifacts\bin\Microsoft.Testing.Platform.UnitTests\Release\net8.0\Microsoft.Testing.Platform.UnitTests.dll --filter 'FullyQualifiedName~DiscoveredTestsJsonSerializerTests' --report-trx --report-trx-filename green-json-net8.trx --results-directory $results --no-ansi --progress off
# Passed: 13/13, exit 0. Same selector in the separately built red state: 12 passed / 1 intended failure, exit 2.

.\.dotnet\dotnet.exe artifacts\bin\Microsoft.Testing.Platform.Acceptance.IntegrationTests\Release\net11.0\Microsoft.Testing.Platform.Acceptance.IntegrationTests.dll --filter 'FullyQualifiedName~DotnetTestPipeListTestsJsonTests' --report-trx --report-trx-filename green-acceptance.trx --results-directory $results --no-ansi --progress off
# Passed: ListTestsJson_HeadlessProcess_PreservesUnicode and DotnetTestPipe_ListTestsJson_StreamsDiscoveredTestsOverPipeAndKeepsStdoutClean; 2/2, exit 0.

Here $results is a task-owned results directory, .dotnet is on PATH, and DOTNET_ROOT points there. The net11 acceptance host uses DOTNET_ROLL_FORWARD=Major; the real generated packed consumer runs on .NET 10.0.12 and uses the package's lib/net9.0 runtime asset. Each red/green state was separately packed, used a fresh generated-consumer cache, and retained the same tests. project.assets.json SHA-512 and executed DLL bytes match each state's respective package; the distinct binaries rule out stale-package evidence.

State Package SHA-256 Executed consumer DLL SHA-256
Green E76610FA164FFBD3AA296802D119B9D61ADC2D3F2F40D7178BDBD8E3E10C01EC 80DCAA3D8F59A40461FE9EB2F12D1413FD089749F4327FF7B2C36A734FF694A5
Red 996AEBA34C8956573144B0E6F61416DBDB306A73A57F6C12E990910AC963B78A 2FBF11222BE3E9D3535746BAB79FF14BFD537E0988900DD2CA3143AA3E25F729

Additional combined-state checks: JSON/AI selections passed 24/24 on net8/net9/net462; pipe authorization passed 51/51 on net8/net9 with no skipped AppContainer case; both desktop XML cases passed on net462. Both acceptance runners and VSTestBridge produced parseable --list-tests json with their diagnostics confined to stderr. Detailed commands, TRX counters and unavailable checks are retained in #11897.

git diff --check 620a5e3101d34ba5d3802efab80b62b0252eda9b 6c903802eb0f25eadf718fbdfa8aa0534c80788a passed. C# BOMs and source scope were verified. No independent-head full build/test matrix or remote CI result is claimed.

Backport #11896 from f355efc. Preserve the release SDK, package versions and disabled affected-test rollout; retain the coupled regressions and headless test-harness corrections.

Split from closed combined PR #11897 for one squash merge per source backport.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 230c0442-5e09-47eb-a28e-46dd66f7297f
@github-actions github-actions Bot added the state/needs-review Awaiting review from the team. label Oct 12, 2026

@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

This PR is a verified backport of already-merged, already-reviewed main PR #11896 ("Fix MTP affected-test collection in headless Windows runs", merged by Amaury Levé (@Evangelink)) onto rel/4.5 for the MTP 2.5.2 servicing release. I compared the two commits (1a29ef2b83a604e31278e9b4efa769f7c832cb92 on main vs. 6c903802eb0f25eadf718fbdfa8aa0534c80788a here) and confirmed the production code, test code, and documentation diffs are byte-for-byte equivalent across all 9 files. No independent review risk is introduced beyond re-validating the original fix itself, which I did below.

Correctness & design (dims 1, 8, 15, 16, 17, 21)

JsonStringWriter.WriteEscapedString / AppendUnicodeEscape (core fix) — verified correct:

  • The new else if (c >= 0x7F) { AppendUnicodeEscape(c); } branch only fires in the default: switch arm, mutually exclusive with the char.IsHighSurrogate/char.IsLowSurrogate branches above it — no double-escaping risk.
  • AppendUnicodeEscape emits \u + 4 lowercase hex digits ("x4" format), which is valid JSON and round-trips losslessly through JsonDocument.Parse for both BMP characters and surrogate pairs (confirmed via the new unit test's exact raw-string assertion and the decoded-value assertions that follow it).
  • Removing the special-cased \u2028/\u2029 branch is safe: those code points are >= 0x7F, so they now fall through to the new generic branch and still get escaped — behavior preserved, code simplified (good dimension-15 simplification).
  • Previously-unpaired-surrogate handling (\uFFFD replacement) is unchanged in logic, only now also routes through AppendUnicodeEscape instead of raw _builder.Append, which is correct since \uFFFD (U+FFFD) is itself >= 0x7F and must be escaped too.

Scope discipline: The PR bundles the JSON-escaping fix with Console.WriteLine → Console.Error.WriteLine changes (3 files), an AppContext.BaseDirectory path fix for a desktop XML data-source test, and an assembly-preload fix in NamedPipeServerSecurityTests. On first read, these look like unrelated drive-by changes, but the PR/commit description explicitly documents this as one coupled fix for multiple causes of the same Windows Release headless-collection failure (stdout must stay pure JSON; a child process launch directory differs from the repo cwd; AppContainer impersonation needs eager assembly loads). Since the original PR (#11896) was reviewed and merged on this exact multi-cause framing, and this backport makes no changes beyond mechanically reproducing that diff, I'm not asking for a split here — it's an intentional atomic regression fix, not uncontrolled scope creep.

Doc accuracy (docs/affected-test-selection.md): The new "Headless collection compatibility" section accurately describes the shipped behavior (ASCII-only escaped JSON, stderr-only diagnostics, base-directory fixture resolution, assembly preload) and correctly names the two regression tests. No discrepancies found between prose and code.

Security & protocol / IPC wire compatibility (dims 3, 19)

  • This is backward-compatible: the JSON schema and schemaVersion are unchanged; only the encoding of non-ASCII characters within string values changes (raw UTF-16 code unit → \uXXXX escape). Any conformant JSON parser decodes both forms identically, so existing consumers (including older ones reading this schema) are unaffected.
  • No new attack surface — this only affects an already-trusted, in-process JSON writer for discovery output.

Performance (dim 5)

Flagged inline on JsonStringWriter.cs: the new c >= 0x7F branch escapes every non-ASCII character to \uXXXX on all platforms and TFMs, not only on Windows headless processes where the legacy-encoding corruption actually occurs. This is a deliberate, documented tradeoff (see the code comment and docs/affected-test-selection.md) favoring universal correctness over avoiding unnecessary escaping on non-Windows hosts, and matches the already-merged main behavior — non-blocking, but worth a maintainer nod that the cost (larger discovery JSON payloads for non-ASCII test names, always, everywhere) was accepted knowingly rather than being an accidental platform-scoping gap.

Tests (dims 10–14)

  • Serialize_NonAsciiCharacters_ProducesLosslessAsciiJson is a strong regression test: exact raw-string JSON equality (per repo convention for deterministic multiline output) plus decoded round-trip assertions for both Uid and DisplayName.
  • ListTestsJson_HeadlessProcess_PreservesUnicode exercises the full headless apphost path end-to-end (not just in-memory serialization), asserts StandardError is empty (validating the stderr-diagnostics fix in the same PR), and asserts the Unicode test survives discovery — good coverage of the actual failure mode (CreateNoWindow=true + legacy console encoding).
  • DesktopTestDataSourceTests: the dataRows.Should().HaveCount(4) addition tightens a previously under-asserting test (existence vs. correctness, dimension 11) — good catch, not a regression.
  • NamedPipeServerSecurityTests assembly-preload fix is minimal, #if NET-guarded, and matches the existing pattern already used for GenericSecurityDescriptor.

Cross-TFM (dim 6)

No new TFM-gating concerns; the #if NET guard added to NamedPipeServerSecurityTests is consistent with the adjacent pre-existing guard for the AccessControl assembly preload.

Verdict

No blocking issues found. This is a faithful, verified backport of an already-reviewed and merged fix, with solid regression coverage and no wire-compatibility or security regressions. One non-blocking performance/scope question was staged inline for maintainer confirmation.

Comments that could not be inline-anchored

src/Platform/Microsoft.Testing.Platform/OutputDevice/JsonStringWriter.cs:184

Nit (Performance, non-blocking): this c &gt;= 0x7F branch escapes every non-ASCII BMP character unconditionally, on all platforms and TFMs — not just headless Windows processes. For test suites with non-ASCII display names (e.g. localized test names), this increases JSON payload size (6 bytes (uxxxx/redacted) vs 1-4 UTF-8 bytes per character) and adds a branch on every char in the hot discovery-serialization path on Linux/macOS where the legacy-encoding problem this PR targets does not exi…

@microsoft-github-policy-service microsoft-github-policy-service Bot added state/needs-review Awaiting review from the team. and removed state/needs-review Awaiting review from the team. labels Oct 12, 2026
@github-actions github-actions Bot added state/needs-review Awaiting review from the team. and removed state/needs-review Awaiting review from the team. labels Oct 12, 2026
@microsoft-github-policy-service microsoft-github-policy-service Bot added state/needs-review Awaiting review from the team. and removed state/needs-review Awaiting review from the team. labels Oct 12, 2026
@github-actions github-actions Bot added state/approved Proposal approved; ready for implementation. and removed state/needs-review Awaiting review from the team. labels Oct 12, 2026
@Evangelink
Amaury Levé (Evangelink) merged commit e981150 into rel/4.5 Oct 12, 2026
33 checks passed
@Evangelink
Amaury Levé (Evangelink) deleted the dev/amauryleve/rel-4-5-backport-11896 branch October 12, 2026 19:43
@github-actions github-actions Bot removed the state/approved Proposal approved; ready for implementation. label Oct 12, 2026
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.

2 participants