Skip to content

Commit

Permalink
fix(js): detect helpers correctly when pnpm external nodes are suffix…
Browse files Browse the repository at this point in the history
…ed 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
  • Loading branch information
jaysoo committed Jun 20, 2023
1 parent 30c57fd commit 7e72307
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 4 deletions.
44 changes: 44 additions & 0 deletions packages/js/src/utils/compiler-helper-dependency.spec.ts
Original file line number Diff line number Diff line change
@@ -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: {
'[email protected]': {
name: 'npm:[email protected]' 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:[email protected]',
type: 'npm',
data: { packageName: 'tslib', version: '2.0.0' },
},
});
});
});
16 changes: 13 additions & 3 deletions packages/js/src/utils/compiler-helper-dependency.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import {
logger,
ProjectGraph,
ProjectGraphDependency,
type ProjectGraph,
type ProjectGraphDependency,
type ProjectGraphExternalNode,
readJsonFile,
} from '@nx/devkit';
import { DependentBuildableProjectNode } from './buildable-libs-utils';
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -71,7 +73,15 @@ export function getHelperDependency(

if (!isHelperNeeded) return null;

const libNode = projectGraph.externalNodes[helperDependency];
let libNode: ProjectGraphExternalNode | null = null;

for (const key of Object.keys(projectGraph.externalNodes)) {
const node = projectGraph.externalNodes[key];
if (`npm:${node.data.packageName}` === helperDependency) {
libNode = node;
break;
}
}

if (!libNode) {
logger.warn(
Expand Down
5 changes: 5 additions & 0 deletions packages/js/src/utils/test-fixtures/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"compilerOptions": {
"importHelpers": true
}
}
2 changes: 1 addition & 1 deletion packages/js/tsconfig.lib.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"exclude": [
"**/*.spec.ts",
"**/*.test.ts",
"./src/utils/typescript/test-fixtures/**/*.ts",
"./src/**/test-fixtures/**/*",
"jest.config.ts"
],
"include": ["**/*.ts"]
Expand Down

0 comments on commit 7e72307

Please sign in to comment.