Skip to content

Claude plugin WorktreeCreate hook: missing pipefail swallows wt failures ("hook succeeded but returned no worktree path") #3545

Closed

Description

@avdi

Symptom

When wt switch --create fails partway (in my case a repo .config/wt.toml hook step, after the branch/worktree had already been created), Claude Code's EnterWorktree(name: ...) reports:

WorktreeCreate hook failed: hook succeeded but returned no worktree path
(command: echo the path to stdout; http/callback: return hookSpecificOutput.worktreePath)

The real wt error is swallowed, and the half-created branch/worktree is left behind — a follow-up wt switch --create <same-name> then fails with "Branch already exists", which is confusing to recover from (the fix is wt switch <name> without --create, but nothing tells you that).

Cause

The shipped hook command in the plugin's hooks.json (observed in marketplace cache versions 17add1f6dda3 and 4121dc979c2c):

bash -c 'name=$(jq -er .name) || exit 1; cd "${CLAUDE_PROJECT_DIR:-.}" || exit 1; bash "$CLAUDE_PLUGIN_ROOT/hooks/wt.sh" switch --create "$name" --no-cd --format=json | jq -er .path'

No set -o pipefail, so the pipeline's exit status is jq's — and jq -er .path on empty input exits 0:

$ printf '' | jq -er .path; echo $?
0
$ echo '{}' | jq -er .path; echo $?
null
1

So when wt dies with empty stdout, the hook "succeeds" while printing nothing, producing the misleading no-path error above instead of wt's actual stderr.

Suggested fix

Prefix the pipeline with set -o pipefail; (the pattern the repo's own skills/wt-switch-create/rationale.md describes) — or capture wt's output and check its exit status before piping to jq. Possibly related shell-portability ground touched in #2962.

Environment: worktrunk Claude plugin via marketplace, Linux devcontainer, bash.

Activity

  1. worktrunk-bot commented on Jul 22, 2026

    @worktrunk-bot
    Collaborator

    Thanks for the excellent, precise report — the diagnosis is exactly right.

    I reproduced it and confirmed the root cause: the WorktreeCreate command in plugins/worktrunk/hooks/hooks.json never carried set -o pipefail, even though skills/wt-switch-create/rationale.md documents that wrapper as the intended design ("The hooks.json pipefail wrapper"). So the code had drifted from its own rationale — the pipeline's exit status was jq's, and on jq 1.6 jq -er .path exits 0 on empty stdin, swallowing wt's real failure.

    I've opened #3546, which prefixes the existing bash -c command with set -o pipefail; — the safe home for it (dash rejects the option fatally, which is why the bash -c wrapper exists), and it makes the pipeline surface wt's nonzero exit regardless of jq version. It also adds a test asserting the hook keeps set -o pipefail, so this can't silently regress again.

    Reproduction and jq-version note

    The behavior you saw is jq-version-dependent, which the fix sidesteps entirely:

    # jq 1.6 (your environment) — bug fires:
    $ printf '' | jq -er .path; echo $?
    0
    # jq 1.7+ — empty input exits 4, so the bug happens to be masked there:
    $ printf '' | jq -er .path; echo $?
    4

    With set -o pipefail, a failed wt (nonzero exit, empty stdout) propagates regardless:

    $ bash -c 'set -o pipefail; (echo "✗ Branch foo already exists" >&2; exit 1) | jq -er .path'; echo $?
    ✗ Branch foo already exists
    4                       # hook correctly fails; wt's stderr is shown

    Your point about the leftover half-created branch/worktree (the confusing "Branch already exists" on retry, recovered with wt switch <name> without --create) is a separate rough edge from the swallowed error — worth its own look, but out of scope for this one-line fix.

    A maintainer will review #3546.

  2. max-sixty commented on Jul 22, 2026

    @max-sixty
    Owner

    thanks @avdi !

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