Skip to content

Commit c16dacd

Browse files
leosvelperezFrozenPandaz
authored andcommitted
fix(core): use scoped cache key for unresolved npm imports in TargetProjectLocator (#34605)
## Current Behavior `TargetProjectLocator.findProjectFromImport` stores `null` for unresolved imports using a bare `importExpr` key, but `findNpmProjectFromImport` looks up cache entries using `${packageName}__${dirPath}`. The key mismatch means repeated lookups for the same import+directory re-run the full resolution waterfall (typescript + require.resolve) instead of returning the cached `null`. ## Expected Behavior Store `null` for unresolved imports using the same `${packageName}__${dirPath}` key that `findNpmProjectFromImport` uses for lookups. Repeated lookups for already-failed imports skip the expensive resolution steps. Also removes an unused cache write for builtin module imports as a minor cleanup. (cherry picked from commit 098a830)
1 parent f17d00e commit c16dacd

1 file changed

Lines changed: 8 additions & 3 deletions

File tree

packages/nx/src/plugins/js/project-graph/build-dependencies/target-project-locator.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,6 @@ export class TargetProjectLocator {
138138
}
139139

140140
if (isBuiltinModuleImport(importExpr)) {
141-
this.npmResolutionCache.set(importExpr, null);
142141
return null;
143142
}
144143

@@ -176,8 +175,14 @@ export class TargetProjectLocator {
176175
return localProject;
177176
}
178177

179-
// nothing found, cache for later
180-
this.npmResolutionCache.set(importExpr, null);
178+
// nothing found, cache the negative result with the scoped key
179+
const packageName = getPackageNameFromImportPath(importExpr);
180+
const dirPath = dirname(
181+
filePath.startsWith(workspaceRoot)
182+
? filePath.replace(workspaceRoot, '')
183+
: filePath
184+
);
185+
this.npmResolutionCache.set(`${packageName}__${dirPath}`, null);
181186
return null;
182187
}
183188

0 commit comments

Comments
 (0)