Skip to content

Commit

Permalink
fix: installation of packages with () in scope name (#6349)
Browse files Browse the repository at this point in the history
close #6348
  • Loading branch information
zkochan committed Apr 3, 2023
1 parent af3e555 commit 5087636
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 8 deletions.
6 changes: 6 additions & 0 deletions .changeset/healthy-goats-tell.md
@@ -0,0 +1,6 @@
---
"@pnpm/lockfile-file": patch
"pnpm": patch
---

Repeat installation should work on a project that has a dependency with () chars in the scope name [#6348](https://github.com/pnpm/pnpm/issues/6348).
Expand Up @@ -140,7 +140,7 @@ export function revertFromInlineSpecifiersFormat (lockfile: InlineSpecifiersLock
if (newSnapshot.optionalDependencies != null) {
newSnapshot.optionalDependencies = mapValues(newSnapshot.optionalDependencies, convertNewRefToOldRef)
}
return [convertNewDepPathToOldDepPath(depPath), newSnapshot]
return [convertLockfileV6DepPathToV5DepPath(depPath), newSnapshot]
})
)
}
Expand All @@ -153,25 +153,27 @@ export function revertFromInlineSpecifiersFormat (lockfile: InlineSpecifiersLock
if (originalVersion === 6 && newLockfile.time) {
newLockfile.time = Object.fromEntries(
Object.entries(newLockfile.time)
.map(([depPath, time]) => [convertNewDepPathToOldDepPath(depPath), time])
.map(([depPath, time]) => [convertLockfileV6DepPathToV5DepPath(depPath), time])
)
}
return newLockfile
}

export function convertNewDepPathToOldDepPath (oldDepPath: string) {
if (!oldDepPath.includes('@', 2)) return oldDepPath
const index = oldDepPath.indexOf('@', oldDepPath.indexOf('/@') + 2)
if (oldDepPath.includes('(') && index > oldDepPath.indexOf('(')) return oldDepPath
return `${oldDepPath.substring(0, index)}/${oldDepPath.substring(index + 1)}`
const PEERS_SUFFIX_REGEX = /(\([^)]+\))+$/

export function convertLockfileV6DepPathToV5DepPath (newDepPath: string) {
if (!newDepPath.includes('@', 2)) return newDepPath
const index = newDepPath.indexOf('@', newDepPath.indexOf('/@') + 2)
if (newDepPath.includes('(') && index > newDepPath.search(PEERS_SUFFIX_REGEX)) return newDepPath
return `${newDepPath.substring(0, index)}/${newDepPath.substring(index + 1)}`
}

function convertNewRefToOldRef (oldRef: string) {
if (oldRef.startsWith('link:') || oldRef.startsWith('file:')) {
return oldRef
}
if (oldRef.includes('@')) {
return convertNewDepPathToOldDepPath(oldRef)
return convertLockfileV6DepPathToV5DepPath(oldRef)
}
return oldRef
}
Expand Down
8 changes: 8 additions & 0 deletions packages/dependency-path/test/index.ts
Expand Up @@ -98,6 +98,14 @@ test('parse()', () => {
version: '1.0.0',
})

expect(parse('/@(-.-)/foo/1.0.0(@types/babel__core@7.1.14)(foo@1.0.0)')).toStrictEqual({
host: undefined,
isAbsolute: false,
name: '@(-.-)/foo',
peersSuffix: '(@types/babel__core@7.1.14)(foo@1.0.0)',
version: '1.0.0',
})

expect(() => parse('/foo/bar')).toThrow(/\/foo\/bar is an invalid relative dependency path/)

expect(parse('file:project(foo@1.0.0)')).toStrictEqual({
Expand Down
7 changes: 7 additions & 0 deletions pkg-manager/core/test/lockfile.ts
Expand Up @@ -1596,3 +1596,10 @@ test('lockfile is not written when it has no changes', async () => {
await install(manifest, await testDefaults())
expect(await fs.stat(WANTED_LOCKFILE)).toHaveProperty('mtimeMs', initialMtime)
})

test('installation should work with packages that have () in the scope name', async () => {
prepareEmpty()
const opts = await testDefaults()
const manifest = await addDependenciesToPackage({}, ['@(-.-)/env@0.3.1'], opts)
await install(manifest, opts)
})

0 comments on commit 5087636

Please sign in to comment.