Skip to content

Commit

Permalink
fix: correctly resolve workspace dependencies with relative paths (#8907
Browse files Browse the repository at this point in the history
)
  • Loading branch information
zaverden authored and zkochan committed Dec 27, 2024
1 parent 2aaf205 commit a072699
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 6 deletions.
6 changes: 6 additions & 0 deletions .changeset/dull-zoos-knock.md
Original file line number Diff line number Diff line change
@@ -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)).
11 changes: 5 additions & 6 deletions pkg-manifest/exportable-manifest/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,16 +101,15 @@ 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',
`Cannot resolve workspace protocol of dependency "${depName}" ` +
'because this dependency is not installed. Try running "pnpm install".'
)
}

return manifest
}

Expand All @@ -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) {
Expand All @@ -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}`
Expand Down Expand Up @@ -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}`)
Expand Down
8 changes: 8 additions & 0 deletions pkg-manifest/exportable-manifest/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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/**'] })
Expand All @@ -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',
Expand Down

0 comments on commit a072699

Please sign in to comment.