From e1b257420b9dc8a0fb6b74b30ccbac0ec033b5c3 Mon Sep 17 00:00:00 2001 From: Vitor Bandeira Date: Mon, 16 Nov 2026 01:15:50 -0300 Subject: [PATCH 1/5] fix(claude): make git work when claude.sh runs from a worktree A worktree's .git file points to the main checkout by absolute path. claude.sh mounts only the worktree, so git in the container failed with "not a git repository". claude.sh now also mounts the main .git directory at the same path. The paths of the other worktrees do not exist in the container. git gc there could then prune their records from the shared .git directory. claude.sh sets gc.worktreePruneExpire=never in the container to stop this. Signed-off-by: Vitor Bandeira --- claude.sh | 19 +++++++++++++++ docker/test-claude-sh.sh | 52 ++++++++++++++++++++++++++++++++++++++++ docs/user/ClaudeCode.md | 14 ++++++++++- 3 files changed, 84 insertions(+), 1 deletion(-) create mode 100755 docker/test-claude-sh.sh diff --git a/claude.sh b/claude.sh index 6252e76989..65845a444c 100755 --- a/claude.sh +++ b/claude.sh @@ -94,6 +94,25 @@ DOCKER_RUN_ARGS=( -w /workspace ) +# A git worktree's .git file points to the main checkout by absolute path. +# Mount that git directory at the same path, so git works in the container. +GIT_COMMON_DIR=$(git -C "${SCRIPT_DIR}" rev-parse --path-format=absolute \ + --git-common-dir 2> /dev/null || true) +if [[ -n "${GIT_COMMON_DIR}" ]]; then + case "${GIT_COMMON_DIR}/" in + "$(cd "${SCRIPT_DIR}" && pwd -P)"/*) ;; + *) DOCKER_RUN_ARGS+=(-v "${GIT_COMMON_DIR}:${GIT_COMMON_DIR}") ;; + esac +fi + +# Host paths of other worktrees do not exist in the container, so git gc +# would prune their records from the shared .git. Stop that. +DOCKER_RUN_ARGS+=( + -e GIT_CONFIG_COUNT=1 + -e GIT_CONFIG_KEY_0=gc.worktreePruneExpire + -e GIT_CONFIG_VALUE_0=never +) + # TTY/stdin handling for interactive, CI, and piped-input cases: # * terminal in + terminal out → -it (normal interactive use) # * anything else → -i (pass stdin through; no TTY) diff --git a/docker/test-claude-sh.sh b/docker/test-claude-sh.sh new file mode 100755 index 0000000000..34888eefcd --- /dev/null +++ b/docker/test-claude-sh.sh @@ -0,0 +1,52 @@ +#!/usr/bin/env bash +# Check the git mounts that claude.sh adds, with a stub docker command. +# Needs bash and git only; no container engine runs. +set -euo pipefail + +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +tmp=$(mktemp -d) +trap 'rm -rf "${tmp}"' EXIT + +mkdir -p "${tmp}/bin" "${tmp}/home" +cat > "${tmp}/bin/docker" << 'EOF' +#!/usr/bin/env bash +# The image exists. For "run", print each argument on its own line. +if [[ "$1" == run ]]; then + shift + printf '%s\n' "$@" +fi +EOF +chmod +x "${tmp}/bin/docker" + +git -c init.defaultBranch=main init -q "${tmp}/main" +cp "${SCRIPT_DIR}/../claude.sh" "${tmp}/main/claude.sh" +git -C "${tmp}/main" add claude.sh +git -C "${tmp}/main" -c user.name=test -c user.email=test@example.invalid \ + -c core.hooksPath=/dev/null commit -q -m init +git -C "${tmp}/main" worktree add -q "${tmp}/wt" +common=$(git -C "${tmp}/wt" rev-parse --path-format=absolute --git-common-dir) + +run_args() { # Print the docker run arguments of claude.sh in checkout $1. + env -u SSH_AUTH_SOCK HOME="${tmp}/home" PATH="${tmp}/bin:${PATH}" \ + bash "$1/claude.sh" --shell < /dev/null +} + +fail() { + echo "FAIL: $*" >&2 + exit 1 +} + +wt_args=$(run_args "${tmp}/wt") +grep -qxF "${common}:${common}" <<< "${wt_args}" || + fail "worktree: ${common} is not mounted at the same path" +grep -qxF "GIT_CONFIG_VALUE_0=never" <<< "${wt_args}" || + fail "worktree: automatic worktree prune is not disabled" +echo "A worktree gets the main .git at the same path" + +main_args=$(run_args "${tmp}/main") +if grep -qF "${common}:" <<< "${main_args}"; then + fail "main checkout: .git is mounted a second time" +fi +grep -qxF "GIT_CONFIG_VALUE_0=never" <<< "${main_args}" || + fail "main checkout: automatic worktree prune is not disabled" +echo "A main checkout needs no extra .git mount" diff --git a/docs/user/ClaudeCode.md b/docs/user/ClaudeCode.md index c4128cb78f..31bf1e504c 100644 --- a/docs/user/ClaudeCode.md +++ b/docs/user/ClaudeCode.md @@ -143,13 +143,25 @@ and the `--user` mode in `flow/util/docker_shell`. Wrapper script that assembles the `docker run` command with the correct volume mounts, environment variables, and entrypoint arguments. +When you run `claude.sh` from a git worktree, it also mounts the main checkout's `.git` directory at its host path. +The worktree's `.git` file points to that path, so git works in the container. +The wrapper also sets `gc.worktreePruneExpire=never` in the container. +The other worktrees' paths do not exist in the container, so without this setting `git gc` could delete their records from the shared `.git` directory. + +Check the mounts with a stub `docker` command, without a container engine: + +```shell +bash docker/test-claude-sh.sh +``` + ## Security model Claude Code runs with `--dangerously-skip-permissions` **inside** the container, meaning it can execute any shell command without confirmation. The Docker container provides the isolation: -- File access is limited to the mounted repository +- File access is limited to the mounted repository. + From a git worktree, this includes the main checkout's `.git` directory, which holds the refs and objects of all its worktrees. - No access to host system packages, services, or other projects - No access to host network services bound to localhost - `docker kill claude-orfs` stops everything instantly From 9b47aa2ea9e4c0b996b5e59e04966de9bf5d6ea2 Mon Sep 17 00:00:00 2001 From: Vitor Bandeira Date: Mon, 16 Nov 2026 01:23:46 -0300 Subject: [PATCH 2/5] fix(claude): mount the worktree at its host path too Submodule .git files in a worktree hold a relative path to the main .git directory. That path did not resolve from /workspace, so git failed for an initialized submodule. The worktree is now also mounted at its host path, and the shell starts there. The current worktree is then no longer stale for git in the container. Git before 2.31 prints the unknown --path-format option as output, so the mount was wrong on those hosts. Resolve --git-common-dir with cd and pwd -P instead. Host paths under /workspace or /home/claude-user would nest inside the container mounts. Skip the extra mounts there and print a warning. Signed-off-by: Vitor Bandeira --- claude.sh | 51 ++++++++++++++++++++++++++---------- docker/test-claude-sh.sh | 56 +++++++++++++++++++++++++++++++--------- docs/user/ClaudeCode.md | 18 +++++++++---- 3 files changed, 95 insertions(+), 30 deletions(-) diff --git a/claude.sh b/claude.sh index 65845a444c..cd3d82d89c 100755 --- a/claude.sh +++ b/claude.sh @@ -84,6 +84,39 @@ fi # The entrypoint creates /home/claude-user for the mapped user. CONTAINER_HOME="/home/claude-user" +# --- Git data of a worktree --- +# A worktree's .git file, and the .git files of its submodules, point to the +# main checkout's git directory by host path, absolute or relative. Mount that +# directory and the worktree at their host paths, and start in the worktree's +# host path, so these links resolve in the container. /workspace stays. +HOST_DIR=$(cd "${SCRIPT_DIR}" && pwd -P) +CONTAINER_WORKDIR=/workspace +GIT_COMMON_DIR="" +if git_common=$(git -C "${HOST_DIR}" rev-parse --git-common-dir 2> /dev/null); then + GIT_COMMON_DIR=$(cd "${HOST_DIR}" && cd "${git_common}" 2> /dev/null && pwd -P) || + GIT_COMMON_DIR="" +fi + +# True when host path $1 does not overlap a path that the container already uses. +outside_container_paths() { + [[ "$1/" != /workspace/* && "$1/" != "${CONTAINER_HOME}/"* ]] +} + +GIT_MOUNTS=() +if [[ -n "${GIT_COMMON_DIR}" && "${GIT_COMMON_DIR}/" != "${HOST_DIR}/"* ]]; then + if outside_container_paths "${HOST_DIR}" && + outside_container_paths "${GIT_COMMON_DIR}"; then + GIT_MOUNTS=( + -v "${HOST_DIR}:${HOST_DIR}" + -v "${GIT_COMMON_DIR}:${GIT_COMMON_DIR}" + ) + CONTAINER_WORKDIR=${HOST_DIR} + else + echo "Warning: git data in ${GIT_COMMON_DIR} cannot be mounted at its" \ + "host path, so git does not work in the container." >&2 + fi +fi + # --- Assemble docker run arguments --- DOCKER_RUN_ARGS=( --rm @@ -91,22 +124,14 @@ DOCKER_RUN_ARGS=( -v "${SCRIPT_DIR}:/workspace" -e "HOST_UID=$(id -u)" -e "HOST_GID=$(id -g)" - -w /workspace + -w "${CONTAINER_WORKDIR}" ) - -# A git worktree's .git file points to the main checkout by absolute path. -# Mount that git directory at the same path, so git works in the container. -GIT_COMMON_DIR=$(git -C "${SCRIPT_DIR}" rev-parse --path-format=absolute \ - --git-common-dir 2> /dev/null || true) -if [[ -n "${GIT_COMMON_DIR}" ]]; then - case "${GIT_COMMON_DIR}/" in - "$(cd "${SCRIPT_DIR}" && pwd -P)"/*) ;; - *) DOCKER_RUN_ARGS+=(-v "${GIT_COMMON_DIR}:${GIT_COMMON_DIR}") ;; - esac +if [[ ${#GIT_MOUNTS[@]} -gt 0 ]]; then + DOCKER_RUN_ARGS+=("${GIT_MOUNTS[@]}") fi -# Host paths of other worktrees do not exist in the container, so git gc -# would prune their records from the shared .git. Stop that. +# The host paths of other worktrees do not exist in the container, so git gc +# there would prune their records from the shared .git directory. Stop that. DOCKER_RUN_ARGS+=( -e GIT_CONFIG_COUNT=1 -e GIT_CONFIG_KEY_0=gc.worktreePruneExpire diff --git a/docker/test-claude-sh.sh b/docker/test-claude-sh.sh index 34888eefcd..551cb25c40 100755 --- a/docker/test-claude-sh.sh +++ b/docker/test-claude-sh.sh @@ -24,29 +24,61 @@ git -C "${tmp}/main" add claude.sh git -C "${tmp}/main" -c user.name=test -c user.email=test@example.invalid \ -c core.hooksPath=/dev/null commit -q -m init git -C "${tmp}/main" worktree add -q "${tmp}/wt" -common=$(git -C "${tmp}/wt" rev-parse --path-format=absolute --git-common-dir) +common=$(cd "${tmp}/main/.git" && pwd -P) +wt=$(cd "${tmp}/wt" && pwd -P) +mkdir "${tmp}/plain" +cp "${SCRIPT_DIR}/../claude.sh" "${tmp}/plain/claude.sh" run_args() { # Print the docker run arguments of claude.sh in checkout $1. env -u SSH_AUTH_SOCK HOME="${tmp}/home" PATH="${tmp}/bin:${PATH}" \ bash "$1/claude.sh" --shell < /dev/null } +workdir() { # Print the value of -w in the arguments $1. + grep -A1 -xF -- -w <<< "$1" | tail -n 1 +} + fail() { echo "FAIL: $*" >&2 exit 1 } -wt_args=$(run_args "${tmp}/wt") -grep -qxF "${common}:${common}" <<< "${wt_args}" || - fail "worktree: ${common} is not mounted at the same path" -grep -qxF "GIT_CONFIG_VALUE_0=never" <<< "${wt_args}" || +check_plain() { # check_plain NAME ARGS: only /workspace, prune guard set. + if grep -qF "${common}:" <<< "$2" || grep -qF "${wt}:" <<< "$2"; then + fail "$1: unexpected git mount" + fi + [[ "$(workdir "$2")" == /workspace ]] || fail "$1: workdir is not /workspace" + grep -qxF GIT_CONFIG_VALUE_0=never <<< "$2" || + fail "$1: automatic worktree prune is not disabled" +} + +args=$(run_args "${tmp}/wt") +grep -qxF "${common}:${common}" <<< "${args}" || + fail "worktree: ${common} is not mounted at its host path" +grep -qxF "${wt}:${wt}" <<< "${args}" || + fail "worktree: ${wt} is not mounted at its host path" +[[ "$(workdir "${args}")" == "${wt}" ]] || + fail "worktree: workdir is not ${wt}" +grep -qxF GIT_CONFIG_VALUE_0=never <<< "${args}" || fail "worktree: automatic worktree prune is not disabled" -echo "A worktree gets the main .git at the same path" +echo "A worktree gets its host path and the main .git at their host paths" -main_args=$(run_args "${tmp}/main") -if grep -qF "${common}:" <<< "${main_args}"; then - fail "main checkout: .git is mounted a second time" +check_plain "main checkout" "$(run_args "${tmp}/main")" +echo "A main checkout gets no extra mount" + +check_plain "not a repository" "$(run_args "${tmp}/plain")" +echo "A folder outside git gets no extra mount" + +# Git before 2.31 prints an unknown option as output and exits 0. +mkdir "${tmp}/oldgit" +cat > "${tmp}/oldgit/git" << EOF +#!/usr/bin/env bash +if [[ "\$*" == *rev-parse* ]]; then + echo --path-format=absolute fi -grep -qxF "GIT_CONFIG_VALUE_0=never" <<< "${main_args}" || - fail "main checkout: automatic worktree prune is not disabled" -echo "A main checkout needs no extra .git mount" +exec "$(command -v git)" "\$@" +EOF +chmod +x "${tmp}/oldgit/git" +PATH="${tmp}/oldgit:${PATH}" check_plain "old git, main checkout" \ + "$(PATH="${tmp}/oldgit:${PATH}" run_args "${tmp}/main")" +echo "Unexpected git output adds no mount" diff --git a/docs/user/ClaudeCode.md b/docs/user/ClaudeCode.md index 31bf1e504c..2a155c4531 100644 --- a/docs/user/ClaudeCode.md +++ b/docs/user/ClaudeCode.md @@ -93,6 +93,7 @@ are mounted so their contents survive: | `~/.claude/` | `~/.claude/` | Credentials, settings, conversation history | | `~/.cache/bazel-claude/` | `~/.cache/bazel/` | Bazel external dependencies | | `~/.gitconfig` | `~/.gitconfig` | Git identity (read-only) | +| Worktree and main `.git` (worktree only) | Same host paths | Git refs, objects, hooks, config | | `$SSH_AUTH_SOCK` | forwarded | SSH agent for git over SSH | ## Environment variables @@ -143,10 +144,16 @@ and the `--user` mode in `flow/util/docker_shell`. Wrapper script that assembles the `docker run` command with the correct volume mounts, environment variables, and entrypoint arguments. -When you run `claude.sh` from a git worktree, it also mounts the main checkout's `.git` directory at its host path. -The worktree's `.git` file points to that path, so git works in the container. -The wrapper also sets `gc.worktreePruneExpire=never` in the container. -The other worktrees' paths do not exist in the container, so without this setting `git gc` could delete their records from the shared `.git` directory. +When you run `claude.sh` from a git worktree, it also mounts the worktree and the main checkout's `.git` directory at their host paths. +The shell starts in the worktree's host path. +The worktree's `.git` file and the `.git` files of its submodules point to these paths, so git works in the container. +The worktree is also at `/workspace`. +If one of these host paths is under `/workspace` or `/home/claude-user`, the wrapper prints a warning and adds no mounts, and git does not work in the container. + +The paths of the other worktrees do not exist in the container, so git sees them as stale. +The wrapper sets `gc.worktreePruneExpire=never` in the container, so `git gc` keeps their records. +`git gc` still removes the record of a worktree that has no index file. +Do not run `git worktree prune` or `git worktree repair` in the container: they change the records of the other worktrees. Check the mounts with a stub `docker` command, without a container engine: @@ -161,7 +168,8 @@ container, meaning it can execute any shell command without confirmation. The Docker container provides the isolation: - File access is limited to the mounted repository. - From a git worktree, this includes the main checkout's `.git` directory, which holds the refs and objects of all its worktrees. + From a git worktree, this includes the main checkout's `.git` directory, with the refs and objects of all its worktrees. + It also holds git hooks and config, which run on the host at the next git command, so code in the container can run code on the host. - No access to host system packages, services, or other projects - No access to host network services bound to localhost - `docker kill claude-orfs` stops everything instantly From 6fa2d077ca73b2f15a546b6b92be6879bf7152d6 Mon Sep 17 00:00:00 2001 From: Vitor Bandeira Date: Mon, 16 Nov 2026 01:29:18 -0300 Subject: [PATCH 3/5] fix(claude): mount git data only for the top of a worktree git rev-parse searches upward. A plain folder inside another repository, such as a home folder in git, got that repository's .git mounted read-write. Mount it only when the folder is the top of its checkout. The entrypoint sourced /workspace/env.sh, so OPENROAD, FLOW_HOME and PATH pointed to /workspace even when the shell started in the host path. Git links in submodules do not resolve from there. Source env.sh from the start directory when it has one. The old-git test now passes --path-format only when the caller does, and the worktree case runs through it too. The docs state the changed session and build paths. Signed-off-by: Vitor Bandeira --- claude.sh | 15 ++++++--- docker/claude-entrypoint.sh | 15 ++++++--- docker/test-claude-sh.sh | 62 ++++++++++++++++++++++++------------- docs/user/ClaudeCode.md | 12 +++++-- 4 files changed, 72 insertions(+), 32 deletions(-) diff --git a/claude.sh b/claude.sh index cd3d82d89c..a66ed2abef 100755 --- a/claude.sh +++ b/claude.sh @@ -91,8 +91,11 @@ CONTAINER_HOME="/home/claude-user" # host path, so these links resolve in the container. /workspace stays. HOST_DIR=$(cd "${SCRIPT_DIR}" && pwd -P) CONTAINER_WORKDIR=/workspace +GIT_TOP="" GIT_COMMON_DIR="" -if git_common=$(git -C "${HOST_DIR}" rev-parse --git-common-dir 2> /dev/null); then +if git_top=$(git -C "${HOST_DIR}" rev-parse --show-toplevel 2> /dev/null) && + git_common=$(git -C "${HOST_DIR}" rev-parse --git-common-dir 2> /dev/null); then + GIT_TOP=$(cd "${git_top}" 2> /dev/null && pwd -P) || GIT_TOP="" GIT_COMMON_DIR=$(cd "${HOST_DIR}" && cd "${git_common}" 2> /dev/null && pwd -P) || GIT_COMMON_DIR="" fi @@ -103,7 +106,10 @@ outside_container_paths() { } GIT_MOUNTS=() -if [[ -n "${GIT_COMMON_DIR}" && "${GIT_COMMON_DIR}/" != "${HOST_DIR}/"* ]]; then +# Only for the top of a checkout: a plain folder inside another repository +# must not get that repository's .git. +if [[ "${GIT_TOP}" == "${HOST_DIR}" && -n "${GIT_COMMON_DIR}" && + "${GIT_COMMON_DIR}/" != "${HOST_DIR}/"* ]]; then if outside_container_paths "${HOST_DIR}" && outside_container_paths "${GIT_COMMON_DIR}"; then GIT_MOUNTS=( @@ -112,8 +118,9 @@ if [[ -n "${GIT_COMMON_DIR}" && "${GIT_COMMON_DIR}/" != "${HOST_DIR}/"* ]]; then ) CONTAINER_WORKDIR=${HOST_DIR} else - echo "Warning: git data in ${GIT_COMMON_DIR} cannot be mounted at its" \ - "host path, so git does not work in the container." >&2 + echo "Warning: ${HOST_DIR} and its git data in ${GIT_COMMON_DIR} cannot" \ + "both be mounted at their host paths, because one of them is under" \ + "/workspace or ${CONTAINER_HOME}. Git does not work in the container." >&2 fi fi diff --git a/docker/claude-entrypoint.sh b/docker/claude-entrypoint.sh index 18b77b98c9..28280421eb 100755 --- a/docker/claude-entrypoint.sh +++ b/docker/claude-entrypoint.sh @@ -4,6 +4,13 @@ # inside the container have the correct ownership on the host volume. set -e +# claude.sh starts a git worktree in its host path. env.sh sets paths from +# its own location, so source the copy there to keep the paths the same. +ENV_SH=/workspace/env.sh +if [ -f "${PWD}/env.sh" ]; then + ENV_SH="${PWD}/env.sh" +fi + if [ -n "${HOST_UID}" ] && [ -n "${HOST_GID}" ]; then USER_NAME="claude-user" USER_HOME="/home/${USER_NAME}" @@ -40,9 +47,9 @@ if [ -n "${HOST_UID}" ] && [ -n "${HOST_GID}" ]; then # Set up ORFS environment (PATH to tools, FLOW_HOME, etc.) # Sourced here; inherited through setpriv into all child processes. - if [ -f /workspace/env.sh ]; then + if [ -f "${ENV_SH}" ]; then set +e - . /workspace/env.sh > /dev/null + . "${ENV_SH}" > /dev/null set -e fi @@ -53,9 +60,9 @@ if [ -n "${HOST_UID}" ] && [ -n "${HOST_GID}" ]; then -- "$@" else # No UID/GID mapping requested; run as root - if [ -f /workspace/env.sh ]; then + if [ -f "${ENV_SH}" ]; then set +e - . /workspace/env.sh > /dev/null + . "${ENV_SH}" > /dev/null set -e fi exec "$@" diff --git a/docker/test-claude-sh.sh b/docker/test-claude-sh.sh index 551cb25c40..4619b7ff19 100755 --- a/docker/test-claude-sh.sh +++ b/docker/test-claude-sh.sh @@ -28,6 +28,27 @@ common=$(cd "${tmp}/main/.git" && pwd -P) wt=$(cd "${tmp}/wt" && pwd -P) mkdir "${tmp}/plain" cp "${SCRIPT_DIR}/../claude.sh" "${tmp}/plain/claude.sh" +# A plain folder inside another repository, for example a home in git. +git -c init.defaultBranch=main init -q "${tmp}/parent" +mkdir -p "${tmp}/parent/src/orfs" +cp "${SCRIPT_DIR}/../claude.sh" "${tmp}/parent/src/orfs/claude.sh" +parent_git=$(cd "${tmp}/parent/.git" && pwd -P) + +# Git before 2.31 does not know --path-format. rev-parse then prints the +# option as output and exits 0. This wrapper does the same. +mkdir "${tmp}/oldgit" +cat > "${tmp}/oldgit/git" << EOF +#!/usr/bin/env bash +args=() +for arg in "\$@"; do + case "\${arg}" in + --path-format=*) echo "\${arg}" ;; + *) args+=("\${arg}") ;; + esac +done +exec "$(command -v git)" "\${args[@]}" +EOF +chmod +x "${tmp}/oldgit/git" run_args() { # Print the docker run arguments of claude.sh in checkout $1. env -u SSH_AUTH_SOCK HOME="${tmp}/home" PATH="${tmp}/bin:${PATH}" \ @@ -52,15 +73,17 @@ check_plain() { # check_plain NAME ARGS: only /workspace, prune guard set. fail "$1: automatic worktree prune is not disabled" } -args=$(run_args "${tmp}/wt") -grep -qxF "${common}:${common}" <<< "${args}" || - fail "worktree: ${common} is not mounted at its host path" -grep -qxF "${wt}:${wt}" <<< "${args}" || - fail "worktree: ${wt} is not mounted at its host path" -[[ "$(workdir "${args}")" == "${wt}" ]] || - fail "worktree: workdir is not ${wt}" -grep -qxF GIT_CONFIG_VALUE_0=never <<< "${args}" || - fail "worktree: automatic worktree prune is not disabled" +check_worktree() { # check_worktree NAME ARGS: host-path mounts and workdir. + grep -qxF "${common}:${common}" <<< "$2" || + fail "$1: ${common} is not mounted at its host path" + grep -qxF "${wt}:${wt}" <<< "$2" || + fail "$1: ${wt} is not mounted at its host path" + [[ "$(workdir "$2")" == "${wt}" ]] || fail "$1: workdir is not ${wt}" + grep -qxF GIT_CONFIG_VALUE_0=never <<< "$2" || + fail "$1: automatic worktree prune is not disabled" +} + +check_worktree worktree "$(run_args "${tmp}/wt")" echo "A worktree gets its host path and the main .git at their host paths" check_plain "main checkout" "$(run_args "${tmp}/main")" @@ -69,16 +92,13 @@ echo "A main checkout gets no extra mount" check_plain "not a repository" "$(run_args "${tmp}/plain")" echo "A folder outside git gets no extra mount" -# Git before 2.31 prints an unknown option as output and exits 0. -mkdir "${tmp}/oldgit" -cat > "${tmp}/oldgit/git" << EOF -#!/usr/bin/env bash -if [[ "\$*" == *rev-parse* ]]; then - echo --path-format=absolute +args=$(run_args "${tmp}/parent/src/orfs") +check_plain "folder in a parent repository" "${args}" +if grep -qF "${parent_git}" <<< "${args}"; then + fail "folder in a parent repository: the parent .git is mounted" fi -exec "$(command -v git)" "\$@" -EOF -chmod +x "${tmp}/oldgit/git" -PATH="${tmp}/oldgit:${PATH}" check_plain "old git, main checkout" \ - "$(PATH="${tmp}/oldgit:${PATH}" run_args "${tmp}/main")" -echo "Unexpected git output adds no mount" +echo "A folder inside another repository does not get its .git" + +check_worktree "old git, worktree" "$(PATH="${tmp}/oldgit:${PATH}" run_args "${tmp}/wt")" +check_plain "old git, main checkout" "$(PATH="${tmp}/oldgit:${PATH}" run_args "${tmp}/main")" +echo "Git without --path-format gives the same mounts" diff --git a/docs/user/ClaudeCode.md b/docs/user/ClaudeCode.md index 2a155c4531..251bdc68e5 100644 --- a/docs/user/ClaudeCode.md +++ b/docs/user/ClaudeCode.md @@ -133,7 +133,7 @@ Handles UID/GID mapping so files created inside the container have the correct ownership on the host. The entrypoint: 1. Creates a user matching the host UID/GID -2. Sources `env.sh` to set up tool paths +2. Sources `env.sh` to set up tool paths, from the start directory if it has one, else from `/workspace` 3. Drops privileges via `setpriv` before running the command This follows the same pattern as `tools/OpenROAD/etc/docker-entrypoint.sh` @@ -144,10 +144,12 @@ and the `--user` mode in `flow/util/docker_shell`. Wrapper script that assembles the `docker run` command with the correct volume mounts, environment variables, and entrypoint arguments. -When you run `claude.sh` from a git worktree, it also mounts the worktree and the main checkout's `.git` directory at their host paths. +When you run `claude.sh` from the top of a git worktree, it also mounts the worktree and the main checkout's `.git` directory at their host paths. The shell starts in the worktree's host path. The worktree's `.git` file and the `.git` files of its submodules point to these paths, so git works in the container. -The worktree is also at `/workspace`. +The entrypoint sources `env.sh` from there, so `OPENROAD`, `FLOW_HOME` and `PATH` use the host path too. +Rebuild the image with `./claude.sh --build` to get this entrypoint. +The worktree is also at `/workspace`, but git links in submodules do not resolve from that path. If one of these host paths is under `/workspace` or `/home/claude-user`, the wrapper prints a warning and adds no mounts, and git does not work in the container. The paths of the other worktrees do not exist in the container, so git sees them as stale. @@ -155,6 +157,10 @@ The wrapper sets `gc.worktreePruneExpire=never` in the container, so `git gc` ke `git gc` still removes the record of a worktree that has no index file. Do not run `git worktree prune` or `git worktree repair` in the container: they change the records of the other worktrees. +The start directory is part of some keys and caches. +Claude Code stores the history of a worktree session under the host path, not under `/workspace`, so earlier `/workspace` sessions of that worktree do not resume. +Bazel uses a new output base, and a CMake build directory that you configured under `/workspace` must be configured again. + Check the mounts with a stub `docker` command, without a container engine: ```shell From 2c1f49ffc58b68618fe420a756effb9ba867ba92 Mon Sep 17 00:00:00 2001 From: Vitor Bandeira Date: Mon, 16 Nov 2026 01:33:21 -0300 Subject: [PATCH 4/5] fix(env): keep paths with spaces in env.sh env.sh split its own path at spaces, because the inner command substitution was not quoted. The Claude container entrypoint now sources env.sh from the worktree's host path, which can contain spaces, so OPENROAD, FLOW_HOME and PATH came out wrong. The test now checks env.sh in a path with a space, and which env.sh the entrypoint sources. Signed-off-by: Vitor Bandeira --- docker/test-claude-sh.sh | 28 +++++++++++++++++++++++++++- env.sh | 4 ++-- 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/docker/test-claude-sh.sh b/docker/test-claude-sh.sh index 4619b7ff19..f5051cb96d 100755 --- a/docker/test-claude-sh.sh +++ b/docker/test-claude-sh.sh @@ -1,5 +1,6 @@ #!/usr/bin/env bash -# Check the git mounts that claude.sh adds, with a stub docker command. +# Check the git mounts that claude.sh adds, with a stub docker command, and +# which env.sh the entrypoint sources. # Needs bash and git only; no container engine runs. set -euo pipefail @@ -102,3 +103,28 @@ echo "A folder inside another repository does not get its .git" check_worktree "old git, worktree" "$(PATH="${tmp}/oldgit:${PATH}" run_args "${tmp}/wt")" check_plain "old git, main checkout" "$(PATH="${tmp}/oldgit:${PATH}" run_args "${tmp}/main")" echo "Git without --path-format gives the same mounts" + +# The entrypoint, here without UID mapping, sources env.sh from its start +# directory, which claude.sh sets to the worktree's host path. +mkdir "${tmp}/start" +echo 'export CLAUDE_TEST_ENV="${BASH_SOURCE[0]}"' > "${tmp}/start/env.sh" +sourced() { # Print which env.sh the entrypoint sourced when started in $1. + (cd "$1" && env -u HOST_UID -u HOST_GID \ + bash "${SCRIPT_DIR}/claude-entrypoint.sh" printenv CLAUDE_TEST_ENV) || true +} +[[ "$(sourced "${tmp}/start")" == "${tmp}/start/env.sh" ]] || + fail "entrypoint: env.sh in the start directory is not sourced" +[[ -z "$(sourced "${tmp}/plain")" ]] || + fail "entrypoint: a start directory without env.sh sourced something" +echo "The entrypoint sources env.sh from its start directory" + +# The entrypoint sources env.sh from a host path, which can contain spaces. +mkdir "${tmp}/with space" +cp "${SCRIPT_DIR}/../env.sh" "${tmp}/with space/env.sh" +printf '#!/bin/sh\necho /brew\n' > "${tmp}/bin/brew" # env.sh calls brew on macOS +chmod +x "${tmp}/bin/brew" +flow_home=$(PATH="${tmp}/bin:${PATH}" bash -c \ + '. "$1/env.sh" > /dev/null && echo "${FLOW_HOME}"' _ "${tmp}/with space") +[[ "${flow_home}" == "$(cd "${tmp}/with space" && pwd -P)/flow" ]] || + fail "env.sh in a path with a space: FLOW_HOME is '${flow_home}'" +echo "env.sh works in a path with a space" diff --git a/env.sh b/env.sh index e4194c306f..2c84d52c51 100755 --- a/env.sh +++ b/env.sh @@ -1,9 +1,9 @@ #!/usr/bin/env bash function __setpaths() { if [[ "$OSTYPE" == "darwin"* ]]; then - DIR="$(dirname $(perl -e 'use Cwd "abs_path";print abs_path(shift)' "${BASH_SOURCE[0]:-${(%):-%x}}"))" + DIR="$(dirname "$(perl -e 'use Cwd "abs_path";print abs_path(shift)' "${BASH_SOURCE[0]:-${(%):-%x}}")")" else - DIR="$(dirname $(readlink -f "${BASH_SOURCE[0]:-${(%):-%x}}"))" + DIR="$(dirname "$(readlink -f "${BASH_SOURCE[0]:-${(%):-%x}}")")" fi export OPENROAD=${DIR}/tools/OpenROAD From 9a4d9eda87b97e027f2b08ec8e1338c74ec099ba Mon Sep 17 00:00:00 2001 From: Vitor Bandeira Date: Mon, 16 Nov 2026 03:40:29 -0300 Subject: [PATCH 5/5] test(claude): do not sign the fixture commit With commit.gpgsign=true in the user's git config, the fixture commit tried to sign with a key for the test identity and failed. Signed-off-by: Vitor Bandeira --- docker/test-claude-sh.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docker/test-claude-sh.sh b/docker/test-claude-sh.sh index f5051cb96d..ea75830b74 100755 --- a/docker/test-claude-sh.sh +++ b/docker/test-claude-sh.sh @@ -23,7 +23,7 @@ git -c init.defaultBranch=main init -q "${tmp}/main" cp "${SCRIPT_DIR}/../claude.sh" "${tmp}/main/claude.sh" git -C "${tmp}/main" add claude.sh git -C "${tmp}/main" -c user.name=test -c user.email=test@example.invalid \ - -c core.hooksPath=/dev/null commit -q -m init + -c core.hooksPath=/dev/null -c commit.gpgsign=false commit -q -m init git -C "${tmp}/main" worktree add -q "${tmp}/wt" common=$(cd "${tmp}/main/.git" && pwd -P) wt=$(cd "${tmp}/wt" && pwd -P)