Skip to content

Commit 57952b8

Browse files
MaxBlack-devMax Black
andauthored
fix: prevent crash when expanding directories.bin without filesystem path (#164)
Fixes npm/cli#8722 When normalizing a package manifest fetched from a registry (without a filesystem path), attempting to expand 'directories.bin' would crash with ERR_INVALID_ARG_TYPE because pkg.path is undefined. This adds a check to only expand directories.bin when pkg.path exists, preventing the crash while maintaining correct behavior for packages with a filesystem location. **Impact:** - Affects users with custom/private registries (GitLab, Artifactory, etc.) that don't pre-calculate the 'bin' field - The npm public registry pre-calculates this field, which is why the issue isn't visible there - Multiple users have reported this issue in npm/cli#8722 **Testing:** - All existing tests pass - The fix is minimal and non-breaking: it simply skips the binDir expansion when no filesystem path is available --------- Co-authored-by: Max Black <[email protected]>
1 parent ba5c736 commit 57952b8

2 files changed

Lines changed: 15 additions & 1 deletion

File tree

lib/normalize.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,7 @@ async function asyncSteps (pkg, { steps, root, changes }) {
474474
}
475475

476476
// expand "directories.bin"
477-
if (steps.includes('binDir') && data.directories?.bin && !data.bin) {
477+
if (steps.includes('binDir') && data.directories?.bin && !data.bin && pkg.path) {
478478
const binPath = secureAndUnixifyPath(data.directories.bin)
479479
const bins = await lazyLoadGlob()('**', { cwd: path.resolve(pkg.path, binPath) })
480480
data.bin = bins.reduce((acc, binFile) => {

test/normalize.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,20 @@ for (const [name, testNormalize] of Object.entries(testMethods)) {
172172
t.strictSame(content.bin, { echo: 'bin/echo' })
173173
})
174174

175+
t.test('directories.bin without filesystem path', async t => {
176+
const p = new Pkg()
177+
p.fromContent({
178+
name: 'registry-test',
179+
version: '1.0.0',
180+
directories: {
181+
bin: './bin',
182+
},
183+
})
184+
await p.normalize({ steps: Pkg.normalizeSteps })
185+
t.notOk(p.content.bin, 'bin should not be expanded without filesystem path')
186+
t.ok(p.content.directories.bin, 'directories.bin should be preserved')
187+
})
188+
175189
t.test('dedupe optional deps out of regular deps', async t => {
176190
t.test('choose optional deps in conflict, removing empty dependencies', async t => {
177191
const { content } = await testNormalize(t, ({

0 commit comments

Comments
 (0)