Always respect preserveSymlinks (#51842)

This commit is contained in:
Sheetal Nandi
2023-02-02 12:37:35 -08:00
committed by GitHub
parent e7dfe82cc5
commit ab5067bd43
11 changed files with 69 additions and 32 deletions

View File

@@ -206,6 +206,38 @@ function resolvedTypeScriptOnly(resolved: Resolved | undefined): PathAndPackageI
return { fileName: resolved.path, packageId: resolved.packageId };
}
function createResolvedModuleWithFailedLookupLocationsHandlingSymlink(
moduleName: string,
resolved: Resolved | undefined,
isExternalLibraryImport: boolean | undefined,
failedLookupLocations: string[],
affectingLocations: string[],
diagnostics: Diagnostic[],
state: ModuleResolutionState,
legacyResult?: string,
): ResolvedModuleWithFailedLookupLocations {
// If this is from node_modules for non relative name, always respect preserveSymlinks
if (!state.resultFromCache &&
!state.compilerOptions.preserveSymlinks &&
resolved &&
isExternalLibraryImport &&
!resolved.originalPath &&
!isExternalModuleNameRelative(moduleName)
) {
const { resolvedFileName, originalPath } = getOriginalAndResolvedFileName(resolved.path, state.host, state.traceEnabled);
if (originalPath) resolved = { ...resolved, path: resolvedFileName, originalPath };
}
return createResolvedModuleWithFailedLookupLocations(
resolved,
isExternalLibraryImport,
failedLookupLocations,
affectingLocations,
diagnostics,
state.resultFromCache,
legacyResult,
);
}
function createResolvedModuleWithFailedLookupLocations(
resolved: Resolved | undefined,
isExternalLibraryImport: boolean | undefined,
@@ -450,6 +482,16 @@ function arePathsEqual(path1: string, path2: string, host: ModuleResolutionHost)
return comparePaths(path1, path2, !useCaseSensitiveFileNames) === Comparison.EqualTo;
}
function getOriginalAndResolvedFileName(fileName: string, host: ModuleResolutionHost, traceEnabled: boolean) {
const resolvedFileName = realPath(fileName, host, traceEnabled);
const pathsAreEqual = arePathsEqual(fileName, resolvedFileName, host);
return {
// If the fileName and realpath are differing only in casing prefer fileName so that we can issue correct errors for casing under forceConsistentCasingInFileNames
resolvedFileName: pathsAreEqual ? fileName : resolvedFileName,
originalPath: pathsAreEqual ? undefined : fileName,
};
}
/**
* @param {string | undefined} containingFile - file that contains type reference directive, can be undefined if containing file is unknown.
* This is possible in case if resolution is performed for directives specified via 'types' parameter. In this case initial path for secondary lookups
@@ -541,13 +583,12 @@ export function resolveTypeReferenceDirective(typeReferenceDirectiveName: string
let resolvedTypeReferenceDirective: ResolvedTypeReferenceDirective | undefined;
if (resolved) {
const { fileName, packageId } = resolved;
const resolvedFileName = options.preserveSymlinks ? fileName : realPath(fileName, host, traceEnabled);
const pathsAreEqual = arePathsEqual(fileName, resolvedFileName, host);
let resolvedFileName = fileName, originalPath: string | undefined;
if (!options.preserveSymlinks) ({ resolvedFileName, originalPath } = getOriginalAndResolvedFileName(fileName, host, traceEnabled));
resolvedTypeReferenceDirective = {
primary,
// If the fileName and realpath are differing only in casing prefer fileName so that we can issue correct errors for casing under forceConsistentCasingInFileNames
resolvedFileName: pathsAreEqual ? fileName : resolvedFileName,
originalPath: pathsAreEqual ? undefined : fileName,
resolvedFileName,
originalPath,
packageId,
isExternalLibraryImport: pathContainsNodeModules(fileName),
};
@@ -1702,13 +1743,14 @@ function nodeModuleNameResolverWorker(features: NodeResolutionFeatures, moduleNa
}
}
return createResolvedModuleWithFailedLookupLocations(
return createResolvedModuleWithFailedLookupLocationsHandlingSymlink(
moduleName,
result?.value?.resolved,
result?.value?.isExternalLibraryImport,
failedLookupLocations,
affectingLocations,
diagnostics,
state.resultFromCache,
state,
legacyResult,
);
@@ -1733,18 +1775,8 @@ function nodeModuleNameResolverWorker(features: NodeResolutionFeatures, moduleNa
}
resolved = loadModuleFromNearestNodeModulesDirectory(extensions, moduleName, containingDirectory, state, cache, redirectedReference);
}
if (!resolved) return undefined;
let resolvedValue = resolved.value;
if (!compilerOptions.preserveSymlinks && resolvedValue && !resolvedValue.originalPath) {
const path = realPath(resolvedValue.path, host, traceEnabled);
const pathsAreEqual = arePathsEqual(path, resolvedValue.path, host);
const originalPath = pathsAreEqual ? undefined : resolvedValue.path;
// If the path and realpath are differing only in casing prefer path so that we can issue correct errors for casing under forceConsistentCasingInFileNames
resolvedValue = { ...resolvedValue, path: pathsAreEqual ? resolvedValue.path : path, originalPath };
}
// 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, isExternalLibraryImport: true } };
return resolved && { value: resolved.value && { resolved: resolved.value, isExternalLibraryImport: true } };
}
else {
const { path: candidate, parts } = normalizePathForCJSResolution(containingDirectory, moduleName);
@@ -2991,13 +3023,14 @@ export function classicNameResolver(moduleName: string, containingFile: string,
tryResolve(Extensions.TypeScript | Extensions.Declaration) ||
tryResolve(Extensions.JavaScript | (compilerOptions.resolveJsonModule ? Extensions.Json : 0));
// No originalPath because classic resolution doesn't resolve realPath
return createResolvedModuleWithFailedLookupLocations(
return createResolvedModuleWithFailedLookupLocationsHandlingSymlink(
moduleName,
resolved && resolved.value,
resolved?.value && pathContainsNodeModules(resolved.value.path),
failedLookupLocations,
affectingLocations,
diagnostics,
state.resultFromCache
state,
);
function tryResolve(extensions: Extensions): SearchResult<Resolved> {