Skip to content

Turbocharge Claude Code, Cursor, Codex, Gemini & every coding agent: faster, cheaper, with contextual understanding specific to your codebase.

fix(trail): log only the file's name for a hook error on Windows too

Shrish Dwivedi84136d0ab297
diff --git a/src/claude/skills-hook-util.ts b/src/claude/skills-hook-util.ts
index bca3fec1..fa535b47 100644
--- a/src/claude/skills-hook-util.ts
+++ b/src/claude/skills-hook-util.ts
@@ -32,7 +32,8 @@ export function hookError(hook: string, e: unknown, env: NodeJS.ProcessEnv = pro
try {
const err = e instanceof Error ? e : new Error('not an Error');
const where = (err.stack ?? '').split('\n').slice(1).map((l) => /\(?([^\s()]+:\d+):\d+\)?$/.exec(l.trim())?.[1]).find(Boolean) ?? '';
- const file = (where.split('/').pop() ?? '').replace(/[^\w.:-]/g, '');
+ // The file's own name only: a Windows frame's folders are joined by '\', and they would name the person.
+ const file = (where.split(/[\\/]/).pop() ?? '').replace(/[^\w.:-]/g, '');
const path = trailPath(env, 'state', 'hook-errors.log');
appendPrivateLine(path, `${new Date().toISOString()} ${hook} ${err.name.replace(/[^\w]/g, '').slice(0, 40)}${file ? ` ${file}` : ''}`);
if (statSync(path).size > LOG_MAX) {
diff --git a/test/skills-contracts.test.ts b/test/skills-contracts.test.ts
index f0f2a757..4207f00b 100644
--- a/test/skills-contracts.test.ts
+++ b/test/skills-contracts.test.ts
@@ -14,6 +14,7 @@ import { tmpRepo } from "./helpers.js";
import {
appendPrivateLine, ensurePrivateDir, readJsonSafe, trailPath, withLock, writePrivate, writePrivateJson,
} from "../src/util/private-fs.js";
+import { hookError } from "../src/claude/skills-hook-util.js";
const SRC = join(dirname(fileURLToPath(import.meta.url)), "..", "src");
const POSIX = process.platform !== "win32";
@@ -324,6 +325,18 @@ test("trailPath joins under TRAIL_HOME or ~/.trail", () => {
assert.match(trailPath({}, "settings.json"), /[\\/]\.trail[\\/]settings\.json$/);
});
+test("hookError logs the file's own name, never its folders, from a Windows frame too", () => {
+ const root = tmpRepo("hook-error");
+ const env = { TRAIL_HOME: join(root, "trail") };
+ for (const frame of ["C:\\Users\\Priya Shah\\shop\\dist\\stop.js:12:5", "/home/priya/shop/dist/stop.js:12:5", "file:///C:/Users/priya/shop/dist/stop.js:12:5"]) {
+ const e = new TypeError("the prompt's words");
+ e.stack = `TypeError: the prompt's words\n at fold (${frame})`;
+ hookError("stop", e, env);
+ }
+ const lines = readFileSync(join(root, "trail", "state", "hook-errors.log"), "utf8").trim().split("\n");
+ assert.deepEqual(lines.map((l) => l.split(" ").slice(1).join(" ")), Array(3).fill("stop TypeError stop.js:12"));
+});
+
/* -------------------------------------------------------------------------- */
/* Static rules */
/* -------------------------------------------------------------------------- */