Skip to content

Write xUnit command log before running tests - #17609

Merged
mmitche merged 3 commits into
mainfrom
mmitche-xunit-log-append-mitigation
Sep 22, 2026
Merged

mmitche merged 3 commits into
mainfrom
mmitche-xunit-log-append-mitigation

Conversation

@mmitche

@mmitche mmitche commented Sep 21, 2026

Copy link
Copy Markdown
Member

Summary

Move xUnit's WriteLinesToFile immediately after output-directory creation and deletion, before Exec. Initialize the log with Overwrite="true" and append runner stdout/stderr with >> ... 2>&1. Remove the post-run write.

The command is intentionally a header instead of a footer. Retaining the MSBuild writer keeps the command as data rather than introducing another shell-parsing layer through echo. Capture-disabled console behavior, exit-code reporting, timeout, XML/HTML paths, batching, and FileWrites are unchanged. No retries or suppressed errors are added.

Motivation and evidence

In ASP.NET Core build 1597302, the failing Windows job was on stage/job attempt 2, while the Run build.cmd task log recorded task attempt 1. XUnit.Runner.targets(96,5) in Arcade SDK 11.0.0-beta.26453.108 reported MSB3491 while appending to ServerComparison.FunctionalTests_net11.0_x64.log: the file was being used by another process.

The investigation found one RunTests execution: Exec returned 0 after 24,851 ms, then WriteLinesToFile failed after 14 ms. The runner log reported 37 tests, 31 passed, 6 skipped, no failures/errors, and no command footer. Build attempt 3 succeeded with the same counts and the footer present. Another occurrence was observed in build 1597356, followed by recovery in 1597643; test success is not inferred for that second incident.

This follows the ordering/append approach used for analogous VSTest contention in #9864, #9915, and #10037, while retaining WriteLinesToFile. The conflicting handle owner is unknown. This does not establish antivirus or concurrency as the cause, or guarantee elimination of every sharing violation. It removes the post-run reopen where this failure occurred.

Validation

  • Added six tests using the actual target and Exec with a fake runner: command ordering, default/explicit/disabled capture, successful and failing exit codes, stdout and stderr preservation, quoted metacharacters and paths with spaces, XML/HTML arguments, FileWrites, and repeat execution without stale log contents.
  • All six new tests passed within the normal full SDK suite. SDK totals: 47 total, 43 passed, 1 failed, 3 skipped. The remaining failure is CentralPackageManagementTests.ImplicitPackageReferences_ShouldNotConflictWithPackageVersionEntries, reporting the implicit Microsoft.VisualStudioEng.MicroBuild.Plugins.SwixBuild reference also present in central package versions. A clean-baseline comparison was not performed.
  • Excluding that single existing test class: 46 total, 43 passed, 0 failed, 3 skipped. The runner tests use a collection with DisableParallelization=true to avoid overlapping the shared in-process MSBuild BuildManager used by other tests.
  • Temporarily restoring the old ordering/redirection in the generated test asset produced four expected regression failures; the modified asset was restored afterward.
  • Build.cmd -configuration Release -test -binaryLog compiled the repository but did not pass all tests. In addition to the SDK failure above, observed failures included Workloads MSI/Visual Studio tests (including missing VS MSBuild and a WiX compilation failure) and SignToolTests.SignBundleDoubleNested. These were not baseline-verified or changed here.
  • Executed on Windows. The tests include a Unix /bin/sh runner path, but Unix execution was not performed (no WSL distribution installed). No live CI reproduction or retries were performed.

To double check:

Initialize the captured log before Exec and append runner output, avoiding the post-run MSBuild reopen. Add coverage for ordering, captured and console output, failure status, quoting, and fresh logs on reruns.

Co-authored-by: Copilot App <[email protected]>
Copilot AI lite review requested due to automatic review settings September 21, 2026 21:19

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.

Copilot review overview

🔵 Needs a closer look

The changes require final human review because they are too complex or risky for automated approval.

Review effort: Lite
Findings: None

What changed in this PR

Moves xUnit command logging before test execution to avoid reopening the output log afterward, while preserving output capture and exit-code behavior.

