Skip to content

Mount worktree common dir when a custom workspaceMount is set#1261

Open
AvishayHirsh wants to merge 1 commit into
devcontainers:mainfrom
AvishayHirsh:fix-worktree-common-dir-custom-workspacemount
Open

Mount worktree common dir when a custom workspaceMount is set#1261
AvishayHirsh wants to merge 1 commit into
devcontainers:mainfrom
AvishayHirsh:fix-worktree-common-dir-custom-workspacemount

Conversation

@AvishayHirsh

Copy link
Copy Markdown

Fixes #1243

Problem

--mount-git-worktree-common-dir (added in #1127) is silently ignored when a devcontainer.json sets a custom workspaceMount. Git worktrees then break inside the container:

$ devcontainer exec --workspace-folder wt -- git -C /workspace status
fatal: not a git repository: (null)

This is common in monorepo setups where a custom workspaceMount/workspaceFolder is used.

Root cause

In getWorkspaceConfiguration (src/spec-node/utils.ts), the worktree common-dir computation lives inside the guard that only auto-computes workspaceMount/workspaceFolder when they aren't already set in the config:

if (workspace && (!workspaceFolder || !('workspaceMount' in config))) { ... }

When the config sets both workspaceFolder and workspaceMount, the whole block is skipped, so additionalMountString (the common-dir bind mount) is never built and the main repo's .git is never mounted.

Fix

The common-dir mount is an additive, separate mount — it can't conflict with a user-defined workspaceMount, so it shouldn't be gated by the "don't overwrite user config" guard:

  • Widen the guard so the worktree block also runs when the feature is enabled. --mount-git-worktree-common-dir defaults to false, so default behavior is unchanged unless the flag is opted into.
  • Resolve the common-dir target relative to the custom workspaceMount target. Because additionalMountString is not part of the later variable-substitution pass, the target is substituted here (handles the canonical target=/workspaces/${localWorkspaceFolderBasename}). Without a custom mount, the previously computed target is used, so existing behavior is unchanged.

Tests

Added unit tests in src/test/workspaceConfiguration.test.ts (across linux/darwin/win32) covering: the reported scenario, ${localWorkspaceFolderBasename} substitution in the target, feature-off no-op, non-worktree no-op, and a worktree nested inside the main repo. yarn lint, yarn type-check, and the test suite all pass locally.

Known limitations (pre-existing, unchanged by this PR)

  • Only a relative gitdir is handled (worktrees created with git worktree add --relative-paths); absolute gitdirs were already unsupported.
  • Assumes the custom mount's source is the worktree root (the directory holding the .git file).

The --mount-git-worktree-common-dir logic was nested inside the guard
that skips workspace mount computation when devcontainer.json sets both
workspaceFolder and workspaceMount. As a result the worktree common dir
was never mounted and git operations failed in the container
("fatal: not a git repository").

Decouple the additive common dir mount from that guard and resolve its
target relative to the custom (substituted) workspaceMount target so the
relative gitdir resolves correctly inside the container.
@AvishayHirsh AvishayHirsh requested a review from a team as a code owner July 2, 2026 10:08
@AvishayHirsh

Copy link
Copy Markdown
Author

@microsoft-github-policy-service agree

@AvishayHirsh

AvishayHirsh commented Jul 2, 2026

Copy link
Copy Markdown
Author

Thanks for the worktree common-dir feature in #1127! This is a small follow-up for #1243: the common-dir mount is skipped when devcontainer.json sets a custom workspaceMount (common in monorepos), so worktree git operations break in the container (fatal: not a git repository).

The fix is gated behind the existing --mount-git-worktree-common-dir flag (default off → no behavior change otherwise), and resolves the common-dir target relative to the custom (substituted) workspaceMount target. Added unit tests across linux/darwin/win32. CLA is signed; yarn lint, yarn type-check, and the test suite pass locally.

@Mathiyarasy @chrmarti would appreciate a workflow-run approval and a review whenever you have a moment — thanks!

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.

--mount-git-worktree-common-dir silently ignored when devcontainer.json sets a custom workspaceMount

1 participant