Skip to content

Commit ce309a4

Browse files
committed
fix(e2e): evict the monorepo's packages from Yarn Berry's cache
Verdaccio republishes a package missing from npm, hardhat-slang among them, under the same version on every run, and Yarn Berry reuses a cached zip by name and version when the lockfile carries no checksum for it. On the persistent benchmark runner the lido scenarios therefore installed the hardhat-slang built before its last change, which their "assert fresh hardhat-slang" step caught (run 38567194567). Before a Berry install, the scenario's cache folders now lose every monorepo package's zips, so they are fetched from Verdaccio again; the rest of the cache stays.
1 parent 74e791e commit ce309a4

2 files changed

Lines changed: 123 additions & 2 deletions

File tree

‎scripts/end-to-end/helpers/install.test.ts‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
buildPackageManagerEnv,
66
getInstallArgs,
77
getUpdateArgs,
8+
isYarnCacheEntryOf,
89
} from "./install.ts";
910

1011
const REGISTRY = "http://127.0.0.1:4873";
@@ -131,3 +132,34 @@ describe("buildPackageManagerEnv", () => {
131132
assert.equal(env.SKIP_LINT_SOLIDITY, "1");
132133
});
133134
});
135+
136+
describe("isYarnCacheEntryOf", () => {
137+
const names = ["hardhat", "@nomicfoundation/hardhat-slang"];
138+
139+
it("matches a Berry cache zip of a listed package, at any version", () => {
140+
assert.ok(
141+
isYarnCacheEntryOf(
142+
"@nomicfoundation-hardhat-slang-npm-3.0.0-0fa5e312b7-10c0.zip",
143+
names,
144+
),
145+
);
146+
assert.ok(
147+
isYarnCacheEntryOf("hardhat-npm-3.16.0-1d74c5c469-10c0.zip", names),
148+
);
149+
});
150+
151+
it("does not match a package whose name only starts with a listed one", () => {
152+
assert.ok(
153+
!isYarnCacheEntryOf(
154+
"@nomicfoundation-hardhat-slang-solx-npm-3.0.4-fb65b954a0-10c0.zip",
155+
names,
156+
),
157+
);
158+
assert.ok(
159+
!isYarnCacheEntryOf(
160+
"hardhat-deploy-npm-1.0.0-6f76b01394-10c0.zip",
161+
names,
162+
),
163+
);
164+
});
165+
});

‎scripts/end-to-end/helpers/install.ts‎

Lines changed: 91 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
1-
import { readFileSync, writeFileSync } from "node:fs";
1+
import {
2+
existsSync,
3+
readdirSync,
4+
readFileSync,
5+
rmSync,
6+
writeFileSync,
7+
} from "node:fs";
28
import { resolve } from "node:path";
39
import { execFileSync } from "node:child_process";
410

511
import { which } from "./shell.ts";
612
import { log, logStep } from "./log.ts";
713

8-
import { VERDACCIO_URL } from "../../verdaccio/helpers/shell.ts";
14+
import { ROOT_DIR, VERDACCIO_URL } from "../../verdaccio/helpers/shell.ts";
915
import type { ScenarioDefinition } from "../types.ts";
1016

1117
/**
@@ -67,6 +73,11 @@ function runPackageManager(
6773
cwd: workDir,
6874
stdio: "inherit",
6975
});
76+
77+
evictLocalPackagesFromYarnCache(
78+
workDir,
79+
buildPackageManagerEnv(packageManager, env),
80+
);
7081
}
7182

7283
execFileSync(which(packageManager), args, {
@@ -76,6 +87,84 @@ function runPackageManager(
7687
});
7788
}
7889

90+
/**
91+
* Drop the monorepo's packages from Yarn Berry's caches before an install.
92+
*
93+
* Verdaccio republishes them under versions that repeat across runs (a
94+
* package missing from npm keeps its own version) with new contents, and
95+
* Berry reuses a cached zip by name and version when the lockfile has no
96+
* checksum for it, which a scenario lockfile never has. A persistent runner
97+
* would then install a previous run's build. Yarn Classic checks the
98+
* registry's shasum, so it needs nothing.
99+
*/
100+
function evictLocalPackagesFromYarnCache(
101+
workDir: string,
102+
env: NodeJS.ProcessEnv,
103+
): void {
104+
const yarn = (args: string[]) =>
105+
execFileSync(which("yarn"), args, {
106+
cwd: workDir,
107+
env,
108+
encoding: "utf8",
109+
}).trim();
110+
111+
if (Number(yarn(["--version"]).split(".")[0]) < 2) {
112+
return;
113+
}
114+
115+
// The global cache is used unless enableGlobalCache is off; clear both.
116+
const cacheDirs = [
117+
resolve(workDir, yarn(["config", "get", "cacheFolder"])),
118+
resolve(yarn(["config", "get", "globalFolder"]), "cache"),
119+
];
120+
const names = localPackageNames();
121+
122+
for (const dir of cacheDirs) {
123+
if (!existsSync(dir)) {
124+
continue;
125+
}
126+
const stale = readdirSync(dir).filter((file) =>
127+
isYarnCacheEntryOf(file, names),
128+
);
129+
for (const file of stale) {
130+
rmSync(resolve(dir, file));
131+
}
132+
if (stale.length > 0) {
133+
log(`Evicted ${stale.length} local package(s) from ${dir}`);
134+
}
135+
}
136+
}
137+
138+
/** The names of the monorepo packages Verdaccio may serve. */
139+
function localPackageNames(): string[] {
140+
const packagesDir = resolve(ROOT_DIR, "packages");
141+
return readdirSync(packagesDir, { withFileTypes: true })
142+
.filter((entry) =>
143+
existsSync(resolve(packagesDir, entry.name, "package.json")),
144+
)
145+
.map(
146+
(entry) =>
147+
JSON.parse(
148+
readFileSync(
149+
resolve(packagesDir, entry.name, "package.json"),
150+
"utf8",
151+
),
152+
) as { name: string; private?: boolean },
153+
)
154+
.filter((pkg) => pkg.private !== true)
155+
.map((pkg) => pkg.name);
156+
}
157+
158+
/**
159+
* Whether `file` is Yarn Berry's cache zip of an npm version of one of
160+
* `names`: "@scope/name" is slugged to "@scope-name-npm-<version>-…".
161+
*/
162+
export function isYarnCacheEntryOf(file: string, names: string[]): boolean {
163+
return names.some((name) =>
164+
file.startsWith(`${name.replace("/", "-")}-npm-`),
165+
);
166+
}
167+
79168
/**
80169
* Environment for a scenario's package-manager run.
81170
*

0 commit comments

Comments
 (0)