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 c932c1f3fe76d..9de08833ad41b 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 @@ -2144,4 +2144,106 @@ __metadata: `); }); }); + + describe('patches', () => { + beforeEach(() => { + const fileSys = { + 'node_modules/resolve/package.json': '{"version": "1.22.3"}', + }; + vol.fromJSON(fileSys, '/root'); + }); + + it('should keep the builtin patch', () => { + const lockFile = `# This file is generated by running "yarn install" inside your project. +# Manual changes might be lost - proceed with caution! + +__metadata: + version: 6 + cacheKey: 8 + +"resolve@npm:^1.12.0, resolve@npm:^1.14.2, resolve@npm:^1.20.0, resolve@npm:^1.22.1": + version: 1.22.3 + resolution: "resolve@npm:1.22.3" + dependencies: + is-core-module: ^2.12.0 + path-parse: ^1.0.7 + supports-preserve-symlinks-flag: ^1.0.0 + bin: + resolve: bin/resolve + checksum: fb834b81348428cb545ff1b828a72ea28feb5a97c026a1cf40aa1008352c72811ff4d4e71f2035273dc536dcfcae20c13604ba6283c612d70fa0b6e44519c374 + languageName: node + linkType: hard + +"resolve@patch:resolve@^1.12.0#~builtin, resolve@patch:resolve@^1.14.2#~builtin, resolve@patch:resolve@^1.20.0#~builtin, resolve@patch:resolve@^1.22.1#~builtin": + version: 1.22.3 + resolution: "resolve@patch:resolve@npm%3A1.22.3#~builtin::version=1.22.3&hash=c3c19d" + dependencies: + is-core-module: ^2.12.0 + path-parse: ^1.0.7 + supports-preserve-symlinks-flag: ^1.0.0 + bin: + resolve: bin/resolve + checksum: ad59734723b596d0891321c951592ed9015a77ce84907f89c9d9307dd0c06e11a67906a3e628c4cae143d3e44898603478af0ddeb2bba3f229a9373efe342665 + languageName: node + linkType: hard + `; + + const packageJson: PackageJson = { + name: '@my-ns/example', + version: '0.0.1', + type: 'commonjs', + dependencies: { + resolve: '^1.12.0', + }, + }; + const builder = new ProjectGraphBuilder(); + parseYarnLockfile(lockFile, packageJson, builder); + const graph = builder.getUpdatedProjectGraph(); + const prunedGraph = pruneProjectGraph(graph, packageJson); + const result = stringifyYarnLockfile(prunedGraph, lockFile, packageJson); + expect(result).toMatchInlineSnapshot(` + "# This file is generated by running "yarn install" inside your project. + # Manual changes might be lost - proceed with caution! + + __metadata: + version: 6 + cacheKey: 8 + + "@my-ns/example@workspace:.": + version: 0.0.0-use.local + resolution: "@my-ns/example@workspace:." + dependencies: + resolve: ^1.12.0 + languageName: unknown + linkType: soft + + "resolve@npm:^1.12.0": + version: 1.22.3 + resolution: "resolve@npm:1.22.3" + dependencies: + is-core-module: ^2.12.0 + path-parse: ^1.0.7 + supports-preserve-symlinks-flag: ^1.0.0 + bin: + resolve: bin/resolve + checksum: fb834b81348428cb545ff1b828a72ea28feb5a97c026a1cf40aa1008352c72811ff4d4e71f2035273dc536dcfcae20c13604ba6283c612d70fa0b6e44519c374 + languageName: node + linkType: hard + + "resolve@patch:resolve@^1.12.0#~builtin": + version: 1.22.3 + resolution: "resolve@patch:resolve@npm%3A1.22.3#~builtin::version=1.22.3&hash=c3c19d" + dependencies: + is-core-module: ^2.12.0 + path-parse: ^1.0.7 + supports-preserve-symlinks-flag: ^1.0.0 + bin: + resolve: bin/resolve + checksum: ad59734723b596d0891321c951592ed9015a77ce84907f89c9d9307dd0c06e11a67906a3e628c4cae143d3e44898603478af0ddeb2bba3f229a9373efe342665 + languageName: node + linkType: hard + " + `); + }); + }); }); 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 4c902ea705a84..7756c5892c60a 100644 --- a/packages/nx/src/plugins/js/lock-file/yarn-parser.ts +++ b/packages/nx/src/plugins/js/lock-file/yarn-parser.ts @@ -421,8 +421,7 @@ function mapSnapshots( if (isBerry && key.includes('@patch:') && key.includes('#')) { normalizedKey = key .slice(0, key.indexOf('#')) - .replace(`@patch:${packageName}@`, '@npm:') - .replace(/:.*$/u, ':' + snapshotKey.version); + .replace(`@patch:${packageName}@`, '@npm:'); } if ( !existingKeys.get(packageName) ||