Skip to content

Generated hooks fail during git worktree add — vendor/bin/captainhook not found #317

@delolmo

Description

@delolmo

Description

The hook scripts generated by CaptainHook unconditionally call vendor/bin/captainhook without checking that the binary exists. This causes git worktree add to fail, because the post-checkout hook fires in the new worktree before composer install has been run.

Steps to reproduce

  1. Have a project with CaptainHook installed and hooks active (including post-checkout)
  2. Run git worktree add /tmp/my-worktree HEAD

Expected behavior

The worktree is created successfully. The hook either skips execution or exits gracefully when vendor/bin/captainhook is not available.

Actual behavior

The hook fails with:

Preparing worktree (new branch 'worktree-xxx')
.git/hooks/post-checkout: 17: vendor/bin/captainhook: not found

The worktree creation is aborted.

Root cause

The generated hook template calls vendor/bin/captainhook unconditionally:

vendor/bin/captainhook $INTERACTIVE --configuration=captainhook.json --input="$input" hook:post-checkout "$@"

In a fresh worktree, vendor/ does not exist yet — composer install has not run.

Suggested fix

Guard the invocation with an existence check:

[ -x vendor/bin/captainhook ] && vendor/bin/captainhook $INTERACTIVE --configuration=captainhook.json --input="$input" hook:post-checkout "$@"

This affects all generated hooks, not just post-checkout, since any hook could theoretically fire in a context where vendor/ is absent (e.g., sparse checkouts, CI environments that install hooks before dependencies).

Environment

  • CaptainHook 5.25.11
  • Git 2.49.0
  • PHP 8.5.0

Metadata

Metadata

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions