From 65d8bece3b3c134a3d6761768c5ffeb4ff4325ed Mon Sep 17 00:00:00 2001 From: Miroslav Jonas Date: Thu, 30 Mar 2023 15:26:35 +0200 Subject: [PATCH 1/2] fix(core): use yarn berry disclaimer --- packages/nx/src/plugins/js/lock-file/yarn-parser.spec.ts | 2 +- packages/nx/src/plugins/js/lock-file/yarn-parser.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/nx/src/plugins/js/lock-file/yarn-parser.spec.ts b/packages/nx/src/plugins/js/lock-file/yarn-parser.spec.ts index 0b6c6035183ba..568cf901f039c 100644 --- a/packages/nx/src/plugins/js/lock-file/yarn-parser.spec.ts +++ b/packages/nx/src/plugins/js/lock-file/yarn-parser.spec.ts @@ -930,7 +930,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: diff --git a/packages/nx/src/plugins/js/lock-file/yarn-parser.ts b/packages/nx/src/plugins/js/lock-file/yarn-parser.ts index 7ef4c1369a7ed..49bb00a58407a 100644 --- a/packages/nx/src/plugins/js/lock-file/yarn-parser.ts +++ b/packages/nx/src/plugins/js/lock-file/yarn-parser.ts @@ -414,7 +414,7 @@ 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`; +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 From 58d7c23a35be0909ccedcc46f39771247b3ca28a Mon Sep 17 00:00:00 2001 From: Miroslav Jonas Date: Fri, 31 Mar 2023 00:16:22 +0200 Subject: [PATCH 2/2] fix(core): add support for berry patches --- .../yarn-berry.lock.pruned.ts | 21 +++++++++++ .../auxiliary-packages/yarn-berry.lock.ts | 20 ++++++++++ .../plugins/js/lock-file/yarn-parser.spec.ts | 5 ++- .../src/plugins/js/lock-file/yarn-parser.ts | 37 +++++++++++++++++-- 4 files changed, 78 insertions(+), 5 deletions(-) diff --git a/packages/nx/src/plugins/js/lock-file/__fixtures__/auxiliary-packages/yarn-berry.lock.pruned.ts b/packages/nx/src/plugins/js/lock-file/__fixtures__/auxiliary-packages/yarn-berry.lock.pruned.ts index c11f526c7e404..f05a00fec7cc3 100644 --- a/packages/nx/src/plugins/js/lock-file/__fixtures__/auxiliary-packages/yarn-berry.lock.pruned.ts +++ b/packages/nx/src/plugins/js/lock-file/__fixtures__/auxiliary-packages/yarn-berry.lock.pruned.ts @@ -1037,6 +1037,7 @@ __metadata: "@nrwl/devkit": 15.0.13 eslint-plugin-disable-autofix: "npm:@mattlewis92/eslint-plugin-disable-autofix@3.0.0" postgres: "https://github.com/charsleysa/postgres.git#commit=3b1a01b2da3e2fafb1a79006f838eff11a8de3cb" + typescript: 4.8.4 yargs: 17.6.2 devDependencies: react: 18.2.0 @@ -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:typescript@4.8.4#~builtin": + version: 4.8.4 + resolution: "typescript@patch:typescript@npm%3A4.8.4#~builtin::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" diff --git a/packages/nx/src/plugins/js/lock-file/__fixtures__/auxiliary-packages/yarn-berry.lock.ts b/packages/nx/src/plugins/js/lock-file/__fixtures__/auxiliary-packages/yarn-berry.lock.ts index da7e54abcc662..323b68477369c 100644 --- a/packages/nx/src/plugins/js/lock-file/__fixtures__/auxiliary-packages/yarn-berry.lock.ts +++ b/packages/nx/src/plugins/js/lock-file/__fixtures__/auxiliary-packages/yarn-berry.lock.ts @@ -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:typescript@4.8.4#~builtin": + version: 4.8.4 + resolution: "typescript@patch:typescript@npm%3A4.8.4#~builtin::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" diff --git a/packages/nx/src/plugins/js/lock-file/yarn-parser.spec.ts b/packages/nx/src/plugins/js/lock-file/yarn-parser.spec.ts index 568cf901f039c..649e49641e162 100644 --- a/packages/nx/src/plugins/js/lock-file/yarn-parser.spec.ts +++ b/packages/nx/src/plugins/js/lock-file/yarn-parser.spec.ts @@ -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 { @@ -433,6 +433,7 @@ describe('yarn LockFile utility', () => { 'npm:@mattlewis92/eslint-plugin-disable-autofix@3.0.0', postgres: 'https://github.com/charsleysa/postgres.git#commit=3b1a01b2da3e2fafb1a79006f838eff11a8de3cb', + typescript: '4.8.4', yargs: '17.6.2', }, devDependencies: { @@ -930,7 +931,7 @@ __metadata: const prunedGraph = pruneProjectGraph(graph, prunedPackageJson); expect(stringifyYarnLockfile(prunedGraph, lockFile, prunedPackageJson)) .toMatchInlineSnapshot(` - "# This file is generated by running "yarn install" inside your project. + "# This file is generated by running \\"yarn install\\" inside your project. # Manual changes might be lost - proceed with caution! __metadata: diff --git a/packages/nx/src/plugins/js/lock-file/yarn-parser.ts b/packages/nx/src/plugins/js/lock-file/yarn-parser.ts index 49bb00a58407a..1d7587574ed51 100644 --- a/packages/nx/src/plugins/js/lock-file/yarn-parser.ts +++ b/packages/nx/src/plugins/js/lock-file/yarn-parser.ts @@ -58,8 +58,8 @@ function addNodes( const nodes: Map> = 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)); @@ -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); } } @@ -414,6 +429,22 @@ function findOriginalKeys( } } +function findPatchedKeys( + dependencies: Record, + 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(