From 3633f5e46adb6e9328405faf84ec2e13d69aca94 Mon Sep 17 00:00:00 2001 From: Zoltan Kochan Date: Sun, 13 Sep 2020 15:53:31 +0300 Subject: [PATCH] fix: print the actual version in no matching version error close #1314 PR #2851 --- .changeset/shiny-years-brush.md | 5 +++++ packages/npm-resolver/src/index.ts | 10 ++++++---- packages/npm-resolver/test/index.ts | 2 +- 3 files changed, 12 insertions(+), 5 deletions(-) create mode 100644 .changeset/shiny-years-brush.md diff --git a/.changeset/shiny-years-brush.md b/.changeset/shiny-years-brush.md new file mode 100644 index 0000000000..c4e08364ed --- /dev/null +++ b/.changeset/shiny-years-brush.md @@ -0,0 +1,5 @@ +--- +"@pnpm/npm-resolver": patch +--- + +When no matching version is found, report the actually specified version spec in the error message (not the normalized one). diff --git a/packages/npm-resolver/src/index.ts b/packages/npm-resolver/src/index.ts index 2b4c46a08f..5306b2bee9 100644 --- a/packages/npm-resolver/src/index.ts +++ b/packages/npm-resolver/src/index.ts @@ -23,7 +23,6 @@ import pickPackage, { PackageMetaCache, PickPackageOptions, } from './pickPackage' -import toRaw from './toRaw' import path = require('path') import LRU = require('lru-cache') import normalize = require('normalize-path') @@ -33,8 +32,11 @@ import ssri = require('ssri') class NoMatchingVersionError extends PnpmError { public readonly packageMeta: PackageMeta - constructor (opts: { spec: RegistryPackageSpec, packageMeta: PackageMeta}) { - super('NO_MATCHING_VERSION', `No matching version found for ${toRaw(opts.spec)}`) + constructor (opts: { wantedDependency: WantedDependency, packageMeta: PackageMeta}) { + const dep = opts.wantedDependency.alias + ? `${opts.wantedDependency.alias}@${opts.wantedDependency.pref ?? ''}` + : opts.wantedDependency.pref! + super('NO_MATCHING_VERSION', `No matching version found for ${dep}`) this.packageMeta = opts.packageMeta } } @@ -149,7 +151,7 @@ async function resolveNpm ( const resolvedFromLocal = tryResolveFromWorkspacePackages(workspacePackages, spec, opts.projectDir) if (resolvedFromLocal) return resolvedFromLocal } - throw new NoMatchingVersionError({ spec, packageMeta: meta }) + throw new NoMatchingVersionError({ wantedDependency, packageMeta: meta }) } if (workspacePackages?.[pickedPackage.name] && opts.projectDir) { diff --git a/packages/npm-resolver/test/index.ts b/packages/npm-resolver/test/index.ts index 11d3388312..88422fcb04 100644 --- a/packages/npm-resolver/test/index.ts +++ b/packages/npm-resolver/test/index.ts @@ -792,7 +792,7 @@ test('error is thrown when there is no package found for the requested range', a await resolveFromNpm({ alias: 'is-positive', pref: '^1000.0.0' }, { registry }) t.fail('installation should have failed') } catch (err) { - t.ok(err.message.startsWith('No matching version found for is-positive@>=1000.0.0 <1001.0.0'), 'failed with correct error message') + t.ok(err.message.startsWith('No matching version found for is-positive@^1000.0.0'), 'failed with correct error message') t.end() } })