From 1962ae55ae79c7e647e44d897052a3475055c819 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leosvel=20P=C3=A9rez=20Espinosa?= Date: Thu, 25 Jul 2024 18:24:56 +0200 Subject: [PATCH] fix(js): locate npm nodes correctly for aliased packages --- .../target-project-locator.spec.ts | 38 +++++++++++++++++++ .../target-project-locator.ts | 9 ++++- 2 files changed, 45 insertions(+), 2 deletions(-) diff --git a/packages/nx/src/plugins/js/project-graph/build-dependencies/target-project-locator.spec.ts b/packages/nx/src/plugins/js/project-graph/build-dependencies/target-project-locator.spec.ts index 16199daf5b8d2..fc15bf32b8490 100644 --- a/packages/nx/src/plugins/js/project-graph/build-dependencies/target-project-locator.spec.ts +++ b/packages/nx/src/plugins/js/project-graph/build-dependencies/target-project-locator.spec.ts @@ -96,6 +96,14 @@ describe('TargetProjectLocator', () => { name: '@proj/proj123-base', version: '1.0.0', }), + './node_modules/lodash/package.json': JSON.stringify({ + name: 'lodash', + version: '3.0.0', + }), + './node_modules/lodash-4/package.json': JSON.stringify({ + name: 'lodash', + version: '4.0.0', + }), }; vol.fromJSON(fsJson, '/root'); projects = { @@ -263,6 +271,22 @@ describe('TargetProjectLocator', () => { packageName: '@proj/proj123-base', }, }, + 'npm:lodash': { + name: 'npm:lodash', + type: 'npm', + data: { + version: '3.0.0', + packageName: 'lodash', + }, + }, + 'npm:lodash-4': { + name: 'npm:lodash-4', + type: 'npm', + data: { + packageName: 'lodash-4', + version: 'npm:lodash@4.0.0', + }, + }, }; targetProjectLocator = new TargetProjectLocator(projects, npmProjects); @@ -454,6 +478,20 @@ describe('TargetProjectLocator', () => { ); expect(proj5).toEqual('proj5'); }); + + it('should be able to resolve packages alises', () => { + const lodash = targetProjectLocator.findProjectFromImport( + 'lodash', + 'libs/proj/index.ts' + ); + expect(lodash).toEqual('npm:lodash'); + + const lodash4 = targetProjectLocator.findProjectFromImport( + 'lodash-4', + 'libs/proj/index.ts' + ); + expect(lodash4).toEqual('npm:lodash-4'); + }); }); describe('findTargetProjectWithImport (without tsconfig.json)', () => { diff --git a/packages/nx/src/plugins/js/project-graph/build-dependencies/target-project-locator.ts b/packages/nx/src/plugins/js/project-graph/build-dependencies/target-project-locator.ts index 5383b685af690..86b21bd19875b 100644 --- a/packages/nx/src/plugins/js/project-graph/build-dependencies/target-project-locator.ts +++ b/packages/nx/src/plugins/js/project-graph/build-dependencies/target-project-locator.ts @@ -188,9 +188,14 @@ export class TargetProjectLocator { const version = clean(externalPackageJson.version); const npmProjectKey = `npm:${externalPackageJson.name}@${version}`; - const matchingExternalNode = this.npmProjects[npmProjectKey]; + let matchingExternalNode = this.npmProjects[npmProjectKey]; if (!matchingExternalNode) { - return null; + // check if it's a package alias, where the resolved package key is used as the version + const aliasNpmProjectKey = `npm:${packageName}@${npmProjectKey}`; + matchingExternalNode = this.npmProjects[aliasNpmProjectKey]; + if (!matchingExternalNode) { + return null; + } } this.npmResolutionCache.set(