Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
34 changes: 34 additions & 0 deletions lib/commands/ls.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ class LS extends ArboristWorkspaceCmd {
const unicode = this.npm.config.get('unicode')
const packageLockOnly = this.npm.config.get('package-lock-only')
const workspacesEnabled = this.npm.flatOptions.workspacesEnabled
const installStrategy = this.npm.flatOptions.installStrategy

const path = global ? resolve(this.npm.globalDir, '..') : this.npm.prefix

Expand Down Expand Up @@ -136,6 +137,9 @@ class LS extends ArboristWorkspaceCmd {
link,
omit,
}) : () => true)
.filter(installStrategy === 'linked'
? filterLinkedStrategyEdges({ node, currentDepth })
: () => true)
.map(mapEdgesToNodes({ seenPaths }))
.concat(appendExtraneousChildren({ node, seenPaths }))
.sort(sortAlphabetically)
Expand Down Expand Up @@ -403,6 +407,36 @@ const getJsonOutputItem = (node, { global, long }) => {
return augmentItemWithIncludeMetadata(node, item)
}

// In linked strategy, two types of edges produce false UNMET DEPENDENCYs:
// 1. Workspace edges for undeclared workspaces: the lockfile records edges from root to ALL workspaces, but only declared workspaces are hoisted to root/node_modules in linked mode. Undeclared ones are intentionally absent.
// 2. Dev edges on non-root packages: store package link targets have no parent in the node tree, so they are treated as "top" nodes and their devDependencies are loaded as edges. Those devDeps are never installed.
const filterLinkedStrategyEdges = ({ node, currentDepth }) => {
const declaredDeps = currentDepth === 0
? new Set(Object.keys(Object.assign({},

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this approach is cleaner than what we're doing with things like this.#rootDeclaredDeps in makeIdealGraph.

    this.#rootDeclaredDeps = new Set([
      ...Object.keys(rootPkg.dependencies || {}),
      ...(!omit.has('dev') ? Object.keys(rootPkg.devDependencies || {}) : []),
      ...(!omit.has('optional') ? Object.keys(rootPkg.optionalDependencies || {}) : []),
      ...(!omit.has('peer') ? Object.keys(rootPkg.peerDependencies || {}) : []),
    ])

could become

    this.#rootDeclaredDeps = new Set(Object.keys(Object.assign({},
      rootPkg.dependencies,
      (!omit.has('dev') && rootPkg.devDependencies),
      (!omit.has('optional') && rootPkg.optionalDependencies),
      (!omit.has('peer') && rootPkg.peerDependencies),
    )))

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes! That refactor in isolated-reifier.js would be a nice follow-up.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or should I make that change here? It doesn't seem to affect the coverage, so the changes should be minimal.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated in #9097

node.target.package.dependencies,
node.target.package.devDependencies,
node.target.package.optionalDependencies,
node.target.package.peerDependencies
)))
: null
Comment thread
manzoorwanijk marked this conversation as resolved.
Outdated

return (edge) => {
// Skip workspace edges for undeclared workspaces at root level
if (currentDepth === 0 && edge.type === 'workspace' && edge.missing) {
if (!declaredDeps.has(edge.name)) {
return false
}
}

// Skip dev edges for non-root packages (store packages)
if (currentDepth > 0 && edge.dev) {
return false
}

return true
}
}

const filterByEdgesTypes = ({ link, omit }) => (edge) => {
for (const omitType of omit) {
if (edge[omitType]) {
Expand Down
104 changes: 104 additions & 0 deletions test/lib/commands/ls.js
Original file line number Diff line number Diff line change
Expand Up @@ -5301,3 +5301,107 @@ t.test('completion', async t => {
const res = await ls.completion({ conf: { argv: { remain: ['npm', 'ls'] } } })
t.type(res, Array)
})

t.test('ls --install-strategy=linked', async t => {
t.test('should not report undeclared workspaces as UNMET DEPENDENCY', async t => {
const { result, ls } = await mockLs(t, {
config: {
'install-strategy': 'linked',
},
prefixDir: {
'package.json': JSON.stringify({
name: 'test-linked-ws',
version: '1.0.0',
workspaces: ['packages/*'],
dependencies: { 'workspace-a': '*' },
}),
packages: {
'workspace-a': {
'package.json': JSON.stringify({
name: 'workspace-a',
version: '1.0.0',
}),
},
'workspace-b': {
'package.json': JSON.stringify({
name: 'workspace-b',
version: '1.0.0',
}),
},
},
node_modules: {
'workspace-a': t.fixture('symlink', '../packages/workspace-a'),
// workspace-b intentionally NOT linked (undeclared in dependencies)
},
},
})
await ls.exec([])
const output = cleanCwd(result())
t.notMatch(output, /UNMET DEPENDENCY/, 'should not report undeclared workspace as UNMET DEPENDENCY')
t.match(output, /workspace-a/, 'should list declared workspace')
})

t.test('should not report devDeps of store packages as UNMET DEPENDENCY', async t => {
const { result, ls } = await mockLs(t, {
config: {
'install-strategy': 'linked',
},
prefixDir: {
'package.json': JSON.stringify({
name: 'test-linked-store',
version: '1.0.0',
dependencies: { nopt: '^1.0.0' },
}),
node_modules: {
nopt: t.fixture('symlink', '.store/nopt@1.0.0/node_modules/nopt'),
'.store': {
'nopt@1.0.0': {
node_modules: {
nopt: {
'package.json': JSON.stringify({
name: 'nopt',
version: '1.0.0',
devDependencies: { tap: '^16.0.0' },
}),
},
},
},
},
},
},
})
await ls.exec([])
const output = cleanCwd(result())
t.notMatch(output, /UNMET DEPENDENCY/, 'should not report devDeps of store packages')
t.match(output, /nopt/, 'should list the dependency')
})

t.test('should still report declared workspace as UNMET DEPENDENCY when missing', async t => {
const { ls } = await mockLs(t, {
config: {
'install-strategy': 'linked',
},
prefixDir: {
'package.json': JSON.stringify({
name: 'test-linked-ws-missing',
version: '1.0.0',
workspaces: ['packages/*'],
dependencies: { 'workspace-a': '*' },
}),
packages: {
'workspace-a': {
'package.json': JSON.stringify({
name: 'workspace-a',
version: '1.0.0',
}),
},
},
node_modules: {
// workspace-a is declared but its symlink is missing
},
},
})
await t.rejects(ls.exec([]), { code: 'ELSPROBLEMS' },
'should report declared workspace as UNMET DEPENDENCY')
})
})
Loading