Skip to content

Commit

Permalink
fix: standardize x x@ and x@*
Browse files Browse the repository at this point in the history
BREAKING CHANGE: `x` and `x@` now return the same spec as `x@*`

From #45:

Right now, `name@` and `name` are parsed with
`{type:'tag', fetchSpec: 'latest'}`, but `name@*` is parsed as
`{type: 'range'}`.

But since `''` is a valid semver range, it should be parsed the same as
`*`.

This also saves npm-package-arg from guessing the default tag, which
currently poses some semantic hazards.  npm (via npm-pick-manifest) will
prefer its default tag if given a range that includes it.  But
`name@latest` should always and only resolve to that specific version in
the dist-tags.  So npm-pick-manifest and pacote have to detect this and
do some extra work to figure out if `latest` was actually specified or
just guessed as a default.

Closes npm/statusboard#460
  • Loading branch information
wraithgar committed Sep 1, 2022
1 parent 0f48127 commit 32a88d5
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 17 deletions.
7 changes: 4 additions & 3 deletions lib/npa.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,12 @@ function npa (arg, where) {
spec = arg
} else if (nameEndsAt > 0) {
name = namePart
spec = arg.slice(nameEndsAt + 1)
spec = arg.slice(nameEndsAt + 1) || '*'
} else {
const valid = validatePackageName(arg)
if (valid.validForOldPackages) {
name = arg
spec = '*'
} else {
spec = arg
}
Expand Down Expand Up @@ -113,7 +114,7 @@ function Result (opts) {
this.name = undefined
this.escapedName = undefined
this.scope = undefined
this.rawSpec = opts.rawSpec == null ? '' : opts.rawSpec
this.rawSpec = opts.rawSpec
this.saveSpec = opts.saveSpec
this.fetchSpec = opts.fetchSpec
if (opts.name) {
Expand Down Expand Up @@ -381,7 +382,7 @@ function fromAlias (res, where) {

function fromRegistry (res) {
res.registry = true
const spec = res.rawSpec === '' ? 'latest' : res.rawSpec.trim()
const spec = res.rawSpec.trim()
// no save spec for registry components as we save based on the fetched
// version, not on the argument so this can't compute that.
res.saveSpec = null
Expand Down
26 changes: 13 additions & 13 deletions test/basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,21 +44,21 @@ t.test('basic', function (t) {
name: '@foo/bar',
escapedName: '@foo%2fbar',
scope: '@foo',
rawSpec: '',
rawSpec: '*',
saveSpec: null,
fetchSpec: 'latest',
type: 'tag',
fetchSpec: '*',
type: 'range',
},

'@foo/bar@': {
raw: '@foo/bar@',
name: '@foo/bar',
escapedName: '@foo%2fbar',
scope: '@foo',
rawSpec: '',
rawSpec: '*',
saveSpec: null,
fetchSpec: 'latest',
type: 'tag',
fetchSpec: '*',
type: 'range',
},

'@foo/bar@baz': {
Expand Down Expand Up @@ -113,11 +113,11 @@ t.test('basic', function (t) {
registry: true,
name: 'bar',
escapedName: 'bar',
type: 'tag',
type: 'range',
raw: 'bar',
rawSpec: '',
rawSpec: '*',
saveSpec: null,
fetchSpec: 'latest',
fetchSpec: '*',
},
},

Expand Down Expand Up @@ -366,10 +366,10 @@ t.test('basic', function (t) {
name: 'git',
type: 'alias',
subSpec: {
type: 'tag',
type: 'range',
registry: true,
name: 'not-git',
fetchSpec: 'latest',
fetchSpec: '*',
},
raw: 'git@npm:not-git',
},
Expand Down Expand Up @@ -550,9 +550,9 @@ t.test('basic', function (t) {
foo: {
name: 'foo',
escapedName: 'foo',
type: 'tag',
type: 'range',
saveSpec: null,
fetchSpec: 'latest',
fetchSpec: '*',
raw: 'foo',
},

Expand Down
2 changes: 1 addition & 1 deletion test/realize-package-specifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ test('realize-package-specifier', function (t) {
result = npa('a.tar.gz', '/test/a/b')
t.equal(result.type, 'file', 'local tarball')
result = npa('d', '/test/a/b')
t.equal(result.type, 'tag', 'remote package')
t.equal(result.type, 'range', 'remote package')
result = npa('file:./a.tar.gz', '/test/a/b')
t.equal(result.type, 'file', 'local tarball')
result = npa('file:./b', '/test/a/b')
Expand Down

0 comments on commit 32a88d5

Please sign in to comment.