Skip to content

Commit

Permalink
fix: git-hosted dependencies (#8005)
Browse files Browse the repository at this point in the history
close #7990
  • Loading branch information
zkochan authored Apr 24, 2024
1 parent 63adcb5 commit c969f37
Show file tree
Hide file tree
Showing 9 changed files with 75 additions and 21 deletions.
7 changes: 7 additions & 0 deletions .changeset/new-jeans-sparkle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@pnpm/lockfile-file": patch
"@pnpm/git-resolver": patch
"pnpm": patch
---

Lockfiles that have git-hosted dependencies specified should be correctly converted to the new lockfile format [#7990](https://github.com/pnpm/pnpm/issues/7990).
1 change: 1 addition & 0 deletions lockfile/lockfile-file/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
"@pnpm/constants": "workspace:*",
"@pnpm/dependency-path": "workspace:*",
"@pnpm/error": "workspace:*",
"@pnpm/git-resolver": "workspace:*",
"@pnpm/git-utils": "workspace:*",
"@pnpm/lockfile-types": "workspace:*",
"@pnpm/lockfile-utils": "workspace:*",
Expand Down
17 changes: 13 additions & 4 deletions lockfile/lockfile-file/src/lockfileFormatConverters.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { parseDepPath, removePeersSuffix } from '@pnpm/dependency-path'
import { createGitHostedPkgId } from '@pnpm/git-resolver'
import {
type Lockfile,
type ProjectSnapshot,
Expand Down Expand Up @@ -210,10 +211,7 @@ function convertPkgIds (lockfile: LockfileFile): void {
newId += `#path:${pkg.resolution.path}`
}
} else if ('repo' in pkg.resolution) {
newId += `${pkg.resolution.repo.startsWith('git+') ? '' : 'git+'}${pkg.resolution.repo}#${pkg.resolution.commit}`
if (pkg.resolution.path) {
newId += `&path:${pkg.resolution.path}`
}
newId += createGitHostedPkgId(pkg.resolution)
} else if ('directory' in pkg.resolution) {
newId += id
} else {
Expand Down Expand Up @@ -255,6 +253,17 @@ function convertPkgIds (lockfile: LockfileFile): void {
}
}
lockfile.packages = newLockfilePackages
if ((lockfile.dependencies != null || lockfile.devDependencies != null || lockfile.optionalDependencies != null) && !lockfile.importers?.['.']) {
lockfile.importers = lockfile.importers ?? {}
lockfile.importers['.'] = {
dependencies: lockfile.dependencies,
devDependencies: lockfile.devDependencies,
optionalDependencies: lockfile.optionalDependencies,
}
delete lockfile.dependencies
delete lockfile.devDependencies
delete lockfile.optionalDependencies
}
for (const importer of Object.values(lockfile.importers ?? {})) {
for (const depType of ['dependencies', 'optionalDependencies', 'devDependencies'] as const) {
for (const [alias, { version }] of Object.entries(importer[depType] ?? {})) {
Expand Down
26 changes: 24 additions & 2 deletions lockfile/lockfile-file/test/lockfileFormatConverters.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ test('convertToLockfileObject converts git-hosted dependencies', () => {
})

test('convertToLockfileObject converts git-hosted dependencies via ssh', () => {
expect(convertToLockfileObject({
const result = convertToLockfileObject({
lockfileVersion: '',
importers: {
'.': {
Expand All @@ -96,6 +96,10 @@ test('convertToLockfileObject converts git-hosted dependencies via ssh', () => {
specifier: 'ssh://git@gitlab:pnpm/git-resolver#988c61e11dc8d9ca0b5580cb15291951812549dc',
version: 'git+ssh://git@gitlab/pnpm/git-resolver#988c61e11dc8d9ca0b5580cb15291951812549dc',
},
foo: {
specifier: 'https://gitlab.com/foo/foo.git',
version: 'git@gitlab.com+foo/foo/6ae3f32d7c631f64fbaf70cdd349ae6e2cc68e6c',
},
},
},
},
Expand All @@ -108,15 +112,26 @@ test('convertToLockfileObject converts git-hosted dependencies via ssh', () => {
type: 'git',
},
} as any, // eslint-disable-line
'git@gitlab.com+foo/foo/6ae3f32d7c631f64fbaf70cdd349ae6e2cc68e6c': {
name: 'foo',
resolution: {
commit: '6ae3f32d7c631f64fbaf70cdd349ae6e2cc68e6c',
repo: 'git@gitlab.com:foo/foo.git',
type: 'git',
},
} as any, // eslint-disable-line
},
})).toMatchObject({
})
expect(result).toMatchObject({
lockfileVersion: '',
importers: {
'.': {
dependencies: {
foo: 'git+https://git@gitlab.com:foo/foo.git#6ae3f32d7c631f64fbaf70cdd349ae6e2cc68e6c',
'git-resolver': 'git+ssh://git@gitlab/pnpm/git-resolver#988c61e11dc8d9ca0b5580cb15291951812549dc',
},
specifiers: {
foo: 'https://gitlab.com/foo/foo.git',
'git-resolver': 'ssh://git@gitlab:pnpm/git-resolver#988c61e11dc8d9ca0b5580cb15291951812549dc',
},
},
Expand All @@ -129,6 +144,13 @@ test('convertToLockfileObject converts git-hosted dependencies via ssh', () => {
type: 'git',
},
},
'foo@git+https://git@gitlab.com:foo/foo.git#6ae3f32d7c631f64fbaf70cdd349ae6e2cc68e6c': {
resolution: {
commit: '6ae3f32d7c631f64fbaf70cdd349ae6e2cc68e6c',
repo: 'git@gitlab.com:foo/foo.git',
type: 'git',
},
},
},
})
})
3 changes: 3 additions & 0 deletions lockfile/lockfile-file/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
{
"path": "../../packages/types"
},
{
"path": "../../resolving/git-resolver"
},
{
"path": "../lockfile-types"
},
Expand Down
17 changes: 6 additions & 11 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions resolving/git-resolver/src/createGitHostedPkgId.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export function createGitHostedPkgId ({ repo, commit, path }: { repo: string, commit: string, path?: string }): string {
let id = `${repo.includes('://') ? '' : 'https://'}${repo}#${commit}`
if (!id.startsWith('git+')) id = `git+${id}`
if (path) {
id += `&path:${path}`
}
return id
}
8 changes: 4 additions & 4 deletions resolving/git-resolver/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ import { type TarballResolution, type GitResolution, type ResolveResult } from '
import git from 'graceful-git'
import semver from 'semver'
import { parsePref, type HostedPackageSpec } from './parsePref'
import { createGitHostedPkgId } from './createGitHostedPkgId'

export { createGitHostedPkgId }

export type { HostedPackageSpec }

Expand Down Expand Up @@ -54,10 +57,7 @@ export function createGitResolver (
id += `#path:${resolution.path}`
}
} else {
id = `${resolution.repo.startsWith('git+') ? '' : 'git+'}${resolution.repo}#${resolution.commit}`
if (resolution.path) {
id += `&path:${resolution.path}`
}
id = createGitHostedPkgId(resolution)
}

return {
Expand Down
9 changes: 9 additions & 0 deletions resolving/git-resolver/test/createGitHostedPkgId.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { createGitHostedPkgId } from '@pnpm/git-resolver'

test.each([
[{ repo: 'ssh://git@example.com/org/repo.git', commit: 'cba04669e621b85fbdb33371604de1a2898e68e9' }, 'git+ssh://git@example.com/org/repo.git#cba04669e621b85fbdb33371604de1a2898e68e9'],
[{ repo: 'https://0000000000000000000000000000000000000000:x-oauth-basic@github.com/foo/bar.git', commit: '0000000000000000000000000000000000000000' }, 'git+https://0000000000000000000000000000000000000000:x-oauth-basic@github.com/foo/bar.git#0000000000000000000000000000000000000000'],
[{ repo: 'file:///Users/zoltan/src/pnpm/pnpm/resolving/git-resolver', commit: '988c61e11dc8d9ca0b5580cb15291951812549dc' }, 'git+file:///Users/zoltan/src/pnpm/pnpm/resolving/git-resolver#988c61e11dc8d9ca0b5580cb15291951812549dc'],
])('createGitHostedPkgId', (resolution, id) => {
expect(createGitHostedPkgId(resolution)).toBe(id)
})

0 comments on commit c969f37

Please sign in to comment.