Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 56 additions & 1 deletion scopes/dependencies/pnpm/lockfile-deps-graph-converter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,8 +223,9 @@ describe('convertLockfileToGraph simple case', () => {
// The lockfile records no pnpmfileChecksum. Whether the graph then has the
// property set to undefined or not at all depends on how the class is
// compiled, so it is checked on its own.
const { pnpmfileChecksum, ...graphFields } = graph;
const { pnpmfileChecksum, lockfileSettings, ...graphFields } = graph;
expect(pnpmfileChecksum).to.equal(undefined);
expect(lockfileSettings).to.equal(undefined);
expect({
...graphFields,
packages: Object.fromEntries(graph.packages.entries()),
Expand Down Expand Up @@ -1249,3 +1250,57 @@ describe('pnpmfileChecksum', () => {
expect(lockfile).not.to.have.property('pnpmfileChecksum');
});
});

describe('lockfile settings', () => {
const componentDir = 'comps/comp1';
const settings = { autoInstallPeers: true, dedupePeers: true, injectWorkspacePackages: true };

function createLockfile(lockfileSettings?: BitLockfileFile['settings']): BitLockfileFile {
return {
lockfileVersion: '9.0',
...(lockfileSettings ? { settings: lockfileSettings } : {}),
importers: {
[componentDir]: {
dependencies: { foo: { version: '1.0.0', specifier: '1.0.0' } },
},
},
packages: {
'[email protected]': { resolution: { integrity: 'sha512-aaa' } },
},
snapshots: {
'[email protected]': {},
},
} as BitLockfileFile;
}

function graphToLockfile(graph: DependenciesGraph) {
return convertGraphToLockfile(graph, {
manifests: {
[path.resolve(componentDir)]: { dependencies: { foo: '1.0.0' } },
},
rootDir: process.cwd(),
resolve: () => ({ resolution: { integrity: '0000' } }) as any,
});
}

it('should be kept from the lockfile to the graph and back', async () => {
const graph = convertLockfileToGraph(createLockfile(settings), {
componentRelativeDir: componentDir,
componentIdByPkgName: new Map(),
});
expect(graph.lockfileSettings).to.eql(settings);

const lockfile = await graphToLockfile(DependenciesGraph.deserialize(graph.serialize())!);
expect(lockfile.settings).to.eql(settings);
});

it('should stay unset for a graph saved without them', async () => {
const graph = convertLockfileToGraph(createLockfile(), {
componentRelativeDir: componentDir,
componentIdByPkgName: new Map(),
});

const lockfile = await graphToLockfile(graph);
expect(lockfile).not.to.have.property('settings');
});
});
8 changes: 8 additions & 0 deletions scopes/dependencies/pnpm/lockfile-deps-graph-converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ function _convertLockfileToGraph(
edges: buildEdges(lockfile, { directDependencies, componentIdByPkgName }),
packages: buildPackages(lockfile, { componentIdByPkgName }),
pnpmfileChecksum: lockfile.pnpmfileChecksum,
lockfileSettings: lockfile.settings,
});
dropOrphanFilePkgs(graph);
return graph;
Expand Down Expand Up @@ -367,6 +368,13 @@ export async function convertGraphToLockfile(
if (graph.pnpmfileChecksum != null) {
lockfile.pnpmfileChecksum = graph.pnpmfileChecksum;
}
// Carry the resolution settings the graph was resolved under as well. pnpm
// compares them with the install's settings, and a lockfile without them
// reads as resolved with dedupePeers and injectWorkspacePackages off, which
// Bit's installs never match, so every restored lockfile was resolved again.
if (graph.lockfileSettings != null) {
lockfile.settings = graph.lockfileSettings;
}
const rootEdge = graph.findRootEdge();
if (rootEdge) {
for (const [projectDir, manifest] of Object.entries(manifests)) {
Expand Down
39 changes: 36 additions & 3 deletions scopes/dependencies/pnpm/pnpm.package-manager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,16 +81,49 @@ describe('mergeGraphLockfileIntoExisting', () => {
function lockfile(pnpmfileChecksum?: string) {
return { lockfileVersion: '9.0', importers: {}, ...(pnpmfileChecksum ? { pnpmfileChecksum } : {}) };
}
/** A graph lockfile that adds a resolution to the merge. */
function resolved<T extends object>(graphLockfile: T) {
return { ...graphLockfile, packages: { '[email protected]': { resolution: { integrity: 'sha512-a' } } } };
}
const settings = { autoInstallPeers: true, dedupePeers: true };

it('keeps the pnpmfileChecksum both lockfiles were resolved with', () => {
const merged = mergeGraphLockfileIntoExisting(lockfile('bit-1'), lockfile('bit-1'));
const merged = mergeGraphLockfileIntoExisting(lockfile('bit-1'), resolved(lockfile('bit-1')));
expect(merged.pnpmfileChecksum).to.equal('bit-1');
});

it('drops a pnpmfileChecksum the lockfiles disagree on', () => {
expect(mergeGraphLockfileIntoExisting(lockfile('bit-1'), lockfile('bit-2'))).not.to.have.property(
expect(mergeGraphLockfileIntoExisting(lockfile('bit-1'), resolved(lockfile('bit-2')))).not.to.have.property(
'pnpmfileChecksum'
);
expect(mergeGraphLockfileIntoExisting(lockfile('bit-1'), resolved(lockfile()))).not.to.have.property(
'pnpmfileChecksum'
);
expect(mergeGraphLockfileIntoExisting(lockfile('bit-1'), lockfile())).not.to.have.property('pnpmfileChecksum');
});

it('keeps the settings both lockfiles were resolved under', () => {
const merged = mergeGraphLockfileIntoExisting(
{ ...lockfile(), settings },
resolved({ ...lockfile(), settings: { ...settings } })
);
expect(merged.settings).to.eql(settings);
});

it('drops settings the lockfiles disagree on', () => {
expect(
mergeGraphLockfileIntoExisting(
{ ...lockfile(), settings },
resolved({ ...lockfile(), settings: { ...settings, dedupePeers: false } })
)
).not.to.have.property('settings');
expect(mergeGraphLockfileIntoExisting({ ...lockfile(), settings }, resolved(lockfile()))).not.to.have.property(
'settings'
);
});

it('keeps the checksum and settings of the existing lockfile when the graph adds no resolutions', () => {
const merged = mergeGraphLockfileIntoExisting({ ...lockfile('bit-1'), settings }, lockfile());
expect(merged.pnpmfileChecksum).to.equal('bit-1');
expect(merged.settings).to.eql(settings);
});
});
25 changes: 21 additions & 4 deletions scopes/dependencies/pnpm/pnpm.package-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -642,10 +642,17 @@ export function mergeGraphLockfileIntoExisting(existing: LockfileFile, graph: Lo
snapshots: mergeEntryRecords(existing.snapshots, graph.snapshots),
};
// The merged lockfile mixes resolutions from both sides, so it can only
// claim the readPackage hooks checksum both were resolved with. Otherwise
// drop it, and pnpm resolves again instead of trusting the mix.
if (existing.pnpmfileChecksum !== graph.pnpmfileChecksum) {
delete merged.pnpmfileChecksum;
// claim the readPackage hooks checksum and the resolution settings both were
// resolved with. Otherwise drop them, and pnpm resolves again instead of
// trusting the mix. A graph that adds no resolutions (an empty or link-only
// one) mixes nothing in, so the existing lockfile keeps its own.
if (hasResolutions(graph)) {
if (existing.pnpmfileChecksum !== graph.pnpmfileChecksum) {
delete merged.pnpmfileChecksum;
}
if (!sameLockfileSettings(existing.settings, graph.settings)) {
delete merged.settings;
}
}
if (existingBit || graphBit) {
(merged as LockfileFile & { bit?: Record<string, unknown> }).bit = {
Expand All @@ -658,6 +665,16 @@ export function mergeGraphLockfileIntoExisting(existing: LockfileFile, graph: Lo
return merged;
}

function hasResolutions(lockfile: LockfileFile): boolean {
return Object.keys(lockfile.packages ?? {}).length > 0 || Object.keys(lockfile.snapshots ?? {}).length > 0;
}

function sameLockfileSettings(settings1: LockfileFile['settings'], settings2: LockfileFile['settings']): boolean {
if (settings1 == null || settings2 == null) return settings1 === settings2;
const keys = new Set([...Object.keys(settings1), ...Object.keys(settings2)]);
return [...keys].every((key) => settings1[key] === settings2[key]);
}

function mergeEntryRecords<T extends object>(
existing: Record<string, T> | undefined,
graph: Record<string, T> | undefined
Expand Down
50 changes: 50 additions & 0 deletions scopes/scope/objects/models/dependencies-graph.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,3 +185,53 @@ function rootEdge(neighbours: DependencyEdge['neighbours']): DependencyEdge {
function edge(id: string, neighbours: DependencyEdge['neighbours'] = []): DependencyEdge {
return { id, neighbours };
}

describe('DependenciesGraph lockfileSettings', () => {
const settings = { autoInstallPeers: true, dedupePeers: true, injectWorkspacePackages: true };

it('survives serialize and deserialize', () => {
const graph = createGraph([rootEdge([])], []);
graph.lockfileSettings = settings;

expect(DependenciesGraph.deserialize(graph.serialize())?.lockfileSettings).to.eql(settings);
});

it('is unset on a graph serialized before the field existed', () => {
const restored = DependenciesGraph.deserialize(
JSON.stringify({ schemaVersion: '2.0', packages: {}, edges: [rootEdge([])] })
);

expect(restored?.lockfileSettings).to.equal(undefined);
});

it('is kept by merge when both graphs were resolved under the same settings', () => {
const base = createGraph([rootEdge([{ id: '[email protected]', name: 'foo', specifier: '1.0.0' }])], ['[email protected]']);
base.lockfileSettings = settings;
const incoming = createGraph([rootEdge([{ id: '[email protected]', name: 'bar', specifier: '1.0.0' }])], ['[email protected]']);
incoming.lockfileSettings = { ...settings };

base.merge(incoming);

expect(base.lockfileSettings).to.eql(settings);
});

it('is dropped by merge when the graphs were resolved under different settings', () => {
const base = createGraph([rootEdge([{ id: '[email protected]', name: 'foo', specifier: '1.0.0' }])], ['[email protected]']);
base.lockfileSettings = settings;
const incoming = createGraph([rootEdge([{ id: '[email protected]', name: 'bar', specifier: '1.0.0' }])], ['[email protected]']);
incoming.lockfileSettings = { ...settings, dedupePeers: false };

base.merge(incoming);

expect(base.lockfileSettings).to.equal(undefined);
});

it('is dropped by merge when only one graph has them', () => {
const base = createGraph([rootEdge([{ id: '[email protected]', name: 'foo', specifier: '1.0.0' }])], ['[email protected]']);
base.lockfileSettings = settings;

base.merge(createGraph([rootEdge([{ id: '[email protected]', name: 'bar', specifier: '1.0.0' }])], ['[email protected]']));

expect(base.lockfileSettings).to.equal(undefined);
});
});
35 changes: 33 additions & 2 deletions scopes/scope/objects/models/dependencies-graph.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import semver from 'semver';
import type { LockfilePackageInfo } from '@pnpm/lockfile.types';
import type { LockfilePackageInfo, LockfileSettings } from '@pnpm/lockfile.types';

export type PackagesMap = Map<string, PackageAttributes>;

Expand Down Expand Up @@ -47,31 +47,44 @@ export class DependenciesGraph {
* existed don't have it.
*/
pnpmfileChecksum?: string;
/**
* The `settings` block of the lockfile the graph was created from: the
* resolution settings (dedupePeers, injectWorkspacePackages, ...) the
* dependencies were resolved under. A lockfile restored from the graph
* carries them, so pnpm can install the restored resolution without
* resolving again while the install's settings still match. Graphs created
* before this field existed don't have it, and pnpm resolves them again.
*/
lockfileSettings?: LockfileSettings;

constructor({
packages,
edges,
schemaVersion,
pnpmfileChecksum,
lockfileSettings,
}: {
packages: PackagesMap;
edges: DependencyEdge[];
schemaVersion?: string;
pnpmfileChecksum?: string;
lockfileSettings?: LockfileSettings;
}) {
this.packages = packages;
this.edges = edges;
this.schemaVersion = schemaVersion ?? DEPENDENCIES_GRAPH_SCHEMA_VERSION;
this.pnpmfileChecksum = pnpmfileChecksum;
this.lockfileSettings = lockfileSettings;
}

serialize(): string {
return JSON.stringify({
schemaVersion: this.schemaVersion,
packages: Object.fromEntries(this.packages.entries()),
edges: this.edges,
// JSON.stringify omits it when undefined.
// JSON.stringify omits these when undefined.
pnpmfileChecksum: this.pnpmfileChecksum,
lockfileSettings: this.lockfileSettings,
});
}

Expand All @@ -86,11 +99,13 @@ export class DependenciesGraph {
edges: parsed.edges,
packages: new Map(Object.entries(parsed.packages)),
pnpmfileChecksum: parsed.pnpmfileChecksum,
lockfileSettings: parsed.lockfileSettings,
});
}

merge(graph: DependenciesGraph): void {
this.pnpmfileChecksum = mergePnpmfileChecksums(this, graph);
this.lockfileSettings = mergeLockfileSettings(this, graph);
const rootEdge = this.findRootEdge();
const incomingRootEdge = graph.findRootEdge();
const directDependencies = incomingRootEdge?.neighbours;
Expand Down Expand Up @@ -310,6 +325,22 @@ function mergePnpmfileChecksums(graph1: DependenciesGraph, graph2: DependenciesG
return graph1.pnpmfileChecksum === graph2.pnpmfileChecksum ? graph1.pnpmfileChecksum : undefined;
}

/**
* A merged graph mixes resolutions from both graphs, so it can only claim the
* settings both were resolved under.
*/
function mergeLockfileSettings(graph1: DependenciesGraph, graph2: DependenciesGraph): LockfileSettings | undefined {
if (!graph2.hasResolutions()) return graph1.lockfileSettings;
if (!graph1.hasResolutions()) return graph2.lockfileSettings;
return sameLockfileSettings(graph1.lockfileSettings, graph2.lockfileSettings) ? graph1.lockfileSettings : undefined;
}

function sameLockfileSettings(settings1?: LockfileSettings, settings2?: LockfileSettings): boolean {
if (settings1 == null || settings2 == null) return settings1 === settings2;
const keys = new Set([...Object.keys(settings1), ...Object.keys(settings2)]);
return [...keys].every((key) => settings1[key] === settings2[key]);
Comment thread
zkochan marked this conversation as resolved.
Resolved
}

function isSameDirectDependency(dep1: DependencyNeighbour, dep2: DependencyNeighbour): boolean {
if (!dep1.name || !dep2.name) return dep1.id === dep2.id;
if (dep1.name !== dep2.name) return false;
Expand Down
Loading