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: skip missing optional deps when bundling, closes npm/cli#5924 #149

Merged
merged 1 commit into from
Dec 7, 2022
Merged
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
5 changes: 5 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,11 @@ class PackWalker extends IgnoreWalker {

// get a reference to the node we're bundling
const node = this.tree.edgesOut.get(dep).to
// if there's no node, this is most likely an optional dependency that hasn't been
// installed. just skip it.
if (!node) {
continue
}
// we use node.path for the path because we want the location the node was linked to,
// not where it actually lives on disk
const path = node.path
Expand Down
52 changes: 52 additions & 0 deletions test/bundled-missing-optional.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
'use strict'

const Arborist = require('@npmcli/arborist')
const t = require('tap')
const packlist = require('../')

const elfJS = `
module.exports = elf =>
console.log("i'm a elf")
`

t.test('includes bundled dependency using bundleDependencies', async (t) => {
const pkg = t.testdir({
'package.json': JSON.stringify({
name: 'test-package',
version: '3.1.4',
main: 'elf.js',
dependencies: {
history: '1.0.0',
},
bundleDependencies: [
'history',
],
}),
'elf.js': elfJS,
'.npmrc': 'packaged=false',
node_modules: {
history: {
'package.json': JSON.stringify({
name: 'history',
version: '1.0.0',
main: 'index.js',
optionalDependencies: {
// defined here, but not installed
optionalDep: '^1.0.0',
},
}),
'index.js': elfJS,
},
},
})

const arborist = new Arborist({ path: pkg })
const tree = await arborist.loadActual()
const files = await packlist(tree)
t.same(files, [
'elf.js',
'node_modules/history/index.js',
'node_modules/history/package.json',
'package.json',
])
})