|
| 1 | +/** |
| 2 | + * Regression guard for issue #1838: .claude/hooks/track-edits.sh (PostToolUse |
| 3 | + * hook for Edit/Write) resolved PROJECT_DIR from the hook process's own |
| 4 | + * ambient cwd (`git rev-parse --show-toplevel` with no `-C`). Edit/Write tool |
| 5 | + * calls carry only an absolute `file_path` with no associated "current |
| 6 | + * directory" state, so the hook's ambient cwd is not guaranteed to match the |
| 7 | + * worktree that actually owns the edited file — especially in worktree |
| 8 | + * sessions where the ambient cwd can lag behind interleaved Bash `cd` calls. |
| 9 | + * When it doesn't match, the log entry lands in the wrong worktree's |
| 10 | + * `.claude/session-edits.log` (or is silently dropped as a `..`-relative |
| 11 | + * path), and guard-git.sh later blocks a legitimate commit claiming the file |
| 12 | + * was "NOT edited in this session". |
| 13 | + * |
| 14 | + * The fix derives PROJECT_DIR from the edited file's own path |
| 15 | + * (`git -C "<dir containing file_path>" rev-parse --show-toplevel`) instead |
| 16 | + * of the ambient cwd — mirroring the `-C "$WORK_DIR"` pattern guard-git.sh |
| 17 | + * already uses on the read side of this same check. |
| 18 | + */ |
| 19 | +import { execFileSync } from 'node:child_process'; |
| 20 | +import fs from 'node:fs'; |
| 21 | +import os from 'node:os'; |
| 22 | +import path from 'node:path'; |
| 23 | +import { fileURLToPath } from 'node:url'; |
| 24 | +import { afterEach, beforeEach, describe, expect, it } from 'vitest'; |
| 25 | + |
| 26 | +const __dirname = path.dirname(fileURLToPath(import.meta.url)); |
| 27 | +const REPO_ROOT = path.resolve(__dirname, '..', '..'); |
| 28 | +const HOOK_PATH = path.join(REPO_ROOT, '.claude', 'hooks', 'track-edits.sh'); |
| 29 | + |
| 30 | +function initRepo(dir: string): void { |
| 31 | + fs.mkdirSync(dir, { recursive: true }); |
| 32 | + execFileSync('git', ['init', '-q'], { cwd: dir }); |
| 33 | + execFileSync('git', ['config', 'user.email', 'test@example.com'], { cwd: dir }); |
| 34 | + execFileSync('git', ['config', 'user.name', 'Test'], { cwd: dir }); |
| 35 | +} |
| 36 | + |
| 37 | +function runHook(filePath: string, cwd: string): void { |
| 38 | + const toolInput = JSON.stringify({ tool_input: { file_path: filePath } }); |
| 39 | + execFileSync('bash', [HOOK_PATH], { cwd, input: toolInput }); |
| 40 | +} |
| 41 | + |
| 42 | +describe("track-edits.sh resolves the log to the edited file's own worktree", () => { |
| 43 | + let tmpRoot: string; |
| 44 | + |
| 45 | + beforeEach(() => { |
| 46 | + tmpRoot = fs.realpathSync(fs.mkdtempSync(path.join(os.tmpdir(), 'track-edits-test-'))); |
| 47 | + }); |
| 48 | + |
| 49 | + afterEach(() => { |
| 50 | + fs.rmSync(tmpRoot, { recursive: true, force: true }); |
| 51 | + }); |
| 52 | + |
| 53 | + it('logs to the target worktree even when the hook process cwd is a different worktree', () => { |
| 54 | + const ambientRepo = path.join(tmpRoot, 'ambient-repo'); |
| 55 | + const targetRepo = path.join(tmpRoot, 'target-worktree'); |
| 56 | + initRepo(ambientRepo); |
| 57 | + initRepo(targetRepo); |
| 58 | + |
| 59 | + const targetFile = path.join(targetRepo, 'src', 'thing.ts'); |
| 60 | + fs.mkdirSync(path.dirname(targetFile), { recursive: true }); |
| 61 | + fs.writeFileSync(targetFile, '// content\n'); |
| 62 | + |
| 63 | + // The hook process's ambient cwd is a DIFFERENT git worktree than the one |
| 64 | + // that owns the edited file — this is the racy condition from #1838. |
| 65 | + runHook(targetFile, ambientRepo); |
| 66 | + |
| 67 | + const targetLog = path.join(targetRepo, '.claude', 'session-edits.log'); |
| 68 | + const ambientLog = path.join(ambientRepo, '.claude', 'session-edits.log'); |
| 69 | + |
| 70 | + expect(fs.existsSync(targetLog)).toBe(true); |
| 71 | + expect(fs.readFileSync(targetLog, 'utf8')).toMatch(/ src\/thing\.ts$/m); |
| 72 | + expect(fs.existsSync(ambientLog)).toBe(false); |
| 73 | + }); |
| 74 | + |
| 75 | + it('walks up to the nearest existing ancestor when Write targets a not-yet-created nested directory', () => { |
| 76 | + const targetRepo = path.join(tmpRoot, 'target-worktree-2'); |
| 77 | + initRepo(targetRepo); |
| 78 | + // a/b/c does not exist yet — simulates the Write tool creating new nested dirs. |
| 79 | + const nestedFile = path.join(targetRepo, 'a', 'b', 'c', 'new-file.ts'); |
| 80 | + |
| 81 | + // Ambient cwd is entirely outside any git repo. |
| 82 | + runHook(nestedFile, tmpRoot); |
| 83 | + |
| 84 | + const targetLog = path.join(targetRepo, '.claude', 'session-edits.log'); |
| 85 | + expect(fs.existsSync(targetLog)).toBe(true); |
| 86 | + expect(fs.readFileSync(targetLog, 'utf8')).toMatch(/ a\/b\/c\/new-file\.ts$/m); |
| 87 | + }); |
| 88 | + |
| 89 | + it('logs a clean relative path when the edited file is reached through a symlink to the worktree', () => { |
| 90 | + // Reproduces (on any OS) the same class of bug as Windows' 8.3 short-name |
| 91 | + // aliases (e.g. "runneradmin" vs "RUNNER~1"): two different, valid string |
| 92 | + // forms resolve to the identical real directory. `git rev-parse |
| 93 | + // --show-toplevel` returns the real (symlink-resolved) path, so without |
| 94 | + // canonicalizing FILE_PATH's directory the same way before computing the |
| 95 | + // relative path, path.relative sees two unrelated-looking trees and |
| 96 | + // produces a long, useless chain of '../' segments instead of a clean |
| 97 | + // relative path. |
| 98 | + const realRepo = path.join(tmpRoot, 'real-worktree'); |
| 99 | + const symlinkRepo = path.join(tmpRoot, 'symlink-to-worktree'); |
| 100 | + initRepo(realRepo); |
| 101 | + fs.mkdirSync(path.join(realRepo, 'src'), { recursive: true }); |
| 102 | + fs.writeFileSync(path.join(realRepo, 'src', 'thing.ts'), '// content\n'); |
| 103 | + fs.symlinkSync(realRepo, symlinkRepo, 'dir'); |
| 104 | + |
| 105 | + const targetFile = path.join(symlinkRepo, 'src', 'thing.ts'); |
| 106 | + runHook(targetFile, tmpRoot); |
| 107 | + |
| 108 | + const realLog = path.join(realRepo, '.claude', 'session-edits.log'); |
| 109 | + expect(fs.existsSync(realLog)).toBe(true); |
| 110 | + const content = fs.readFileSync(realLog, 'utf8'); |
| 111 | + expect(content).toMatch(/ src\/thing\.ts$/m); |
| 112 | + expect(content).not.toContain('..'); |
| 113 | + }); |
| 114 | +}); |
| 115 | + |
| 116 | +describe('track-edits.sh Windows path normalization', () => { |
| 117 | + // dirname (GNU/BSD coreutils) only splits on '/'. On Windows, Edit/Write's |
| 118 | + // file_path arrives backslash-delimited (e.g. from Node's path.join), which |
| 119 | + // made dirname silently no-op and return "." — undetectable on a POSIX test |
| 120 | + // runner via the integration tests above, since they only ever construct |
| 121 | + // paths with the host OS's own separator. Exercise the normalization |
| 122 | + // snippet directly (extracted from the real file, not duplicated by hand) |
| 123 | + // so a regression here is caught on any host OS. |
| 124 | + function extractNormalizationSnippet(hookPath: string): string { |
| 125 | + const src = fs.readFileSync(hookPath, 'utf8'); |
| 126 | + const start = src.indexOf('if printf'); |
| 127 | + const end = src.indexOf('\nfi', start); |
| 128 | + if (start === -1 || end === -1) { |
| 129 | + throw new Error(`could not locate normalization if-block in ${hookPath}`); |
| 130 | + } |
| 131 | + return src.slice(start, end + '\nfi'.length); |
| 132 | + } |
| 133 | + |
| 134 | + // Passed via env var, not argv: Windows command-line argument marshalling |
| 135 | + // has its own backslash-escaping rules that don't apply to environment |
| 136 | + // variables, and the real hook never receives FILE_PATH as a CLI arg |
| 137 | + // either (it comes from JSON on stdin) — so this keeps the test's |
| 138 | + // transport mechanism from confounding what's actually being verified. |
| 139 | + function normalize(hookPath: string, filePath: string): string { |
| 140 | + const snippet = extractNormalizationSnippet(hookPath); |
| 141 | + const script = `${snippet}\nprintf '%s' "$FILE_PATH"`; |
| 142 | + return execFileSync('bash', ['-c', script], { |
| 143 | + env: { ...process.env, FILE_PATH: filePath }, |
| 144 | + }).toString(); |
| 145 | + } |
| 146 | + |
| 147 | + const DOCS_HOOK_PATH = path.join( |
| 148 | + REPO_ROOT, |
| 149 | + 'docs', |
| 150 | + 'examples', |
| 151 | + 'claude-code-hooks', |
| 152 | + 'track-edits.sh', |
| 153 | + ); |
| 154 | + |
| 155 | + it.each([ |
| 156 | + ['live hook', HOOK_PATH], |
| 157 | + ['docs example', DOCS_HOOK_PATH], |
| 158 | + ])('%s: converts a Windows drive-letter path to forward slashes', (_label, hookPath) => { |
| 159 | + expect(normalize(hookPath, 'C:\\Users\\dev\\project\\src\\thing.ts')).toBe( |
| 160 | + 'C:/Users/dev/project/src/thing.ts', |
| 161 | + ); |
| 162 | + }); |
| 163 | + |
| 164 | + it.each([ |
| 165 | + ['live hook', HOOK_PATH], |
| 166 | + ['docs example', DOCS_HOOK_PATH], |
| 167 | + ])('%s: leaves a POSIX path with a literal backslash in the filename untouched', (_label, hookPath) => { |
| 168 | + expect(normalize(hookPath, '/tmp/proj/weird\\name.ts')).toBe('/tmp/proj/weird\\name.ts'); |
| 169 | + }); |
| 170 | +}); |
0 commit comments