{"record":{"id":"23b9cde518a44fdf","repo":"JuliusBrussee/caveman","slug":"windows-command-shim-target-missing-script","errorCode":null,"errorMessage":"Windows command shim target missing: ${script}","messagePattern":"Windows command shim target missing: (.+?)","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"agents/delegate/portable-process.mjs","lineNumber":53,"sourceCode":"    const match = line.match(/\"%(?:dp0%|~dp0)\\\\([^\"\\r\\n]+\\.(?:cjs|mjs|js))\"\\s+%\\*/i)\n      || line.match(/\"([A-Za-z]:[\\\\/][^\"\\r\\n]+\\.(?:cjs|mjs|js))\"\\s+%\\*/i);\n    if (match) return match[1];\n  }\n  return null;\n}\n\nexport function portableInvocation(command, args, platform = process.platform, env = process.env) {\n  if (platform !== \"win32\") return { command, args: [...args] };\n  const executable = resolveWindowsCommand(command, env) ?? command;\n  if (!/\\.(?:cmd|bat)$/i.test(executable)) return { command: executable, args: [...args] };\n  const stat = statSync(executable);\n  if (!stat.isFile() || stat.size > 256 * 1024) throw new Error(`unsafe Windows command shim: ${executable}`);\n  const relativeScript = parseWindowsNodeShim(readFileSync(executable, \"utf8\"));\n  if (!relativeScript) throw new Error(`non-Node Windows command shim: ${executable}`);\n  const script = /^[A-Za-z]:[\\\\/]/.test(relativeScript)\n    ? relativeScript\n    : resolve(dirname(executable), ...relativeScript.split(/[\\\\/]+/));\n  if (!statSync(script).isFile()) throw new Error(`Windows command shim target missing: ${script}`);\n  return { command: process.execPath, args: [script, ...args] };\n}\n\nexport function delegateSpawnOptions(platform = process.platform) {\n  return {\n    detached: platform !== \"win32\",\n    windowsHide: true,\n  };\n}\n\nexport async function killProcessTree(\n  child,\n  platform = process.platform,\n  taskkill = spawnSync,\n  kill = process.kill,\n  graceMs = 1500,\n) {\n  if (!child?.pid) return;","sourceCodeStart":35,"sourceCodeEnd":71,"githubUrl":"https://github.com/JuliusBrussee/caveman/blob/3ee70a102609e550bd2e68004bf5990a9341c851/agents/delegate/portable-process.mjs#L35-L71","documentation":"After extracting the script path from a Windows Node .cmd shim, `portableInvocation` stats the target .js file to confirm it exists before re-spawning node with it. This error means the shim exists but the script it points at is missing, so the delegation cannot proceed safely.","triggerScenarios":"A .cmd shim whose relative target (resolved against the shim's dirname if not absolute) fails statSync's isFile check — target deleted, moved, or the shim was written with a wrong relative path.","commonSituations":"node_modules partially pruned (npm prune or manual deletion) leaving orphaned .bin shims; package upgrades that moved the entry script; a shim copied to another directory so %~dp0-relative resolution no longer matches; broken install after a crashed npm run.","solutions":["Reinstall the owning package: npm ci (or npm install) to restore the missing script","Verify the path printed in the error and check whether the file exists at that exact location","Remove the stale .cmd shim from node_modules/.bin if its package was uninstalled","If the shim was copied/moved, restore it next to its target or use an absolute script path"],"exampleFix":"// before\nspawn(\"lefthook\", [\"install\"], opts); // stale shim, target gone\n// after\nexecSync(\"npm ci\"); // restore script, then spawn again\nspawn(\"lefthook\", [\"install\"], opts);","handlingStrategy":"validation","validationCode":"import { statSync } from \"node:fs\";\nimport { dirname, resolve } from \"node:path\";\nfunction shimTargetExists(shimPath, targetRelative) {\n  const script = /^[A-Za-z]:[\\\\/]/.test(targetRelative)\n    ? targetRelative\n    : resolve(dirname(shimPath), ...targetRelative.split(/[\\\\/]+/));\n  try { return statSync(script).isFile(); } catch { return false; }\n}","typeGuard":null,"tryCatchPattern":"try {\n  const inv = portableInvocation(command, args);\n  spawn(inv.command, inv.args, opts);\n} catch (e) {\n  if (String(e.message).startsWith(\"Windows command shim target missing:\")) {\n    console.error(\"Stale shim — run npm ci to restore the package entry script\");\n  } else throw e;\n}","preventionTips":["Run npm ci in CI rather than reusing pruned node_modules","Don't copy .bin shims out of node_modules — their targets are resolved relative to the shim","After upgrades, clean installs fix moved entry scripts","Check that antivirus/prune tools aren't deleting .js files from node_modules"],"tags":["windows","spawn","file-not-found"],"backgroundTag":"file-not-found","analyzedSha":"3ee70a102609e550bd2e68004bf5990a9341c851","analyzedAt":"2026-09-20T15:53:39.229Z","contentChangedAt":"2026-09-20T15:53:39.229Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}