Files
yetone 740e1af252 tests: grok's nocli puts PATH back when its file ends, so gumloop's Node children find node on CI
publish.yml has been red on main since 7ebab07c (run 43011771755, and
43012668584, 43012789287 after it): 4 of gumloop's regression tests
failed with `Executable not found in $PATH: "node"`, so dist and npm
were skipped and gumloop 0.1.0 and agy-cli 0.1.19 reached neither.

Cause: bun test runs every file of the root run in one process.
packages/grok/nocli.mjs sets PATH=/usr/bin:/bin (and HOME, USERPROFILE,
GROK_HOME, GROK_BIN_DIR) for grok's files and never put them back, so
every file after grok's ran with that PATH. On ubuntu CI node is
actions/setup-node's, under /opt/hostedtoolcache, so gumloop's
spawn("node") found none whenever bun ran grok's files before
gumloop's (CI's order). Locally bun ran gumloop first, so it passed.

Change:
- grok/nocli.mjs: the first noRealGrok of a file snapshots process.env
  and registers an afterAll that puts it back, removing what was added.
- gumloop/regression.test.mjs: node's absolute path is found once with
  Bun.which at load and spawned by path; not found, the Node tests fail
  as before (no skip).
- Siblings the same way (a file-level env change never put back), found
  by a full-run env audit: chat/chat.test.mjs (XDG_CONFIG_HOME pointing
  at its deleted temp dir, MAGPIE_API_KEY/GATEWAY_KEY/ADDR deleted) and
  cursor/calls.test.mjs (XDG_DATA_HOME) now restore them in afterAll.
  window-warmup's fake-gateway.ts/scheduler.test.ts XDG pins are left,
  filed as #996 (fake-gateway is also the --serve demo's).
  Other PATH writers (devin, gcp-ai, grok's own tests, sap-aicore,
  agy-cli, layerfs) already restore theirs; the audit saw PATH change
  only at grok's files.

Ran (magpie-desktop WSL, node only in ~/sdk/node/bin, bun 1.4.2):
- Red first, origin/main: `bun test --randomize --seed=3` (and 5) on
  grok/loop-check + gumloop/regression: the 4 CI failures, same error;
  also with --preload ./packages/grok/nocli.mjs.
- Same seeds with this change: 95 pass 0 fail (new gumloop test plus
  origin/main's copy of it).
- Broken by hand: afterAll(putBack) commented out -> both seeds red
  again, 4 fail.
- Root `HOME=$(mktemp -d) bun test` (with an env-audit preload): 8298
  pass, 19 skip, 0 fail, 8317 tests, 584 files; PATH back to the start
  value at the first file after grok's.
- Touched packages (chat, cursor, grok, gumloop, qoder) after the
  chat/cursor change: 1117 pass 0 fail, each XDG value back to start.

Not tried: ubuntu CI itself before pushing (publish.yml runs it next);
Windows (nocli sets PATH empty there, restored the same way).
2026-11-11 19:53:46 +08:00

62 lines
2.8 KiB
JavaScript

// Imported first by every test here, and called before the file reads its
// environment: no test finds an installed Grok Build (plugins#325). The
// plugin looks for grok in GROK_BIN_DIR, GROK_HOME/bin, ~/.grok/bin, each
// folder on PATH and ~/.local/bin, and runs what it finds (`grok --version`,
// `grok models`), which can reach xAI. So PATH holds only /usr/bin and /bin
// (the fakes' scripts run cat, rm and sleep), and neither if it has a grok;
// on Windows it is empty. GROK_BIN_DIR is a folder that doesn't exist, and
// HOME (USERPROFILE on Windows) and GROK_HOME are empty folders of the
// test's own. A test that wants a CLI puts its fake
// in GROK_BIN_DIR, as before.
//
// bun test runs every file of the run in one process, so the file puts the
// environment back when it ends: every variable as it was before the file's
// first noRealGrok, and none it added. Left as it was, the next package's
// files ran with PATH=/usr/bin:/bin, and on a runner whose node is elsewhere
// (actions/setup-node's /opt/hostedtoolcache) gumloop's spawn("node") found
// none.
//
// Bun's os.homedir() keeps the HOME the process started with, so a HOME set
// here would leave ~/.grok/bin and ~/.local/bin where they were. homedir is
// made to read HOME (USERPROFILE) when called, as Node's does; this module
// is imported before index.mjs, so the plugin gets that one. Other packages'
// tests in the same run see Node's behaviour, the started HOME unless a test
// changed it.
import { afterAll, mock } from "bun:test"
import { existsSync, mkdirSync, mkdtempSync, rmSync } from "node:fs"
import * as os from "node:os"
import { delimiter, join } from "node:path"
const started = os.homedir()
const homedir = () => (process.platform === "win32" ? process.env.USERPROFILE : process.env.HOME) || started
mock.module("node:os", () => ({ ...os, default: { ...os, homedir }, homedir }))
const system = process.platform === "win32" ? [] : ["/usr/bin", "/bin"].filter((d) => !existsSync(join(d, "grok")))
const root = mkdtempSync(join(os.tmpdir(), "grok-nocli-"))
process.on("exit", () => rmSync(root, { recursive: true, force: true }))
let outside // the environment before this file's first noRealGrok
function putBack() {
if (!outside) return
for (const key of Object.keys(process.env)) if (!(key in outside)) delete process.env[key]
Object.assign(process.env, outside)
outside = undefined
}
export function noRealGrok() {
if (!outside) {
outside = { ...process.env }
afterAll(putBack)
}
const home = join(root, "home")
mkdirSync(home, { recursive: true })
mkdirSync(join(root, "grok-home"), { recursive: true })
process.env.PATH = system.join(delimiter)
process.env.GROK_BIN_DIR = join(root, "no-bin")
process.env.GROK_HOME = join(root, "grok-home")
process.env.HOME = home
process.env.USERPROFILE = home
}
noRealGrok()