Skip to content

Commit a8e666e

Browse files
authored
fix(arborist): condition to include name field in package-lock fixed (#7602)
When metadata is committed for the first time when there is no package-lock, when target node has the same name field value as target package name and link node also share the same name field, name field is omitted from lock file, in subsequent times when there is already a lock file, it reads target node with name field derived from realpath value of the node and included in lock file. this creates mismatch of lock file between installs. This PR adds additional condition to check if name derived from realpath is the same name as package and adds the name property. Fixes: #7166
1 parent 55639ef commit a8e666e

File tree

3 files changed

+66
-2
lines changed

3 files changed

+66
-2
lines changed

workspaces/arborist/lib/shrinkwrap.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ const versionFromTgz = require('./version-from-tgz.js')
5050
const npa = require('npm-package-arg')
5151
const pkgJson = require('@npmcli/package-json')
5252
const parseJSON = require('parse-conflict-json')
53+
const nameFromFolder = require('@npmcli/name-from-folder')
5354

5455
const stringify = require('json-stringify-nice')
5556
const swKeyOrder = [
@@ -233,7 +234,8 @@ class Shrinkwrap {
233234
// root to help prevent churn based on the name of the directory the
234235
// project is in
235236
const pname = node.packageName
236-
if (pname && (node === node.root || pname !== node.name)) {
237+
// when Target package name and Target node share the same name, we include the name, target node should have name as per realpath.
238+
if (pname && (node === node.root || pname !== node.name || nameFromFolder(node.realpath) !== pname)) {
237239
meta.name = pname
238240
}
239241

workspaces/arborist/tap-snapshots/test/shrinkwrap.js.test.cjs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,7 @@ Object {
280280
"a": "",
281281
"link": "",
282282
"link2": "",
283+
"link3": "",
283284
},
284285
"devDependencies": Object {
285286
"d": "",
@@ -349,6 +350,10 @@ Object {
349350
"link": true,
350351
"resolved": "target",
351352
},
353+
"node_modules/link3": Object {
354+
"link": true,
355+
"resolved": "realPkg",
356+
},
352357
"node_modules/nopkg": Object {
353358
"extraneous": true,
354359
},
@@ -401,6 +406,14 @@ Object {
401406
"resolved": "file:archives/tarball-pkg-resolved.tgz",
402407
"version": "1.2.3",
403408
},
409+
"realPkg": Object {
410+
"funding": Object {
411+
"url": "https://example.com/payme",
412+
},
413+
"name": "link3",
414+
"resolved": "git+ssh://[email protected]/isaacs/foobarbaz.git#aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
415+
"version": "1.2.3",
416+
},
404417
"target": Object {
405418
"funding": Object {
406419
"url": "https://example.com/payme",
@@ -466,6 +479,13 @@ Object {
466479
}
467480
`
468481

482+
exports[`test/shrinkwrap.js TAP construct metadata from node and package data > link metadata with same pkg name as link target 1`] = `
483+
Object {
484+
"link": true,
485+
"resolved": "realPkg",
486+
}
487+
`
488+
469489
exports[`test/shrinkwrap.js TAP construct metadata from node and package data > link target metadata 1`] = `
470490
Object {
471491
"funding": Object {
@@ -477,6 +497,17 @@ Object {
477497
}
478498
`
479499

500+
exports[`test/shrinkwrap.js TAP construct metadata from node and package data > link target metadata with same pkg name as link target 1`] = `
501+
Object {
502+
"funding": Object {
503+
"url": "https://example.com/payme",
504+
},
505+
"name": "link3",
506+
"resolved": "git+ssh://[email protected]/isaacs/foobarbaz.git#aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
507+
"version": "1.2.3",
508+
}
509+
`
510+
480511
exports[`test/shrinkwrap.js TAP construct metadata from node and package data > meta for dev dep 1`] = `
481512
Object {
482513
"dependencies": Object {
@@ -549,6 +580,7 @@ Object {
549580
"a": "",
550581
"link": "",
551582
"link2": "",
583+
"link3": "",
552584
},
553585
"devDependencies": Object {
554586
"d": "",
@@ -572,6 +604,7 @@ Object {
572604
"a": "",
573605
"link": "",
574606
"link2": "",
607+
"link3": "",
575608
},
576609
"devDependencies": Object {
577610
"d": "",

workspaces/arborist/test/shrinkwrap.js

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ t.test('construct metadata from node and package data', t => {
331331
const root = new Node({
332332
pkg: {
333333
name: 'root',
334-
dependencies: { a: '', link: '', link2: '' },
334+
dependencies: { a: '', link: '', link2: '', link3: '' },
335335
devDependencies: { d: '', e: 'https://foo.com/e.tgz', devit: '' },
336336
optionalDependencies: { optin: '' },
337337
peerDependencies: { peer: '' },
@@ -430,6 +430,31 @@ t.test('construct metadata from node and package data', t => {
430430
},
431431
}),
432432
})
433+
// special case when link alias is the same as target package name
434+
const link3 = new Link({
435+
name: 'link3',
436+
path: '/home/user/projects/root/node_modules/link3',
437+
realpath: '/home/user/projects/root/realPkg',
438+
parent: root,
439+
pkg: {
440+
name: 'link3',
441+
version: '1.2.3',
442+
funding: 'https://example.com/payme',
443+
_resolved: 'github:isaacs/foobarbaz#aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',
444+
},
445+
target: new Node({
446+
name: 'link3',
447+
realpath: '/home/user/projects/root/realPkg',
448+
path: '/home/user/projects/root/realPkg',
449+
root,
450+
pkg: {
451+
name: 'link3',
452+
version: '1.2.3',
453+
funding: 'https://example.com/payme',
454+
_resolved: 'github:isaacs/foobarbaz#aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',
455+
},
456+
}),
457+
})
433458

434459
const a = new Node({
435460
resolved: 'https://example.com/a.tgz',
@@ -520,6 +545,10 @@ t.test('construct metadata from node and package data', t => {
520545
'metadata for tarball file pkg with _resolved value')
521546
t.matchSnapshot(meta.get(link.path), 'link metadata')
522547
t.matchSnapshot(meta.get(link.target.path), 'link target metadata')
548+
549+
t.matchSnapshot(meta.get(link3.path), 'link metadata with same pkg name as link target')
550+
t.matchSnapshot(meta.get(link3.target.path), 'link target metadata with same pkg name as link target')
551+
523552
t.matchSnapshot(meta.get(a.location), 'dep a metadata')
524553
t.matchSnapshot(meta.get(d.location), 'dep d metadata')
525554
t.matchSnapshot(meta.get(e.location), 'dep e metadata')

0 commit comments

Comments
 (0)