fix: correctly resolve workspace dependencies with relative paths (#8907)

This commit is contained in:
Denis Zavershinskiy
2024-12-27 09:23:36 +07:00
committed by GitHub
parent b10096228c
commit 738d9e4679
3 changed files with 19 additions and 6 deletions

View File

@@ -0,0 +1,6 @@
---
"@pnpm/exportable-manifest": patch
"pnpm": patch
---
Fixed `publish`/`pack` error with workspace dependencies with relative paths [#8904](https://github.com/pnpm/pnpm/pull/8904). It was broken in `v9.4.0` ([398472c](https://github.com/pnpm/pnpm/commit/398472c)).

View File

@@ -101,8 +101,8 @@ async function makePublishDependencies (
return publishDependencies
}
async function resolveManifest (depName: string, modulesDir: string): Promise<ProjectManifest> {
const { manifest } = await tryReadProjectManifest(path.join(modulesDir, depName))
async function readAndCheckManifest (depName: string, dependencyDir: string): Promise<ProjectManifest> {
const { manifest } = await tryReadProjectManifest(dependencyDir)
if (!manifest?.name || !manifest?.version) {
throw new PnpmError(
'CANNOT_RESOLVE_WORKSPACE_PROTOCOL',
@@ -110,7 +110,6 @@ async function resolveManifest (depName: string, modulesDir: string): Promise<Pr
'because this dependency is not installed. Try running "pnpm install".'
)
}
return manifest
}
@@ -133,7 +132,7 @@ async function replaceWorkspaceProtocol (depName: string, depSpec: string, dir:
const versionAliasSpecParts = /^workspace:(.*?)@?([\^~*])$/.exec(depSpec)
if (versionAliasSpecParts != null) {
modulesDir = modulesDir ?? path.join(dir, 'node_modules')
const manifest = await resolveManifest(depName, modulesDir)
const manifest = await readAndCheckManifest(depName, path.join(modulesDir, depName))
const semverRangeToken = versionAliasSpecParts[2] !== '*' ? versionAliasSpecParts[2] : ''
if (depName !== manifest.name) {
@@ -142,7 +141,7 @@ async function replaceWorkspaceProtocol (depName: string, depSpec: string, dir:
return `${semverRangeToken}${manifest.version}`
}
if (depSpec.startsWith('workspace:./') || depSpec.startsWith('workspace:../')) {
const manifest = await resolveManifest(depName, path.join(dir, depSpec.slice(10)))
const manifest = await readAndCheckManifest(depName, path.join(dir, depSpec.slice(10)))
if (manifest.name === depName) return `${manifest.version}`
return `npm:${manifest.name}@${manifest.version}`
@@ -171,7 +170,7 @@ async function replaceWorkspaceProtocolPeerDependency (depName: string, depSpec:
}
modulesDir = modulesDir ?? path.join(dir, 'node_modules')
const manifest = await resolveManifest(depName, modulesDir)
const manifest = await readAndCheckManifest(depName, path.join(modulesDir, depName))
const semverRangeToken = semverRangGroup !== '*' ? semverRangGroup : ''
return depSpec.replace(workspaceSemverRegex, `${semverRangeToken}${manifest.version}`)

View File

@@ -97,6 +97,8 @@ test('workspace deps are replaced', async () => {
foo: 'workspace:*',
qux: 'workspace:^',
waldo: 'workspace:^',
xerox: 'workspace:../xerox',
xeroxAlias: 'workspace:../xerox',
},
peerDependencies: {
foo: 'workspace:>= || ^3.9.0',
@@ -129,6 +131,10 @@ test('workspace deps are replaced', async () => {
name: 'waldo',
version: '1.9.0',
},
{
name: 'xerox',
version: '4.5.6',
},
])
writeYamlFile('pnpm-workspace.yaml', { packages: ['**', '!store/**'] })
@@ -146,6 +152,8 @@ test('workspace deps are replaced', async () => {
foo: '4.5.6',
qux: '^1.0.0-alpha-a.b-c-something+build.1-aef.1-its-okay',
waldo: '^1.9.0',
xerox: '4.5.6',
xeroxAlias: 'npm:xerox@4.5.6',
},
peerDependencies: {
baz: '^1.0.0 || >1.2.3',