From 50ce2fe6d350e895d11158ffb34d5dee1674f8af Mon Sep 17 00:00:00 2001 From: Zach Tindall Date: Fri, 20 Dec 2024 21:04:48 +0000 Subject: [PATCH] fix(core): update yarn-parser to handle yarn v4 syntax for root workspace package --- .../js/lock-file/utils/package-json.ts | 3 ++ .../plugins/js/lock-file/yarn-parser.spec.ts | 4 ++- .../src/plugins/js/lock-file/yarn-parser.ts | 32 ++++++++++++++++--- 3 files changed, 34 insertions(+), 5 deletions(-) diff --git a/packages/nx/src/plugins/js/lock-file/utils/package-json.ts b/packages/nx/src/plugins/js/lock-file/utils/package-json.ts index abca909f4a7de..8c272f8289d71 100644 --- a/packages/nx/src/plugins/js/lock-file/utils/package-json.ts +++ b/packages/nx/src/plugins/js/lock-file/utils/package-json.ts @@ -25,6 +25,7 @@ export type NormalizedPackageJson = Pick< | 'peerDependencies' | 'peerDependenciesMeta' | 'optionalDependencies' + | 'packageManager' >; /** @@ -42,6 +43,7 @@ export function normalizePackageJson( peerDependencies, peerDependenciesMeta, optionalDependencies, + packageManager, } = packageJson; return { @@ -53,5 +55,6 @@ export function normalizePackageJson( peerDependencies, peerDependenciesMeta, optionalDependencies, + packageManager, }; } 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 2854648a33a04..7f0da52f40ebe 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 @@ -2596,6 +2596,7 @@ __metadata: dependencies: { resolve: '^1.12.0', }, + packageManager: 'yarn@3.0.0', }; const hash = uniq('mock-hash'); @@ -2721,6 +2722,7 @@ __metadata: dependencies: { resolve: '^1.12.0', }, + packageManager: 'yarn@4.0.0', }; const hash = uniq('mock-hash'); @@ -2771,7 +2773,7 @@ __metadata: version: 0.0.0-use.local resolution: "@my-ns/example@workspace:." dependencies: - resolve: ^1.12.0 + resolve: "npm:^1.12.0" languageName: unknown linkType: soft 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 bdddd4a39b656..12c8480e5e860 100644 --- a/packages/nx/src/plugins/js/lock-file/yarn-parser.ts +++ b/packages/nx/src/plugins/js/lock-file/yarn-parser.ts @@ -602,18 +602,42 @@ const BERRY_LOCK_FILE_DISCLAIMER = `# This file is generated by running "yarn in function generateRootWorkspacePackage( packageJson: NormalizedPackageJson ): YarnDependency { + let isVersion4 = false; + if (!!packageJson.packageManager) { + const [_, version] = packageJson.packageManager.split('@'); + isVersion4 = !!version && satisfies(version, '>=4.0.0'); + } + + const reducer = (acc, [name, version]) => { + acc[name] = isVersion4 ? `npm:${version}` : version; + return acc; + }; + return { version: '0.0.0-use.local', resolution: `${packageJson.name}@workspace:.`, - ...(packageJson.dependencies && { dependencies: packageJson.dependencies }), + ...(packageJson.dependencies && { + dependencies: Object.entries(packageJson.dependencies).reduce( + reducer, + {} + ), + }), ...(packageJson.peerDependencies && { - peerDependencies: packageJson.peerDependencies, + peerDependencies: Object.entries(packageJson.peerDependencies).reduce( + reducer, + {} + ), }), ...(packageJson.devDependencies && { - devDependencies: packageJson.devDependencies, + devDependencies: Object.entries(packageJson.devDependencies).reduce( + reducer, + {} + ), }), ...(packageJson.optionalDependencies && { - optionalDependencies: packageJson.optionalDependencies, + optionalDependencies: Object.entries( + packageJson.optionalDependencies + ).reduce(reducer, {}), }), languageName: 'unknown', linkType: 'soft',