claude-code - 💡(How to fix) Fix WorktreeCreate hook payload change causes undefined worktrees on Windows [1 comments, 1 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
anthropics/claude-code#46223Fetched 2026-04-11 06:25:54
View on GitHub
Comments
1
Participants
1
Timeline
4
Reactions
0
Participants
Timeline (top)
labeled ×3commented ×1

Claude Code 2.1.98 appears to send a different WorktreeCreate hook payload than the older sample hook implementation expects. On Windows, a legacy hook that reads worktree_path / branch_name / base_path can end up creating a real worktree named undefined and then throwing when it tries to write undefined to stdout.

The failure I saw was:

WorktreeCreate hook failed: node "C:/Users/panit/.claude/hooks/worktree-create.js": Failed to create worktree: The "chunk" argument must be of type string or an instance of Buffer, TypedArray, or DataView. Received undefined

Error Message

  • Claude tries to create an agent worktree.
  • The legacy hook reads missing fields as undefined.
  • git worktree add still runs, which can register a real worktree at a path like <repo>/undefined on branch undefined.
  • The hook then crashes on process.stdout.write(worktreePath) because worktreePath is undefined.
  • Claude falls back to sequential execution with no worktrees.

Root Cause

Observed behavior

  • Claude tries to create an agent worktree.
  • The legacy hook reads missing fields as undefined.
  • git worktree add still runs, which can register a real worktree at a path like <repo>/undefined on branch undefined.
  • The hook then crashes on process.stdout.write(worktreePath) because worktreePath is undefined.
  • Claude falls back to sequential execution with no worktrees.

Code Example

WorktreeCreate hook failed: node "C:/Users/panit/.claude/hooks/worktree-create.js": Failed to create worktree: The "chunk" argument must be of type string or an instance of Buffer, TypedArray, or DataView. Received undefined

---

const worktreePath = data.worktree_path;
const branchName = data.branch_name;
const basePath = data.base_path;

execSync(`git worktree add "${worktreePath}" -b "${branchName}"`, { cwd: basePath });
process.stdout.write(worktreePath);
RAW_BUFFERClick to expand / collapse

Summary

Claude Code 2.1.98 appears to send a different WorktreeCreate hook payload than the older sample hook implementation expects. On Windows, a legacy hook that reads worktree_path / branch_name / base_path can end up creating a real worktree named undefined and then throwing when it tries to write undefined to stdout.

The failure I saw was:

WorktreeCreate hook failed: node "C:/Users/panit/.claude/hooks/worktree-create.js": Failed to create worktree: The "chunk" argument must be of type string or an instance of Buffer, TypedArray, or DataView. Received undefined

Environment

  • Claude Code: 2.1.98
  • OS: Windows
  • Hook type: WorktreeCreate command hook

Observed behavior

  • Claude tries to create an agent worktree.
  • The legacy hook reads missing fields as undefined.
  • git worktree add still runs, which can register a real worktree at a path like <repo>/undefined on branch undefined.
  • The hook then crashes on process.stdout.write(worktreePath) because worktreePath is undefined.
  • Claude falls back to sequential execution with no worktrees.

Expected behavior

  • The hook contract should be clear and stable, or the sample hook should be updated for the current payload.
  • A current-format hook should be able to read cwd and name, create the worktree, and print the absolute worktree path.
  • Stale sample hooks should not silently create undefined branches or worktree directories.

Likely root cause

I verified that a working hook for 2.1.98 needs to treat the current payload as cwd + name for create, with legacy fallbacks for older field names.

Minimal fix that worked locally

  • Read cwd and name for WorktreeCreate.
  • Keep fallbacks for base_path, branch_name, and worktree_path.
  • Resolve repo root with git rev-parse --show-toplevel.
  • Create the worktree at <repo>/.claude/worktrees/<name>.
  • If the branch already exists, run git worktree add <path> <branch>.
  • Otherwise, run git worktree add <path> -b <branch>.
  • Print only the absolute created path to stdout.

Repro notes

The legacy hook shape that broke looked like this:

const worktreePath = data.worktree_path;
const branchName = data.branch_name;
const basePath = data.base_path;

execSync(`git worktree add "${worktreePath}" -b "${branchName}"`, { cwd: basePath });
process.stdout.write(worktreePath);

Thanks � happy to provide the full working replacement script if useful.

Working replacement hooks

Public gist: https://gist.github.com/pithpusher/8596767975959b8ceac96574b7dd4285

extent analysis

TL;DR

Update the WorktreeCreate hook to read cwd and name instead of worktree_path, branch_name, and base_path to fix the issue with Claude Code 2.1.98.

Guidance

  • Verify that the hook is using the correct payload fields by checking the cwd and name properties.
  • Update the hook to use the new fields and provide fallbacks for older field names to ensure compatibility.
  • Use git rev-parse --show-toplevel to resolve the repository root and create the worktree at the correct path.
  • Print only the absolute created path to stdout to avoid errors.

Example

const cwd = data.cwd;
const name = data.name;
const basePath = data.base_path || data.cwd;
const branchName = data.branch_name || data.name;
const worktreePath = data.worktree_path;

// Fallbacks for older field names
const repoRoot = execSync('git rev-parse --show-toplevel', { cwd: basePath }).toString().trim();
const worktreeDir = `${repoRoot}/.claude/worktrees/${name}`;

// Create the worktree
if (execSync(`git rev-parse --quiet --verify ${branchName}`, { cwd: repoRoot }).toString().trim() !== '') {
  execSync(`git worktree add "${worktreeDir}" ${branchName}`, { cwd: repoRoot });
} else {
  execSync(`git worktree add "${worktreeDir}" -b ${branchName}`, { cwd: repoRoot });
}

// Print the absolute created path to stdout
process.stdout.write(worktreeDir);

Notes

The provided example is based on the working replacement script mentioned in the issue. It's recommended to use the public gist (https://gist.github.com/pithpusher/8596767975959b8ceac96574b7dd4285) for the full working replacement script.

Recommendation

Apply the workaround by updating the WorktreeCreate hook to use the new payload fields and providing fallbacks for older field names. This will ensure compatibility with Claude Code 2.1.98 and prevent errors when creating worktrees.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

FAQ

Expected behavior

  • The hook contract should be clear and stable, or the sample hook should be updated for the current payload.
  • A current-format hook should be able to read cwd and name, create the worktree, and print the absolute worktree path.
  • Stale sample hooks should not silently create undefined branches or worktree directories.

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING