Skip to content

Commit

Permalink
fix(core): project locator should use nodes when tsconfig is missing (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
meeroslav committed Jan 17, 2023
1 parent ada727c commit ebb2007
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
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);
return project.name;
}
}
}

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

1 comment on commit ebb2007

@vercel
Copy link

@vercel vercel bot commented on ebb2007 Jan 17, 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-five.vercel.app
nx-dev-nrwl.vercel.app
nx.dev

Please sign in to comment.