From 7795a7cf71ffe880535cdd06562e24819cfb7f5c Mon Sep 17 00:00:00 2001 From: ivan Date: Wed, 7 Sep 2022 21:19:39 +0800 Subject: [PATCH 1/3] fix: clear non-exist workspace part in package-lock.json when npm install (fix #5463) --- workspaces/arborist/lib/shrinkwrap.js | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/workspaces/arborist/lib/shrinkwrap.js b/workspaces/arborist/lib/shrinkwrap.js index d5448bbcba927..4ef3202ca747a 100644 --- a/workspaces/arborist/lib/shrinkwrap.js +++ b/workspaces/arborist/lib/shrinkwrap.js @@ -922,7 +922,18 @@ class Shrinkwrap { if (node === this.tree || node.isRoot || node.location === '') { continue } + const loc = relpath(this.path, node.path) + + // extraneous ws node should not commit + if(node.extraneous) { + const wsIndex = root.workspaces.findIndex(ws => ws === loc) + if(wsIndex > -1) { + root.workspaces.splice(wsIndex, 1) + continue + } + } + this.data.packages[loc] = Shrinkwrap.metaFromNode( node, this.path, From 17eeba623125afdb103366357d03d02fde3ad9ae Mon Sep 17 00:00:00 2001 From: ivan Date: Fri, 16 Sep 2022 11:18:18 +0800 Subject: [PATCH 2/3] test: add test case for remove non-exist workspace node --- test/lib/commands/install.js | 36 +++++++++++++++++++++++++++ workspaces/arborist/lib/shrinkwrap.js | 2 +- 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/test/lib/commands/install.js b/test/lib/commands/install.js index 632ca22be9e71..3bac44b1b0ba8 100644 --- a/test/lib/commands/install.js +++ b/test/lib/commands/install.js @@ -1,4 +1,6 @@ const t = require('tap') +const { join } = require('path') +const { rmSync } = require('fs') const { load: _loadMockNpm } = require('../../fixtures/mock-npm') @@ -327,3 +329,37 @@ t.test('completion', async t => { t.strictSame(res, []) }) }) + +t.test('workspace', async it => { + const cwd = process.cwd() + + t.afterEach(() => { + process.chdir(cwd) + }) + + t.test('remove non-exist workspace node and its dependency', async t => { + const localPrefix = join(t.testdir(), 'prefix') + const { npm } = await loadMockNpm(t) + + process.chdir(localPrefix) + // init and install root package and its workspaces + npm.config.set('yes', true) + await npm.exec('init', []) + npm.config.set('workspace', ['packages/a', 'packages/b']) + await npm.exec('init', []) + await npm.exec('install', []) + + // remove one workspace node and reinstall + rmSync(join(localPrefix, 'packages/b'), { recursive: true, force: true }) + await npm.exec('install', []) + const lockJson = require(join(localPrefix, 'package-lock.json')) + + t.strictSame(lockJson.packages[''].workspaces, ['packages/a'], 'remove non-exist ws node') + t.equal(lockJson.packages['packages/b'], undefined, 'remove non-exist ws package') + t.equal(lockJson.packages['node_modules/b'], + undefined, + 'remove non-exist ws node_modules package' + ) + t.equal(lockJson.dependencies.b, undefined, 'remove non-exist ws dependency') + }) +}) diff --git a/workspaces/arborist/lib/shrinkwrap.js b/workspaces/arborist/lib/shrinkwrap.js index 4ef3202ca747a..cb8f1a3841457 100644 --- a/workspaces/arborist/lib/shrinkwrap.js +++ b/workspaces/arborist/lib/shrinkwrap.js @@ -926,7 +926,7 @@ class Shrinkwrap { const loc = relpath(this.path, node.path) // extraneous ws node should not commit - if(node.extraneous) { + if(node.extraneous && root.workspaces && root.workspaces.length) { const wsIndex = root.workspaces.findIndex(ws => ws === loc) if(wsIndex > -1) { root.workspaces.splice(wsIndex, 1) From c0683b47bf541a9ed125fe01afaa495af0c419b6 Mon Sep 17 00:00:00 2001 From: ivan Date: Thu, 22 Sep 2022 10:32:44 +0800 Subject: [PATCH 3/3] style(shrinkwrap): lint --- workspaces/arborist/lib/shrinkwrap.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/workspaces/arborist/lib/shrinkwrap.js b/workspaces/arborist/lib/shrinkwrap.js index cb8f1a3841457..cb382b0ce456f 100644 --- a/workspaces/arborist/lib/shrinkwrap.js +++ b/workspaces/arborist/lib/shrinkwrap.js @@ -926,9 +926,9 @@ class Shrinkwrap { const loc = relpath(this.path, node.path) // extraneous ws node should not commit - if(node.extraneous && root.workspaces && root.workspaces.length) { + if (node.extraneous && root.workspaces && root.workspaces.length) { const wsIndex = root.workspaces.findIndex(ws => ws === loc) - if(wsIndex > -1) { + if (wsIndex > -1) { root.workspaces.splice(wsIndex, 1) continue }