From bb5d74eba8d6ff4cee82fe0f05e42ff4a26f474c 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 --- e2e/js/src/js-tsc.test.ts | 5 +++ .../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 +- 5 files changed, 71 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/e2e/js/src/js-tsc.test.ts b/e2e/js/src/js-tsc.test.ts index 71ad2cebdb87f0..4d3187537c579f 100644 --- a/e2e/js/src/js-tsc.test.ts +++ b/e2e/js/src/js-tsc.test.ts @@ -132,6 +132,11 @@ describe('js e2e', () => { runCLI(`build ${lib}`); const rootPackageJson = readJson(`package.json`); + // TODO: debug in CI + console.log( + '>>>>> package.json', + readJson(`dist/libs/${lib}/package.json`) + ); expect( satisfies( 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..c67e95ca4c3aac 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 = projectGraph[helperDependency]; + + // If libNode is not found due to the version suffix, try to match it by package name. + if (!libNode) { + for (const nodeName of Object.keys(projectGraph.externalNodes)) { + const node = projectGraph.externalNodes[nodeName]; + if (`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"]