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 nrwl#17674
  • Loading branch information
jaysoo committed Jun 21, 2023
1 parent 30c57fd commit bb5d74e
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 4 deletions.
5 changes: 5 additions & 0 deletions e2e/js/src/js-tsc.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
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' },
},
});
});
});
19 changes: 16 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,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(
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 bb5d74e

Please sign in to comment.