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): project locator should use nodes when tsconfig is missing #14417

Merged
merged 1 commit into from
Jan 17, 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
25 changes: 23 additions & 2 deletions packages/nx/src/utils/target-project-locator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ jest.mock('nx/src/utils/workspace-root', () => ({
jest.mock('fs', () => require('memfs').fs);

describe('findTargetProjectWithImport', () => {
let ctx: ProjectGraphProcessorContext;
let projects: Record<string, ProjectGraphProjectNode>;
let npmProjects: Record<string, ProjectGraphExternalNode>;
let fsJson;
Expand Down Expand Up @@ -69,7 +68,7 @@ describe('findTargetProjectWithImport', () => {
};
vol.fromJSON(fsJson, '/root');

ctx = {
const ctx = {
workspace: {
...workspaceJson,
...nxJson,
Expand Down Expand Up @@ -594,6 +593,14 @@ describe('findTargetProjectWithImport (without tsconfig.json)', () => {
} as any;

projects = {
'@org/proj1': {
name: '@org/proj1',
type: 'lib',
data: {
root: 'libs/proj1',
files: [],
},
},
proj3a: {
name: 'proj3a',
type: 'lib',
Expand Down Expand Up @@ -773,6 +780,20 @@ describe('findTargetProjectWithImport (without tsconfig.json)', () => {
expect(res4).toEqual('proj');
});

it('should be able to resolve local project', () => {
const result1 = targetProjectLocator.findProjectWithImport(
'@org/proj1',
'libs/proj1/index.ts'
);
const result2 = targetProjectLocator.findProjectWithImport(
'@org/proj1/some/nested/path',
'libs/proj1/index.ts'
);

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

it('should be able to npm dependencies', () => {
const result1 = targetProjectLocator.findProjectWithImport(
'@ng/core',
Expand Down
24 changes: 24 additions & 0 deletions packages/nx/src/utils/target-project-locator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ 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 All @@ -40,6 +41,7 @@ export class TargetProjectLocator {
return this.findProjectOfResolvedModule(resolvedModule);
}

// find project using tsconfig paths
const paths = this.findPaths(normalizedImportExpr);
if (paths) {
for (let p of paths) {
Expand All @@ -50,6 +52,12 @@ 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 Down Expand Up @@ -127,6 +135,22 @@ 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.npmResolutionCache.set(importExpr, project.name);
Copy link
Member

Choose a reason for hiding this comment

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

Is this meant to be the project resolution cache? The memoisation here seems to read from one cache but set a different one.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good catch, I will make a quick fix

return project.name;
}
}
}

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