cli: the bin runs its own package, never another iterate CLI of the working folder (#29)
jonas, 2026-11-28 20:48 UTC · 935a3aba63f25923f7dafaa07aeb9d3bc38c65a7 · parent acfe7f1
3 files changed
M core/lib/bin/iterate.js
@@ -1,103 +1,22 @@ #!/usr/bin/env node -import { existsSync, readFileSync, realpathSync } from "node:fs"; -import { dirname, join, resolve } from "node:path"; +// THE BIN RUNS ITS OWN PACKAGE, AND NO OTHER. It never looks at the working folder for another +// iterate CLI: git runs `iterate git-credential` in git's own working folder, and a service starts +// `iterate daemon` wherever it likes, so a command must answer the same in every folder. (Until +// 28 Nov 2026 it handed every command to a CLI found above the working folder, and git's helper +// answered "unknown command 'git-credential'" in a checkout with an older CLI.) To run a checkout's +// CLI, run that checkout's bin: `pnpm exec iterate` there, or `node <checkout>/core/lib/bin/iterate.js`. +import { existsSync } from "node:fs"; +import { dirname, join } from "node:path"; import process from "node:process"; import { fileURLToPath } from "node:url"; -/** - * Walk up from `startDir` looking for `relativePath` to exist. - * Returns the directory where it was found, or null. - * @param {string} relativePath - * @param {string} [startDir] - * @returns {string | null} - */ -const findUp = (relativePath, startDir = process.cwd()) => { - let dir = resolve(startDir); - while (true) { - if (existsSync(join(dir, relativePath))) return dir; - const parent = dirname(dir); - if (parent === dir) return null; - dir = parent; - } -}; +const pkgRoot = dirname(dirname(fileURLToPath(import.meta.url))); -const __filename = fileURLToPath(import.meta.url); -const __dirname = dirname(__filename); -const pkgRoot = dirname(__dirname); - -// `npx iterate` and `bunx iterate` should exercise the package that the runner -// just installed. Normal installed/global executions still delegate to local -// repo source below, which keeps monorepo development fast. -const isEphemeralPackageRunner = () => - process.env.npm_command === "exec" && - (process.env.npm_lifecycle_event === "npx" || process.env.npm_lifecycle_event === "bunx"); - -// The black-box PTY test (test/vitest/os/iterate-cli.e2e.test.ts) builds the -// package, then exercises this same public bin while the monorepo source tree -// is still present. Force the published -// artifact path so that test cannot accidentally fall back to TypeScript. +// A checkout has the source and runs it; the published package has only the build. +// ITERATE_FORCE_BUILT_PACKAGE=1 runs a checkout's build: a test of the published artifact. +const srcPath = join(pkgRoot, "src/cli/cli.ts"); +const distPath = join(pkgRoot, "dist/cli.mjs"); const forceBuiltPackage = process.env.ITERATE_FORCE_BUILT_PACKAGE === "1"; - -/** - * Whether every runtime dependency of the package at `pkgDir` is installed. A - * checkout that has not run `pnpm install` has the CLI's source but not what it - * imports, so it cannot run it. - * @param {string} pkgDir - * @returns {boolean} - */ -const dependenciesInstalled = (pkgDir) => { - const { dependencies = {} } = JSON.parse(readFileSync(join(pkgDir, "package.json"), "utf8")); - return Object.keys(dependencies).every((name) => existsSync(join(pkgDir, "node_modules", name))); -}; - -/** - * Find a local version of the iterate CLI that differs from the currently - * running script. Returns an importable module path, or null. - * @returns {string | null} - */ -const findLocalModule = () => { - const selfReal = realpathSync(__filename); - - const repoRoot = findUp("pnpm-workspace.yaml"); - if (repoRoot) { - const repoPkg = join(repoRoot, "core/lib"); - const repoBin = join(repoPkg, "bin/iterate.js"); - if ( - existsSync(repoBin) && - realpathSync(repoBin) !== selfReal && - dependenciesInstalled(repoPkg) - ) { - const repoSrc = join(repoPkg, "src/cli/cli.ts"); - if (existsSync(repoSrc)) return repoSrc; - const repoDist = join(repoPkg, "dist/cli.mjs"); - if (existsSync(repoDist)) return repoDist; - } - } - - const nmRoot = findUp("node_modules/.bin/iterate"); - if (nmRoot) { - // pnpm's .bin entry is a shell shim, so compare the package's actual bin. - // Comparing the shim itself mistakes our own package for another install - // and sends repository development to a stale dist build. - const nmScript = join(nmRoot, "node_modules/iterate/bin/iterate.js"); - if (existsSync(nmScript) && realpathSync(nmScript) !== selfReal) { - const nmDist = join(nmRoot, "node_modules/iterate/dist/cli.mjs"); - if (existsSync(nmDist)) return nmDist; - } - } - - return null; -}; - -const localModule = isEphemeralPackageRunner() || forceBuiltPackage ? null : findLocalModule(); -if (localModule) { - const { runCli } = await import(localModule); - await runCli(); -} else { - const srcPath = join(pkgRoot, "src/cli/cli.ts"); - const distPath = join(pkgRoot, "dist/cli.mjs"); - const modulePath = !forceBuiltPackage && existsSync(srcPath) ? srcPath : distPath; - const { runCli } = await import(modulePath); - await runCli(); -} +const { runCli } = await import(!forceBuiltPackage && existsSync(srcPath) ? srcPath : distPath); +await runCli();
M core/lib/src/cli/README.md
@@ -750,5 +750,8 @@ ## Development -In this repository `pnpm exec iterate` runs the source (`src/cli.ts`); the launcher uses the -published build when installed through `npx`, or when `ITERATE_FORCE_BUILT_PACKAGE=1`. +The launcher (`bin/iterate.js`) runs its own package and no other: the source (`src/cli/cli.ts`) +in a checkout, so `pnpm exec iterate` here runs the source, and the build in the published +package, or when `ITERATE_FORCE_BUILT_PACKAGE=1`. It never hands a command to another iterate CLI +of the working folder: git's credential helper and a service run in any folder, and must answer +the same in each. To run a checkout's CLI from elsewhere: `node <checkout>/core/lib/bin/iterate.js`.
M core/lib/src/cli/cli.test.ts
@@ -1,5 +1,7 @@ import { spawnSync } from "node:child_process"; -import { readFileSync } from "node:fs"; +import { mkdirSync, mkdtempDisposableSync, readFileSync, writeFileSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { dirname, join } from "node:path"; import { fileURLToPath } from "node:url"; import { createCli, os } from "trpc-cli"; import { expect, test } from "vitest"; @@ -106,4 +108,34 @@ stdout: pkg.version, }); } +}); + +test("the bin runs its own package in a folder that holds another iterate CLI", () => { + // git runs `iterate git-credential` in git's own working folder: a checkout with an older CLI + // there must not answer for this one + using folder = mkdtempDisposableSync(join(tmpdir(), "iterate-test-")); + const decoy = `export const runCli = () => console.log("the decoy ran");`; + writeFileSync(join(folder.path, "pnpm-workspace.yaml"), "packages: []\n"); + for (const [file, text] of [ + ["core/lib/bin/iterate.js", ""], + ["core/lib/package.json", "{}"], + ["core/lib/src/cli/cli.ts", decoy], + ["node_modules/.bin/iterate", ""], + ["node_modules/iterate/bin/iterate.js", ""], + ["node_modules/iterate/dist/cli.mjs", decoy], + ] as const) { + mkdirSync(dirname(join(folder.path, file)), { recursive: true }); + writeFileSync(join(folder.path, file), text); + } + const bin = fileURLToPath(new URL("../../bin/iterate.js", import.meta.url)); + const result = spawnSync(process.execPath, [bin, "--version"], { + encoding: "utf8", + cwd: folder.path, + // not as `npx` runs it: the old launcher looked for another CLI only then + env: { ...process.env, npm_command: "", npm_lifecycle_event: "" }, + }); + expect({ status: result.status, stdout: result.stdout.trim() }).toEqual({ + status: 0, + stdout: packageVersionOf(), + }); });