From 8b457c2395d0c01dc93e0765e5ee451bafba57b1 Mon Sep 17 00:00:00 2001 From: isaacs Date: Fri, 11 Dec 2020 10:41:28 -0800 Subject: [PATCH] Do not save with ^ and no version Fix: https://github.com/npm/cli/issues/2320 PR-URL: https://github.com/npm/arborist/pull/192 Credit: @isaacs Close: #192 Reviewed-by: @ruyadorno --- lib/arborist/reify.js | 2 +- .../test-arborist-reify.js-TAP.test.js | 14 +++++++ test/arborist/reify.js | 42 ++++++++++++++++++- 3 files changed, 55 insertions(+), 3 deletions(-) diff --git a/lib/arborist/reify.js b/lib/arborist/reify.js index b16f20855..1f979ba14 100644 --- a/lib/arborist/reify.js +++ b/lib/arborist/reify.js @@ -808,7 +808,7 @@ module.exports = cls => class Reifier extends cls { if (req.registry) { const version = child.version - const prefixRange = this[_savePrefix] + version + const prefixRange = version ? this[_savePrefix] + version : '*' // if we installed a range, then we save the range specified // if it is not a subset of the ^x.y.z. eg, installing a range // of `1.x <1.2.3` will not be saved as `^1.2.0`, because that diff --git a/tap-snapshots/test-arborist-reify.js-TAP.test.js b/tap-snapshots/test-arborist-reify.js-TAP.test.js index b35100ef1..01863869b 100644 --- a/tap-snapshots/test-arborist-reify.js-TAP.test.js +++ b/tap-snapshots/test-arborist-reify.js-TAP.test.js @@ -27202,6 +27202,9 @@ Object { "resolved": "https://registry.npmjs.org/c/-/c-1.2.3.tgz", "version": "npm:c@1.2.3", }, + "e": Object { + "version": "file:e", + }, }, "lockfileVersion": 2, "name": "reify-saving-the-ideal-tree-save-some-stuff", @@ -27216,10 +27219,17 @@ Object { "a": "github:foo/bar#baz", "b": "^1.2.3", "d": "npm:c@1.x <1.9.9", + "e": "*", }, "devDependencies": Object { "c": "git+ssh://git@githost.com:a/b/c.git#master", }, + "workspaces": Array [ + "e", + ], + }, + "e": Object { + "extraneous": true, }, "node_modules/a": Object { "extraneous": true, @@ -27243,6 +27253,10 @@ Object { "resolved": "https://registry.npmjs.org/c/-/c-1.2.3.tgz", "version": "1.2.3", }, + "node_modules/e": Object { + "link": true, + "resolved": "e", + }, }, "requires": true, } diff --git a/test/arborist/reify.js b/test/arborist/reify.js index 0b7a861cc..e4b30ec42 100644 --- a/test/arborist/reify.js +++ b/test/arborist/reify.js @@ -55,14 +55,20 @@ const warningTracker = () => { } const Node = requireInject('../../lib/node.js', { fs: fsMock }) +const Link = requireInject('../../lib/link.js', { + fs: fsMock, + '../../lib/node.js': Node, +}) const Shrinkwrap = requireInject('../../lib/shrinkwrap.js', { fs: fsMock, '../../lib/node.js': Node, + '../../lib/link.js': Link, }) const Arborist = requireInject('../../lib/arborist', { rimraf: rimrafMock, fs: fsMock, '../../lib/node.js': Node, + '../../lib/link.js': Link, }) const registryServer = require('../fixtures/registry-mocks/server.js') @@ -837,16 +843,26 @@ t.test('saving the ideal tree', t => { a: 'git+ssh://git@github.com:foo/bar#baz', b: '', d: 'd@npm:c@1.x <1.9.9', + e: 'file:e', }, devDependencies: { c: `git+ssh://git@githost.com:a/b/c.git#master`, }, + workspaces: [ + 'e', + ], } const npa = require('npm-package-arg') const kResolvedAdd = Symbol.for('resolvedAdd') const path = t.testdir({ - 'package.json': JSON.stringify(pkg) + 'package.json': JSON.stringify(pkg), + e: { + 'package.json': JSON.stringify({name: 'e'}), + }, + node_modules: { + e: t.fixture('symlink', '../e'), + }, }) const a = newArb({ path }) const hash = '71f3ccfefba85d2048484569dba8c1829f6f41d7' @@ -886,12 +902,30 @@ t.test('saving the ideal tree', t => { }, parent: tree, }) + const target = new Node({ + name: 'e', + pkg: { + name: 'e', + }, + path: resolve(tree.path, 'e'), + fsParent: tree, + }) + new Link({ + name: 'e', + realpath: target.path, + path: resolve(tree.path, 'node_modules/e'), + resolved: 'file:../e', + pkg: { + name: 'e', + }, + }) a[kResolvedAdd] = [ npa('a@git+ssh://git@github.com:foo/bar#baz'), npa('b'), npa('d@npm:c@1.x <1.9.9'), - npa(`c@git+ssh://git@githost.com:a/b/c.git#master`), + npa('c@git+ssh://git@githost.com:a/b/c.git#master'), + npa('e'), ] return a[kSaveIdealTree]({ savePrefix: '~', @@ -904,7 +938,11 @@ t.test('saving the ideal tree', t => { a: 'github:foo/bar#baz', b: '^1.2.3', d: 'npm:c@1.x <1.9.9', + e: '*', }, + workspaces: [ + 'e', + ], devDependencies: { c: 'git+ssh://git@githost.com:a/b/c.git#master', },