From e2fdc7970ede4921ee8d86ae1a7eebbc090b34d5 Mon Sep 17 00:00:00 2001 From: Milan Meva Date: Wed, 19 Jun 2024 17:02:34 -0400 Subject: [PATCH] fix(arborist): condition to include name field in package-lock fixed --- workspaces/arborist/lib/shrinkwrap.js | 4 ++- .../tap-snapshots/test/shrinkwrap.js.test.cjs | 33 +++++++++++++++++++ workspaces/arborist/test/shrinkwrap.js | 31 ++++++++++++++++- 3 files changed, 66 insertions(+), 2 deletions(-) diff --git a/workspaces/arborist/lib/shrinkwrap.js b/workspaces/arborist/lib/shrinkwrap.js index 6f6c17cb95374..5f720ed9bd440 100644 --- a/workspaces/arborist/lib/shrinkwrap.js +++ b/workspaces/arborist/lib/shrinkwrap.js @@ -50,6 +50,7 @@ const versionFromTgz = require('./version-from-tgz.js') const npa = require('npm-package-arg') const pkgJson = require('@npmcli/package-json') const parseJSON = require('parse-conflict-json') +const nameFromFolder = require('@npmcli/name-from-folder') const stringify = require('json-stringify-nice') const swKeyOrder = [ @@ -233,7 +234,8 @@ class Shrinkwrap { // root to help prevent churn based on the name of the directory the // project is in const pname = node.packageName - if (pname && (node === node.root || pname !== node.name)) { + // when Target package name and Target node share the same name, we include the name, target node should have name as per realpath. + if (pname && (node === node.root || pname !== node.name || nameFromFolder(node.realpath) !== pname)) { meta.name = pname } diff --git a/workspaces/arborist/tap-snapshots/test/shrinkwrap.js.test.cjs b/workspaces/arborist/tap-snapshots/test/shrinkwrap.js.test.cjs index 1b5a393f8846d..dab53a6800794 100644 --- a/workspaces/arborist/tap-snapshots/test/shrinkwrap.js.test.cjs +++ b/workspaces/arborist/tap-snapshots/test/shrinkwrap.js.test.cjs @@ -280,6 +280,7 @@ Object { "a": "", "link": "", "link2": "", + "link3": "", }, "devDependencies": Object { "d": "", @@ -349,6 +350,10 @@ Object { "link": true, "resolved": "target", }, + "node_modules/link3": Object { + "link": true, + "resolved": "realPkg", + }, "node_modules/nopkg": Object { "extraneous": true, }, @@ -401,6 +406,14 @@ Object { "resolved": "file:archives/tarball-pkg-resolved.tgz", "version": "1.2.3", }, + "realPkg": Object { + "funding": Object { + "url": "https://example.com/payme", + }, + "name": "link3", + "resolved": "git+ssh://git@github.com/isaacs/foobarbaz.git#aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", + "version": "1.2.3", + }, "target": Object { "funding": Object { "url": "https://example.com/payme", @@ -466,6 +479,13 @@ Object { } ` +exports[`test/shrinkwrap.js TAP construct metadata from node and package data > link metadata with same pkg name as link 1`] = ` +Object { + "link": true, + "resolved": "realPkg", +} +` + exports[`test/shrinkwrap.js TAP construct metadata from node and package data > link target metadata 1`] = ` Object { "funding": Object { @@ -477,6 +497,17 @@ Object { } ` +exports[`test/shrinkwrap.js TAP construct metadata from node and package data > link target metadata same pkg name as link node 1`] = ` +Object { + "funding": Object { + "url": "https://example.com/payme", + }, + "name": "link3", + "resolved": "git+ssh://git@github.com/isaacs/foobarbaz.git#aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", + "version": "1.2.3", +} +` + exports[`test/shrinkwrap.js TAP construct metadata from node and package data > meta for dev dep 1`] = ` Object { "dependencies": Object { @@ -549,6 +580,7 @@ Object { "a": "", "link": "", "link2": "", + "link3": "", }, "devDependencies": Object { "d": "", @@ -572,6 +604,7 @@ Object { "a": "", "link": "", "link2": "", + "link3": "", }, "devDependencies": Object { "d": "", diff --git a/workspaces/arborist/test/shrinkwrap.js b/workspaces/arborist/test/shrinkwrap.js index e3c604c096631..243af08ad81db 100644 --- a/workspaces/arborist/test/shrinkwrap.js +++ b/workspaces/arborist/test/shrinkwrap.js @@ -331,7 +331,7 @@ t.test('construct metadata from node and package data', t => { const root = new Node({ pkg: { name: 'root', - dependencies: { a: '', link: '', link2: '' }, + dependencies: { a: '', link: '', link2: '', link3: '' }, devDependencies: { d: '', e: 'https://foo.com/e.tgz', devit: '' }, optionalDependencies: { optin: '' }, peerDependencies: { peer: '' }, @@ -430,6 +430,31 @@ t.test('construct metadata from node and package data', t => { }, }), }) + // special case when link alias is the same as target package name + const link3 = new Link({ + name: 'link3', + path: '/home/user/projects/root/node_modules/link3', + realpath: '/home/user/projects/root/realPkg', + parent: root, + pkg: { + name: 'link3', + version: '1.2.3', + funding: 'https://example.com/payme', + _resolved: 'github:isaacs/foobarbaz#aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', + }, + target: new Node({ + name: 'link3', + realpath: '/home/user/projects/root/realPkg', + path: '/home/user/projects/root/realPkg', + root, + pkg: { + name: 'link3', + version: '1.2.3', + funding: 'https://example.com/payme', + _resolved: 'github:isaacs/foobarbaz#aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', + }, + }), + }) const a = new Node({ resolved: 'https://example.com/a.tgz', @@ -520,6 +545,10 @@ t.test('construct metadata from node and package data', t => { 'metadata for tarball file pkg with _resolved value') t.matchSnapshot(meta.get(link.path), 'link metadata') t.matchSnapshot(meta.get(link.target.path), 'link target metadata') + + t.matchSnapshot(meta.get(link3.path), 'link metadata with same pkg name as link') + t.matchSnapshot(meta.get(link3.target.path), 'link target metadata same pkg name as link node') + t.matchSnapshot(meta.get(a.location), 'dep a metadata') t.matchSnapshot(meta.get(d.location), 'dep d metadata') t.matchSnapshot(meta.get(e.location), 'dep e metadata')