Skip to content

Commit

Permalink
fix(core): support yarn berry patches in pruned lock file (#15993)
Browse files Browse the repository at this point in the history
  • Loading branch information
meeroslav authored Mar 31, 2023
1 parent d9c55aa commit 30f993e
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1037,6 +1037,7 @@ __metadata:
"@nrwl/devkit": 15.0.13
eslint-plugin-disable-autofix: "npm:@mattlewis92/[email protected]"
postgres: "https://github.com/charsleysa/postgres.git#commit=3b1a01b2da3e2fafb1a79006f838eff11a8de3cb"
typescript: 4.8.4
yargs: 17.6.2
devDependencies:
react: 18.2.0
Expand Down Expand Up @@ -1073,6 +1074,26 @@ __metadata:
languageName: node
linkType: hard
"typescript@npm:4.8.4":
version: 4.8.4
resolution: "typescript@npm:4.8.4"
bin:
tsc: bin/tsc
tsserver: bin/tsserver
checksum: ee000bc26848147ad423b581bd250075662a354d84f0e06eb76d3b892328d8d4440b7487b5a83e851b12b255f55d71835b008a66cbf8f255a11e4400159237db
languageName: node
linkType: hard
"typescript@patch:[email protected]#~builtin<compat/typescript>":
version: 4.8.4
resolution: "typescript@patch:typescript@npm%3A4.8.4#~builtin<compat/typescript>::version=4.8.4&hash=23ec76"
bin:
tsc: bin/tsc
tsserver: bin/tsserver
checksum: ab417a2f398380c90a6cf5a5f74badd17866adf57f1165617d6a551f059c3ba0a3e4da0d147b3ac5681db9ac76a303c5876394b13b3de75fdd5b1eaa06181c9d
languageName: node
linkType: hard
"uri-js@npm:^4.2.2":
version: 4.4.1
resolution: "uri-js@npm:4.4.1"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1074,6 +1074,26 @@ __metadata:
languageName: node
linkType: hard
"typescript@npm:4.8.4":
version: 4.8.4
resolution: "typescript@npm:4.8.4"
bin:
tsc: bin/tsc
tsserver: bin/tsserver
checksum: ee000bc26848147ad423b581bd250075662a354d84f0e06eb76d3b892328d8d4440b7487b5a83e851b12b255f55d71835b008a66cbf8f255a11e4400159237db
languageName: node
linkType: hard
"typescript@patch:[email protected]#~builtin<compat/typescript>":
version: 4.8.4
resolution: "typescript@patch:typescript@npm%3A4.8.4#~builtin<compat/typescript>::version=4.8.4&hash=23ec76"
bin:
tsc: bin/tsc
tsserver: bin/tsserver
checksum: ab417a2f398380c90a6cf5a5f74badd17866adf57f1165617d6a551f059c3ba0a3e4da0d147b3ac5681db9ac76a303c5876394b13b3de75fdd5b1eaa06181c9d
languageName: node
linkType: hard
"uri-js@npm:^4.2.2":
version: 4.4.1
resolution: "uri-js@npm:4.4.1"
Expand Down
5 changes: 3 additions & 2 deletions packages/nx/src/plugins/js/lock-file/yarn-parser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ describe('yarn LockFile utility', () => {
const builder = new ProjectGraphBuilder();
parseYarnLockfile(berryLockFile, builder);
const graph = builder.getUpdatedProjectGraph();
expect(Object.keys(graph.externalNodes).length).toEqual(128); //124 hoisted
expect(Object.keys(graph.externalNodes).length).toEqual(129); //124 hoisted

expect(graph.externalNodes['npm:minimatch']).toMatchInlineSnapshot(`
Object {
Expand Down Expand Up @@ -433,6 +433,7 @@ describe('yarn LockFile utility', () => {
'npm:@mattlewis92/[email protected]',
postgres:
'https://github.com/charsleysa/postgres.git#commit=3b1a01b2da3e2fafb1a79006f838eff11a8de3cb',
typescript: '4.8.4',
yargs: '17.6.2',
},
devDependencies: {
Expand Down Expand Up @@ -930,7 +931,7 @@ __metadata:
const prunedGraph = pruneProjectGraph(graph, prunedPackageJson);
expect(stringifyYarnLockfile(prunedGraph, lockFile, prunedPackageJson))
.toMatchInlineSnapshot(`
"# This file was generated by Nx. Do not edit this file directly
"# This file is generated by running \\"yarn install\\" inside your project.
# Manual changes might be lost - proceed with caution!
__metadata:
Expand Down
39 changes: 35 additions & 4 deletions packages/nx/src/plugins/js/lock-file/yarn-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ function addNodes(
const nodes: Map<string, Map<string, ProjectGraphExternalNode>> = new Map();

Object.entries(dependencies).forEach(([keys, snapshot]) => {
// ignore workspace projects
if (snapshot.linkType === 'soft') {
// ignore workspace projects & patches
if (snapshot.linkType === 'soft' || keys.includes('@patch:')) {
return;
}
const packageName = keys.slice(0, keys.indexOf('@', 1));
Expand Down Expand Up @@ -313,13 +313,28 @@ function mapSnapshots(
snapshotMap.get(snapshot).add(requestedKey);
}
}

if (isBerry) {
// look for patched versions
const patch = findPatchedKeys(groupedDependencies, node);
if (patch) {
const [matchedKeys, snapshot] = patch;
snapshotMap.set(snapshot, new Set(matchedKeys));
}
}
});

// remove keys that match version ranges that have been pruned away
snapshotMap.forEach((keysSet) => {
for (const key of keysSet.values()) {
const packageName = key.slice(0, key.indexOf('@', 1));
if (!existingKeys.get(packageName).has(key)) {
let normalizedKey = key;
if (isBerry && key.includes('@patch:') && key.includes('#')) {
normalizedKey = key
.slice(0, key.indexOf('#'))
.replace(`@patch:${packageName}@`, '@npm:');
}
if (!existingKeys.get(packageName).has(normalizedKey)) {
keysSet.delete(key);
}
}
Expand Down Expand Up @@ -414,7 +429,23 @@ function findOriginalKeys(
}
}

const BERRY_LOCK_FILE_DISCLAIMER = `# This file was generated by Nx. Do not edit this file directly\n# Manual changes might be lost - proceed with caution!\n\n`;
function findPatchedKeys(
dependencies: Record<string, YarnDependency>,
node: ProjectGraphExternalNode
): [string[], YarnDependency] | void {
for (const keyExpr of Object.keys(dependencies)) {
const snapshot = dependencies[keyExpr];
const keys = keyExpr.split(', ');
if (!keys[0].startsWith(`${node.data.packageName}@patch:`)) {
continue;
}
if (snapshot.version === node.data.version) {
return [keys, snapshot];
}
}
}

const BERRY_LOCK_FILE_DISCLAIMER = `# This file is generated by running "yarn install" inside your project.\n# Manual changes might be lost - proceed with caution!\n\n`;

function generateRootWorkspacePackage(
packageJson: NormalizedPackageJson
Expand Down

1 comment on commit 30f993e

@vercel
Copy link

@vercel vercel bot commented on 30f993e Mar 31, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

nx-dev – ./

nx-dev-git-master-nrwl.vercel.app
nx-dev-nrwl.vercel.app
nx-five.vercel.app
nx.dev

Please sign in to comment.