Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: clear non-exist workspace part in package-lock.json when npm install (fix #5463) #5478

Open
wants to merge 4 commits into
base: latest
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
36 changes: 36 additions & 0 deletions 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')

Expand Down Expand Up @@ -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')
})
})
11 changes: 11 additions & 0 deletions workspaces/arborist/lib/shrinkwrap.js
Expand Up @@ -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,
Expand Down