diff --git a/changelog.d/fix-windows-setup-apostrophe-home.md b/changelog.d/fix-windows-setup-apostrophe-home.md new file mode 100644 index 000000000..f1ee6dfc3 --- /dev/null +++ b/changelog.d/fix-windows-setup-apostrophe-home.md @@ -0,0 +1 @@ +**`./setup` on Windows now works when your home folder has an apostrophe in its name, such as `C:\Users\O'Brien`.** Git Bash converts `/c/...` paths to `C:/...` before it starts Bun, but it skips any path that holds an apostrophe, so setup stopped with `Module not found`. Setup now converts the paths it hands Bun itself. A Windows run of `./setup` with such a home folder now installs, starts the core skills and runs `/browse`. diff --git a/setup b/setup index b189eae8c..449493492 100755 --- a/setup +++ b/setup @@ -5,6 +5,12 @@ GSTACK_BUN_CONFIG=/dev/null; case "${OSTYPE:-}" in msys*|cygwin*) if command -v # Native Bun and Git cannot open a /c/x path: keep Git Bash's argument # conversion on for everything setup starts, even where a user turned it off (#4386). case "${OSTYPE:-}" in msys*|cygwin*) unset MSYS_NO_PATHCONV MSYS2_ARG_CONV_EXCL ;; esac +# That conversion skips any path holding an apostrophe (C:/Users/O'Brien), so a +# path handed to Bun goes through _bun_path, which spells it C:/... on Windows (#4386). +_bun_path() { + case "${OSTYPE:-}" in msys*|cygwin*) if command -v cygpath >/dev/null 2>&1; then cygpath -m "$1"; return; fi ;; esac + printf '%s\n' "$1" +} umask 077 # Restrict new files to owner-only (0o600 files, 0o700 dirs) # Heredoc delivery guard. bash 5.2+ writes a heredoc body <=64KiB through a @@ -609,7 +615,7 @@ EOF grep -q ' failed ' "$_RF_SUMMARY" && _RF_FAILED=1 if [ "$HARNESS_AUTO" -eq 1 ] && [ -d "$GSTACK_STATE_ROOT/harness" ]; then # Exit 4: every registration refreshed; a start hook this run turned on is not active (printed above). - bun --no-env-file --config="${GSTACK_BUN_CONFIG:-/dev/null}" "$SOURCE_GSTACK_DIR/bin/gstack-harness" refresh || [ "$?" -eq 4 ] || _RF_FAILED=1 + bun --no-env-file --config="${GSTACK_BUN_CONFIG:-/dev/null}" "$(_bun_path "$SOURCE_GSTACK_DIR/bin/gstack-harness")" refresh || [ "$?" -eq 4 ] || _RF_FAILED=1 fi echo "" if [ "$_RF_FAILED" -eq 1 ]; then @@ -1436,10 +1442,10 @@ if [ "$LOCAL_INSTALL" -eq 0 ]; then _CODEX_PREFLIGHT_SELECTED=1 fi fi -bun --no-env-file --config="${GSTACK_BUN_CONFIG:-/dev/null}" "$SOURCE_GSTACK_DIR/scripts/preflight-codex-overlap.ts" \ - --source "$SOURCE_GSTACK_DIR" --namespace "$CODEX_SKILLS" \ +bun --no-env-file --config="${GSTACK_BUN_CONFIG:-/dev/null}" "$(_bun_path "$SOURCE_GSTACK_DIR/scripts/preflight-codex-overlap.ts")" \ + --source "$(_bun_path "$SOURCE_GSTACK_DIR")" --namespace "$(_bun_path "$CODEX_SKILLS")" \ --selected "$_CODEX_PREFLIGHT_SELECTED" --local "$CODEX_REPO_LOCAL" \ - --windows "$IS_WINDOWS" --relocation "$HOME/.gstack/repos/gstack" || exit 1 + --windows "$IS_WINDOWS" --relocation "$(_bun_path "$HOME/.gstack/repos/gstack")" || exit 1 # ─── Resolve skill prefix preference ───────────────────────── # Priority: CLI flag > saved config > interactive prompt (or flat default for non-TTY) @@ -2475,7 +2481,7 @@ mkdir -p "$HOME/.gstack/projects" # gstack-config harness_start_hook; lib/harness-start-hook.ts). Exit 4 from # register means the skills are registered and the start hook is not active. # Nothing else in this run applies to them. -_gstack_harness() { bun --no-env-file --config="${GSTACK_BUN_CONFIG:-/dev/null}" "$SOURCE_GSTACK_DIR/bin/gstack-harness" "$@"; } +_gstack_harness() { bun --no-env-file --config="${GSTACK_BUN_CONFIG:-/dev/null}" "$(_bun_path "$SOURCE_GSTACK_DIR/bin/gstack-harness")" "$@"; } if [ -n "$INSTALL_HARNESS" ]; then _setup_arm_begin "$INSTALL_HARNESS" global "$INSTALL_HARNESS" _HARNESS_RC=0 @@ -4998,7 +5004,7 @@ _codex_hook_gate() { } _HOOKS_ON_BY_DEFAULT="" -SKILL_ROUTE_BIN="$SOURCE_GSTACK_DIR/bin/gstack-skill-route" +SKILL_ROUTE_BIN="$(_bun_path "$SOURCE_GSTACK_DIR/bin/gstack-skill-route")" SKILL_ROUTE_HOOK="$(_hook_command_path hosts/claude/hooks/skill-route-hook || true)" SKILL_CONTRACT_HOOK="$(_hook_command_path hosts/claude/hooks/skill-contract-stop-hook || true)" _skill_hook_gate skill_routing "$SKILL_ROUTING_MODE" GSTACK_SKILL_ROUTING UserPromptSubmit \ @@ -5043,7 +5049,7 @@ fi # gate deadline plus a margin (GST-1615): Claude Code kills a hook at its # timeout and lets the stop through unchecked. Empty if bun cannot run, which # omits the timeout and leaves Claude Code's default. -SKILL_GATE_HOOK_TIMEOUT="$(bun --no-env-file --config="${GSTACK_BUN_CONFIG:-/dev/null}" "$SOURCE_GSTACK_DIR/bin/gstack-skill-gates" hook-timeout 2>/dev/null || true)" +SKILL_GATE_HOOK_TIMEOUT="$(bun --no-env-file --config="${GSTACK_BUN_CONFIG:-/dev/null}" "$(_bun_path "$SOURCE_GSTACK_DIR/bin/gstack-skill-gates")" hook-timeout 2>/dev/null || true)" # /investigate and its gate are retired (GST-1717): an earlier setup's # registration is removed whatever investigate_gate says, and the KNOWN_HOOKS # rows stay so the old entries are still recognized as ours. diff --git a/test/setup-bun-path-apostrophe.test.ts b/test/setup-bun-path-apostrophe.test.ts new file mode 100644 index 000000000..ef9167513 --- /dev/null +++ b/test/setup-bun-path-apostrophe.test.ts @@ -0,0 +1,63 @@ +/** + * #4386: Git Bash converts a /c/... argument to C:/... before it starts a + * native program such as Bun, but it skips any argument holding an apostrophe. + * Under a home folder like C:\Users\O'Brien, setup handed Bun + * /c/Users/O'Brien/.claude/skills/gstack/scripts/preflight-codex-overlap.ts + * and Bun stopped with "Module not found". Setup now spells every path it + * hands Bun through _bun_path, which uses cygpath -m on Windows. A one-off + * windows-smoke run with an apostrophe in HOME proved the fix on a real + * Windows runner; this test guards the source and the helper on any OS. + */ +import { describe, expect, test } from 'bun:test'; +import { spawnSync } from 'node:child_process'; +import * as fs from 'node:fs'; +import * as os from 'node:os'; +import * as path from 'node:path'; + +const ROOT = path.resolve(import.meta.dir, '..'); +const SETUP = fs.readFileSync(path.join(ROOT, 'setup'), 'utf8'); +const BUN_START = 'bun --no-env-file --config="${GSTACK_BUN_CONFIG:-/dev/null}" '; + +function bunPathHelper(): string { + const start = SETUP.indexOf('_bun_path() {'); + expect(start).toBeGreaterThan(-1); + const end = SETUP.indexOf('\n}\n', start); + return SETUP.slice(start, end + 2); +} + +function runHelper(ostype: string, arg: string, stubDir?: string) { + const script = `OSTYPE=${ostype}\n${bunPathHelper()}\n_bun_path "$1"`; + const PATH = stubDir ? `${stubDir}${path.delimiter}${process.env.PATH}` : process.env.PATH; + return spawnSync('bash', ['-c', script, 'bash', arg], { encoding: 'utf8', timeout: 15_000, env: { ...process.env, PATH } }); +} + +describe('#4386: setup hands Bun paths it can open under an apostrophe home', () => { + test('every Bun start whose script is a variable path goes through _bun_path', () => { + const unwrapped = SETUP.split('\n').filter((line) => line.includes(`${BUN_START}"$`) && !line.includes(`${BUN_START}"$(_bun_path `)); + expect(unwrapped.map((line) => line.trim())).toEqual([`def="$(${BUN_START}"$SKILL_ROUTE_BIN" --default "$key" 2>/dev/null || true)"`]); + expect(SETUP).toContain('SKILL_ROUTE_BIN="$(_bun_path "$SOURCE_GSTACK_DIR/bin/gstack-skill-route")"'); + }); + + test('the Codex preflight gets converted --source, --namespace and --relocation paths', () => { + expect(SETUP).toContain('--source "$(_bun_path "$SOURCE_GSTACK_DIR")" --namespace "$(_bun_path "$CODEX_SKILLS")"'); + expect(SETUP).toContain('--relocation "$(_bun_path "$HOME/.gstack/repos/gstack")"'); + }); + + test.skipIf(process.platform === 'win32')('_bun_path leaves a path unchanged off Windows, apostrophe included', () => { + const r = runHelper('linux-gnu', "/home/o'brien/gstack/bin/gstack-harness"); + expect(r.status).toBe(0); + expect(r.stdout).toBe("/home/o'brien/gstack/bin/gstack-harness\n"); + }); + + test.skipIf(process.platform === 'win32')('_bun_path asks cygpath -m for the Windows spelling under Git Bash', () => { + const stubDir = fs.mkdtempSync(path.join(os.tmpdir(), 'bun-path-')); + try { + fs.writeFileSync(path.join(stubDir, 'cygpath'), '#!/bin/sh\n[ "$1" = -m ] || exit 9\nprintf "C:%s\\n" "$2"\n', { mode: 0o755 }); + const r = runHelper('msys', "/c/Users/O'Brien/.claude/skills/gstack/setup", stubDir); + expect(r.status).toBe(0); + expect(r.stdout).toBe("C:/c/Users/O'Brien/.claude/skills/gstack/setup\n"); + } finally { + fs.rmSync(stubDir, { recursive: true, force: true }); + } + }); +}); diff --git a/test/setup-hook-syntax-gate.test.ts b/test/setup-hook-syntax-gate.test.ts index 99d0f4c64..a90237de8 100644 --- a/test/setup-hook-syntax-gate.test.ts +++ b/test/setup-hook-syntax-gate.test.ts @@ -36,6 +36,7 @@ function slice(from: string, to: string | null): string { } const HOOK_SECTIONS = [ + slice('_bun_path() {', '\n}\n') + '\n}\n', slice('_setup_ready_line() {', '\n}\n') + '\n}\n', slice('CANONICAL_GSTACK_ROOT="$(gstack_canonical_claude_root)"', '# ─── GBrain detection'), slice('# 11. Plan-tune cathedral hook install', '# ─── Redact pre-push guard consent'), diff --git a/test/setup-skill-hooks-gate.test.ts b/test/setup-skill-hooks-gate.test.ts index c2156581d..ec090e970 100644 --- a/test/setup-skill-hooks-gate.test.ts +++ b/test/setup-skill-hooks-gate.test.ts @@ -22,6 +22,7 @@ import { const setupSrc = fs.readFileSync(path.join(ROOT, 'setup'), 'utf8'); const settingsHookSrc = fs.readFileSync(path.join(ROOT, 'bin', 'gstack-settings-hook'), 'utf8'); const gateFn = setupSrc.slice(setupSrc.indexOf('_skill_hook_gate() {'), setupSrc.indexOf('\n}\n', setupSrc.indexOf('_skill_hook_gate() {')) + 3); +const bunPathFn = setupSrc.slice(setupSrc.indexOf('_bun_path() {'), setupSrc.indexOf('\n}\n', setupSrc.indexOf('_bun_path() {')) + 3); const gateTimeoutLine = setupSrc.match(/^SKILL_GATE_HOOK_TIMEOUT=.*$/m)?.[0] ?? 'echo "setup has no SKILL_GATE_HOOK_TIMEOUT line" >&2; exit 9'; let state = '', home = ''; @@ -51,6 +52,7 @@ function gate(mode: string, env: Record = {}, noTeam = 0, root = 'SKILL_ROUTE_BIN="$2/bin/gstack-skill-route"', `IS_WINDOWS=0; NO_TEAM_MODE=${noTeam}`, 'SOURCE_GSTACK_DIR="$2"', + bunPathFn, gateTimeoutLine, gateFn, `_skill_hook_gate ${key} "$1" ${GATES[key]}`, @@ -429,13 +431,13 @@ describe('setup: the autoplan_gate switch (GST-1666) registers Stop, and PreTool describe('setup: both skill gates register the hook timeout lib/skill-gates.ts derives (GST-1615)', () => { test('source: one SKILL_GATE_HOOK_TIMEOUT from gstack-skill-gates hook-timeout, passed to both gates', () => { - expect(gateTimeoutLine).toBe('SKILL_GATE_HOOK_TIMEOUT="$(bun --no-env-file --config="${GSTACK_BUN_CONFIG:-/dev/null}" "$SOURCE_GSTACK_DIR/bin/gstack-skill-gates" hook-timeout 2>/dev/null || true)"'); + expect(gateTimeoutLine).toBe('SKILL_GATE_HOOK_TIMEOUT="$(bun --no-env-file --config="${GSTACK_BUN_CONFIG:-/dev/null}" "$(_bun_path "$SOURCE_GSTACK_DIR/bin/gstack-skill-gates")" hook-timeout 2>/dev/null || true)"'); expect(setupSrc).toContain('"$CEO_REVIEW_GATE_STOP" gstack-ceo-review-gate "$SKILL_GATE_HOOK_TIMEOUT" \\'); expect(setupSrc).not.toMatch(/gstack-(ceo-review|autoplan)-gate \d/); }); test('the setup line evaluates to HOOK_TIMEOUT_S', () => { - const r = spawnSync('bash', ['-c', `SOURCE_GSTACK_DIR="$1"\n${gateTimeoutLine}\nprintf %s "$SKILL_GATE_HOOK_TIMEOUT"`, 'gate', ROOT], { env: { PATH: process.env.PATH ?? '', HOME: home }, encoding: 'utf8', timeout: 30_000 }); + const r = spawnSync('bash', ['-c', `SOURCE_GSTACK_DIR="$1"\n${bunPathFn}${gateTimeoutLine}\nprintf %s "$SKILL_GATE_HOOK_TIMEOUT"`, 'gate', ROOT], { env: { PATH: process.env.PATH ?? '', HOME: home }, encoding: 'utf8', timeout: 30_000 }); expect(r.stdout).toBe(String(HOOK_TIMEOUT_S)); }); });