Skip to content
Closed
Show file tree
Hide file tree
Changes from 5 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: 8 additions & 1 deletion src/runtime/cli/bunx_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1013,6 +1013,9 @@ impl BunxCommand {
let passthrough: &[Box<[u8]>] = opts.passthrough_list.as_slice();

let mut do_cache_bust = update_request.version.tag == VersionTag::DistTag;
// Stale or untrusted tree only: `--force` re-links every cached package,
// which is slow on Windows (#41211); `--no-cache` alone keeps #4981 fixed.
Comment thread
robobun marked this conversation as resolved.
Resolved
let mut force_reinstall = false;
let look_for_existing_bin = update_request.version.literal.is_empty()
|| update_request.version.tag != VersionTag::DistTag;

Expand Down Expand Up @@ -1075,6 +1078,7 @@ impl BunxCommand {
BStr::new(out)
);
do_cache_bust = true;
force_reinstall = true;
break 'try_run_existing;
}
let is_stale: bool = 'is_stale: {
Expand Down Expand Up @@ -1133,6 +1137,7 @@ impl BunxCommand {
if is_stale {
bun_output::scoped_log!(bunx, "found stale binary: {}", BStr::new(out));
do_cache_bust = true;
force_reinstall = true;
if opts.no_install {
bun_core::warn!(
"Using a stale installation of <b>{}<r> because --no-install was passed. Run `bunx` without --no-install to use a fresh binary.",
Expand Down Expand Up @@ -1246,6 +1251,7 @@ impl BunxCommand {
BStr::new(out)
);
do_cache_bust = true;
force_reinstall = true;
break 'try_run_existing;
}
let stored = fs.dirname_store.append_slice(out)?;
Expand Down Expand Up @@ -1336,8 +1342,9 @@ impl BunxCommand {
// disable the manifest cache when a tag is specified
// so that @latest is fetched from the registry
args.append(b"--no-cache").expect("unreachable"); // upper bound is known
}

// forcefully re-install packages in this mode too
if force_reinstall {
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
args.append(b"--force").expect("unreachable"); // upper bound is known
}
Comment thread
robobun marked this conversation as resolved.
Resolved

Expand Down
142 changes: 141 additions & 1 deletion test/cli/install/bunx.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { spawn } from "bun";
import { afterAll, beforeAll, beforeEach, describe, expect, it, setDefaultTimeout } from "bun:test";
import { mkdir, rm, writeFile } from "fs/promises";
import { bunEnv, bunExe, isWindows, readdirSorted, tmpdirSync } from "harness";
import { chmodSync, copyFileSync, readdirSync, symlinkSync } from "node:fs";
import { chmodSync, copyFileSync, readdirSync, symlinkSync, utimesSync } from "node:fs";
import { tmpdir } from "os";
import { delimiter, join, resolve } from "path";
import { dummyAfterAll, dummyBeforeAll, dummyBeforeEach, dummyRegistry, getPort, setHandler } from "./dummy.registry";
Expand Down Expand Up @@ -840,6 +840,146 @@ console.log("EXECUTED: multi-tool-alt (alternate binary)");
});
});

// `bunx pkg@latest` (any dist-tag) used to spawn `bun add pkg@latest
// --no-cache --force`. `--force` re-installs every package in the cached tree
// on every invocation, which is slow on Windows (per-file copy + AV scanning;
// issues #41211, #23597). Only `--no-cache` is needed for a dist-tag: it
// re-fetches the manifest, and `bun add pkg@tag` re-installs the package when
// the tag moved (#4981).
describe("bunx dist-tag cache", () => {
let port: number;

beforeAll(() => {
dummyBeforeAll();
port = getPort()!;
});

afterAll(() => {
dummyAfterAll();
});

beforeEach(async () => {
await dummyBeforeEach();
});

async function makeTarball(tgzDir: string, name: string, version: string) {
const pkgRoot = tmpdirSync();
const packageDir = join(pkgRoot, "package");
await mkdir(packageDir, { recursive: true });
await writeFile(join(packageDir, "package.json"), JSON.stringify({ name, version, bin: { [name]: "cli.js" } }));
await writeFile(
join(packageDir, "cli.js"),
`#!/usr/bin/env node\nconsole.log(${JSON.stringify(`${name} ${version}`)});\n`,
);
chmodSync(join(packageDir, "cli.js"), 0o755);
await Bun.$`cd ${pkgRoot} && tar -czf ${join(tgzDir, `${name}-${version}.tgz`)} package`;
}

const runBunx = 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: {
...env,
npm_config_registry: `http://localhost:${port}/`,
},
});
return Promise.all([subprocess.stderr.text(), subprocess.stdout.text(), subprocess.exited]);
};

