Skip to content

Commit

Permalink
fix(core): use require.resolve instead of randomly matching from our … (
Browse files Browse the repository at this point in the history
  • Loading branch information
FrozenPandaz committed Jan 23, 2023
1 parent 51b1f48 commit 21d65cb
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 24 deletions.
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)) {
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

1 comment on commit 21d65cb

@vercel
Copy link

@vercel vercel bot commented on 21d65cb Jan 23, 2023

Choose a reason for hiding this comment

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

Successfully deployed to the following URLs:

nx-dev – ./

nx-dev-git-master-nrwl.vercel.app
nx-dev-nrwl.vercel.app
nx-five.vercel.app
nx.dev

Please sign in to comment.