-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Include actual generated module specifiers in module specifier cache #44176
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
Changes from 1 commit
0e722f0
8063299
8f2c5d1
b3bb946
1179003
5582f48
fdd76be
7ee4dc7
1eaf854
24c07ac
90a6c19
c7b6f1f
d4f2633
4604dde
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -106,9 +106,19 @@ namespace ts.moduleSpecifiers { | |
if (!moduleSourceFile) { | ||
return []; | ||
} | ||
const modulePaths = getAllModulePaths(importingSourceFile.path, moduleSourceFile.originalFileName, host); | ||
const preferences = getPreferences(userPreferences, compilerOptions, importingSourceFile); | ||
|
||
const cache = host.getModuleSpecifierCache?.(); | ||
const cached = cache?.get(importingSourceFile.path, moduleSourceFile.path); | ||
let modulePaths; | ||
if (typeof cached === "object") { | ||
if (cached.moduleSpecifiers) return cached.moduleSpecifiers; | ||
modulePaths = cached.modulePaths; | ||
} | ||
else { | ||
modulePaths = getAllModulePathsWorker(importingSourceFile.path, moduleSourceFile.originalFileName, host); | ||
} | ||
|
||
const preferences = getPreferences(userPreferences, compilerOptions, importingSourceFile); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should user preferences be part of the key for the cache as it does affect the module specifiers (esp what if compileOnSave with declarations enabled which uses module specifiers(? but not sure if same path applies) and auto import are inter mixed?) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Only There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Latest commit makes it part of the cache key instead of clearing when the host config changes. It has to be threaded through a lot more places, but it’s definitely more obvious that it’s a dependency, and will play nicer with other hosts/callers. |
||
const existingSpecifier = forEach(modulePaths, modulePath => forEach( | ||
host.getFileIncludeReasons().get(toPath(modulePath.path, host.getCurrentDirectory(), info.getCanonicalFileName)), | ||
reason => { | ||
|
@@ -120,7 +130,11 @@ namespace ts.moduleSpecifiers { | |
undefined; | ||
} | ||
)); | ||
if (existingSpecifier) return [existingSpecifier]; | ||
if (existingSpecifier) { | ||
const moduleSpecifiers = [existingSpecifier]; | ||
cache?.set(importingSourceFile.path, moduleSourceFile.path, { modulePaths, moduleSpecifiers }); | ||
return moduleSpecifiers; | ||
} | ||
|
||
const importedFileIsInNodeModules = some(modulePaths, p => p.isInNodeModules); | ||
|
||
|
@@ -138,6 +152,7 @@ namespace ts.moduleSpecifiers { | |
if (specifier && modulePath.isRedirect) { | ||
// If we got a specifier for a redirect, it was a bare package specifier (e.g. "@foo/bar", | ||
// not "@foo/bar/path/to/file"). No other specifier will be this good, so stop looking. | ||
cache?.set(importingSourceFile.path, moduleSourceFile.path, { modulePaths, moduleSpecifiers: nodeModulesSpecifiers! }); | ||
return nodeModulesSpecifiers!; | ||
} | ||
|
||
|
@@ -161,9 +176,11 @@ namespace ts.moduleSpecifiers { | |
} | ||
} | ||
|
||
return pathsSpecifiers?.length ? pathsSpecifiers : | ||
const moduleSpecifiers = pathsSpecifiers?.length ? pathsSpecifiers : | ||
nodeModulesSpecifiers?.length ? nodeModulesSpecifiers : | ||
Debug.checkDefined(relativeSpecifiers); | ||
cache?.set(importingSourceFile.path, moduleSourceFile.path, { modulePaths, moduleSpecifiers }); | ||
return moduleSpecifiers; | ||
} | ||
|
||
interface Info { | ||
|
@@ -332,13 +349,26 @@ namespace ts.moduleSpecifiers { | |
* Looks for existing imports that use symlinks to this module. | ||
* Symlinks will be returned first so they are preferred over the real path. | ||
*/ | ||
function getAllModulePaths(importingFileName: Path, importedFileName: string, host: ModuleSpecifierResolutionHost): readonly ModulePath[] { | ||
function getAllModulePaths( | ||
importingFilePath: Path, | ||
importedFileName: string, | ||
host: ModuleSpecifierResolutionHost, | ||
importedFilePath = toPath(importedFileName, host.getCurrentDirectory(), hostGetCanonicalFileName(host)) | ||
) { | ||
const cache = host.getModuleSpecifierCache?.(); | ||
const getCanonicalFileName = hostGetCanonicalFileName(host); | ||
if (cache) { | ||
const cached = cache.get(importingFileName, toPath(importedFileName, host.getCurrentDirectory(), getCanonicalFileName)); | ||
if (typeof cached === "object") return cached; | ||
const cached = cache.get(importingFilePath, importedFilePath); | ||
if (typeof cached === "object") return cached.modulePaths; | ||
} | ||
const modulePaths = getAllModulePathsWorker(importingFilePath, importedFileName, host); | ||
if (cache) { | ||
cache.setModulePaths(importingFilePath, importedFilePath, modulePaths); | ||
} | ||
return modulePaths; | ||
} | ||
|
||
function getAllModulePathsWorker(importingFileName: Path, importedFileName: string, host: ModuleSpecifierResolutionHost): readonly ModulePath[] { | ||
const getCanonicalFileName = hostGetCanonicalFileName(host); | ||
const allFileNames = new Map<string, { path: string, isRedirect: boolean, isInNodeModules: boolean }>(); | ||
let importedFileFromNodeModules = false; | ||
forEachFileNameOfModule( | ||
|
@@ -384,9 +414,6 @@ namespace ts.moduleSpecifiers { | |
sortedPaths.push(...remainingPaths); | ||
} | ||
|
||
if (cache) { | ||
cache.set(importingFileName, toPath(importedFileName, host.getCurrentDirectory(), getCanonicalFileName), sortedPaths); | ||
} | ||
return sortedPaths; | ||
} | ||
|
||
|
Uh oh!
There was an error while loading. Please reload this page.