From 4aa41ef3a6fb2489479059ba3701d1c4f043fb6c Mon Sep 17 00:00:00 2001 From: Zoltan Kochan Date: Wed, 11 Oct 2023 17:55:54 +0300 Subject: [PATCH] perf(rebuild): only link bins if something was built (#7193) --- .changeset/seven-moles-deny.md | 5 ++ .../src/implementation/index.ts | 46 ++++++++++--------- 2 files changed, 30 insertions(+), 21 deletions(-) create mode 100644 .changeset/seven-moles-deny.md diff --git a/.changeset/seven-moles-deny.md b/.changeset/seven-moles-deny.md new file mode 100644 index 0000000000..6073acdee6 --- /dev/null +++ b/.changeset/seven-moles-deny.md @@ -0,0 +1,5 @@ +--- +"@pnpm/plugin-commands-rebuild": patch +--- + +Only link bins after rebuild, when at least one dependency was actually built. diff --git a/exec/plugin-commands-rebuild/src/implementation/index.ts b/exec/plugin-commands-rebuild/src/implementation/index.ts index d4cdfbf7cb..b6f8055397 100644 --- a/exec/plugin-commands-rebuild/src/implementation/index.ts +++ b/exec/plugin-commands-rebuild/src/implementation/index.ts @@ -285,6 +285,7 @@ async function _rebuild ( } const allowBuild = createAllowBuildFunction(opts) ?? (() => true) + const builtDepPaths = new Set() const groups = chunks.map((chunk) => chunk.filter((depPath) => ctx.pkgsToRebuild.has(depPath) && !ctx.skipped.has(depPath)).map((depPath) => async () => { @@ -336,6 +337,7 @@ async function _rebuild ( unsafePerm: opts.unsafePerm || false, }) if (hasSideEffects && (opts.sideEffectsCacheWrite ?? true) && resolution.integrity) { + builtDepPaths.add(depPath) const filesIndexFile = getFilePathInCafs(cafsDir, resolution.integrity!.toString(), 'index') try { if (!sideEffectsCacheKey) { @@ -388,27 +390,29 @@ async function _rebuild ( await runGroups(opts.childConcurrency || 5, groups) - // It may be optimized because some bins were already linked before running lifecycle scripts - await Promise.all( - Object - .keys(pkgSnapshots) - .filter((depPath) => !packageIsIndependent(pkgSnapshots[depPath])) - .map(async (depPath) => limitLinking(async () => { - const pkgSnapshot = pkgSnapshots[depPath] - const pkgInfo = nameVerFromPkgSnapshot(depPath, pkgSnapshot) - const modules = path.join(ctx.virtualStoreDir, dp.depPathToFilename(depPath), 'node_modules') - const binPath = path.join(modules, pkgInfo.name, 'node_modules', '.bin') - return linkBins(modules, binPath, { warn }) - })) - ) - await Promise.all(Object.values(ctx.projects).map(async ({ rootDir }) => limitLinking(async () => { - const modules = path.join(rootDir, 'node_modules') - const binPath = path.join(modules, '.bin') - return linkBins(modules, binPath, { - allowExoticManifests: true, - warn, - }) - }))) + if (builtDepPaths.size > 0) { + // It may be optimized because some bins were already linked before running lifecycle scripts + await Promise.all( + Object + .keys(pkgSnapshots) + .filter((depPath) => !packageIsIndependent(pkgSnapshots[depPath])) + .map(async (depPath) => limitLinking(async () => { + const pkgSnapshot = pkgSnapshots[depPath] + const pkgInfo = nameVerFromPkgSnapshot(depPath, pkgSnapshot) + const modules = path.join(ctx.virtualStoreDir, dp.depPathToFilename(depPath), 'node_modules') + const binPath = path.join(modules, pkgInfo.name, 'node_modules', '.bin') + return linkBins(modules, binPath, { warn }) + })) + ) + await Promise.all(Object.values(ctx.projects).map(async ({ rootDir }) => limitLinking(async () => { + const modules = path.join(rootDir, 'node_modules') + const binPath = path.join(modules, '.bin') + return linkBins(modules, binPath, { + allowExoticManifests: true, + warn, + }) + }))) + } return pkgsThatWereRebuilt }