fix: converting lockfile with directory dependencies (#7983)

close #7955
close #7958
This commit is contained in:
Zoltan Kochan
2024-04-21 21:39:58 +02:00
committed by GitHub
parent abaf12e8cf
commit 2cbf7b767b
4 changed files with 161 additions and 11 deletions

View File

@@ -0,0 +1,6 @@
---
"@pnpm/lockfile-file": patch
"pnpm": patch
---
Lockfiles with local or git-hosted dependencies are now successfully converted to the new lockfile format [#7955](https://github.com/pnpm/pnpm/issues/7955).

View File

@@ -201,21 +201,23 @@ function convertPkgIds (lockfile: LockfileFile): void {
if (lockfile.packages == null || isEmpty(lockfile.packages)) return
for (const [pkgId, pkg] of Object.entries(lockfile.packages ?? {})) {
if (pkg.name) {
let newId: string
const { id, peersSuffix } = parseDepPath(pkgId)
let newId = `${pkg.name}@`
if ('tarball' in pkg.resolution) {
newId = pkg.resolution.tarball
newId += pkg.resolution.tarball
if (pkg.resolution.path) {
newId += `#path:${pkg.resolution.path}`
}
} else if ('repo' in pkg.resolution) {
newId = `${pkg.resolution.repo.startsWith('git+') ? '' : 'git+'}${pkg.resolution.repo}#${pkg.resolution.commit}`
newId += `${pkg.resolution.repo.startsWith('git+') ? '' : 'git+'}${pkg.resolution.repo}#${pkg.resolution.commit}`
if (pkg.resolution.path) {
newId += `&path:${pkg.resolution.path}`
}
} else if ('directory' in pkg.resolution) {
newId += id
} else {
continue
}
const { id, peersSuffix } = parseDepPath(pkgId)
oldIdToNewId[pkgId] = `${newId}${peersSuffix}`
if (id !== pkgId) {
oldIdToNewId[id] = newId
@@ -242,7 +244,11 @@ function convertPkgIds (lockfile: LockfileFile): void {
for (const depType of ['dependencies', 'optionalDependencies'] as const) {
for (const [alias, depPath] of Object.entries(pkg[depType] ?? {})) {
if (oldIdToNewId[depPath]) {
pkg[depType]![alias] = oldIdToNewId[depPath]
if (oldIdToNewId[depPath].startsWith(`${alias}@`)) {
pkg[depType]![alias] = oldIdToNewId[depPath].substring(alias.length + 1)
} else {
pkg[depType]![alias] = oldIdToNewId[depPath]
}
}
}
}
@@ -252,7 +258,11 @@ function convertPkgIds (lockfile: LockfileFile): void {
for (const depType of ['dependencies', 'optionalDependencies', 'devDependencies'] as const) {
for (const [alias, { version }] of Object.entries(importer[depType] ?? {})) {
if (oldIdToNewId[version]) {
importer[depType]![alias].version = oldIdToNewId[version]
if (oldIdToNewId[version].startsWith(`${alias}@`)) {
importer[depType]![alias].version = oldIdToNewId[version].substring(alias.length + 1)
} else {
importer[depType]![alias].version = oldIdToNewId[version]
}
}
}
}

View File

@@ -0,0 +1,134 @@
import { convertToLockfileObject } from '../lib/lockfileFormatConverters'
test('convertToLockfileObject converts directory dependencies', () => {
expect(convertToLockfileObject({
lockfileVersion: '',
importers: {
'.': {
dependencies: {
a: {
specifier: 'file:../a',
version: 'file:../a',
},
},
},
},
packages: {
'file:../a': {
resolution: { directory: '../a', type: 'directory' },
name: 'a',
dev: false,
} as any, // eslint-disable-line
},
})).toMatchObject({
lockfileVersion: '',
importers: {
'.': {
dependencies: {
a: 'file:../a',
},
specifiers: {
a: 'file:../a',
},
},
},
packages: {
'a@file:../a': {
resolution: { directory: '../a', type: 'directory' },
name: 'a',
},
},
})
})
test('convertToLockfileObject converts git-hosted dependencies', () => {
expect(convertToLockfileObject({
lockfileVersion: '',
importers: {
'.': {
dependencies: {
'is-negative': {
specifier: 'github:kevva/is-negative',
version: 'github.com/kevva/is-negative/1d7e288222b53a0cab90a331f1865220ec29560c',
},
},
},
},
packages: {
'github.com/kevva/is-negative/1d7e288222b53a0cab90a331f1865220ec29560c': {
resolution: { tarball: 'https://codeload.github.com/kevva/is-negative/tar.gz/1d7e288222b53a0cab90a331f1865220ec29560c' },
name: 'is-negative',
version: '2.1.0',
engines: { node: '>=0.10.0' },
dev: false,
} as any, // eslint-disable-line
},
})).toMatchObject({
lockfileVersion: '',
importers: {
'.': {
dependencies: {
'is-negative': 'https://codeload.github.com/kevva/is-negative/tar.gz/1d7e288222b53a0cab90a331f1865220ec29560c',
},
specifiers: {
'is-negative': 'github:kevva/is-negative',
},
},
},
packages: {
'is-negative@https://codeload.github.com/kevva/is-negative/tar.gz/1d7e288222b53a0cab90a331f1865220ec29560c': {
resolution: { tarball: 'https://codeload.github.com/kevva/is-negative/tar.gz/1d7e288222b53a0cab90a331f1865220ec29560c' },
name: 'is-negative',
version: '2.1.0',
engines: { node: '>=0.10.0' },
},
},
})
})
test('convertToLockfileObject converts git-hosted dependencies via ssh', () => {
expect(convertToLockfileObject({
lockfileVersion: '',
importers: {
'.': {
dependencies: {
'git-resolver': {
specifier: 'ssh://git@gitlab:pnpm/git-resolver#988c61e11dc8d9ca0b5580cb15291951812549dc',
version: 'git+ssh://git@gitlab/pnpm/git-resolver#988c61e11dc8d9ca0b5580cb15291951812549dc',
},
},
},
},
packages: {
'git+ssh://git@gitlab/pnpm/git-resolver#988c61e11dc8d9ca0b5580cb15291951812549dc': {
name: 'git-resolver',
resolution: {
commit: '988c61e11dc8d9ca0b5580cb15291951812549dc',
repo: 'ssh://git@gitlab/pnpm/git-resolver',
type: 'git',
},
} as any, // eslint-disable-line
},
})).toMatchObject({
lockfileVersion: '',
importers: {
'.': {
dependencies: {
'git-resolver': 'git+ssh://git@gitlab/pnpm/git-resolver#988c61e11dc8d9ca0b5580cb15291951812549dc',
},
specifiers: {
'git-resolver': 'ssh://git@gitlab:pnpm/git-resolver#988c61e11dc8d9ca0b5580cb15291951812549dc',
},
},
},
packages: {
'git-resolver@git+ssh://git@gitlab/pnpm/git-resolver#988c61e11dc8d9ca0b5580cb15291951812549dc': {
resolution: {
commit: '988c61e11dc8d9ca0b5580cb15291951812549dc',
repo: 'ssh://git@gitlab/pnpm/git-resolver',
type: 'git',
},
},
},
})
})

View File

@@ -236,7 +236,7 @@ test('convertToLockfileObject() converts package IDs', () => {
project1: {
dependencies: {
'is-positive': 'https://codeload.github.com/kevva/is-positive/tar.gz/97edff6f525f192a3f83cea1944765f769ae2678(@babel/core@2.0.0)',
tarball: 'https://registry.npmjs.org/is-positive/-/is-positive-1.0.0.tgz',
tarball: 'is-positive@https://registry.npmjs.org/is-positive/-/is-positive-1.0.0.tgz',
'git-hosted': 'git+ssh://git@gitlab/pnpm/git-resolver#988c61e11dc8d9ca0b5580cb15291951812549dc(foo@1.0.0)',
'is-odd': '1.0.0',
},
@@ -249,16 +249,16 @@ test('convertToLockfileObject() converts package IDs', () => {
},
},
packages: {
'https://codeload.github.com/kevva/is-positive/tar.gz/97edff6f525f192a3f83cea1944765f769ae2678(@babel/core@2.0.0)': {
'is-positive@https://codeload.github.com/kevva/is-positive/tar.gz/97edff6f525f192a3f83cea1944765f769ae2678(@babel/core@2.0.0)': {
name: 'is-positive',
resolution: { tarball: 'https://codeload.github.com/kevva/is-positive/tar.gz/97edff6f525f192a3f83cea1944765f769ae2678' },
},
'https://registry.npmjs.org/is-positive/-/is-positive-1.0.0.tgz': {
'is-positive@https://registry.npmjs.org/is-positive/-/is-positive-1.0.0.tgz': {
name: 'is-positive',
resolution: { tarball: 'https://registry.npmjs.org/is-positive/-/is-positive-1.0.0.tgz', integrity: '' },
},
'git+ssh://git@gitlab/pnpm/git-resolver#988c61e11dc8d9ca0b5580cb15291951812549dc(foo@1.0.0)': {
id: 'git+ssh://git@gitlab/pnpm/git-resolver#988c61e11dc8d9ca0b5580cb15291951812549dc',
'git-hosted@git+ssh://git@gitlab/pnpm/git-resolver#988c61e11dc8d9ca0b5580cb15291951812549dc(foo@1.0.0)': {
id: 'git-hosted@git+ssh://git@gitlab/pnpm/git-resolver#988c61e11dc8d9ca0b5580cb15291951812549dc',
name: 'git-hosted',
resolution: {
commit: '988c61e11dc8d9ca0b5580cb15291951812549dc',