mirror of
https://github.com/pnpm/pnpm.git
synced 2026-03-31 13:32:18 -04:00
fix: error message when package is not found in workspace (#6598)
ref #4477
This commit is contained in:
6
.changeset/dirty-spoons-report.md
Normal file
6
.changeset/dirty-spoons-report.md
Normal file
@@ -0,0 +1,6 @@
|
||||
---
|
||||
"@pnpm/npm-resolver": patch
|
||||
"pnpm": patch
|
||||
---
|
||||
|
||||
Print a meaningful error when a project referenced by the `workspace:` protocol is not found in the workspace [#4477](https://github.com/pnpm/pnpm/issues/4477).
|
||||
@@ -233,7 +233,7 @@ test('some projects were removed from the workspace and the ones that are left d
|
||||
pruneLockfileImporters: true,
|
||||
workspacePackages: pick(['project-1'], workspacePackages),
|
||||
} as any)) // eslint-disable-line
|
||||
).rejects.toThrow(/No matching version found for/)
|
||||
).rejects.toThrow(/"project-2@workspace:1.0.0" is in the dependencies but no package named "project-2" is present in the workspace/)
|
||||
})
|
||||
|
||||
test('dependencies of other importers are not pruned when installing for a subset of importers', async () => {
|
||||
|
||||
@@ -163,12 +163,16 @@ async function resolveNpm (
|
||||
})
|
||||
} catch (err: any) { // eslint-disable-line
|
||||
if ((workspacePackages != null) && opts.projectDir) {
|
||||
const resolvedFromLocal = tryResolveFromWorkspacePackages(workspacePackages, spec, {
|
||||
projectDir: opts.projectDir,
|
||||
lockfileDir: opts.lockfileDir,
|
||||
hardLinkLocalPackages: wantedDependency.injected,
|
||||
})
|
||||
if (resolvedFromLocal != null) return resolvedFromLocal
|
||||
try {
|
||||
return tryResolveFromWorkspacePackages(workspacePackages, spec, {
|
||||
wantedDependency,
|
||||
projectDir: opts.projectDir,
|
||||
lockfileDir: opts.lockfileDir,
|
||||
hardLinkLocalPackages: wantedDependency.injected,
|
||||
})
|
||||
} catch {
|
||||
// ignore
|
||||
}
|
||||
}
|
||||
throw err
|
||||
}
|
||||
@@ -176,12 +180,16 @@ async function resolveNpm (
|
||||
const meta = pickResult.meta
|
||||
if (pickedPackage == null) {
|
||||
if ((workspacePackages != null) && opts.projectDir) {
|
||||
const resolvedFromLocal = tryResolveFromWorkspacePackages(workspacePackages, spec, {
|
||||
projectDir: opts.projectDir,
|
||||
lockfileDir: opts.lockfileDir,
|
||||
hardLinkLocalPackages: wantedDependency.injected,
|
||||
})
|
||||
if (resolvedFromLocal != null) return resolvedFromLocal
|
||||
try {
|
||||
return tryResolveFromWorkspacePackages(workspacePackages, spec, {
|
||||
wantedDependency,
|
||||
projectDir: opts.projectDir,
|
||||
lockfileDir: opts.lockfileDir,
|
||||
hardLinkLocalPackages: wantedDependency.injected,
|
||||
})
|
||||
} catch {
|
||||
// ignore
|
||||
}
|
||||
}
|
||||
throw new NoMatchingVersionError({ wantedDependency, packageMeta: meta })
|
||||
}
|
||||
@@ -250,32 +258,40 @@ function tryResolveFromWorkspace (
|
||||
if (!opts.projectDir) {
|
||||
throw new Error('Cannot resolve package from workspace because opts.projectDir is not defined')
|
||||
}
|
||||
const resolvedFromLocal = tryResolveFromWorkspacePackages(opts.workspacePackages, spec, {
|
||||
return tryResolveFromWorkspacePackages(opts.workspacePackages, spec, {
|
||||
wantedDependency,
|
||||
projectDir: opts.projectDir,
|
||||
hardLinkLocalPackages: wantedDependency.injected,
|
||||
lockfileDir: opts.lockfileDir,
|
||||
})
|
||||
if (resolvedFromLocal == null) {
|
||||
throw new PnpmError(
|
||||
'NO_MATCHING_VERSION_INSIDE_WORKSPACE',
|
||||
`In ${path.relative(process.cwd(), opts.projectDir)}: No matching version found for ${wantedDependency.alias ?? ''}@${pref} inside the workspace`
|
||||
)
|
||||
}
|
||||
return resolvedFromLocal
|
||||
}
|
||||
|
||||
function tryResolveFromWorkspacePackages (
|
||||
workspacePackages: WorkspacePackages,
|
||||
spec: RegistryPackageSpec,
|
||||
opts: {
|
||||
wantedDependency: WantedDependency
|
||||
hardLinkLocalPackages?: boolean
|
||||
projectDir: string
|
||||
lockfileDir?: string
|
||||
}
|
||||
) {
|
||||
if (!workspacePackages[spec.name]) return null
|
||||
if (!workspacePackages[spec.name]) {
|
||||
throw new PnpmError(
|
||||
'WORKSPACE_PKG_NOT_FOUND',
|
||||
`In ${path.relative(process.cwd(), opts.projectDir)}: "${spec.name}@${opts.wantedDependency.pref ?? ''}" is in the dependencies but no package named "${spec.name}" is present in the workspace`,
|
||||
{
|
||||
hint: 'Packages found in the workspace: ' + Object.keys(workspacePackages).join(', '),
|
||||
}
|
||||
)
|
||||
}
|
||||
const localVersion = pickMatchingLocalVersionOrNull(workspacePackages[spec.name], spec)
|
||||
if (!localVersion) return null
|
||||
if (!localVersion) {
|
||||
throw new PnpmError(
|
||||
'NO_MATCHING_VERSION_INSIDE_WORKSPACE',
|
||||
`In ${path.relative(process.cwd(), opts.projectDir)}: No matching version found for ${opts.wantedDependency.alias ?? ''}@${opts.wantedDependency.pref ?? ''} inside the workspace`
|
||||
)
|
||||
}
|
||||
return resolveFromLocalPackage(workspacePackages[spec.name][localVersion], spec.normalizedPref, opts)
|
||||
}
|
||||
|
||||
|
||||
@@ -1621,9 +1621,42 @@ test('workspace protocol: resolution fails if there is no matching local package
|
||||
err = _err
|
||||
}
|
||||
|
||||
expect(err).toBeTruthy()
|
||||
expect(err.code).toBe('ERR_PNPM_WORKSPACE_PKG_NOT_FOUND')
|
||||
expect(err.message).toBe(`In ${path.relative(process.cwd(), projectDir)}: "is-positive@workspace:^3.0.0" is in the dependencies but no package named "is-positive" is present in the workspace`)
|
||||
})
|
||||
|
||||
test('workspace protocol: resolution fails if there is no matching local package version', async () => {
|
||||
const cacheDir = tempy.directory()
|
||||
const resolve = createResolveFromNpm({
|
||||
cacheDir,
|
||||
})
|
||||
|
||||
const projectDir = '/home/istvan/src'
|
||||
let err!: Error & { code: string }
|
||||
try {
|
||||
await resolve({ alias: 'is-positive', pref: 'workspace:^3.0.0' }, {
|
||||
projectDir,
|
||||
registry,
|
||||
workspacePackages: {
|
||||
'is-positive': {
|
||||
'2.0.0': {
|
||||
dir: '/home/istvan/src/is-positive',
|
||||
manifest: {
|
||||
name: 'is-positive',
|
||||
version: '2.0.0',
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
} catch (_err: any) { // eslint-disable-line
|
||||
err = _err
|
||||
}
|
||||
|
||||
expect(err).toBeTruthy()
|
||||
expect(err.code).toBe('ERR_PNPM_NO_MATCHING_VERSION_INSIDE_WORKSPACE')
|
||||
expect(err.message).toBe(`In ${path.relative(process.cwd(), projectDir)}: No matching version found for is-positive@^3.0.0 inside the workspace`)
|
||||
expect(err.message).toBe(`In ${path.relative(process.cwd(), projectDir)}: No matching version found for is-positive@workspace:^3.0.0 inside the workspace`)
|
||||
})
|
||||
|
||||
test('workspace protocol: resolution fails if there are no local packages', async () => {
|
||||
|
||||
Reference in New Issue
Block a user