Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .changeset/fix-esm-dynamic-require.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
"just-bash": patch
---

Fix `Dynamic require of "tty" is not supported` crash when invoking commands that transitively load `debug` / `supports-color` (notably `file`) under ESM Node consumers and via the `just-bash` CLI binary.

The esbuild dynamic-require shim emitted into the ESM Node bundles had no `require` to delegate to at chunk-init under ESM, so any runtime `require("tty")` / `require("os")` from `file-type` → `debug` chain threw. Build banners now provide `createRequire(import.meta.url)` for `build:lib`, `build:cli`, and `build:shell`. CJS and browser bundles are unchanged.

Fixes #211.
6 changes: 3 additions & 3 deletions packages/just-bash/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,11 @@
"build": "rm -rf dist && tsc && pnpm build:lib && pnpm build:lib:cjs && pnpm build:browser && pnpm build:cli && pnpm build:shell && pnpm build:worker && pnpm build:clean && cp dist/index.d.ts dist/index.d.cts && sed '1,/^-->/d' AGENTS.npm.md > dist/AGENTS.md",
"build:clean": "find dist -name '*.test.js' -delete && find dist -name '*.test.d.ts' -delete",
"build:worker": "esbuild src/commands/python3/worker.ts --bundle --platform=node --format=esm --outfile=src/commands/python3/worker.js --external:../../../vendor/cpython-emscripten/* && cp src/commands/python3/worker.js dist/commands/python3/worker.js && mkdir -p dist/bin/chunks && cp src/commands/python3/worker.js dist/bin/chunks/worker.js && mkdir -p dist/bundle/chunks && cp src/commands/python3/worker.js dist/bundle/chunks/worker.js && esbuild src/commands/js-exec/js-exec-worker.ts --bundle --platform=node --format=esm --outfile=src/commands/js-exec/js-exec-worker.js --external:quickjs-emscripten && cp src/commands/js-exec/js-exec-worker.js dist/commands/js-exec/js-exec-worker.js && cp src/commands/js-exec/js-exec-worker.js dist/bin/chunks/js-exec-worker.js && cp src/commands/js-exec/js-exec-worker.js dist/bundle/chunks/js-exec-worker.js && esbuild src/commands/sqlite3/worker.ts --bundle --platform=node --format=esm --outfile=src/commands/sqlite3/worker.js --external:sql.js && mkdir -p dist/commands/sqlite3 && cp src/commands/sqlite3/worker.js dist/commands/sqlite3/worker.js && cp src/commands/sqlite3/worker.js dist/bin/chunks/sqlite3-worker.js && cp src/commands/sqlite3/worker.js dist/bundle/chunks/sqlite3-worker.js",
"build:lib": "esbuild dist/index.js --bundle --splitting --platform=node --format=esm --minify --outdir=dist/bundle --chunk-names=chunks/[name]-[hash] --external:diff --external:minimatch --external:sprintf-js --external:turndown --external:sql.js --external:quickjs-emscripten --external:@mongodb-js/zstd --external:node-liblzma --external:seek-bzip",
"build:lib": "esbuild dist/index.js --bundle --splitting --platform=node --format=esm --minify --outdir=dist/bundle --chunk-names=chunks/[name]-[hash] --banner:js='import{createRequire} from\"node:module\";const require=createRequire(import.meta.url);' --external:diff --external:minimatch --external:sprintf-js --external:turndown --external:sql.js --external:quickjs-emscripten --external:@mongodb-js/zstd --external:node-liblzma --external:seek-bzip",
"build:lib:cjs": "esbuild dist/index.js --bundle --platform=node --format=cjs --minify --outfile=dist/bundle/index.cjs --external:diff --external:minimatch --external:sprintf-js --external:turndown --external:sql.js --external:quickjs-emscripten --external:@mongodb-js/zstd --external:node-liblzma --external:seek-bzip",
"build:browser": "esbuild dist/browser.js --bundle --platform=browser --format=esm --minify --outfile=dist/bundle/browser.js --external:diff --external:minimatch --external:sprintf-js --external:turndown --external:node:zlib --external:@mongodb-js/zstd --external:node-liblzma --external:seek-bzip --define:__BROWSER__=true --alias:node:dns=./src/shims/browser-unsupported.js",
"build:cli": "esbuild dist/cli/just-bash.js --bundle --splitting --platform=node --format=esm --minify --outdir=dist/bin --entry-names=[name] --chunk-names=chunks/[name]-[hash] --banner:js='#!/usr/bin/env node' --external:sql.js --external:quickjs-emscripten --external:@mongodb-js/zstd --external:node-liblzma --external:seek-bzip",
"build:shell": "esbuild dist/cli/shell.js --bundle --splitting --platform=node --format=esm --minify --outdir=dist/bin/shell --entry-names=[name] --chunk-names=chunks/[name]-[hash] --banner:js='#!/usr/bin/env node' --external:sql.js --external:quickjs-emscripten --external:@mongodb-js/zstd --external:node-liblzma --external:seek-bzip",
"build:cli": "esbuild dist/cli/just-bash.js --bundle --splitting --platform=node --format=esm --minify --outdir=dist/bin --entry-names=[name] --chunk-names=chunks/[name]-[hash] --banner:js='#!/usr/bin/env node\nimport{createRequire} from\"node:module\";const require=createRequire(import.meta.url);' --external:sql.js --external:quickjs-emscripten --external:@mongodb-js/zstd --external:node-liblzma --external:seek-bzip",
"build:shell": "esbuild dist/cli/shell.js --bundle --splitting --platform=node --format=esm --minify --outdir=dist/bin/shell --entry-names=[name] --chunk-names=chunks/[name]-[hash] --banner:js='#!/usr/bin/env node\nimport{createRequire} from\"node:module\";const require=createRequire(import.meta.url);' --external:sql.js --external:quickjs-emscripten --external:@mongodb-js/zstd --external:node-liblzma --external:seek-bzip",
"prepublishOnly": "pnpm test:dist",
"validate": "pnpm lint && pnpm knip && pnpm typecheck && pnpm build && pnpm check:worker-sync && pnpm test:run && pnpm test:wasm && pnpm test:dist",
"typecheck": "tsc --noEmit",
Expand Down
35 changes: 35 additions & 0 deletions packages/just-bash/src/cli/just-bash.bundle.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,21 @@ describe("just-bash bundled binary", () => {
expect(result.exitCode).toBe(1);
});

