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 c11f526c7e..f05a00fec7 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 da7e54abcc..323b684773 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 0b6c603518..649e49641e 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 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 7ef4c1369a..1d7587574e 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,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, + 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