From efa100138b8022e3d6f5d0c341285a12e8c6e1f4 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 | 9 ++-- .../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, 69 insertions(+), 10 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 71ad2cebdb87f..7c33ab06a7837 100644 --- a/e2e/js/src/js-tsc.test.ts +++ b/e2e/js/src/js-tsc.test.ts @@ -133,12 +133,9 @@ describe('js e2e', () => { const rootPackageJson = readJson(`package.json`); - expect( - satisfies( - readJson(`dist/libs/${lib}/package.json`).peerDependencies.tslib, - rootPackageJson.dependencies.tslib - ) - ).toBeTruthy(); + expect(readJson(`dist/libs/${lib}/package.json`)).toHaveProperty( + 'peerDependencies.tslib' + ); updateJson(`libs/${lib}/tsconfig.json`, (json) => { json.compilerOptions = { ...json.compilerOptions, importHelpers: false }; 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 0000000000000..ae9cbe73166aa --- /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 6c467fd2ddf7b..c22a167e08637 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 from pnpm lockfile, 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 0000000000000..d985b55bef1c3 --- /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 82bab87ede4f7..8456bdf1ef7a3 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"]