-
Notifications
You must be signed in to change notification settings - Fork 12.9k
moduleNameResolver: preserve originalPath of symlinked module on cache hit #26310
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 all commits
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 |
---|---|---|
|
@@ -17,7 +17,7 @@ namespace ts { | |
} | ||
|
||
function withPackageId(packageId: PackageId | undefined, r: PathAndExtension | undefined): Resolved | undefined { | ||
return r && { path: r.path, extension: r.ext, packageId }; | ||
return r && { path: r.path, extension: r.ext, packageId, originalPath: undefined }; | ||
} | ||
|
||
function noPackageId(r: PathAndExtension | undefined): Resolved | undefined { | ||
|
@@ -29,6 +29,7 @@ namespace ts { | |
path: string; | ||
extension: Extension; | ||
packageId: PackageId | undefined; | ||
originalPath: string | undefined; | ||
} | ||
|
||
/** Result of trying to resolve a module at a file. Needs to have 'packageId' added later. */ | ||
|
@@ -62,9 +63,9 @@ namespace ts { | |
return { fileName: resolved.path, packageId: resolved.packageId }; | ||
} | ||
|
||
function createResolvedModuleWithFailedLookupLocations(resolved: Resolved | undefined, originalPath: string | undefined, isExternalLibraryImport: boolean, failedLookupLocations: string[]): ResolvedModuleWithFailedLookupLocations { | ||
function createResolvedModuleWithFailedLookupLocations(resolved: Resolved | undefined, isExternalLibraryImport: boolean, failedLookupLocations: string[]): ResolvedModuleWithFailedLookupLocations { | ||
return { | ||
resolvedModule: resolved && { resolvedFileName: resolved.path, originalPath, extension: resolved.extension, isExternalLibraryImport, packageId: resolved.packageId }, | ||
resolvedModule: resolved && { resolvedFileName: resolved.path, originalPath: resolved.originalPath, extension: resolved.extension, isExternalLibraryImport, packageId: resolved.packageId }, | ||
failedLookupLocations | ||
}; | ||
} | ||
|
@@ -753,12 +754,12 @@ namespace ts { | |
tryResolve(Extensions.JavaScript) || | ||
(compilerOptions.resolveJsonModule ? tryResolve(Extensions.Json) : undefined)); | ||
if (result && result.value) { | ||
const { resolved, originalPath, isExternalLibraryImport } = result.value; | ||
return createResolvedModuleWithFailedLookupLocations(resolved, originalPath, isExternalLibraryImport, failedLookupLocations); | ||
const { resolved, isExternalLibraryImport } = result.value; | ||
return createResolvedModuleWithFailedLookupLocations(resolved, isExternalLibraryImport, failedLookupLocations); | ||
} | ||
return { resolvedModule: undefined, failedLookupLocations }; | ||
|
||
function tryResolve(extensions: Extensions): SearchResult<{ resolved: Resolved, originalPath?: string, isExternalLibraryImport: boolean }> { | ||
function tryResolve(extensions: Extensions): SearchResult<{ resolved: Resolved, isExternalLibraryImport: boolean }> { | ||
const loader: ResolutionKindSpecificLoader = (extensions, candidate, failedLookupLocations, onlyRecordFailures, state) => nodeLoadModuleByRelativeName(extensions, candidate, failedLookupLocations, onlyRecordFailures, state, /*considerPackageJson*/ true); | ||
const resolved = tryLoadModuleUsingOptionalResolutionSettings(extensions, moduleName, containingDirectory, loader, failedLookupLocations, state); | ||
if (resolved) { | ||
|
@@ -773,17 +774,14 @@ namespace ts { | |
if (!resolved) return undefined; | ||
|
||
let resolvedValue = resolved.value; | ||
let originalPath: string | undefined; | ||
if (!compilerOptions.preserveSymlinks && resolvedValue) { | ||
originalPath = resolvedValue.path; | ||
if (!compilerOptions.preserveSymlinks && resolvedValue && !resolvedValue.originalPath) { | ||
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. Hmm we need better way to identify that its from cache and no need to calculate originalPath since when its not symbollink and its coming from cache we shouldnt need to do realPath logic here. Considering this involves more changes than previously expected, do you mind if i take it over? 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. I don't mind you taking over. |
||
const path = realPath(resolvedValue.path, host, traceEnabled); | ||
if (path === originalPath) { | ||
originalPath = undefined; | ||
if (path !== resolvedValue.path) { | ||
resolvedValue = { ...resolvedValue, path, originalPath: resolvedValue.path }; | ||
} | ||
resolvedValue = { ...resolvedValue, path }; | ||
} | ||
// For node_modules lookups, get the real path so that multiple accesses to an `npm link`-ed module do not create duplicate files. | ||
return { value: resolvedValue && { resolved: resolvedValue, originalPath, isExternalLibraryImport: true } }; | ||
return { value: resolvedValue && { resolved: resolvedValue, isExternalLibraryImport: true } }; | ||
} | ||
else { | ||
const { path: candidate, parts } = normalizePathAndParts(combinePaths(containingDirectory, moduleName)); | ||
|
@@ -1233,7 +1231,7 @@ namespace ts { | |
trace(host, Diagnostics.Resolution_for_module_0_was_found_in_cache_from_location_1, moduleName, containingDirectory); | ||
} | ||
failedLookupLocations.push(...result.failedLookupLocations); | ||
return { value: result.resolvedModule && { path: result.resolvedModule.resolvedFileName, extension: result.resolvedModule.extension, packageId: result.resolvedModule.packageId } }; | ||
return { value: result.resolvedModule && { path: result.resolvedModule.resolvedFileName, extension: result.resolvedModule.extension, packageId: result.resolvedModule.packageId, originalPath: result.resolvedModule.originalPath } }; | ||
} | ||
} | ||
|
||
|
@@ -1245,7 +1243,7 @@ namespace ts { | |
|
||
const resolved = tryResolve(Extensions.TypeScript) || tryResolve(Extensions.JavaScript); | ||
// No originalPath because classic resolution doesn't resolve realPath | ||
return createResolvedModuleWithFailedLookupLocations(resolved && resolved.value, /*originalPath*/ undefined, /*isExternalLibraryImport*/ false, failedLookupLocations); | ||
return createResolvedModuleWithFailedLookupLocations(resolved && resolved.value, /*isExternalLibraryImport*/ false, failedLookupLocations); | ||
|
||
function tryResolve(extensions: Extensions): SearchResult<Resolved> { | ||
const resolvedUsingSettings = tryLoadModuleUsingOptionalResolutionSettings(extensions, moduleName, containingDirectory, loadModuleFromFileNoPackageId, failedLookupLocations, state); | ||
|
@@ -1292,7 +1290,7 @@ namespace ts { | |
const state: ModuleResolutionState = { compilerOptions, host, traceEnabled }; | ||
const failedLookupLocations: string[] = []; | ||
const resolved = loadModuleFromNodeModulesOneLevel(Extensions.DtsOnly, moduleName, globalCache, failedLookupLocations, state); | ||
return createResolvedModuleWithFailedLookupLocations(resolved, /*originalPath*/ undefined, /*isExternalLibraryImport*/ true, failedLookupLocations); | ||
return createResolvedModuleWithFailedLookupLocations(resolved, /*isExternalLibraryImport*/ true, failedLookupLocations); | ||
} | ||
|
||
/** | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Make this optional so you dont need to set it in withPackageId