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 d5448bbcba927..cb382b0ce456f 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 && root.workspaces && root.workspaces.length) { + 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,