Skip to content
This repository was archived by the owner on Jan 20, 2022. It is now read-only.

Commit 6aa0c2c

Browse files
committed
Save provided range if not a subset of savePrefix
If a user installs `[email protected] <1.2.3`, and we resolve to `1.2.2`, then we should not save it as `^1.2.2`, since that would allow versions outside of the requested range. Explicit versions and tags are still saved using the savePrefix, since those are not ranges, and users can set `--save-exact` if they wish it to be saved exactly. Fix: #127 Fix: npm/cli#193 Fix: https://npm.community/t/7005
1 parent 410bc2a commit 6aa0c2c

File tree

3 files changed

+16
-6
lines changed

3 files changed

+16
-6
lines changed

lib/arborist/reify.js

+12-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const pacote = require('pacote')
44
const rpj = require('read-package-json-fast')
55
const { orderDeps, updateDepSpec } = require('../dep-spec.js')
66
const AuditReport = require('../audit-report.js')
7+
const {subset} = require('semver')
78

89
const {dirname, resolve, relative} = require('path')
910
const {depth: dfwalk} = require('treeverse')
@@ -788,12 +789,21 @@ module.exports = cls => class Reifier extends cls {
788789
const root = this.idealTree
789790
const pkg = root.package
790791
for (const req of this[_resolvedAdd]) {
791-
const {name} = req
792+
const {name, rawSpec, subSpec} = req
793+
const spec = subSpec ? subSpec.rawSpec : rawSpec
792794
const child = root.children.get(name)
793795

794796
if (req.registry) {
795797
const version = child.version
796-
const range = this[_savePrefix] + version
798+
const prefixRange = this[_savePrefix] + version
799+
// if we installed a range, then we save the range specified
800+
// if it is not a subset of the ^x.y.z. eg, installing a range
801+
// of `1.x <1.2.3` will not be saved as `^1.2.0`, because that
802+
// would allow versions outside the requested range. Tags and
803+
// specific versions save with the save-prefix.
804+
const isRange = (subSpec || req).type === 'range'
805+
const range = !isRange || subset(prefixRange, spec, { loose: true })
806+
? prefixRange : spec
797807
const pname = child.package.name
798808
const alias = name !== pname
799809
updateDepSpec(pkg, name, (alias ? `npm:${pname}@` : '') + range)

tap-snapshots/test-arborist-reify.js-TAP.test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -27051,7 +27051,7 @@ Object {
2705127051
"dependencies": Object {
2705227052
"a": "github:foo/bar#baz",
2705327053
"b": "^1.2.3",
27054-
"d": "npm:c@^1.2.3",
27054+
"d": "npm:c@1.x <1.9.9",
2705527055
},
2705627056
"devDependencies": Object {
2705727057
"c": "git+ssh://[email protected]:a/b/c.git#master",

test/arborist/reify.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -816,7 +816,7 @@ t.test('saving the ideal tree', t => {
816816
dependencies: {
817817
a: 'git+ssh://[email protected]:foo/bar#baz',
818818
b: '',
819-
d: 'd@npm:[email protected]',
819+
d: 'd@npm:[email protected] <1.9.9',
820820
},
821821
devDependencies: {
822822
c: `git+ssh://[email protected]:a/b/c.git#master`,
@@ -870,7 +870,7 @@ t.test('saving the ideal tree', t => {
870870
a[kResolvedAdd] = [
871871
npa('a@git+ssh://[email protected]:foo/bar#baz'),
872872
npa('b'),
873-
npa('d@npm:[email protected]'),
873+
npa('d@npm:[email protected] <1.9.9'),
874874
npa(`c@git+ssh://[email protected]:a/b/c.git#master`),
875875
]
876876
return a[kSaveIdealTree]({
@@ -883,7 +883,7 @@ t.test('saving the ideal tree', t => {
883883
dependencies: {
884884
a: 'github:foo/bar#baz',
885885
b: '^1.2.3',
886-
d: 'npm:c@^1.2.3',
886+
d: 'npm:c@1.x <1.9.9',
887887
},
888888
devDependencies: {
889889
c: 'git+ssh://[email protected]:a/b/c.git#master',

0 commit comments

Comments
 (0)