Repository navigation
[rel/4.5] Preserve discovery JSON in headless Windows runs (#11896) - #11904
Conversation
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
There was a problem hiding this comment.
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 thedefault:switch arm, mutually exclusive with thechar.IsHighSurrogate/char.IsLowSurrogatebranches above it — no double-escaping risk. AppendUnicodeEscapeemits\u+ 4 lowercase hex digits ("x4"format), which is valid JSON and round-trips losslessly throughJsonDocument.Parsefor 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/\u2029branch 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 (
\uFFFDreplacement) is unchanged in logic, only now also routes throughAppendUnicodeEscapeinstead of raw_builder.Append, which is correct since\uFFFD(U+FFFD) is itself>= 0x7Fand 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
schemaVersionare unchanged; only the encoding of non-ASCII characters within string values changes (raw UTF-16 code unit →\uXXXXescape). 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_ProducesLosslessAsciiJsonis a strong regression test: exact raw-string JSON equality (per repo convention for deterministic multiline output) plus decoded round-trip assertions for bothUidandDisplayName.ListTestsJson_HeadlessProcess_PreservesUnicodeexercises the full headless apphost path end-to-end (not just in-memory serialization), assertsStandardErroris 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: thedataRows.Should().HaveCount(4)addition tightens a previously under-asserting test (existence vs. correctness, dimension 11) — good catch, not a regression.NamedPipeServerSecurityTestsassembly-preload fix is minimal,#if NET-guarded, and matches the existing pattern already used forGenericSecurityDescriptor.
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 >= 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…
Summary
Backport #11896 independently to
rel/4.5for 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:
Validation and evidence provenance
These local runs were executed on the combined release head
4393b893485b0af35e790874a2f1780b3f90189din #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 tree666599e52aafb3813e42d2d10b4af923e6efdc6b. 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.
Here
$resultsis a task-owned results directory,.dotnetis on PATH, andDOTNET_ROOTpoints there. The net11 acceptance host usesDOTNET_ROLL_FORWARD=Major; the real generated packed consumer runs on .NET 10.0.12 and uses the package'slib/net9.0runtime asset. Each red/green state was separately packed, used a fresh generated-consumer cache, and retained the same tests.project.assets.jsonSHA-512 and executed DLL bytes match each state's respective package; the distinct binaries rule out stale-package evidence.E76610FA164FFBD3AA296802D119B9D61ADC2D3F2F40D7178BDBD8E3E10C01EC80DCAA3D8F59A40461FE9EB2F12D1413FD089749F4327FF7B2C36A734FF694A5996AEBA34C8956573144B0E6F61416DBDB306A73A57F6C12E990910AC963B78A2FBF11222BE3E9D3535746BAB79FF14BFD537E0988900DD2CA3143AA3E25F729Additional 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 jsonwith their diagnostics confined to stderr. Detailed commands, TRX counters and unavailable checks are retained in #11897.git diff --check 620a5e3101d34ba5d3802efab80b62b0252eda9b 6c903802eb0f25eadf718fbdfa8aa0534c80788apassed. C# BOMs and source scope were verified. No independent-head full build/test matrix or remote CI result is claimed.