Changes:

  • Appends runner output and initializes the log with an overwrite-enabled command header.
  • Adds integration-style tests covering ordering, capture modes, exit codes, paths, arguments, and stale logs.
File Description
src/​Microsoft.DotNet.Arcade.Sdk/​tools/​XUnit/​XUnit.Runner.targets Updated as part of this pull request.
src/​Microsoft.DotNet.Arcade.Sdk.Tests/​XUnitRunnerTests.cs Updated as part of this pull request.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@mmitche
mmitche marked this pull request as ready for review September 21, 2026 22:50
@mmitche
mmitche requested a review from premun September 21, 2026 22:50
premun
premun previously approved these changes Sep 22, 2026
@premun

premun commented Sep 22, 2026

Copy link
Copy Markdown
Member

@copilot all the newly added tests seem to be failing on Windows

Copilot AI review requested due to automatic review settings September 22, 2026 07:03

Copilot AI commented Sep 22, 2026

Copy link
Copy Markdown
Contributor

@copilot all the newly added tests seem to be failing on Windows

Fixed in e7720fe by invoking the fake Windows batch runner through cmd /d /c call instead of using the batch file itself as MonoTool. The targeted XUnitRunnerTests pass locally on Linux; Code Review/CodeQL validation also passed.

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.

Copilot review overview

🔵 Needs a closer look

Validation was Windows-only and lacked a clean baseline, so automated approval is not sufficiently safe.

Review effort: Lite
Findings: None

Windows-built test assemblies retain CRLF in raw string literals even when executed on Unix. Normalize the generated shell script to LF so stderr redirection is parsed correctly in cross-platform Helix runs.

Co-authored-by: Copilot App <[email protected]>
Copilot AI review requested due to automatic review settings September 22, 2026 14:33

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.

Copilot review overview

🔵 Needs a closer look

Validation was not fully conclusive, so final human review is warranted.

Review effort: Lite
Findings: None

@mmitche

mmitche commented Sep 22, 2026 •

Copy link
Copy Markdown
Member Author

Fixed the cross-platform test-script issue in 953fb8d. The failing legs are Windows-built assemblies running on Linux/macOS, not Windows execution. All five execution cases were missing runner stderr; the same failures existed before the cmd /d /c call change.

The C# raw string retains the build checkout's CRLF. The Unix script now uses .ReplaceLineEndings("\n") before writing. The production target and Windows runner are unchanged.

Validation:

  • POSIX dash reproduces the CRLF problem (Bad fd number at >&2); LF succeeds. This uses dash shipped with Git for Windows, not a native Linux environment.
  • Extracted the Unix script literal from the Windows-built test assembly, confirmed embedded CRLF, and executed the LF-normalized script under dash: stdout, stderr, exit codes 0/7, paths with spaces, and quoted metacharacters pass.
  • All six runner tests pass in the normal SDK suite (47 total, 43 passed, 1 existing package-conflict failure, 3 skipped).
  • Full Release build/test completed; remaining local failures are the previously observed central-package conflict, Workloads tests, and SignTool test. Those are not changed here.
  • CI build 1607283 passed on commit 953fb8d. All eight Helix jobs succeeded: 234 work items, zero failures, zero resubmissions. This includes all three previously failing Windows-built-assembly legs on Linux/macOS, as well as Windows execution. All PR checks are green.

@mmitche
mmitche merged commit b24c4bc into main Sep 22, 2026
13 checks passed
@mmitche
mmitche deleted the mmitche-xunit-log-append-mitigation branch September 22, 2026 19:21
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 12.0-preview1 milestone Sep 23, 2026
@mmitche

mmitche commented Oct 8, 2026

Copy link
Copy Markdown
Member Author

/backport to release/11.0

@github-actions

github-actions Bot commented Oct 8, 2026

Copy link
Copy Markdown
Contributor

Started backporting to release/11.0 (link to workflow run)

@mmitche

mmitche commented Oct 8, 2026

Copy link
Copy Markdown
Member Author

Also seen in 11.0 in aspnetcore.

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.

4 participants