From 86ce012c5e21b5cb4208ee17915994f7992f96f2 Mon Sep 17 00:00:00 2001 From: garrytan Date: Thu, 8 Oct 2026 15:27:22 +0000 Subject: [PATCH] feat(windows): setup.ps1 checks prerequisites and runs ./setup in Git Bash A PowerShell entry point for Windows users. It checks that Git for Windows, Bun and Node.js are on PATH (printing the winget command for each missing one and changing nothing), finds Git Bash from git.exe rather than a PATH bash that may be WSL's, and runs ./setup from the checkout with every argument passed as its own argv entry to a constant bash script. No argument is interpolated into shell source. ./setup itself is unchanged. Co-authored-by: Jim McKeeth --- README.md | 2 ++ setup.ps1 | 54 ++++++++++++++++++++++++++++++ test/setup-ps1.test.ts | 75 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 131 insertions(+) create mode 100644 setup.ps1 create mode 100644 test/setup-ps1.test.ts diff --git a/README.md b/README.md index e2c99f362..4de9e2f5d 100644 --- a/README.md +++ b/README.md @@ -708,6 +708,8 @@ types into that element; bare `browse type ` types into whatever has focus **Windows users:** gstack works on Windows 11 via Git Bash or WSL. Aside is macOS-only, so on Windows (and Linux) the browser skills, `/make-pdf`, and `/diagram` always use gstack's bundled browser. Node.js is required in addition to Bun — Bun has a known bug with Playwright's pipe transport on Windows ([bun#4253](https://github.com/oven-sh/bun/issues/4253)). The browse server automatically falls back to Node.js. Make sure both `bun` and `node` are on your PATH. Native `/cso` additionally requires Windows PowerShell and Visual Studio 2022 Build Tools with the Desktop development with C++ workload; setup leaves that skill explicitly unavailable when they are absent. +From PowerShell, `.\setup.ps1` (same arguments as `./setup`) checks that Git for Windows, Bun and Node.js are on PATH, prints the `winget` command for any that are missing, and otherwise runs `./setup` in Git Bash. + On Windows without Developer Mode (MSYS2 / Git Bash), `setup` falls back to file copies instead of symlinks because `ln -snf` produces frozen copies that don't refresh on `git pull`. **Re-run `cd ~/.claude/skills/gstack && ./setup` after every `git pull`** so your skill files match the repo. `setup` prints a one-line note reminding you. Unix and WSL keep symlinks and don't need the re-run. **Chromium install failed or hung during `./setup`?** The bundled browser is diff --git a/setup.ps1 b/setup.ps1 new file mode 100644 index 000000000..ab3b82386 --- /dev/null +++ b/setup.ps1 @@ -0,0 +1,54 @@ +#Requires -Version 5.1 +<# +.SYNOPSIS + Windows entry point for gstack's ./setup. + +.DESCRIPTION + Checks that Git for Windows, Bun and Node.js are on PATH, then runs ./setup + from this directory in Git Bash. Every argument goes to ./setup unchanged, + as separate arguments; none is pasted into shell source. + + .\setup.ps1 + .\setup.ps1 --host codex + + Set GSTACK_GIT_BASH to a bash.exe to use instead of Git for Windows' own. +#> +Set-StrictMode -Version Latest +$ErrorActionPreference = 'Stop' + +$missing = @( + @{ Cmd = 'git'; Name = 'Git for Windows'; Id = 'Git.Git' }, + @{ Cmd = 'bun'; Name = 'Bun'; Id = 'Oven-sh.Bun' }, + @{ Cmd = 'node'; Name = 'Node.js'; Id = 'OpenJS.NodeJS.LTS' } +) | Where-Object { -not (Get-Command $_.Cmd -CommandType Application -ErrorAction SilentlyContinue) } +if ($missing) { + Write-Host 'gstack setup needs these on PATH first:' + foreach ($tool in $missing) { Write-Host (' {0}: winget install --id {1} -e' -f $tool.Name, $tool.Id) } + Write-Host 'Install them, open a new PowerShell window and run .\setup.ps1 again. Nothing was installed or changed.' + exit 1 +} + +# Git Bash, found from git.exe (Git\cmd, Git\bin or Git\mingw64\bin). Never a +# bare `bash` from PATH: on Windows that is often WSL's, with a different home. +$bash = $env:GSTACK_GIT_BASH +if (-not $bash) { + $dir = Split-Path (Get-Command git -CommandType Application | Select-Object -First 1).Source + for ($i = 0; $i -lt 3 -and $dir; $i++) { + $candidate = Join-Path $dir 'bin\bash.exe' + if (Test-Path -LiteralPath $candidate -PathType Leaf) { $bash = $candidate; break } + $dir = Split-Path $dir + } +} +if (-not $bash -or -not (Test-Path -LiteralPath $bash -PathType Leaf)) { + Write-Host 'Git Bash (bash.exe) was not found next to git. Reinstall Git for Windows, or set GSTACK_GIT_BASH to its bash.exe.' + exit 1 +} + +Push-Location -LiteralPath $PSScriptRoot +try { + & $bash -c 'exec ./setup "$@"' setup @args + $code = $LASTEXITCODE +} finally { + Pop-Location +} +exit $code diff --git a/test/setup-ps1.test.ts b/test/setup-ps1.test.ts new file mode 100644 index 000000000..abd1beff6 --- /dev/null +++ b/test/setup-ps1.test.ts @@ -0,0 +1,75 @@ +/** + * setup.ps1: the Windows entry point checks Git for Windows, Bun and Node.js, + * then runs ./setup in Git Bash with every argument passed as its own argv + * entry, never pasted into shell source. The behavioral cases copy the script + * next to a stub ./setup that prints its argv and run it under pwsh. + */ +import { describe, test, expect, afterEach } from 'bun:test'; +import * as fs from 'fs'; +import * as os from 'os'; +import * as path from 'path'; +import { spawnSync } from 'child_process'; + +const SCRIPT = path.resolve(import.meta.dir, '..', 'setup.ps1'); +const SOURCE = fs.readFileSync(SCRIPT, 'utf-8'); +const PWSH = Bun.which('pwsh'); +const BASH = Bun.which('bash'); + +const dirs: string[] = []; +afterEach(() => { + for (const d of dirs.splice(0)) fs.rmSync(d, { recursive: true, force: true }); +}); + +function stubCheckout(): string { + const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'setup-ps1-')); + dirs.push(dir); + fs.copyFileSync(SCRIPT, path.join(dir, 'setup.ps1')); + fs.writeFileSync(path.join(dir, 'setup'), '#!/usr/bin/env bash\nprintf \'cwd=%s\\n\' "$PWD"\nfor a in "$@"; do printf \'arg=[%s]\\n\' "$a"; done\nexit 7\n', { mode: 0o755 }); + return dir; +} + +function runPs1(dir: string, args: string[], env: Record = {}) { + const r = spawnSync(PWSH!, ['-NoProfile', '-NonInteractive', '-File', path.join(dir, 'setup.ps1'), ...args], { + cwd: os.tmpdir(), + encoding: 'utf-8', + env: { ...process.env, ...(process.platform === 'win32' ? {} : { GSTACK_GIT_BASH: BASH! }), ...env }, + timeout: 60_000, + }); + return { code: r.status, out: (r.stdout || '') + (r.stderr || '') }; +} + +describe('setup.ps1', () => { + test('passes arguments as an array to a constant bash script', () => { + expect(SOURCE).toContain(`& $bash -c 'exec ./setup "$@"' setup @args`); + expect(SOURCE).not.toMatch(/-join|Invoke-Expression|\biex\b|git-bash\.exe/); + expect([...SOURCE].every((c) => c.charCodeAt(0) < 128)).toBe(true); + }); + + test.skipIf(!PWSH || !BASH)('forwards every argument literally from the script directory and keeps the exit code', () => { + const dir = stubCheckout(); + const args = ['--host', 'codex', 'two words', '$(touch PWNED)', "it's", '"quoted"', ';echo injected', '`whoami`']; + const { code, out } = runPs1(dir, args); + expect(code).toBe(7); + const lines = out.split(/\r?\n/); + expect(lines.filter((l) => l.startsWith('arg=['))).toEqual(args.map((a) => `arg=[${a}]`)); + expect(lines.find((l) => l.startsWith('cwd='))).toContain(path.basename(dir)); + expect(out).not.toContain('injected\n'); + expect(fs.existsSync(path.join(dir, 'PWNED'))).toBe(false); + expect(fs.existsSync(path.join(os.tmpdir(), 'PWNED'))).toBe(false); + }); + + test.skipIf(!PWSH || !BASH || process.platform === 'win32')('a missing prerequisite names its winget command and runs nothing', () => { + const dir = stubCheckout(); + const bin = fs.mkdtempSync(path.join(os.tmpdir(), 'setup-ps1-bin-')); + dirs.push(bin); + for (const tool of ['git', 'node']) { + const real = Bun.which(tool); + if (real) fs.symlinkSync(real, path.join(bin, tool)); + } + const { code, out } = runPs1(dir, ['--host', 'codex'], { PATH: bin }); + expect(code).toBe(1); + expect(out).toContain('Bun: winget install --id Oven-sh.Bun -e'); + expect(out).not.toContain('Git for Windows:'); + expect(out).not.toContain('arg=['); + }); +});