From a8e875f8be8c134d0065a5518b2f3336c20f28a8 Mon Sep 17 00:00:00 2001 From: Jack Hsu Date: Tue, 20 Jun 2023 14:57:48 -0400 Subject: [PATCH] fix(js): detect helpers correctly when pnpm external nodes are suffixed with version When using pnpm, the version might be added to avoid ambiguity, but it causes the wrong warning when detecting missing helper packages. Closes #17674 --- .../utils/compiler-helper-dependency.spec.ts | 44 +++++++++++++++++++ .../src/utils/compiler-helper-dependency.ts | 19 ++++++-- .../js/src/utils/test-fixtures/tsconfig.json | 5 +++ packages/js/tsconfig.lib.json | 2 +- 4 files changed, 66 insertions(+), 4 deletions(-) create mode 100644 packages/js/src/utils/compiler-helper-dependency.spec.ts create mode 100644 packages/js/src/utils/test-fixtures/tsconfig.json diff --git a/packages/js/src/utils/compiler-helper-dependency.spec.ts b/packages/js/src/utils/compiler-helper-dependency.spec.ts new file mode 100644 index 00000000000000..ae9cbe73166aa2 --- /dev/null +++ b/packages/js/src/utils/compiler-helper-dependency.spec.ts @@ -0,0 +1,44 @@ +import { + getHelperDependency, + HelperDependency, +} from './compiler-helper-dependency'; +import { join } from 'path'; + +describe('getHelperDependency', () => { + it('should support pnpm external nodes where the name is suffixed with the version', () => { + const helperDependency = HelperDependency.tsc; + const configPath = join(__dirname, 'test-fixtures', 'tsconfig.json'); + const dependencies = []; + const projectGraph = { + nodes: {}, + externalNodes: { + 'tslib@2.0.0': { + name: 'npm:tslib@2.0.0' as const, + type: 'npm' as const, + data: { + packageName: 'tslib', + version: '2.0.0', + }, + }, + }, + dependencies: {}, + }; + + const result = getHelperDependency( + helperDependency, + configPath, + dependencies, + projectGraph + ); + + expect(result).toEqual({ + name: 'npm:tslib', + outputs: [], + node: { + name: 'npm:tslib@2.0.0', + type: 'npm', + data: { packageName: 'tslib', version: '2.0.0' }, + }, + }); + }); +}); diff --git a/packages/js/src/utils/compiler-helper-dependency.ts b/packages/js/src/utils/compiler-helper-dependency.ts index 6c467fd2ddf7b5..3b9f6916d00410 100644 --- a/packages/js/src/utils/compiler-helper-dependency.ts +++ b/packages/js/src/utils/compiler-helper-dependency.ts @@ -1,7 +1,8 @@ import { logger, - ProjectGraph, - ProjectGraphDependency, + type ProjectGraph, + type ProjectGraphDependency, + type ProjectGraphExternalNode, readJsonFile, } from '@nx/devkit'; import { DependentBuildableProjectNode } from './buildable-libs-utils'; @@ -38,6 +39,7 @@ const jsExecutors = { * @param {HelperDependency} helperDependency * @param {string} configPath * @param {DependentBuildableProjectNode[]} dependencies + * @param {ProjectGraph} projectGraph * @param {boolean=false} returnDependencyIfFound */ export function getHelperDependency( @@ -71,7 +73,18 @@ export function getHelperDependency( if (!isHelperNeeded) return null; - const libNode = projectGraph.externalNodes[helperDependency]; + let libNode: ProjectGraphExternalNode | null = null; + + for (const nodeName of Object.keys(projectGraph.externalNodes)) { + const node = projectGraph.externalNodes[nodeName]; + if ( + nodeName === helperDependency || + `npm:${node.data.packageName}` === helperDependency + ) { + libNode = node; + break; + } + } if (!libNode) { logger.warn( diff --git a/packages/js/src/utils/test-fixtures/tsconfig.json b/packages/js/src/utils/test-fixtures/tsconfig.json new file mode 100644 index 00000000000000..d985b55bef1c3a --- /dev/null +++ b/packages/js/src/utils/test-fixtures/tsconfig.json @@ -0,0 +1,5 @@ +{ + "compilerOptions": { + "importHelpers": true + } +} diff --git a/packages/js/tsconfig.lib.json b/packages/js/tsconfig.lib.json index 82bab87ede4f73..8456bdf1ef7a3c 100644 --- a/packages/js/tsconfig.lib.json +++ b/packages/js/tsconfig.lib.json @@ -9,7 +9,7 @@ "exclude": [ "**/*.spec.ts", "**/*.test.ts", - "./src/utils/typescript/test-fixtures/**/*.ts", + "./src/**/test-fixtures/**/*", "jest.config.ts" ], "include": ["**/*.ts"]