// The bunx cache dir is $TMPDIR/bunx-<uid>-<pkg>@latest; resolve it by
// globbing so the test does not depend on how the uid is derived per OS.
function findBunxCacheDir(pkg: string): string {
const match = readdirSync(env.TMPDIR!).filter(d => d.startsWith("bunx-") && d.endsWith(`-${pkg}@latest`));
expect(match).toHaveLength(1);
return join(env.TMPDIR!, match[0]);
}

it("a warm @latest run does not re-install the cached packages", async () => {
const pkg = "dist-keep-pkg";
const tgzDir = tmpdirSync();
await makeTarball(tgzDir, pkg, "1.0.0");
setHandler(dummyRegistry([], { "1.0.0": { bin: { [pkg]: "cli.js" }, as: "1.0.0" }, latest: "1.0.0" }, 0, tgzDir));

let [err, out, exited] = await runBunx(`${pkg}@latest`);
expect(err).not.toContain("error:");
expect(out).toContain(`${pkg} 1.0.0`);
expect(exited).toBe(0);

// Plant a marker inside the cached package. A `--force` re-install
// replaces the package directory and deletes it.
const marker = join(findBunxCacheDir(pkg), "node_modules", pkg, "MARKER");
await writeFile(marker, "keep");

[err, out, exited] = await runBunx(`${pkg}@latest`);
expect(err).not.toContain("error:");
expect(out).toContain(`${pkg} 1.0.0`);
expect(exited).toBe(0);
expect(await Bun.file(marker).exists()).toBe(true);
});

it("a stale cached install (>24h) is still force-refreshed", async () => {
const pkg = "dist-stale-pkg";
const tgzDir = tmpdirSync();
await makeTarball(tgzDir, pkg, "1.0.0");
setHandler(dummyRegistry([], { "1.0.0": { bin: { [pkg]: "cli.js" }, as: "1.0.0" }, latest: "1.0.0" }, 0, tgzDir));

// No version: this path runs the cached bin directly unless the cached
// .bin entry is older than 24h, in which case it re-installs with --force.
let [err, out, exited] = await runBunx(pkg);
expect(err).not.toContain("error:");
expect(out).toContain(`${pkg} 1.0.0`);
expect(exited).toBe(0);

const cacheDir = findBunxCacheDir(pkg);
const marker = join(cacheDir, "node_modules", pkg, "MARKER");
await writeFile(marker, "keep");

// Age every cached file bunx's staleness checks can read (the .bin entry
// and the root package.json) past the 24h validity window.
const old = new Date(Date.now() - 25 * 60 * 60 * 1000);
utimesSync(join(cacheDir, "package.json"), old, old);
for (const entry of readdirSync(join(cacheDir, "node_modules", ".bin"))) {
utimesSync(join(cacheDir, "node_modules", ".bin", entry), old, old);
}

[err, out, exited] = await runBunx(pkg);
expect(err).not.toContain("error:");
expect(out).toContain(`${pkg} 1.0.0`);
expect(exited).toBe(0);
// The stale tree was force-reinstalled, so the planted marker is gone.
expect(await Bun.file(marker).exists()).toBe(false);
});

it("a @latest run picks up a newly published latest version", async () => {
const pkg = "dist-move-pkg";
const tgzDir = tmpdirSync();
await makeTarball(tgzDir, pkg, "1.0.0");
await makeTarball(tgzDir, pkg, "1.1.0");
const versions = {
"1.0.0": { bin: { [pkg]: "cli.js" }, as: "1.0.0" },
"1.1.0": { bin: { [pkg]: "cli.js" }, as: "1.1.0" },
};
setHandler(dummyRegistry([], { ...versions, latest: "1.0.0" }, 0, tgzDir));

let [err, out, exited] = await runBunx(`${pkg}@latest`);
expect(err).not.toContain("error:");
expect(out).toContain(`${pkg} 1.0.0`);
expect(exited).toBe(0);

// The dist-tag moves; the next run must install and run 1.1.0.
setHandler(dummyRegistry([], { ...versions, latest: "1.1.0" }, 0, tgzDir));

[err, out, exited] = await runBunx(`${pkg}@latest`);
expect(err).not.toContain("error:");
expect(out).toContain(`${pkg} 1.1.0`);
expect(exited).toBe(0);
});
});

// Regression: `bunx @scope/name` guesses the bin name as `name` (the
// unscoped portion), then searched the full system $PATH with it. When
// `name` happened to match an unrelated system binary — e.g.
Expand Down
Loading