Skip to content

Commit a8efe59

Browse files
authored
fix(js): fix typescript resolution for packages with different resolv… (#26533)
…ed package.json <!-- Please make sure you have read the submission guidelines before posting an PR --> <!-- https://github.com/nrwl/nx/blob/master/CONTRIBUTING.md#-submitting-a-pr --> <!-- Please make sure that your commit message follows our format --> <!-- Example: `fix(nx): must begin with lowercase` --> ## Current Behavior <!-- This is the behavior we have today --> When a package such as `@json2csv/plainjs` reroute their `package.json` (maybe unintentionally) to an invalid `package.json`, the dependency location logic fails. ## Expected Behavior <!-- This is the behavior we should expect with the changes in this PR --> When a package such as `@json2csv/plainjs` reroute their `package.json` (maybe unintentionally) to an invalid `package.json`, the dependency location logic traverses up to the right `package.json`. ## Related Issue(s) <!-- Please link the issue being fixed so it gets closed when this is merged. --> Fixes #
1 parent 508fd86 commit a8efe59

File tree

2 files changed

+40
-3
lines changed

2 files changed

+40
-3
lines changed

packages/nx/src/plugins/js/project-graph/build-dependencies/target-project-locator.spec.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ jest.mock('nx/src/plugins/js/utils/resolve-relative-to-dir', () => ({
3434
if (pathOrPackage === 'minimatch') {
3535
return '/root/node_modules/minimatch/dist/cjs/package.json';
3636
}
37+
if (pathOrPackage === '@json2csv/plainjs/package.json') {
38+
return '/root/node_modules/@json2csv/plainjs/dist/cjs/package.json';
39+
}
3740
return join(
3841
'/root',
3942
'node_modules',
@@ -512,6 +515,15 @@ describe('TargetProjectLocator', () => {
512515
'./node_modules/minimatch/dist/cjs/package.json': JSON.stringify({
513516
type: 'commonjs',
514517
}),
518+
'./node_modules/@json2csv/plainjs/package.json': JSON.stringify({
519+
name: '@json2csv/plainjs',
520+
description: 'something json to csv',
521+
version: '7.0.6',
522+
}),
523+
'./node_modules/@json2csv/plainjs/dist/cjs/package.json':
524+
JSON.stringify({
525+
type: 'commonjs',
526+
}),
515527
};
516528
vol.fromJSON(fsJson, '/root');
517529

@@ -704,6 +716,18 @@ describe('TargetProjectLocator', () => {
704716
hash: 'sha512-RHiac9mvaRw0x3AYRgDC1CxAP7HTcNrrECeA8YYJeWnpo+2Q5CegtZjaotWTWxDG3UeGA1coE05iH1mPjT/2mg==',
705717
},
706718
},
719+
/**
720+
* We use @json2csv/plainjs as an example of a package where package.json is rerouted to an improper package.json.
721+
*/
722+
'npm:@json2csv/plainjs': {
723+
type: 'npm',
724+
name: 'npm:@json2csv/plainjs',
725+
data: {
726+
version: '7.0.6',
727+
packageName: '@json2csv/plainjs',
728+
hash: 'sha512-4Md7RPDCSYpmW1HWIpWBOqCd4vWfIqm53S3e/uzQ62iGi7L3r34fK/8nhOMEe+/eVfCx8+gdSCt1d74SlacQHw==',
729+
},
730+
},
707731
};
708732

709733
targetProjectLocator = new TargetProjectLocator(projects, npmProjects);
@@ -828,6 +852,14 @@ describe('TargetProjectLocator', () => {
828852
);
829853
expect(result).toEqual('npm:minimatch');
830854
});
855+
856+
it('should handle resolving packages which reroutes package.json', () => {
857+
const result = targetProjectLocator.findProjectFromImport(
858+
'@json2csv/plainjs',
859+
'libs/proj/index.ts'
860+
);
861+
expect(result).toEqual('npm:@json2csv/plainjs');
862+
});
831863
});
832864
});
833865

packages/nx/src/plugins/js/project-graph/build-dependencies/target-project-locator.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -336,13 +336,18 @@ export class TargetProjectLocator {
336336
relativeToDir
337337
);
338338
if (packageJsonPath) {
339-
return readJsonFile(packageJsonPath);
339+
const parsedPackageJson = readJsonFile(packageJsonPath);
340+
341+
if (parsedPackageJson.name && parsedPackageJson.version) {
342+
return parsedPackageJson;
343+
}
340344
}
341345

342346
try {
343347
// Resolve the main entry point of the package
344-
const mainPath = resolveRelativeToDir(packageName, relativeToDir);
345-
let dir = dirname(mainPath);
348+
const pathOfFileInPackage =
349+
packageJsonPath ?? resolveRelativeToDir(packageName, relativeToDir);
350+
let dir = dirname(pathOfFileInPackage);
346351

347352
while (dir !== parse(dir).root) {
348353
const packageJsonPath = join(dir, 'package.json');

0 commit comments

Comments
 (0)