-
-
Notifications
You must be signed in to change notification settings - Fork 932
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add experimental use-inline-specifiers-lockfile-format #5091
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -157,6 +157,9 @@ export interface Config { | |
changedFilesIgnorePattern?: string[] | ||
rootProjectManifest?: ProjectManifest | ||
userConfig: Record<string, string> | ||
|
||
// feature flags for experimental testing | ||
useInlineSpecifiersLockfileFormat?: boolean // For https://github.com/pnpm/pnpm/issues/4725 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The idea behind the naming here was that "specifiers" are now "inlined" next to their resolved version instead of in their own block. I think this naming is awful, but can't think of any better ones at the moment. |
||
} | ||
|
||
export interface ConfigWithDeprecatedSettings extends Config { | ||
|
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 | ||
} |
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 { | ||
gluxon marked this conversation as resolved.
Show resolved
Hide resolved
|
||
...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 | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is
minor
the right semver change for a flagged feature? This didn't feel like apatch
, but I could be wrong.