Skip to content

[BUG] safe.directory trusts only /workspace, not worktrees under it #253

In review

Description

@gatezh

Summary

The image trusts only the exact path /workspace (git config --system --add safe.directory /workspace, added in #211). safe.directory matches exact paths, so git worktrees under /workspace/.claude/worktrees/* are not trusted. The intermittent bind-mount ownership mismatch from #172 still makes git fail there with fatal: detected dubious ownership. That includes Hugo's enableGitInfo calls, which can't take a -c override (#211).

As a workaround, Claude Code sessions now start every git call with git -c safe.directory=…. The Bash tool runs zsh, which doesn't word-split variables, so sessions that save the prefix as G="git -c …"; $G log fail with exit 127. A function named g() fails too, because oh-my-zsh's git plugin aliases g. One downstream project saw this wasted retry in 7 sessions.

Reproduction

  1. In the claude-code devcontainer, create a worktree: git worktree add .claude/worktrees/x -b x.
  2. Run git in it while the ownership mismatch is active (intermittent, claude-code template: VS Code's Git extension intermittently hits "dubious ownership" on /workspace, and SSH GitHub remotes can't use the container's gh login #172), e.g. git -C .claude/worktrees/x status, or a Hugo build with enableGitInfo: true.
fatal: detected dubious ownership in repository at '/workspace/.claude/worktrees/x'

Verification

Deterministic, with no dependence on the flake. Make a root-owned repo with a nested repo and test which safe.directory values trust the nested one (git 2.47.3 in the image):

S=$(mktemp -d); sudo mkdir -p $S/ws/wt; sudo git init -q $S/ws; sudo git init -q $S/ws/wt
t(){ GIT_CONFIG_COUNT=1 GIT_CONFIG_KEY_0=safe.directory GIT_CONFIG_VALUE_0="$1" \
     git -C "$2" rev-parse --git-dir >/dev/null 2>&1 && echo trusted || echo DUBIOUS; }
t "$S/ws"   $S/ws/wt   # DUBIOUS  <- current image behaviour for worktrees
t "$S/ws/*" $S/ws/wt   # trusted
t "$S/ws/*" $S/ws      # DUBIOUS  <- "/*" does not cover the directory itself

So both entries are needed. Leading-path /* matching has been supported since git 2.46.

Suggested fix

claude-code/.devcontainer/Dockerfile, in the base stage (shared by default and sandbox):

-RUN git config --system --add safe.directory /workspace
+RUN git config --system --add safe.directory /workspace \
+ && git config --system --add safe.directory '/workspace/*'

/workspace/* is preferred over *: it covers worktrees and nested repos without turning off the check for paths outside the bind mount.

Affected files in this repo

  • claude-code/.devcontainer/Dockerfile:146

Related

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions