Skip to content

fix: recognize git worktree root in find_vcs_root()#4640

Draft
evadeflow wants to merge 1 commit into
ansible:mainfrom
evadeflow:fix/find-vcs-root-worktree
Draft

fix: recognize git worktree root in find_vcs_root()#4640
evadeflow wants to merge 1 commit into
ansible:mainfrom
evadeflow:fix/find-vcs-root-worktree

Conversation

@evadeflow

Copy link
Copy Markdown

Issue

Partially addresses #4142.

When Molecule is run from inside a git worktree, it fails to locate the project root, so molecule.yml (and other project-root config) discovery breaks.

Root cause

find_vcs_root() walks up the directory tree looking for a .git entry, validated by _is_valid_vcs_dir(). That helper rejected anything that wasn't a directory:

vcs_dir = path / name
if not vcs_dir.is_dir():
    return False

In a git worktree (and in a submodule), the .git entry is a file containing a gitdir: <path> pointer, not a directory. So the worktree root was skipped and find_vcs_root() either found an unrelated ancestor repo or fell back to the default.

Fix

Treat a .git file as a valid VCS root marker when its contents start with gitdir:. Stray, unrelated .git files are still rejected, and the existing directory-based validation (a real .git dir contains a HEAD) is unchanged.

A stale comment that claimed worktrees have a .git directory with a HEAD file was also corrected.

Tests

Added two unit tests in tests/unit/test_util.py:

  • test_find_vcs_root_in_git_worktree — a worktree whose .git is a
    gitdir: pointer file is recognized as the root.
  • test_find_vcs_root_skips_bogus_git_file — a .git file without a
    gitdir: pointer is ignored.

Scope

This PR covers only worktree project-root detection. It uses Refs rather than Closes so #4142 stays open for any other aspects.

In a git worktree (or a submodule) the ".git" entry is a file
containing a "gitdir: <path>" pointer rather than a directory.
`_is_valid_vcs_dir()` only accepted directories, so `find_vcs_root()`
walked past the worktree root and failed to locate the project root,
breaking `molecule.yml` discovery for users working inside a worktree.

Treat a ".git" file as a valid VCS root marker when it contains a
"gitdir:" pointer, while still rejecting unrelated stray ".git" files.

Refs: ansible#4142
@shatakshiiii

Copy link
Copy Markdown
Contributor

@evadeflow We can't merge PRs with unsigned commits, please sign your commit and rebase the PR. Moving this to draft until then. Thanks!

@shatakshiiii shatakshiiii marked this pull request as draft June 4, 2026 10:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

2 participants