Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions test/lib/commands/install.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const {
} = require('../../fixtures/clean-snapshot.js')

const path = require('node:path')
const { rmSync } = require('node:fs')
const t = require('tap')

t.cleanSnapshot = (str) => cleanPackumentCache(cleanDate(cleanTime(cleanCwd(str))))
Expand Down Expand Up @@ -428,6 +429,38 @@ t.test('should show install keeps dirty --workspace flag', async t => {
assert.packageInstalled('node_modules/[email protected]')
})

t.test('should remove non-existent workspace from package-lock.json', async t => {
const cwd = process.cwd()

t.afterEach(() => {
process.chdir(cwd)
})

const localPrefix = path.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(path.join(localPrefix, 'packages/b'), { recursive: true, force: true })
await npm.exec('install', [])
const lockJson = require(path.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')
})

t.test('devEngines', async t => {
const mockArguments = {
globals: {
Expand Down
11 changes: 11 additions & 0 deletions workspaces/arborist/lib/shrinkwrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -927,7 +927,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
Loading