-
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
9 changed files
with
195 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,10 @@ | ||
--- | ||
"@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`. | ||
|
||
If this feature flag is committed to a repo, we recommend setting the minimum allowed version of pnpm to this release in the `package.json` `engines` field. Once this is set, older pnpm versions will throw on invalid lockfile versions. |
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 INLINE_SPECIFIERS_FORMAT_LOCKFILE_VERSION_SUFFIX = '-inlineSpecifiers' | ||
|
||
/** | ||
* 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, 'lockfileVersion' | 'importers'> { | ||
lockfileVersion: string | ||
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 | ||
} |
114 changes: 114 additions & 0 deletions
114
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,114 @@ | ||
import type { Lockfile, ProjectSnapshot, ResolvedDependencies } from '@pnpm/lockfile-types' | ||
import { | ||
INLINE_SPECIFIERS_FORMAT_LOCKFILE_VERSION_SUFFIX, | ||
InlineSpecifiersLockfile, | ||
InlineSpecifiersProjectSnapshot, | ||
InlineSpecifiersResolvedDependencies, | ||
} from './InlineSpecifiersLockfile' | ||
|
||
export function isExperimentalInlineSpecifiersFormat ( | ||
lockfile: InlineSpecifiersLockfile | Lockfile | ||
): lockfile is InlineSpecifiersLockfile { | ||
const { lockfileVersion } = lockfile | ||
return typeof lockfileVersion === 'string' && lockfileVersion.endsWith(INLINE_SPECIFIERS_FORMAT_LOCKFILE_VERSION_SUFFIX) | ||
} | ||
|
||
export function convertToInlineSpecifiersFormat (lockfile: Lockfile): InlineSpecifiersLockfile { | ||
return { | ||
...lockfile, | ||
lockfileVersion: `${lockfile.lockfileVersion}${INLINE_SPECIFIERS_FORMAT_LOCKFILE_VERSION_SUFFIX}`, | ||
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 { lockfileVersion, importers, ...rest } = lockfile | ||
|
||
const originalVersionStr = lockfileVersion.replace(INLINE_SPECIFIERS_FORMAT_LOCKFILE_VERSION_SUFFIX, '') | ||
const originalVersion = Number(originalVersionStr) | ||
if (isNaN(originalVersion)) { | ||
throw new Error(`Unable to revert lockfile from inline specifiers format. Invalid version parsed: ${originalVersionStr}`) | ||
} | ||
|
||
return { | ||
...rest, | ||
lockfileVersion: originalVersion, | ||
importers: mapValues(importers, revertProjectSnapshot), | ||
} | ||
} | ||
|
||
function convertProjectSnapshotToInlineSpecifiersFormat ( | ||
projectSnapshot: ProjectSnapshot | ||
): InlineSpecifiersProjectSnapshot { | ||
const { specifiers, ...rest } = projectSnapshot | ||
const convertBlock = (block?: ResolvedDependencies) => | ||
block != null | ||
? convertResolvedDependenciesToInlineSpecifiersFormat(block, { specifiers }) | ||
: block | ||
return { | ||
...rest, | ||
dependencies: convertBlock(projectSnapshot.dependencies), | ||
optionalDependencies: convertBlock(projectSnapshot.optionalDependencies), | ||
devDependencies: convertBlock(projectSnapshot.devDependencies), | ||
} | ||
} | ||
|
||
function convertResolvedDependenciesToInlineSpecifiersFormat ( | ||
resolvedDependencies: ResolvedDependencies, | ||
{ specifiers }: { specifiers: ResolvedDependencies} | ||
): InlineSpecifiersResolvedDependencies { | ||
return mapValues(resolvedDependencies, (version, depName) => ({ | ||
specifier: specifiers[depName], | ||
version, | ||
})) | ||
} | ||
|
||
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: (val: T, key: string) => U): Record<string, U> { | ||
const result = {} | ||
for (const [key, value] of Object.entries(obj)) { | ||
result[key] = mapper(value, key) | ||
} | ||
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