fix(cli): prevent silent zero-exit when the version-check fetch stalls#2767
Conversation
Every zen command awaits checkNewVersion() in a preAction hook, which fetches the npm registry with AbortSignal.timeout(2000). If the connection stalls in a way that strands the fetch promise with no active handle, nothing keeps the event loop alive (AbortSignal.timeout's timer is unref'd), so node exits with code 0 before the command's action runs — the CLI reports success having done nothing. On CI runners this struck roughly 1 in 100-300 invocations, which made most Build-and-Test runs fail in a random packages/cli test with baffling "output file missing after successful CLI run" assertions (observed on PRs and on main alike; root-caused and fix verified over several instrumented runs on the throwaway diagnostic PR #2766). Fix: race the fetch against a ref'd, always-settling timer that is cleared afterward. The timer keeps the event loop alive and guarantees the await settles into the existing catch. A noop catch on the losing fetch promise avoids unhandled rejections in long-lived modes (watch, proxy, studio). Also: runCli in the CLI test harness now captures and prints the child's stdout/stderr when a command fails, so future failures of this kind are self-diagnosing instead of bare assertion errors. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
📝 WalkthroughWalkthroughChangesCLI reliability and diagnostics
Estimated code review effort: 3 (Moderate) | ~20 minutes 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
packages/cli/test/utils.ts (1)
120-121: 🔒 Security & Privacy | 🔵 Trivial | ⚡ Quick winAvoid shell interpolation when launching the CLI.
execSyncparsescommandas shell syntax, so metacharacters can execute unintended commands and paths containing spaces can break invocation. PreferexecFileSync(process.execPath, [cli, ...args])and changerunClito accept an argument array; at minimum, verify every caller supplies trusted, literal input.Proposed direction
-export function runCli(command: string, cwd: string) { +export function runCli(args: readonly string[], cwd: string) { const cli = path.join(__dirname, '../dist/index.mjs'); try { - return execSync(`node ${cli} ${command}`, { cwd, encoding: 'utf8' }); + return execFileSync(process.execPath, [cli, ...args], { + cwd, + encoding: 'utf8', + });🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@packages/cli/test/utils.ts` around lines 120 - 121, Update runCli to accept an argument array and replace execSync shell invocation with execFileSync(process.execPath, [cli, ...args], preserving cwd and UTF-8 output options. Update every runCli caller to pass separate trusted literal arguments rather than a command string, including commands containing spaces or metacharacters.Source: Linters/SAST tools
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Nitpick comments:
In `@packages/cli/test/utils.ts`:
- Around line 120-121: Update runCli to accept an argument array and replace
execSync shell invocation with execFileSync(process.execPath, [cli, ...args],
preserving cwd and UTF-8 output options. Update every runCli caller to pass
separate trusted literal arguments rather than a command string, including
commands containing spaces or metacharacters.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro Plus
Run ID: ac500235-9aa2-4c4f-a2ba-734e29ccaa4d
📒 Files selected for processing (2)
packages/cli/src/utils/version-utils.tspackages/cli/test/utils.ts
What
Fixes the intermittent CI failures where a random
packages/clitest fails with "output file missing after a successful CLI run" (recently hit #2744's CI repeatedly, and reproduces onmain).Root cause
Every
zencommand awaitscheckNewVersion()in a commanderpreActionhook, which fetchesregistry.npmjs.orgwithAbortSignal.timeout(2000). If the connection stalls in a way that strands the fetch promise with no active handle, nothing keeps the event loop alive —AbortSignal.timeout's internal timer is unref'd — so node exits with code 0 mid-command, before the action runs. The caller sees success; nothing was done.On CI this struck ~1 in 100–300 invocations. With ~360 CLI invocations per Build-and-Test run, most runs failed — each time in whatever random test the stall struck (observed across
generate,db push,db pull,migrate, on both sqlite and mysql matrix jobs, and even once in a sample-app build step).Evidence
Diagnosed on throwaway draft PR #2766 by instrumenting the CLI. Failing invocations show exactly:
(
beforeExitfires only on natural event-loop drain, never onprocess.exit— proving the stranded await.)Fix
Race the fetch against a ref'd, always-settling timer (cleared in
finally). The timer keeps the event loop alive and guarantees the await settles into the existingcatch. A noop.catchon the losing fetch promise avoids unhandled rejections in long-lived modes (watch/proxy/studio).Also hardens
runCliin the CLI test harness to print the child's stdout/stderr on failure, so future failures of this kind are self-diagnosing instead of bareexpected false to be trueassertions.Verification
Verified on #2766 (same fix, plus instrumentation): 3 consecutive green Build-and-Test runs with the version check still enabled (~1000+ exercised invocations, zero drain-exits), versus 5-of-6 runs failing before the fix.
Possible follow-up (not in this PR): pass
--no-version-checkfrom the CLI test harness so CI doesn't depend on npm-registry health at all — needs the option added to every command first, since it's currently per-command.🤖 Generated with Claude Code
Summary by CodeRabbit
Bug Fixes
Tests