Skip to content

Fix test repository root detection in git worktrees - #7803

Open
svick wants to merge 1 commit into
dotnet:mainfrom
svick:fix/get-repo-root-worktrees
Open

svick wants to merge 1 commit into
dotnet:mainfrom
svick:fix/get-repo-root-worktrees

Conversation

@svick

@svick svick commented Oct 14, 2026 •

Copy link
Copy Markdown
Member

Problem

When tests run from a Git worktree and ML_TEST_DATADIR is not set, GetRepoRoot() walks up from the test output directory looking for a .git directory. Git worktrees instead have a .git file, so the method skips the worktree root and continues walking. If no ancestor has a .git directory, it reaches the filesystem root, where Directory.GetParent(directory) returns null, and accessing .FullName throws a NullReferenceException.

For tests using BaseTestClass, this happens during its static initialization, before the test body runs. The exception is surfaced as a TypeInitializationException, causing those tests to fail during initialization rather than execute. If an ancestor happens to contain a .git directory, the method instead selects that unrelated repository root and uses the wrong test-data paths. Normal checkouts with a .git directory and runs using the ML_TEST_DATADIR override are unaffected.

Summary

  • Recognize both .git directories and worktree .git files when locating the repository root.
  • Safely return null when the filesystem root is reached without finding a repository.
  • Preserve the ML_TEST_DATADIR override.

Validation

  • Exercised the compiled GetRepoRoot method against the actual worktree, synthetic regular and worktree repositories, a missing repository marker, and the environment-variable override.

Recognize .git files as well as directories and safely stop walking at the filesystem root.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 2ffe5c7a-b92e-45d2-8906-1bfa3f2d99c8

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.

🟢 Approval recommended

The focused diff correctly handles both worktree markers and filesystem-root termination, with no unresolved findings.

0 open findings

What changed in this PR

Fixes test repository-root detection for Git worktrees while preserving regular-checkout behavior and the ML_TEST_DATADIR override.

Changes:

  • Recognizes both .git files and directories.
  • Safely handles reaching the filesystem root without finding a repository.
File Description
test/​Microsoft.ML.TestFrameworkCommon/​TestCommon.cs Adds worktree detection and null-safe parent traversal.

🧠 Review effort: Balanced


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

@codecov

codecov Bot commented Oct 14, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 75.00000% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 70.00%. Comparing base (eb62d29) to head (8bf2e8e).
⚠️ Report is 15 commits behind head on main.

Files with missing lines Patch % Lines
...est/Microsoft.ML.TestFrameworkCommon/TestCommon.cs 75.00% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #7803      +/-   ##
==========================================
+ Coverage   69.95%   70.00%   +0.04%     
==========================================
  Files        1419     1419              
  Lines      272603   272763     +160     
  Branches    27562    27580      +18     
==========================================
+ Hits       190698   190942     +244     
+ Misses      73718    73647      -71     
+ Partials     8187     8174      -13     
Flag Coverage Δ
Debug 70.00% <75.00%> (+0.04%) ⬆️
production 64.18% <ø> (+0.04%) ⬆️
test 89.71% <75.00%> (+0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
...est/Microsoft.ML.TestFrameworkCommon/TestCommon.cs 77.04% <75.00%> (-0.28%) ⬇️

... and 25 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@svick
svick marked this pull request as ready for review October 15, 2026 15:23
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