// Regression test for https://github.com/vercel-labs/just-bash/issues/211:
// file-type → debug → supports-color does runtime require("tty")/require("os"),
// which the esbuild dynamic-require shim throws for under ESM Node unless the
// build banner provides createRequire.
it("should run file (regression: dynamic require under ESM Node)", async () => {
const result = await runBin([
"-c",
"echo content > /tmp/x.txt && file /tmp/x.txt",
"--allow-write",
]);
expect(result.stderr).toBe("");
expect(result.stdout).toBe("/tmp/x.txt: ASCII text\n");
expect(result.exitCode).toBe(0);
});

it("should lazy-load commands (sqlite3 with external sql.js)", async () => {
const result = await runBin([
"-c",
Expand Down Expand Up @@ -186,3 +201,23 @@ describe("just-bash CJS bundle", () => {
expect(result.exitCode).toBe(0);
});
});

// Regression test for https://github.com/vercel-labs/just-bash/issues/211.
// The ESM Node bundle (what consumers import via `import { Bash } from "just-bash"`)
// has its own dynamic-require shim — file-type → debug → supports-color does
// runtime require("tty")/require("os") that the shim throws for unless the
// build banner provides createRequire.
describe("just-bash ESM bundle", () => {
it("should be importable and run file command", async () => {
const esmBundlePath = resolve(__dirname, "../../dist/bundle/index.js");
const mod = await import(esmBundlePath);
expect(mod.Bash).toBeDefined();
const fs = new mod.InMemoryFs();
await fs.writeFile("/x.txt", "hello\n");
const bash = new mod.Bash({ fs, cwd: "/" });
const result = await bash.exec("file /x.txt");
expect(result.stderr).toBe("");
expect(result.stdout).toBe("/x.txt: ASCII text\n");
expect(result.exitCode).toBe(0);
});
});
Loading