import { spawn } from "bun"; import { afterAll, beforeAll, beforeEach, describe, expect, it, setDefaultTimeout } from "bun:test"; import { rm, writeFile } from "fs/promises"; import { bunEnv, bunExe, isWindows, readdirSorted, tmpdirSync } from "harness"; import { copyFileSync, readdirSync } from "node:fs"; import { tmpdir } from "os"; import { join, resolve } from "path"; import { dummyAfterAll, dummyBeforeAll, dummyBeforeEach, dummyRegistry, getPort, setHandler } from "./dummy.registry"; let x_dir: string; let current_tmpdir: string; let install_cache_dir: string; let env = { ...bunEnv }; beforeAll(() => { setDefaultTimeout(1000 * 60 * 5); }); beforeEach(async () => { const waiting: Promise[] = []; if (current_tmpdir) { waiting.push(rm(current_tmpdir, { recursive: true, force: true })); } if (install_cache_dir) { waiting.push(rm(install_cache_dir, { recursive: true, force: true })); } const tmp = isWindows ? tmpdir() : "/tmp"; readdirSync(tmp).forEach(file => { if (file.startsWith("bunx-") || file.startsWith("bun-x.test")) { waiting.push(rm(join(tmp, file), { recursive: true, force: true })); } }); install_cache_dir = tmpdirSync(); current_tmpdir = tmpdirSync(); x_dir = tmpdirSync(); env.TEMP = current_tmpdir; env.BUN_TMPDIR = env.TMPDIR = current_tmpdir; env.TMPDIR = current_tmpdir; env.BUN_INSTALL_CACHE_DIR = install_cache_dir; await Promise.all(waiting); }); it("should choose the tagged versions instead of the PATH versions when a tag is specified", async () => { let semverVersions = [ "7.0.0", "7.1.0", "7.1.1", "7.1.2", "7.1.3", "7.2.0", "7.2.1", "7.2.2", "7.2.3", "7.3.0", "7.3.1", "7.3.2", "7.3.3", "7.3.4", "7.3.5", "7.3.6", "7.3.7", "7.3.8", "7.4.0", "7.5.0", "7.5.1", "7.5.2", "7.5.3", "7.5.4", "7.6.0", ].sort(); if (isWindows) { // Windows does not support race-free installs. semverVersions = semverVersions.slice(0, 2); } const processes = semverVersions.map((version, i) => { return spawn({ cmd: [bunExe(), "x", "semver@" + version, "--help"], cwd: x_dir, stdout: "pipe", stdin: "ignore", stderr: "ignore", env: { ...env, // BUN_DEBUG_QUIET_LOGS: undefined, // BUN_DEBUG: "/tmp/bun-debug.txt." + i, }, }); }); const results = await Promise.all(processes.map(p => p.exited)); expect(results).toEqual(semverVersions.map(() => 0)); const outputs = (await Promise.all(processes.map(p => new Response(p.stdout).text()))).map(a => a.substring(0, a.indexOf("\n")), ); expect(outputs).toEqual(semverVersions.map(v => "SemVer " + v)); }); it("should install and run default (latest) version", async () => { const { stdout, stderr, exited } = spawn({ cmd: [bunExe(), "x", "uglify-js", "--compress"], cwd: x_dir, stdout: "pipe", stdin: new TextEncoder().encode("console.log(6 * 7);"), stderr: "pipe", env, }); const err = await stderr.text(); expect(err).not.toContain("error:"); const out = await stdout.text(); expect(out.split(/\r?\n/)).toEqual(["console.log(42);", ""]); expect(await exited).toBe(0); }); it("should install and run specified version", async () => { const { stdout, stderr, exited } = spawn({ cmd: [bunExe(), "x", "uglify-js@3.14.1", "-v"], cwd: x_dir, stdout: "pipe", stdin: "inherit", stderr: "pipe", env, }); const err = await stderr.text(); expect(err).not.toContain("error:"); const out = await stdout.text(); expect(out.split(/\r?\n/)).toEqual(["uglify-js 3.14.1", ""]); expect(await exited).toBe(0); }); it("should output usage if no arguments are passed", async () => { const { stdout, stderr, exited } = spawn({ cmd: [bunExe(), "x"], cwd: x_dir, stdout: "pipe", stdin: "inherit", stderr: "pipe", env, }); const err = await stderr.text(); expect(err).not.toContain("error:"); expect(err).toContain("Usage: "); const out = await stdout.text(); expect(out).toHaveLength(0); expect(await exited).toBe(1); }); it("should work for @scoped packages", async () => { let exited: number, err: string, out: string; // without cache const withoutCache = spawn({ cmd: [bunExe(), "--bun", "x", "@babel/cli", "--help"], cwd: x_dir, stdout: "pipe", stdin: "inherit", stderr: "pipe", env, }); [err, out, exited] = await Promise.all([ new Response(withoutCache.stderr).text(), new Response(withoutCache.stdout).text(), withoutCache.exited, ]); expect(err).not.toContain("error:"); expect(out.trim()).toContain("Usage: babel [options]"); expect(exited).toBe(0); // cached const cached = spawn({ cmd: [bunExe(), "--bun", "x", "@babel/cli", "--help"], cwd: x_dir, stdout: "pipe", stdin: "inherit", stderr: "pipe", env, }); [err, out, exited] = await Promise.all([ new Response(cached.stderr).text(), new Response(cached.stdout).text(), cached.exited, ]); expect(err).not.toContain("error:"); expect(out.trim()).toContain("Usage: babel [options]"); }); it("should execute from current working directory", async () => { await writeFile( join(x_dir, "test.js"), ` console.log( 6 * 7 )`, ); const { stdout, stderr, exited } = spawn({ cmd: [bunExe(), "--bun", "x", "uglify-js", "test.js", "--compress"], cwd: x_dir, stdout: "pipe", stdin: "inherit", stderr: "pipe", env, }); const [err, out, exitCode] = await Promise.all([stderr.text(), stdout.text(), exited]); expect(err).not.toContain("error:"); expect(await readdirSorted(x_dir)).toEqual(["test.js"]); expect(out.split(/\r?\n/)).toEqual(["console.log(42);", ""]); expect(exitCode).toBe(0); }); it("should work for github repository", async () => { // without cache const withoutCache = spawn({ cmd: [bunExe(), "x", "github:piuccio/cowsay", "--help"], cwd: x_dir, stdout: "pipe", stdin: "inherit", stderr: "pipe", env, }); let [err, out, exited] = await Promise.all([ new Response(withoutCache.stderr).text(), new Response(withoutCache.stdout).text(), withoutCache.exited, ]); expect(err).not.toContain("error:"); expect(out.trim()).toContain("Usage: " + (isWindows ? "cli.js" : "cowsay")); expect(exited).toBe(0); // cached const cached = spawn({ cmd: [bunExe(), "x", "github:piuccio/cowsay", "--help"], cwd: x_dir, stdout: "pipe", stdin: "inherit", stderr: "pipe", env, }); [err, out, exited] = await Promise.all([ new Response(cached.stderr).text(), new Response(cached.stdout).text(), cached.exited, ]); expect(err).not.toContain("error:"); expect(out.trim()).toContain("Usage: " + (isWindows ? "cli.js" : "cowsay")); expect(exited).toBe(0); }); it("should work for github repository with committish", async () => { const withoutCache = spawn({ cmd: [bunExe(), "x", "github:piuccio/cowsay#HEAD", "hello bun!"], cwd: x_dir, stdout: "pipe", stdin: "inherit", stderr: "pipe", env, }); let [err, out, exited] = await Promise.all([ new Response(withoutCache.stderr).text(), new Response(withoutCache.stdout).text(), withoutCache.exited, ]); expect(err).not.toContain("error:"); expect(out.trim()).toContain("hello bun!"); expect(exited).toBe(0); // cached const cached = spawn({ cmd: [bunExe(), "x", "--no-install", "github:piuccio/cowsay#HEAD", "hello bun!"], cwd: x_dir, stdout: "pipe", stdin: "inherit", stderr: "pipe", env, }); [err, out, exited] = await Promise.all([ new Response(cached.stderr).text(), new Response(cached.stdout).text(), cached.exited, ]); expect(err).not.toContain("error:"); expect(out.trim()).toContain("hello bun!"); expect(exited).toBe(0); }); it.each(["--version", "-v"])("should print the version using %s and exit", async flag => { const subprocess = spawn({ cmd: [bunExe(), "x", flag], cwd: x_dir, stdout: "pipe", stdin: "inherit", stderr: "pipe", env, }); let [err, out, exited] = await Promise.all([subprocess.stderr.text(), subprocess.stdout.text(), subprocess.exited]); expect(err).not.toContain("error:"); expect(out.trim()).toContain(Bun.version); expect(exited).toBe(0); }); it("should print the revision and exit", async () => { const subprocess = spawn({ cmd: [bunExe(), "x", "--revision"], cwd: x_dir, stdout: "pipe", stdin: "inherit", stderr: "pipe", env, }); let [err, out, exited] = await Promise.all([subprocess.stderr.text(), subprocess.stdout.text(), subprocess.exited]); expect(err).not.toContain("error:"); expect(out.trim()).toContain(Bun.version); expect(out.trim()).toContain(Bun.revision.slice(0, 7)); expect(exited).toBe(0); }); it("should pass --version to the package if specified", async () => { const subprocess = spawn({ cmd: [bunExe(), "x", "esbuild", "--version"], cwd: x_dir, stdout: "pipe", stdin: "inherit", stderr: "pipe", env, }); let [err, out, exited] = await Promise.all([subprocess.stderr.text(), subprocess.stdout.text(), subprocess.exited]); expect(err).not.toContain("error:"); expect(out.trim()).not.toContain(Bun.version); expect(exited).toBe(0); }); it('should set "npm_config_user_agent" to bun', async () => { await writeFile( join(x_dir, "package.json"), JSON.stringify({ dependencies: { "print-pm": resolve(import.meta.dir, "print-pm-1.0.0.tgz"), }, }), ); const { exited: installFinished } = spawn({ cmd: [bunExe(), "install"], cwd: x_dir, }); expect(await installFinished).toBe(0); const subprocess = spawn({ cmd: [bunExe(), "x", "print-pm"], cwd: x_dir, stdout: "pipe", stderr: "pipe", }); const [err, out, exited] = await Promise.all([subprocess.stderr.text(), subprocess.stdout.text(), subprocess.exited]); expect(err).not.toContain("error:"); expect(out.trim()).toContain(`bun/${Bun.version}`); expect(exited).toBe(0); }); /** * IMPORTANT * Please only use packages with small unpacked sizes for tests. It helps keep them fast. */ describe("bunx --no-install", () => { const run = (...args: string[]): Promise<[stderr: string, stdout: string, exitCode: number]> => { const subprocess = spawn({ cmd: [bunExe(), "x", ...args], cwd: x_dir, env: bunEnv, stdout: "pipe", stderr: "pipe", }); return Promise.all([subprocess.stderr.text(), subprocess.stdout.text(), subprocess.exited] as const); }; it("if the package is not installed, it should fail and print an error message", async () => { const [err, out, exited] = await run("--no-install", "http-server", "--version"); expect(err.trim()).toContain("Could not find an existing 'http-server' binary to run."); expect(out).toHaveLength(0); expect(exited).toBe(1); }); /* yes, multiple package tests are neccessary. 1. there's specialized logic for `bunx tsc` and `bunx typescript` 2. http-server checks for non-alphanumeric edge cases. Plus it's small 3. eslint is alphanumeric and extremely common */ it.each(["typescript", "http-server", "eslint"])("`bunx --no-install %s` should find cached packages", async pkg => { // not cached { const [err, out, code] = await run(pkg, "--version"); expect(err).not.toContain("error:"); expect(out).not.toBeEmpty(); expect(code).toBe(0); } // cached { const [err, out, code] = await run("--no-install", pkg, "--version"); expect(err).not.toContain("error:"); expect(out).not.toBeEmpty(); expect(code).toBe(0); } }); it("when an exact version match is found, should find cached packages", async () => { // not cached { const [err, out, code] = await run("http-server@14.0.0", "--version"); expect(err).not.toContain("error:"); expect(out).not.toBeEmpty(); expect(code).toBe(0); } // cached { const [err, out, code] = await run("--no-install", "http-server@14.0.0", "--version"); expect(err).not.toContain("error:"); expect(out).not.toBeEmpty(); expect(code).toBe(0); } }); }); it("should handle postinstall scripts correctly with symlinked bunx", async () => { // Create a symlink to bun called "bunx" copyFileSync(bunExe(), join(x_dir, isWindows ? "bun.exe" : "bun")); copyFileSync(bunExe(), join(x_dir, isWindows ? "bunx.exe" : "bunx")); const subprocess = spawn({ cmd: ["bunx", "esbuild@latest", "--version"], cwd: x_dir, stdout: "pipe", stdin: "inherit", stderr: "pipe", env: { ...env, PATH: `${x_dir}${isWindows ? ";" : ":"}${env.PATH || ""}`, }, }); let [err, out, exited] = await Promise.all([subprocess.stderr.text(), subprocess.stdout.text(), subprocess.exited]); expect(err).not.toContain("error:"); expect(err).not.toContain("Cannot find module 'exec'"); expect(out.trim()).not.toContain(Bun.version); expect(exited).toBe(0); }); it("should handle package that requires node 24", async () => { const subprocess = spawn({ cmd: [bunExe(), "x", "--bun", "@angular/cli@latest", "--help"], cwd: x_dir, stdout: "pipe", stdin: "inherit", stderr: "pipe", env, }); let [err, out, exited] = await Promise.all([subprocess.stderr.text(), subprocess.stdout.text(), subprocess.exited]); expect(err).not.toContain("error:"); expect(out.trim()).not.toContain(Bun.version); expect(exited).toBe(0); }); describe("--package flag", () => { const run = async (...args: string[]): Promise<[err: string, out: string, exited: number]> => { const subprocess = spawn({ cmd: [bunExe(), "x", ...args], cwd: x_dir, stdout: "pipe", stdin: "inherit", stderr: "pipe", env, }); const [err, out, exited] = await Promise.all([ subprocess.stderr.text(), subprocess.stdout.text(), subprocess.exited, ]); return [err, out, exited]; }; it("should error when --package is provided without package name", async () => { const [err, out, exited] = await run("--package"); expect(err).toContain("--package requires a package name"); expect(exited).toBe(1); }); it("should error when --package is provided without binary name", async () => { const [err, out, exited] = await run("--package", "some-package"); expect(err).toContain("When using --package, you must specify the binary to run"); expect(exited).toBe(1); }); describe("with mock registry", () => { let port: number; beforeAll(() => { dummyBeforeAll(); port = getPort()!; }); afterAll(() => { dummyAfterAll(); }); beforeEach(async () => { await dummyBeforeEach(); }); const runWithRegistry = async ( ...args: string[] ): Promise<[err: string, out: string, exited: number, urls: string[]]> => { const urls: string[] = []; const subprocess = spawn({ cmd: [bunExe(), "x", ...args], cwd: x_dir, stdout: "pipe", stdin: "inherit", stderr: "pipe", env: { ...env, npm_config_registry: `http://localhost:${port}/`, }, }); const [err, out, exited] = await Promise.all([ subprocess.stderr.text(), subprocess.stdout.text(), subprocess.exited, ]); return [err, out, exited, urls]; }; it("should install specified package when binary differs from package name", async () => { const urls: string[] = []; // Set up dummy registry with a package that has a different binary name setHandler( dummyRegistry(urls, { "1.0.0": { bin: { "different-bin": "index.js", }, as: "1.0.0", }, }), ); // Tarball already exists in test directory // Without --package, bunx different-bin would fail // With --package, we correctly install my-special-pkg const subprocess = spawn({ cmd: [bunExe(), "x", "--package", "my-special-pkg", "different-bin", "--help"], cwd: x_dir, stdout: "pipe", stdin: "inherit", stderr: "pipe", env: { ...env, npm_config_registry: `http://localhost:${port}/`, }, }); const [err, out, exited] = await Promise.all([ subprocess.stderr.text(), subprocess.stdout.text(), subprocess.exited, ]); expect(urls.some(url => url.includes("/my-special-pkg"))).toBe(true); // The package should install successfully expect(err).toContain("Saved lockfile"); }); it("should support -p shorthand with mock registry", async () => { const urls: string[] = []; setHandler( dummyRegistry(urls, { "2.0.0": { bin: { "tool": "cli.js", }, as: "2.0.0", }, }), ); // Tarball already exists in test directory const subprocess = spawn({ cmd: [bunExe(), "x", "-p", "actual-package", "tool", "--version"], cwd: x_dir, stdout: "pipe", stdin: "inherit", stderr: "pipe", env: { ...env, npm_config_registry: `http://localhost:${port}/`, }, }); const [err, out, exited] = await Promise.all([ subprocess.stderr.text(), subprocess.stdout.text(), subprocess.exited, ]); expect(urls.some(url => url.includes("/actual-package"))).toBe(true); }); it("should support --package= syntax with mock registry", async () => { const urls: string[] = []; setHandler( dummyRegistry(urls, { "3.0.0": { bin: { "runner": "run.js", }, as: "3.0.0", }, }), ); // Tarball already exists in test directory const subprocess = spawn({ cmd: [bunExe(), "x", "--package=runner-pkg", "runner", "--help"], cwd: x_dir, stdout: "pipe", stdin: "inherit", stderr: "pipe", env: { ...env, npm_config_registry: `http://localhost:${port}/`, }, }); const [err, out, exited] = await Promise.all([ subprocess.stderr.text(), subprocess.stdout.text(), subprocess.exited, ]); expect(urls.some(url => url.includes("/runner-pkg"))).toBe(true); }); it("should fail to run alternate binary without --package flag", async () => { // Attempt to run multi-tool-alt without --package flag // This should fail because bunx would try to install a package named "multi-tool-alt" const subprocess = spawn({ cmd: [bunExe(), "x", "multi-tool-alt"], cwd: x_dir, stdout: "pipe", stdin: "inherit", stderr: "pipe", env: { ...env, npm_config_registry: `http://localhost:${port}/`, }, }); const [err, _out, exited] = await Promise.all([ subprocess.stderr.text(), subprocess.stdout.text(), subprocess.exited, ]); // Should fail because there's no package named "multi-tool-alt" expect(err).toContain("error:"); expect(exited).not.toBe(0); }); it("should execute the correct binary when package has multiple binaries", async () => { const urls: string[] = []; // Set up a package with two different binaries setHandler( dummyRegistry(urls, { "1.0.0": { bin: { "multi-tool": "bin/multi-tool.js", "multi-tool-alt": "bin/multi-tool-alt.js", }, as: "1.0.0", }, }), ); // Create the tarball with both binaries that output different messages // First, let's create the package structure const tempDir = tmpdirSync(); const packageDir = join(tempDir, "package"); await Bun.$`mkdir -p ${packageDir}/bin`; await writeFile( join(packageDir, "package.json"), JSON.stringify({ name: "multi-tool-pkg", version: "1.0.0", bin: { "multi-tool": "bin/multi-tool.js", "multi-tool-alt": "bin/multi-tool-alt.js", }, }), ); await writeFile( join(packageDir, "bin", "multi-tool.js"), `#!/usr/bin/env node console.log("EXECUTED: multi-tool (main binary)"); `, ); await writeFile( join(packageDir, "bin", "multi-tool-alt.js"), `#!/usr/bin/env node console.log("EXECUTED: multi-tool-alt (alternate binary)"); `, ); // Make the binaries executable await Bun.$`chmod +x ${packageDir}/bin/multi-tool.js ${packageDir}/bin/multi-tool-alt.js`; // Create the tarball with package/ prefix await Bun.$`cd ${tempDir} && tar -czf ${join(import.meta.dir, "multi-tool-pkg-1.0.0.tgz")} package`; // Test 1: Without --package, bunx multi-tool-alt should fail or install wrong package // Test 2: With --package, we can run the alternate binary const subprocess = spawn({ cmd: [bunExe(), "x", "--package", "multi-tool-pkg", "multi-tool-alt"], cwd: x_dir, stdout: "pipe", stdin: "inherit", stderr: "pipe", env: { ...env, npm_config_registry: `http://localhost:${port}/`, }, }); const [_err, out, exited] = await Promise.all([ subprocess.stderr.text(), subprocess.stdout.text(), subprocess.exited, ]); // Verify the correct package was requested expect(urls.some(url => url.includes("/multi-tool-pkg"))).toBe(true); // Verify the correct binary was executed expect(out).toContain("EXECUTED: multi-tool-alt (alternate binary)"); expect(out).not.toContain("EXECUTED: multi-tool (main binary)"); expect(exited).toBe(0); }); }); }); // Regression test: bunx should not crash on corrupted .bunx files (Windows only) // When the .bunx metadata file is corrupted (e.g., missing quote terminator in bin_path), // bunx should gracefully fall back to the slow path instead of panicking. it.skipIf(!isWindows)("should not crash on corrupted .bunx file with missing quote", async () => { // First, install a package to create a valid .bunx file // Use typescript which creates both .exe and .bunx files // Need to init first to create package.json const initProc = spawn({ cmd: [bunExe(), "init", "-y"], cwd: x_dir, stdout: "pipe", stderr: "pipe", env, }); await initProc.exited; const subprocess1 = spawn({ cmd: [bunExe(), "add", "typescript@5.0.0"], cwd: x_dir, stdout: "pipe", stderr: "pipe", env, }); const [err1, out1, exitCode1] = await Promise.all([ subprocess1.stderr.text(), subprocess1.stdout.text(), subprocess1.exited, ]); // Find the .bunx file const binDir = join(x_dir, "node_modules", ".bin"); const bunxFile = join(binDir, "tsc.bunx"); // Verify the file exists before corrupting it expect(await Bun.file(bunxFile).exists()).toBe(true); // Create a corrupted .bunx file: // Valid format: [bin_path UTF-16LE]["(quote)][null][shebang][bin_len u32][args_len u32][flags u16] // Corrupted: Replace the quote with 'X' but keep valid lengths/flags const binPath = Buffer.from("typescript\\bin\\tsc", "utf16le"); const corruptedQuote = Buffer.from("X", "utf16le"); // 'X' instead of '"' const nullChar = Buffer.alloc(2, 0); const shebang = Buffer.from("node ", "utf16le"); const binLen = Buffer.alloc(4); binLen.writeUInt32LE(binPath.length); const argsLen = Buffer.alloc(4); argsLen.writeUInt32LE(shebang.length); // Valid flags with has_shebang=true, is_node_or_bun=true, version=v5 const flags = Buffer.alloc(2); flags.writeUInt16LE(0xab37); const corruptedData = Buffer.concat([binPath, corruptedQuote, nullChar, shebang, binLen, argsLen, flags]); await writeFile(bunxFile, corruptedData); // Now run bunx - it should NOT crash, but may fail gracefully // Using bun run to invoke tsc.exe, which triggers the BunXFastPath const subprocess2 = spawn({ cmd: [bunExe(), "run", "tsc", "--version"], cwd: x_dir, stdout: "pipe", stderr: "pipe", env, }); const [stderr, stdout, exitCode] = await Promise.all([ subprocess2.stderr.text(), subprocess2.stdout.text(), subprocess2.exited, ]); // The key assertion: we should NOT see a panic expect(stderr).not.toContain("panic"); expect(stderr).not.toContain("reached unreachable code"); });