fix(n8n): re-link peer dependencies dropped by pnpm 12 prune --prod

This commit is contained in:
Sascha König committed 2026-10-28 19:38:17 +01:00
1 parent 4a047cb3be
commit 1fa50fe47a
2 files changed
+62 -1

No files matched your search

+46 -1
View File
@@ -32,6 +32,37 @@
fixUpstreamLockfile = ''
sed -i 's/patch_hash=81ead1b07fc8efda87bd2724dbd876ab49f4a0aa03f96c3770060b2ea47d9e0b/patch_hash=0384abb0bf57b0811e09219dbce0b926741d818285ee77526ad4cb75a60b87a1/g' pnpm-lock.yaml
'';
# After `pnpm prune --prod` (pnpm 12), walk every workspace package and
# restore each peerDependency that is no longer linked in its node_modules
# from the hoisted copy in node_modules/.pnpm/node_modules (see the
# preInstall comment below). Links are relative so they survive the move
# into $out. Peers with no hoisted copy (e.g. n8n-workflow in the node-cli
# templates) are left alone — they were never linked before either.
relinkPrunedPeers = ''
echo "Re-linking peer dependencies dropped by pnpm prune"
find packages -name node_modules -prune -o -name package.json -type f -print \
| node -e '
const fs = require("fs"), path = require("path");
const hoisted = path.resolve("node_modules/.pnpm/node_modules");
let relinked = 0;
for (const pkgJson of fs.readFileSync(0, "utf8").split("\n").filter(Boolean)) {
let pkg;
try { pkg = JSON.parse(fs.readFileSync(pkgJson, "utf8")); } catch { continue; }
const pkgDir = path.dirname(pkgJson);
for (const name of Object.keys(pkg.peerDependencies || {})) {
const link = path.join(pkgDir, "node_modules", name);
const target = path.join(hoisted, name);
if (fs.existsSync(link) || !fs.existsSync(target)) continue;
try { fs.unlinkSync(link); } catch {} // dangling leftover
fs.mkdirSync(path.dirname(link), { recursive: true });
fs.symlinkSync(path.relative(path.dirname(link), target), link);
relinked++;
}
}
console.log("Re-linked " + relinked + " pruned peer dependencies");
'
'';
in
prev.n8n.overrideAttrs (finalAttrs: previousAttrs: {
version = "2.45.5";
@@ -77,10 +108,24 @@ in
# files". The lockfile is fixed by pnpmDeps' hash anyway, so disable the
# check for prune and force --offline so any future registry access fails
# fast instead of hanging.
#
# pnpm 12 also changed what `prune --prod` keeps: a peerDependency that a
# workspace package only satisfies via its own devDependencies (n8n's
# convention for zod, pg, sqlite3, @langchain/core, …) is now dropped
# from that package's node_modules, whereas pnpm 11 kept the link. The
# packages are still in the store (other prod deps need them) and, with
# `hoist: true`, still linked from node_modules/.pnpm/node_modules — but
# Node's resolver never looks there, so n8n dies at startup with
# "Cannot find module 'zod'" from packages/workflow. Re-link every
# missing peer from the hoisted directory right after the prune.
preInstall =
builtins.replaceStrings
["CI=true pnpm --ignore-scripts prune --prod -w"]
["CI=true pnpm --ignore-scripts --offline --config.minimum-release-age=0 prune --prod -w"]
[
''
CI=true pnpm --ignore-scripts --offline --config.minimum-release-age=0 prune --prod -w
${relinkPrunedPeers}''
]
previousAttrs.preInstall;
preBuild =
+16
View File
@@ -23,6 +23,13 @@
"ln -s .pnpm/node_modules/sass-embedded node_modules/sass-embedded"
"ln -s .pnpm/node_modules/sqlite3 node_modules/sqlite3"
];
# pnpm 12: prune must run offline without the minimumReleaseAge registry
# check, and peers it drops must be re-linked from the hoisted directory.
expectedPreInstallSnippets = [
"pnpm --ignore-scripts --offline --config.minimum-release-age=0 prune --prod -w"
"Re-linking peer dependencies dropped by pnpm prune"
"node_modules/.pnpm/node_modules"
];
checkN8nOverride = overlayName: package:
if package.version != expectedVersion
@@ -44,6 +51,13 @@
if !(pkgs.lib.all (symlink: pkgs.lib.hasInfix symlink (package.preBuild or "")) expectedRootNodeModuleSymlinks)
then throw "${overlayName} n8n preBuild should create root node_modules symlinks required by the upstream buildPhase"
else "ok";
checkPreInstallPnpm12 = overlayName: package:
if !(pkgs.lib.all (snippet: pkgs.lib.hasInfix snippet (package.preInstall or "")) expectedPreInstallSnippets)
then throw "${overlayName} n8n preInstall should prune offline with pnpm 12 and re-link pruned peer dependencies"
else if pkgs.lib.hasInfix "pnpm --ignore-scripts prune --prod -w" (package.preInstall or "")
then throw "${overlayName} n8n preInstall still contains the unmodified upstream prune command"
else "ok";
in
pkgs.runCommand "n8n-overlay-test" {
n8nOverlayFile =
@@ -60,6 +74,8 @@ in
defaultOverlayBuildPhase = checkBuildPhaseInherited "overlays.default" defaultPkgs.n8n;
modificationsOverlayRootNodeModuleSymlinks = checkRootNodeModuleSymlinks "overlays.modifications" modifiedPkgs.n8n;
defaultOverlayRootNodeModuleSymlinks = checkRootNodeModuleSymlinks "overlays.default" defaultPkgs.n8n;
modificationsOverlayPreInstall = checkPreInstallPnpm12 "overlays.modifications" modifiedPkgs.n8n;
defaultOverlayPreInstall = checkPreInstallPnpm12 "overlays.default" defaultPkgs.n8n;
} ''
touch $out
''