|
| 1 | +/** |
| 2 | + * Windows end-to-end check for ripgrep binary resolution. |
| 3 | + * |
| 4 | + * Reproduces the condition behind the outage in |
| 5 | + * https://github.com/AltimateAI/altimate-code/issues/1072: a cold cache on a Windows machine |
| 6 | + * where PowerShell cannot be resolved from PATH. The old implementation extracted ripgrep's zip |
| 7 | + * by shelling out to `powershell.exe -Command Expand-Archive`, so cross-spawn fell back to |
| 8 | + * `cmd.exe /d /s /c` and the extraction died with "is not recognized as an internal or external |
| 9 | + * command" — which `Effect.cached` then replayed for the rest of the session. |
| 10 | + * |
| 11 | + * This performs a real download and a real extraction, then executes the resulting binary. It is |
| 12 | + * deliberately not a unit test: the point is to exercise the actual filesystem, the actual archive |
| 13 | + * and the actual process launch on a real Windows host. |
| 14 | + * |
| 15 | + * Run: bun run script/windows-ripgrep-e2e.ts (from packages/core — `effect` resolves there) |
| 16 | + */ |
| 17 | +import { execFileSync } from "node:child_process" |
| 18 | +import { mkdtempSync, existsSync, readdirSync, statSync, rmSync } from "node:fs" |
| 19 | +import { tmpdir } from "node:os" |
| 20 | +import path from "node:path" |
| 21 | + |
| 22 | +function fail(message: string): never { |
| 23 | + console.error(`FAIL: ${message}`) |
| 24 | + process.exit(1) |
| 25 | +} |
| 26 | + |
| 27 | +function ok(message: string) { |
| 28 | + console.log(`ok ${message}`) |
| 29 | +} |
| 30 | + |
| 31 | +if (process.platform !== "win32") fail(`this check only means anything on Windows (got ${process.platform})`) |
| 32 | + |
| 33 | +// Cold cache: point the XDG cache root at a throwaway directory *before* importing anything that |
| 34 | +// reads it, since Global computes its paths at module load. |
| 35 | +const cacheRoot = mkdtempSync(path.join(tmpdir(), "rg-e2e-")) |
| 36 | +process.env.XDG_CACHE_HOME = cacheRoot |
| 37 | +process.env.LOCALAPPDATA = cacheRoot |
| 38 | + |
| 39 | +// Remove every PATH entry that could provide PowerShell. System32 itself is kept so cmd.exe and |
| 40 | +// the rest of Windows still work — this simulates the locked-down/sanitized-PATH machines in the |
| 41 | +// telemetry, not a broken OS. |
| 42 | +const originalPath = process.env.PATH ?? process.env.Path ?? "" |
| 43 | +const stripped = originalPath |
| 44 | + .split(path.delimiter) |
| 45 | + .filter((entry) => entry && !/powershell/i.test(entry)) |
| 46 | + .join(path.delimiter) |
| 47 | +process.env.PATH = stripped |
| 48 | +process.env.Path = stripped |
| 49 | + |
| 50 | +const { which } = await import("../src/util/which") |
| 51 | + |
| 52 | +// Guard against a vacuous run: if PowerShell is still resolvable, this proves nothing. |
| 53 | +const ps = which("powershell.exe") |
| 54 | +const pwsh = which("pwsh.exe") |
| 55 | +if (ps || pwsh) fail(`PowerShell is still resolvable (${ps ?? pwsh}) — the scenario was not reproduced`) |
| 56 | +ok("PowerShell is not resolvable on PATH (failure condition reproduced)") |
| 57 | + |
| 58 | +// Also assert the binary really is absent, so we exercise download + extract rather than a cache hit. |
| 59 | +const { Global } = await import("../src/global") |
| 60 | +const target = path.join(Global.Path.bin, "rg.exe") |
| 61 | +if (existsSync(target)) fail(`expected a cold cache but ${target} already exists`) |
| 62 | +ok(`cold cache at ${Global.Path.bin}`) |
| 63 | + |
| 64 | +const { Effect } = await import("effect") |
| 65 | +const { RipgrepBinary } = await import("../src/ripgrep/binary") |
| 66 | + |
| 67 | +/** Resolve through the real layer: real HTTP, real filesystem, real process launch. */ |
| 68 | +async function resolveBinary(): Promise<string> { |
| 69 | + const program = Effect.gen(function* () { |
| 70 | + const binary = yield* RipgrepBinary.Service |
| 71 | + return yield* binary.filepath |
| 72 | + }).pipe(Effect.provide(RipgrepBinary.defaultLayer)) |
| 73 | + return (await Effect.runPromise(program as never)) as string |
| 74 | +} |
| 75 | + |
| 76 | +let resolved: string |
| 77 | +try { |
| 78 | + resolved = await resolveBinary() |
| 79 | +} catch (err: unknown) { |
| 80 | + fail(`binary.filepath failed: ${err instanceof Error ? err.message : String(err)}`) |
| 81 | +} |
| 82 | + |
| 83 | +ok(`resolved ${resolved}`) |
| 84 | + |
| 85 | +if (!existsSync(resolved)) fail(`resolved path does not exist: ${resolved}`) |
| 86 | +const size = statSync(resolved).size |
| 87 | +if (size < 100_000) fail(`resolved binary is implausibly small (${size} bytes) — likely a partial write`) |
| 88 | +ok(`binary present, ${size} bytes`) |
| 89 | + |
| 90 | +// No staging files should survive a successful install. |
| 91 | +const leftovers = readdirSync(Global.Path.bin).filter((f) => f.endsWith(".tmp")) |
| 92 | +if (leftovers.length > 0) fail(`staging files left behind: ${leftovers.join(", ")}`) |
| 93 | +ok("no staging files left behind") |
| 94 | + |
| 95 | +// The real proof: the extracted binary actually executes. |
| 96 | +const version = execFileSync(resolved, ["--version"], { encoding: "utf8" }) |
| 97 | +if (!/ripgrep\s+\d/.test(version)) fail(`unexpected --version output: ${version.trim()}`) |
| 98 | +ok(`executes: ${version.split("\n")[0]!.trim()}`) |
| 99 | + |
| 100 | +// And it can actually search. |
| 101 | +const hit = execFileSync(resolved, ["--no-config", "NEEDLE_MARKER", "--", import.meta.filename], { |
| 102 | + encoding: "utf8", |
| 103 | +}) |
| 104 | +if (!hit.includes("NEEDLE_MARKER")) fail("ripgrep did not return the expected match") |
| 105 | +ok("search returns matches") // NEEDLE_MARKER |
| 106 | + |
| 107 | +// A second resolve must hit the cache and stay valid. |
| 108 | +const again = await resolveBinary() |
| 109 | +if (again !== resolved) fail(`second resolve returned a different path: ${again}`) |
| 110 | +ok("second resolve hits the cache") |
| 111 | + |
| 112 | +rmSync(cacheRoot, { recursive: true, force: true }) |
| 113 | +console.log("\nPASS — ripgrep resolves, installs and runs on Windows with PowerShell unavailable") |
0 commit comments