From 82dc70373ffe99e5fa9becf5a808fcd0579e59c1 Mon Sep 17 00:00:00 2001 From: Jack Hsu Date: Tue, 26 Mar 2024 18:05:29 -0400 Subject: [PATCH] fix(js): handle case where tslib or @swc/helpers are missing from externalNodes (#22523) --- .../src/utils/find-npm-dependencies.spec.ts | 67 +++++++++++++++++++ .../js/src/utils/find-npm-dependencies.ts | 14 ++-- 2 files changed, 77 insertions(+), 4 deletions(-) diff --git a/packages/js/src/utils/find-npm-dependencies.spec.ts b/packages/js/src/utils/find-npm-dependencies.spec.ts index 3746a6b109421..a6fe87a3b27d9 100644 --- a/packages/js/src/utils/find-npm-dependencies.spec.ts +++ b/packages/js/src/utils/find-npm-dependencies.spec.ts @@ -169,6 +169,73 @@ describe('findNpmDependencies', () => { }); }); + it('should handle missing ts/swc helper packages from externalNodes', () => { + vol.fromJSON( + { + './nx.json': JSON.stringify(nxJson), + './libs/my-lib/tsconfig.json': JSON.stringify({ + compilerOptions: { + importHelpers: true, + }, + }), + './libs/my-lib/.swcrc': JSON.stringify({ + jsc: { + externalHelpers: true, + }, + }), + }, + '/root' + ); + const libWithHelpers = { + name: 'my-lib', + type: 'lib' as const, + data: { + root: 'libs/my-lib', + targets: { + build1: { + executor: '@nx/js:tsc', + options: { + tsConfig: 'libs/my-lib/tsconfig.json', + }, + }, + build2: { + executor: '@nx/js:swc', + options: {}, + }, + }, + }, + }; + const projectGraph = { + nodes: { + 'my-lib': libWithHelpers, + }, + externalNodes: {}, + dependencies: {}, + }; + const projectFileMap = { + 'my-lib': [], + }; + + expect( + findNpmDependencies( + '/root', + libWithHelpers, + projectGraph, + projectFileMap, + 'build1' + ) + ).toEqual({}); + expect( + findNpmDependencies( + '/root', + libWithHelpers, + projectGraph, + projectFileMap, + 'build2' + ) + ).toEqual({}); + }); + it('should not pick up helper npm dependencies if not required', () => { vol.fromJSON( { diff --git a/packages/js/src/utils/find-npm-dependencies.ts b/packages/js/src/utils/find-npm-dependencies.ts index e6b855d5b84ec..33c7d3767d5a0 100644 --- a/packages/js/src/utils/find-npm-dependencies.ts +++ b/packages/js/src/utils/find-npm-dependencies.ts @@ -201,8 +201,11 @@ function collectHelperDependencies( if (target.executor === '@nx/js:tsc' && target.options?.tsConfig) { const tsConfig = readTsConfig(join(workspaceRoot, target.options.tsConfig)); - if (tsConfig?.options['importHelpers']) { - npmDeps['tslib'] = projectGraph.externalNodes['npm:tslib']?.data.version; + if ( + tsConfig?.options['importHelpers'] && + projectGraph.externalNodes['npm:tslib']?.type === 'npm' + ) { + npmDeps['tslib'] = projectGraph.externalNodes['npm:tslib'].data.version; } } if (target.executor === '@nx/js:swc') { @@ -212,9 +215,12 @@ function collectHelperDependencies( const swcConfig = fileExists(swcConfigPath) ? readJsonFile(swcConfigPath) : {}; - if (swcConfig?.jsc?.externalHelpers) { + if ( + swcConfig?.jsc?.externalHelpers && + projectGraph.externalNodes['npm:@swc/helpers']?.type === 'npm' + ) { npmDeps['@swc/helpers'] = - projectGraph.externalNodes['npm:@swc/helpers']?.data.version; + projectGraph.externalNodes['npm:@swc/helpers'].data.version; } } }