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 efa1001
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 10 deletions.
9 changes: 3 additions & 6 deletions e2e/js/src/js-tsc.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
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 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(
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 efa1001

Please sign in to comment.