diff --git a/suite/scripts/clean-helpers.sh b/suite/scripts/clean-helpers.sh index a5c18fdf96..8f04467a1c 100644 --- a/suite/scripts/clean-helpers.sh +++ b/suite/scripts/clean-helpers.sh @@ -29,13 +29,12 @@ clean_yarn_cache() { safe_remove "${XDG_CACHE_HOME:-$HOME/.cache}/yarn" } +# berry's cache and its metadata cache (packuments) both live in its global +# folder: ~/.yarn/berry, or under $XDG_DATA_HOME when that is set. Removing +# the folder covers both. `yarn cache clean --all` would leave the metadata +# behind (so cold runs skipped every packument download), and Corepack is not +# on PATH anyway since it got its own prefix (BENCH_COREPACK_BIN). clean_berry_cache() { - if command -v corepack &> /dev/null; then - corepack yarn@latest cache clean --all >/dev/null || true - fi - # `cache clean --all` leaves berry's metadata cache (packuments) in its - # global folder, so cold runs skipped every packument download. The global - # folder is ~/.yarn/berry, or under $XDG_DATA_HOME when that is set. safe_remove "$HOME/.yarn/berry" safe_remove "${XDG_DATA_HOME:-$HOME/.local/share}/yarn/berry" } @@ -114,12 +113,16 @@ clean_bun_cache() { if command -v bun &> /dev/null; then bun pm cache rm -g || true bun pm cache rm || true - # Remove the entire install directory (includes cache/ and metadata files) - safe_remove "$HOME/.bun/install" - # Also remove BUN_INSTALL_CACHE_DIR if set to a custom location - if [ -n "${BUN_INSTALL_CACHE_DIR:-}" ]; then - safe_remove "$BUN_INSTALL_CACHE_DIR" - fi + fi + # Remove the entire install directory (includes cache/ and metadata files): + # ~/.bun/install, or $XDG_CACHE_HOME/.bun/install when that is set. + safe_remove "$HOME/.bun/install" + if [ -n "${XDG_CACHE_HOME:-}" ]; then + safe_remove "$XDG_CACHE_HOME/.bun/install" + fi + # Also remove BUN_INSTALL_CACHE_DIR if set to a custom location + if [ -n "${BUN_INSTALL_CACHE_DIR:-}" ]; then + safe_remove "$BUN_INSTALL_CACHE_DIR" fi } @@ -345,6 +348,26 @@ clean_workspace_protocol() { fi } +# Tools also touch the fixture's config files: turbo's build on `next` appends +# `.turbo` to .gitignore (the `run` fixture already ignores it), and the berry, +# zpm and vp prepares write a .yarnrc.yml. Restore a tracked file to what is +# committed and remove an untracked one (next and babylon commit a .yarnrc.yml; +# the other fixtures don't), so the next tool starts from the fixture as +# committed. +clean_fixture_config() { + echo "Restoring fixture config files (.gitignore, .yarnrc.yml)..." + if command -v git &> /dev/null && git rev-parse --git-dir > /dev/null 2>&1; then + local file + for file in .gitignore .yarnrc.yml; do + if git ls-files --error-unmatch "$file" > /dev/null 2>&1; then + git checkout -- "$file" 2>/dev/null || true + elif [ -e "$file" ]; then + rm -f "$file" + fi + done + fi +} + # The inverse of clean_workspace_protocol: migrate the workspace ranges the # way vlt's first install does, untimed, before vlt's first install of a set. # A vlt user's repo stays migrated after that first install (the rewritten @@ -390,6 +413,7 @@ clean_all() { clean_lockfiles clean_package_manager_files clean_workspace_protocol + clean_fixture_config clean_all_cache # Strip packageManager / devEngines.packageManager AFTER the cache cleans, # so nothing they write survives: this must run last to guarantee a @@ -424,6 +448,8 @@ show_help() { echo " clean_all_cache" echo " clean_build_files" echo " clean_build_output" + echo " clean_workspace_protocol" + echo " clean_fixture_config" echo " clean_git" echo " clean_all" echo " migrate_workspace_protocol" @@ -505,6 +531,9 @@ else clean_workspace_protocol) clean_workspace_protocol ;; + clean_fixture_config) + clean_fixture_config + ;; migrate_workspace_protocol) migrate_workspace_protocol ;; diff --git a/suite/scripts/clean-helpers.test.js b/suite/scripts/clean-helpers.test.js index 5ef6ab604f..d7114ccc0e 100644 --- a/suite/scripts/clean-helpers.test.js +++ b/suite/scripts/clean-helpers.test.js @@ -340,11 +340,15 @@ for (const [label, opts] of [ } // Record what a cleaner removes, without touching anything real. -const removedBy = (command, env = {}) => { +const removedBy = (command, env = {}, bins = {}) => { const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), "cleaner-test-")); try { const removeLog = path.join(tempDir, "removed.txt"); fs.writeFileSync(removeLog, ""); + fs.mkdirSync(path.join(tempDir, "bin")); + for (const [name, script] of Object.entries(bins)) { + fs.writeFileSync(path.join(tempDir, "bin", name), script, { mode: 0o755 }); + } const result = spawnSync( "bash", ["-c", `source "$HELPERS" >/dev/null; safe_remove() { printf '%s\\n' "$1" >> "$REMOVE_LOG"; }; ${command}`], @@ -382,16 +386,119 @@ test("clean_zpm_cache removes ~/.yarn/zpm", () => { }); test("clean_berry_cache removes berry's global folder, with and without XDG_DATA_HOME", () => { - assert.deepEqual(removedBy("clean_berry_cache", { XDG_DATA_HOME: "" }).slice(-2), [ + assert.deepEqual(removedBy("clean_berry_cache", { XDG_DATA_HOME: "" }), [ "/home/runner/.yarn/berry", "/home/runner/.local/share/yarn/berry", ]); - assert.deepEqual(removedBy("clean_berry_cache", { XDG_DATA_HOME: "/xdg/data" }).slice(-2), [ + assert.deepEqual(removedBy("clean_berry_cache", { XDG_DATA_HOME: "/xdg/data" }), [ "/home/runner/.yarn/berry", "/xdg/data/yarn/berry", ]); }); +test("clean_bun_cache removes bun's install folder, under $HOME and under XDG_CACHE_HOME", () => { + // a `bun` that does nothing, so the test never touches a real bun cache + const bins = { bun: "#!/bin/sh\nexit 0\n" }; + assert.deepEqual(removedBy("clean_bun_cache", { XDG_CACHE_HOME: "" }, bins), [ + "/home/runner/.bun/install", + ]); + assert.deepEqual(removedBy("clean_bun_cache", { XDG_CACHE_HOME: "/xdg/cache" }, bins), [ + "/home/runner/.bun/install", + "/xdg/cache/.bun/install", + ]); + assert.deepEqual( + removedBy("clean_bun_cache", { XDG_CACHE_HOME: "", BUN_INSTALL_CACHE_DIR: "/custom" }, bins), + ["/home/runner/.bun/install", "/custom"], + ); +}); + +// A git repository with the given files committed: what a fixture looks like. +const committedRepo = (dir, files) => { + fs.mkdirSync(dir, { recursive: true }); + for (const [name, content] of Object.entries(files)) fs.writeFileSync(path.join(dir, name), content); + const git = (...args) => + execFileSync("git", args, { + cwd: dir, + env: { + ...process.env, + GIT_AUTHOR_NAME: "t", + GIT_AUTHOR_EMAIL: "t@t", + GIT_COMMITTER_NAME: "t", + GIT_COMMITTER_EMAIL: "t@t", + }, + }); + git("init", "-q"); + git("add", "."); + git("commit", "-q", "-m", "fixture"); +}; + +test("clean_fixture_config restores a tracked .gitignore and .yarnrc.yml, removes an untracked .yarnrc.yml", (t) => { + const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), "fixture-config-test-")); + t.after(() => fs.rmSync(tempDir, { recursive: true, force: true })); + const run = (cwd) => { + const result = spawnSync("bash", ["-c", 'source "$HELPERS" >/dev/null; clean_fixture_config'], { + cwd, + encoding: "utf8", + env: { ...process.env, HELPERS: path.join(__dirname, "clean-helpers.sh") }, + }); + assert.equal(result.status, 0, result.stderr); + }; + + // like next: both files committed; turbo's build appends to .gitignore + const next = path.join(tempDir, "next"); + committedRepo(next, { ".gitignore": "/node_modules\n", ".yarnrc.yml": "nodeLinker: node-modules\n" }); + fs.appendFileSync(path.join(next, ".gitignore"), "\n# Turborepo\n.turbo\n"); + fs.writeFileSync(path.join(next, ".yarnrc.yml"), "enableMirror: false\n"); + run(next); + assert.equal(fs.readFileSync(path.join(next, ".gitignore"), "utf8"), "/node_modules\n"); + assert.equal(fs.readFileSync(path.join(next, ".yarnrc.yml"), "utf8"), "nodeLinker: node-modules\n"); + + // like run: no .yarnrc.yml committed; berry's prepare writes one + const run_ = path.join(tempDir, "run"); + committedRepo(run_, { ".gitignore": "/node_modules\n", "package.json": "{}" }); + fs.writeFileSync(path.join(run_, ".yarnrc.yml"), "enableMirror: false\n"); + run(run_); + assert.equal(fs.existsSync(path.join(run_, ".yarnrc.yml")), false); + assert.equal(fs.readFileSync(path.join(run_, ".gitignore"), "utf8"), "/node_modules\n"); + + // outside a repository nothing is touched + const plain = path.join(tempDir, "plain"); + fs.mkdirSync(plain); + fs.writeFileSync(path.join(plain, ".yarnrc.yml"), "x\n"); + run(plain); + assert.equal(fs.existsSync(path.join(plain, ".yarnrc.yml")), true); +}); + +test("registry-package-count.sh counts by walking the graph, dotted names included", () => { + const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), "registry-count-test-")); + try { + const project = path.join(tempDir, "project"); + const out = path.join(tempDir, "out"); + for (const [name, manifest] of [ + ["a", { name: "a", dependencies: { "lodash.merge": "1" } }], + ["lodash.merge", { name: "lodash.merge" }], + ["@scope.x/b", { name: "@scope.x/b" }], + [".store/unreachable", { name: "unreachable" }], + ]) { + fs.mkdirSync(path.join(project, "node_modules", name), { recursive: true }); + fs.writeFileSync(path.join(project, "node_modules", name, "package.json"), JSON.stringify(manifest)); + } + fs.writeFileSync( + path.join(project, "package.json"), + JSON.stringify({ name: "x", dependencies: { a: "1", "@scope.x/b": "1" } }), + ); + const script = path.join(__dirname, "registry-package-count.sh"); + for (const registry of ["npm", "aws"]) { + const result = spawnSync("bash", [script, out, registry], { cwd: project, encoding: "utf8" }); + assert.equal(result.status, 0, result.stderr); + } + assert.deepEqual(fs.readdirSync(out).sort(), ["aws-count.txt", "npm-count.txt"]); + assert.equal(fs.readFileSync(path.join(out, "npm-count.txt"), "utf8"), "3\n"); + } finally { + fs.rmSync(tempDir, { recursive: true, force: true }); + } +}); + // Run clean_deno_code_cache from `cwd` against a fake deno dir (reported by a // fake `deno info --json`, or found through env when `denoOnPath` is false) // and record what it removes and what is left in the code cache database. diff --git a/suite/scripts/registry-package-count.sh b/suite/scripts/registry-package-count.sh index 34124e276b..f6b3e81111 100644 --- a/suite/scripts/registry-package-count.sh +++ b/suite/scripts/registry-package-count.sh @@ -17,12 +17,12 @@ fi # If the node_modules directory exists, count the number of packages if [ -d "node_modules" ]; then + # Walk the dependency graph, as package-count.sh does. Scanning node_modules + # with a name pattern skipped package names containing a dot (335 instead of + # 362 on next), so registry per-package times were computed from a smaller + # count than the package-manager ones. BENCH_PACKAGE_COUNT=$( - find node_modules -name package.json -type f \ - | grep -E 'node_modules/([a-zA-Z0-9_-]+)/package\.json$|node_modules/@[a-zA-Z0-9_-]+/[a-zA-Z0-9_-]+/package\.json$' \ - | sort -u \ - | wc -l \ - | xargs + node "$(dirname "${BASH_SOURCE[0]}")/package-count.js" . ) || true else echo "Warning: node_modules directory does not exist" diff --git a/suite/scripts/registry/common.sh b/suite/scripts/registry/common.sh index ee088473b4..69c47f0d04 100644 --- a/suite/scripts/registry/common.sh +++ b/suite/scripts/registry/common.sh @@ -67,6 +67,14 @@ fi # relying on any cached metadata, so we measure actual registry performance. BENCH_NPM_INSTALL="npm install --prefer-online --no-audit --no-fund --no-update-notifier --ignore-scripts --loglevel=$BENCH_LOGLEVEL $FORCE_FLAG" +# Only the selected registries need their variables (checked below). With +# `set -u`, the `${VAR#//}` expansions below abort on an unset one, so give the +# optional registry URLs an empty default first: `--registries=npm` must run +# without CLOUDSMITH_REGISTRY, GH_REGISTRY and JFROG_REGISTRY. +CLOUDSMITH_REGISTRY="${CLOUDSMITH_REGISTRY:-}" +GH_REGISTRY="${GH_REGISTRY:-}" +JFROG_REGISTRY="${JFROG_REGISTRY:-}" + # Registry definitions BENCH_REGISTRY_NPM_URL="https://registry.npmjs.org/" BENCH_REGISTRY_VLT_URL="https://registry.vlt.io/vlt-benchmarks/npm/" diff --git a/suite/scripts/variations/common.sh b/suite/scripts/variations/common.sh index 025e1f9da2..c19994a101 100644 --- a/suite/scripts/variations/common.sh +++ b/suite/scripts/variations/common.sh @@ -110,14 +110,20 @@ BENCH_SETUP_UPM="" BENCH_NX_VERSION="$(node -p "require('$(npm root -g)/nx/package.json').version" 2>/dev/null || echo latest)" BENCH_SETUP_NX="npm install --no-save --no-audit --no-fund --ignore-scripts --silent nx@$BENCH_NX_VERSION && npm pkg set nx='{}' --json" BENCH_SETUP_TURBO="" -# vp install delegates to Yarn 4, so it needs the same yarn.lock as berry above. -BENCH_SETUP_VP="touch yarn.lock" +# vp install delegates to Yarn 4, so it needs the same yarn.lock and +# .yarnrc.yml (nodeLinker: node-modules) as berry above. It used to get the +# .yarnrc.yml only because berry's prepare left one behind in fixtures that +# don't commit one; clean_fixture_config now removes that between tools. +BENCH_SETUP_VP="touch yarn.lock; echo \"$BENCH_COMMAND_YARN_MODERN_CONFIG\" > .yarnrc.yml" BENCH_SETUP_NODE="" # Bare install commands (no log redirection) — used by strace process counting. -# Install scripts are disabled where the PM runs them by default, so benchmarks -# measure dependency resolution + linking only (not arbitrary postinstall work). -# npm, yarn classic, bun: run scripts by default → --ignore-scripts +# Install scripts are disabled so benchmarks measure dependency resolution + +# linking only (not arbitrary postinstall work), whatever a tool's default. +# yarn classic: runs scripts by default → --ignore-scripts +# npm 12, bun: already block dependency scripts by default (npm 12 allows +# only explicitly listed ones; bun skipped esbuild's postinstall without +# the flag too), so --ignore-scripts is explicit, not load-bearing # pnpm v11+: errors on ignored build scripts (ERR_PNPM_IGNORED_BUILDS) → --ignore-scripts # aube: runs scripts for its default trusted dependencies → --ignore-scripts # berry, zpm, vlt: don't run scripts by default → no flag needed