Replies: 1 comment
|
There's no built-in command for this as far as I know, but there's a reliable test you can automate: an override is no longer needed if removing it doesn't change the lockfile. If the resolved versions are identical without it, nothing in your tree still pulls the vulnerable version. One catch: overrides can overlap (e.g. This script does that (run it from the directory with your // find-unused-overrides.mjs
import { readFileSync, writeFileSync, copyFileSync } from "node:fs";
import { execSync } from "node:child_process";
const pkgPath = "package.json", lockPath = "pnpm-lock.yaml";
const pkgSrc = readFileSync(pkgPath, "utf8");
const pkg = JSON.parse(pkgSrc);
const strip = s => s.replace(/^overrides:\n(?: .*\n)*/m, ""); // ignore the overrides block itself
const lock = () => strip(readFileSync(lockPath, "utf8"));
copyFileSync(lockPath, lockPath + ".bak");
const base = lock();
const unused = [];
try {
for (const key of Object.keys(pkg.pnpm?.overrides ?? {})) {
const value = pkg.pnpm.overrides[key];
delete pkg.pnpm.overrides[key];
writeFileSync(pkgPath, JSON.stringify(pkg, null, 2) + "\n");
execSync("pnpm install --lockfile-only --ignore-scripts", { stdio: "ignore" });
if (lock() === base) unused.push(key);
else pkg.pnpm.overrides[key] = value; // still needed, put it back
}
} finally {
writeFileSync(pkgPath, pkgSrc);
copyFileSync(lockPath + ".bak", lockPath);
}
console.log(unused.length ? "No effect (safe to remove together):\n " + unused.join("\n ")
: "Every override still changes the lockfile.");Tested on pnpm 10.18 with Notes:
|
Uh oh!
There was an error while loading. Please reload this page.
what is the best management strategy for pnpm overrides?
We use overrides for quick vuln remediation, but eventually packages get upgraded or dropped and thus the overrides are no longer needed. Its probably not hurting anything but whats the best way to know that an override is no longer needed and will no longer have a vulnerability and thus can be removed?
Im looking for something that can be automated. If i have 30 overrides, removing and checking each one individually is going to be tedious.
All reactions