|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * The WIRING half of #12964 — `bin/run-dev.js` really asks, on a real failing |
| 5 | + * run, whether "command … not found" is about a missing command at all. |
| 6 | + * |
| 7 | + * ``` |
| 8 | + * $ pnpm i18n:extract # fresh worktree, pnpm install done, nothing built |
| 9 | + * … |
| 10 | + * Error: command i18n:extract:packages/platform-objects/scripts/i18n-extract.config.ts not found |
| 11 | + * $ echo $? |
| 12 | + * 2 |
| 13 | + * ``` |
| 14 | + * |
| 15 | + * The command file is right there in `src/commands/i18n/extract.ts`. oclif |
| 16 | + * `import()`s every command module while it builds its manifest, all of them |
| 17 | + * failed on a `@objectstack/spec` that had no `dist/`, and a command whose |
| 18 | + * module will not load is indistinguishable to `Config.runCommand` from one |
| 19 | + * that does not exist. |
| 20 | + * |
| 21 | + * ## Why this suite is spawned, and why it simulates |
| 22 | + * |
| 23 | + * The lead line is produced from a `process.on('warning')` collector installed |
| 24 | + * around `run()` — state that exists only inside a real CLI process, so an |
| 25 | + * in-process test cannot see it and `process.exit`-adjacent behaviour cannot be |
| 26 | + * asserted from a vitest worker at all. |
| 27 | + * |
| 28 | + * And CI's checkout is BUILT. ⚠️ That is the trap this file is written against: |
| 29 | + * an "unbuilt tree" test that runs in a built tree never enters the branch it |
| 30 | + * claims to cover, prints nothing, asserts nothing failed, and reads green |
| 31 | + * forever. So the unbuilt condition is MANUFACTURED for one child process |
| 32 | + * (`fixtures/unbuilt-spec-dist.hook.mjs`, a `--import` resolve hook that touches |
| 33 | + * no disk), and the three cases below are a POSITIVE CONTROL PAIR rather than a |
| 34 | + * single assertion: |
| 35 | + * |
| 36 | + * 1. hook on, real command id → the lead lines appear; |
| 37 | + * 2. hook off, THE SAME command id → the command module loads and runs, so |
| 38 | + * the run never reaches that branch at all; |
| 39 | + * 3. hook off, a command that really is missing → oclif's "not found" stands |
| 40 | + * exactly as it did, with nothing added. |
| 41 | + * |
| 42 | + * (1) without (2) would pass in a tree where every run happens to be diagnosed; |
| 43 | + * (2) and (3) without (1) are two zero readings. Together they say the branch is |
| 44 | + * reachable, is not always taken, and is taken for the right reason. |
| 45 | + */ |
| 46 | + |
| 47 | +import { describe, it, expect, beforeAll, afterAll } from 'vitest'; |
| 48 | +import { execFile } from 'node:child_process'; |
| 49 | +import { mkdtempSync, rmSync } from 'node:fs'; |
| 50 | +import { tmpdir } from 'node:os'; |
| 51 | +import { join, resolve } from 'node:path'; |
| 52 | +import { fileURLToPath, pathToFileURL } from 'node:url'; |
| 53 | +import { childEnv } from './helpers/serve-process.js'; |
| 54 | + |
| 55 | +const HERE = resolve(fileURLToPath(import.meta.url), '..'); |
| 56 | +/** The SOURCE entry point — this suite is about it, and it needs no `dist/`. */ |
| 57 | +const CLI = resolve(HERE, '../bin/run-dev.js'); |
| 58 | +const TSX = resolve(HERE, '../../../node_modules/.bin/tsx'); |
| 59 | +const UNBUILT_HOOK = pathToFileURL(resolve(HERE, 'fixtures/unbuilt-spec-dist.hook.mjs')).href; |
| 60 | + |
| 61 | +/** |
| 62 | + * A REAL command id, so "not found" is a lie rather than the truth. Its |
| 63 | + * argument names nothing: case 2 has to fail for its own reason (no config |
| 64 | + * file) instead of doing work, and the point there is only WHICH failure. |
| 65 | + */ |
| 66 | +const REAL_COMMAND = ['i18n', 'extract', 'nope.ts']; |
| 67 | + |
| 68 | +/** oclif + tsx cold start, plus ~58 failing command imports in case 1. */ |
| 69 | +const RUN_TIMEOUT_MS = 180_000; |
| 70 | + |
| 71 | +interface Run { |
| 72 | + code: number; |
| 73 | + stdout: string; |
| 74 | + stderr: string; |
| 75 | +} |
| 76 | + |
| 77 | +/** |
| 78 | + * `NODE_OPTIONS` is stated on every call, in both directions. `childEnv()` |
| 79 | + * strips the vitest-worker family and `NODE_PATH`, but not this one, so a |
| 80 | + * control leg that said nothing would silently inherit whatever the runner was |
| 81 | + * started with — and the control legs' whole job is to be un-simulated. |
| 82 | + */ |
| 83 | +function runCli(args: string[], cwd: string, nodeOptions: string | undefined): Promise<Run> { |
| 84 | + return new Promise((resolvePromise) => { |
| 85 | + execFile(TSX, [CLI, ...args], { cwd, maxBuffer: 32 * 1024 * 1024, env: childEnv({ NO_COLOR: '1', NODE_OPTIONS: nodeOptions }) }, (err, stdout, stderr) => { |
| 86 | + resolvePromise({ |
| 87 | + // `err.code` is the real exit status; `null`/undefined means the child |
| 88 | + // was signalled — a failure of a different kind, never reported as 0. |
| 89 | + code: err ? (typeof (err as { code?: unknown }).code === 'number' ? (err as unknown as { code: number }).code : 1) : 0, |
| 90 | + stdout: String(stdout), |
| 91 | + stderr: String(stderr), |
| 92 | + }); |
| 93 | + }); |
| 94 | + }); |
| 95 | +} |
| 96 | + |
| 97 | +/** The sentence this change exists to contradict. */ |
| 98 | +const LEAD = 'objectstack: NOT A MISSING COMMAND'; |
| 99 | +const FIX = 'objectstack: Fix: pnpm exec turbo run build --filter=@objectstack/spec'; |
| 100 | + |
| 101 | +let dir: string; |
| 102 | +let unbuilt: Run; |
| 103 | +let built: Run; |
| 104 | +let genuinelyMissing: Run; |
| 105 | + |
| 106 | +beforeAll(async () => { |
| 107 | + dir = mkdtempSync(join(tmpdir(), 'os-run-dev-unbuilt-')); |
| 108 | + unbuilt = await runCli(REAL_COMMAND, dir, `--import ${UNBUILT_HOOK}`); |
| 109 | + built = await runCli(REAL_COMMAND, dir, undefined); |
| 110 | + genuinelyMissing = await runCli(['definitely-not-a-command'], dir, undefined); |
| 111 | +}, RUN_TIMEOUT_MS * 3); |
| 112 | + |
| 113 | +afterAll(() => { |
| 114 | + rmSync(dir, { recursive: true, force: true }); |
| 115 | +}); |
| 116 | + |
| 117 | +describe('run-dev.js on a workspace package with no build output', () => { |
| 118 | + it('reproduces the misdiagnosis it is fixing — oclif still says "not found"', () => { |
| 119 | + // The upstream line is deliberately NOT suppressed: nothing here changes |
| 120 | + // which arguments the CLI accepts or how oclif reports, only what is said |
| 121 | + // alongside. Asserting it also proves case 1 really reached that failure |
| 122 | + // rather than dying earlier for some unrelated reason. |
| 123 | + expect(unbuilt.stderr).toContain('Error: command i18n:extract:nope.ts not found'); |
| 124 | + expect(unbuilt.code).toBe(2); |
| 125 | + }); |
| 126 | + |
| 127 | + it('names the real cause and the one command that fixes it', () => { |
| 128 | + expect(unbuilt.stderr).toContain(LEAD); |
| 129 | + expect(unbuilt.stderr).toContain('@objectstack/spec'); |
| 130 | + expect(unbuilt.stderr).toContain(FIX); |
| 131 | + }); |
| 132 | + |
| 133 | + it('keeps the oclif debug warning blocks — the listener order is load-bearing', () => { |
| 134 | + // @oclif/core installs its `warning` listener only when |
| 135 | + // `process.listenerCount('warning') <= 1`. A collector attached BEFORE |
| 136 | + // `run()` makes that 2, oclif silently declines, and every failing run |
| 137 | + // through this shim loses these blocks with nothing saying why (measured: |
| 138 | + // 1518 lines of report became 476). `at Plugin.warn` is that listener's |
| 139 | + // output, so this case reds if the attachment ever moves back. |
| 140 | + expect(unbuilt.stderr).toContain('at Plugin.warn'); |
| 141 | + }); |
| 142 | +}); |
| 143 | + |
| 144 | +describe('the same probe, un-simulated (positive control)', () => { |
| 145 | + it('takes the other branch entirely: the command module loads and runs', () => { |
| 146 | + // Not "no lead line" alone — that is a zero reading. The command REACHED |
| 147 | + // its own argument handling, which is only possible if its module loaded. |
| 148 | + expect(`${built.stdout}${built.stderr}`).toContain('Config file not found'); |
| 149 | + expect(built.stderr).not.toContain('Error: command'); |
| 150 | + expect(built.stderr).not.toContain(LEAD); |
| 151 | + expect(built.code).toBe(1); |
| 152 | + }); |
| 153 | + |
| 154 | + it('leaves a command that really is missing exactly as it was', () => { |
| 155 | + expect(genuinelyMissing.stderr).toContain('Error: command definitely-not-a-command not found'); |
| 156 | + expect(genuinelyMissing.stderr).not.toContain(LEAD); |
| 157 | + expect(genuinelyMissing.stderr).not.toContain('objectstack: Fix:'); |
| 158 | + expect(genuinelyMissing.code).toBe(2); |
| 159 | + }); |
| 160 | +}); |
0 commit comments