Skip to content

Commit

Permalink
fix(core): fix project locator for built in node modules (#14846)
Browse files Browse the repository at this point in the history
  • Loading branch information
FrozenPandaz committed Feb 7, 2023
1 parent 1802e67 commit e7f5855
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
16 changes: 16 additions & 0 deletions packages/nx/src/utils/target-project-locator.spec.ts
Expand Up @@ -856,4 +856,20 @@ describe('findTargetProjectWithImport (without tsconfig.json)', () => {
);
expect(similarDeepImportFromNpm).toEqual('npm:@proj/proj123-base');
});

it('should return null for native modules', () => {
const result = targetProjectLocator.findProjectWithImport(
'path',
'libs/proj/index.ts'
);
expect(result).toEqual(null);
});

it('should return null for unresolved paths', () => {
const result = targetProjectLocator.findProjectWithImport(
'unresolved-path',
'libs/proj/index.ts'
);
expect(result).toEqual(null);
});
});
10 changes: 9 additions & 1 deletion packages/nx/src/utils/target-project-locator.ts
Expand Up @@ -10,6 +10,9 @@ import {
createProjectRootMappings,
findProjectForPath,
} from '../project-graph/utils/find-project-for-path';
import { builtinModules } from 'module';

const builtInModuleSet = new Set(builtinModules);

export class TargetProjectLocator {
private projectRootMappings = createProjectRootMappings(this.nodes);
Expand Down Expand Up @@ -70,6 +73,11 @@ export class TargetProjectLocator {
}
}

if (builtInModuleSet.has(normalizedImportExpr)) {
this.npmResolutionCache.set(normalizedImportExpr, null);
return null;
}

try {
const resolvedModule = this.resolveImportWithRequire(
normalizedImportExpr,
Expand All @@ -80,7 +88,7 @@ export class TargetProjectLocator {
} catch {}

// nothing found, cache for later
this.npmResolutionCache.set(normalizedImportExpr, undefined);
this.npmResolutionCache.set(normalizedImportExpr, null);
return null;
}

Expand Down

1 comment on commit e7f5855

@vercel
Copy link

@vercel vercel bot commented on e7f5855 Feb 7, 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-five.vercel.app
nx-dev-git-master-nrwl.vercel.app
nx-dev-nrwl.vercel.app
nx.dev

Please sign in to comment.