Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(core): use require.resolve instead of randomly matching from our … #14523

Merged
merged 1 commit into from
Jan 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions packages/nx/src/utils/target-project-locator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -781,16 +781,23 @@ describe('findTargetProjectWithImport (without tsconfig.json)', () => {
});

it('should be able to resolve local project', () => {
jest
.spyOn(targetProjectLocator as any, 'resolveImportWithRequire')
.mockReturnValue('libs/proj1/index.ts');

const result1 = targetProjectLocator.findProjectWithImport(
'@org/proj1',
'libs/proj1/index.ts'
);
expect(result1).toEqual('@org/proj1');

jest
.spyOn(targetProjectLocator as any, 'resolveImportWithRequire')
.mockReturnValue('libs/proj1/some/nested/file.ts');
const result2 = targetProjectLocator.findProjectWithImport(
'@org/proj1/some/nested/path',
'libs/proj1/index.ts'
);

expect(result1).toEqual('@org/proj1');
expect(result2).toEqual('@org/proj1');
});

Expand Down
41 changes: 19 additions & 22 deletions packages/nx/src/utils/target-project-locator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ export class TargetProjectLocator {
private tsConfig = this.getRootTsConfig();
private paths = this.tsConfig.config?.compilerOptions?.paths;
private typescriptResolutionCache = new Map<string, string | null>();
private projectResolutionCache = new Map<string, string | null>();
private npmResolutionCache = new Map<string, string | null>();

constructor(
Expand Down Expand Up @@ -52,12 +51,6 @@ export class TargetProjectLocator {
}
}

// check if it exists in projects
const project = this.findProject(normalizedImportExpr);
if (project) {
return project;
}

// try to find npm package before using expensive typescript resolution
const npmProject = this.findNpmPackage(normalizedImportExpr);
if (npmProject) {
Expand All @@ -77,6 +70,15 @@ export class TargetProjectLocator {
}
}

try {
const resolvedModule = this.resolveImportWithRequire(
normalizedImportExpr,
filePath
);

return this.findProjectOfResolvedModule(resolvedModule);
} catch {}

// nothing found, cache for later
this.npmResolutionCache.set(normalizedImportExpr, undefined);
return null;
Expand Down Expand Up @@ -135,22 +137,17 @@ export class TargetProjectLocator {
return;
}

private findProject(importExpr: string): string | undefined {
if (this.projectResolutionCache.has(importExpr)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think its worth keeping the cache? Or I guess require.resolve may have some caching, I know regular require does.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

require has its own cache:
https://nodejs.org/api/modules.html#modules_caching

But it should also be rare for it to get down there.

return this.projectResolutionCache.get(importExpr);
} else {
const project = Object.values(this.nodes).find(
(project) =>
importExpr === project.name ||
importExpr.startsWith(`${project.name}/`)
);
if (project) {
this.projectResolutionCache.set(importExpr, project.name);
return project.name;
}
}
private resolveImportWithRequire(
normalizedImportExpr: string,
filePath: string
) {
return posix.relative(
workspaceRoot,
require.resolve(normalizedImportExpr, {
paths: [dirname(filePath)],
})
);
}

private findNpmPackage(npmImport: string): string | undefined {
if (this.npmResolutionCache.has(npmImport)) {
return this.npmResolutionCache.get(npmImport);
Expand Down