-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add experimental use-inline-specifiers-lockfile-format
- Loading branch information
Showing
10 changed files
with
186 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
--- | ||
"@pnpm/config": minor | ||
"@pnpm/core": minor | ||
"@pnpm/lockfile-file": minor | ||
"pnpm": minor | ||
--- | ||
|
||
Add experimental lockfile format that should merge conflict less in the `importers` section. Enabled by setting the `use-inline-specifiers-lockfile-format = true` feature flag in `.npmrc`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
packages/lockfile-file/src/experiments/InlineSpecifiersLockfile.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import type { Lockfile } from '@pnpm/lockfile-types' | ||
import type { DependenciesMeta } from '@pnpm/types' | ||
|
||
export const InlineSpecifiersFormatLockfileKey = 'inlineSpecifiersFormat' | ||
|
||
/** | ||
* Similar to the current Lockfile importers format (lockfile version 5.4 at | ||
* time of writing), but specifiers are moved to each ResolvedDependencies block | ||
* instead of being declared on its own dictionary. | ||
* | ||
* This is an experiment to reduce one flavor of merge conflicts in lockfiles. | ||
* For more info: https://github.com/pnpm/pnpm/issues/4725. | ||
*/ | ||
export interface InlineSpecifiersLockfile extends Omit<Lockfile, 'importers'> { | ||
[InlineSpecifiersFormatLockfileKey]: true | ||
importers: Record<string, InlineSpecifiersProjectSnapshot> | ||
} | ||
|
||
/** | ||
* Similar to the current ProjectSnapshot interface, but omits the "specifiers" | ||
* field in favor of inlining each specifier next to its version resolution in | ||
* dependency blocks. | ||
*/ | ||
export interface InlineSpecifiersProjectSnapshot { | ||
dependencies?: InlineSpecifiersResolvedDependencies | ||
devDependencies?: InlineSpecifiersResolvedDependencies | ||
optionalDependencies?: InlineSpecifiersResolvedDependencies | ||
dependenciesMeta?: DependenciesMeta | ||
} | ||
|
||
export interface InlineSpecifiersResolvedDependencies { | ||
[depName: string]: SpecifierAndResolution | ||
} | ||
|
||
export interface SpecifierAndResolution { | ||
specifier: string | ||
version: string | ||
} |
105 changes: 105 additions & 0 deletions
105
packages/lockfile-file/src/experiments/inlineSpecifiersLockfileConverters.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
import type { Lockfile, ProjectSnapshot, ResolvedDependencies } from '@pnpm/lockfile-types' | ||
import type { InlineSpecifiersLockfile, InlineSpecifiersProjectSnapshot, InlineSpecifiersResolvedDependencies } from './InlineSpecifiersLockfile' | ||
|
||
export function isExperimentalInlineSpecifiersFormat ( | ||
lockfile: InlineSpecifiersLockfile | Lockfile | ||
): lockfile is InlineSpecifiersLockfile { | ||
return (lockfile as InlineSpecifiersLockfile).inlineSpecifiersFormat ?? false | ||
} | ||
|
||
export function convertToInlineSpecifiersFormat (lockfile: Lockfile): InlineSpecifiersLockfile { | ||
return { | ||
inlineSpecifiersFormat: true, | ||
...lockfile, | ||
importers: mapValues(lockfile.importers, convertProjectSnapshotToInlineSpecifiersFormat), | ||
} | ||
} | ||
|
||
export function revertFromInlineSpecifiersFormatIfNecessary (lockfile: Lockfile | InlineSpecifiersLockfile): Lockfile { | ||
return isExperimentalInlineSpecifiersFormat(lockfile) | ||
? revertFromInlineSpecifiersFormat(lockfile) | ||
: lockfile | ||
} | ||
|
||
export function revertFromInlineSpecifiersFormat (lockfile: InlineSpecifiersLockfile): Lockfile { | ||
const { importers, inlineSpecifiersFormat, ...rest } = lockfile | ||
|
||
return { | ||
...rest, | ||
importers: mapValues(importers, revertProjectSnapshot), | ||
} | ||
} | ||
|
||
function convertProjectSnapshotToInlineSpecifiersFormat ( | ||
projectSnapshot: ProjectSnapshot | ||
): InlineSpecifiersProjectSnapshot { | ||
const { specifiers } = projectSnapshot | ||
return { | ||
dependencies: projectSnapshot.dependencies != null | ||
? convertResolvedDependenciesToInlineSpecifiersFormat(projectSnapshot.dependencies, { specifiers }) | ||
: projectSnapshot.dependencies, | ||
optionalDependencies: projectSnapshot.optionalDependencies != null | ||
? convertResolvedDependenciesToInlineSpecifiersFormat(projectSnapshot.optionalDependencies, { specifiers }) | ||
: projectSnapshot.optionalDependencies, | ||
devDependencies: projectSnapshot.devDependencies != null | ||
? convertResolvedDependenciesToInlineSpecifiersFormat(projectSnapshot.devDependencies, { specifiers }) | ||
: projectSnapshot.devDependencies, | ||
dependenciesMeta: projectSnapshot.dependenciesMeta, | ||
} | ||
} | ||
|
||
function convertResolvedDependenciesToInlineSpecifiersFormat ( | ||
resolvedDependencies: ResolvedDependencies, | ||
{ specifiers }: { specifiers: ResolvedDependencies} | ||
): InlineSpecifiersResolvedDependencies { | ||
const result: InlineSpecifiersResolvedDependencies = {} | ||
for (const [depName, version] of Object.entries(resolvedDependencies)) { | ||
const specifier = specifiers[depName] | ||
result[depName] = { specifier, version } | ||
} | ||
return result | ||
} | ||
|
||
function revertProjectSnapshot (from: InlineSpecifiersProjectSnapshot): ProjectSnapshot { | ||
const specifiers: ResolvedDependencies = {} | ||
|
||
function moveSpecifiers (from: InlineSpecifiersResolvedDependencies): ResolvedDependencies { | ||
const resolvedDependencies: ResolvedDependencies = {} | ||
for (const [depName, { specifier, version }] of Object.entries(from)) { | ||
const existingValue = specifiers[depName] | ||
if (existingValue != null && existingValue !== specifier) { | ||
throw new Error(`Project snapshot lists the same dependency more than once with conflicting versions: ${depName}`) | ||
} | ||
|
||
specifiers[depName] = specifier | ||
resolvedDependencies[depName] = version | ||
} | ||
return resolvedDependencies | ||
} | ||
|
||
const dependencies = from.dependencies == null | ||
? from.dependencies | ||
: moveSpecifiers(from.dependencies) | ||
const devDependencies = from.devDependencies == null | ||
? from.devDependencies | ||
: moveSpecifiers(from.devDependencies) | ||
const optionalDependencies = from.optionalDependencies == null | ||
? from.optionalDependencies | ||
: moveSpecifiers(from.optionalDependencies) | ||
|
||
return { | ||
...from, | ||
specifiers, | ||
dependencies, | ||
devDependencies, | ||
optionalDependencies, | ||
} | ||
} | ||
|
||
function mapValues<T, U> (obj: Record<string, T>, mapper: (el: T) => U): Record<string, U> { | ||
const result = {} | ||
for (const [key, value] of Object.entries(obj)) { | ||
result[key] = mapper(value) | ||
} | ||
return result | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters