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
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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> {

View File

@ -11,6 +11,7 @@
"File '/node_modules/foo/lib/test.tsx' does not exist.",
"File '/node_modules/foo/lib/test.d.ts' exists - use it as a name resolution result.",
"File '/node_modules/foo/package.json' does not exist.",
"Resolving real path for '/node_modules/foo/lib/test.d.ts', result '/node_modules/foo/lib/test.d.ts'.",
"======== Module name 'foo/test.js' was successfully resolved to '/node_modules/foo/lib/test.d.ts'. ========",
"======== Resolving module 'foo/test' from '/test.ts'. ========",
"Explicitly specified module resolution kind: 'Node10'.",
@ -23,6 +24,7 @@
"File '/node_modules/foo/lib/test.tsx' does not exist.",
"File '/node_modules/foo/lib/test.d.ts' exists - use it as a name resolution result.",
"File '/node_modules/foo/package.json' does not exist according to earlier cached lookups.",
"Resolving real path for '/node_modules/foo/lib/test.d.ts', result '/node_modules/foo/lib/test.d.ts'.",
"======== Module name 'foo/test' was successfully resolved to '/node_modules/foo/lib/test.d.ts'. ========",
"======== Resolving module './relative.js' from '/test.ts'. ========",
"Explicitly specified module resolution kind: 'Node10'.",

View File

@ -13,6 +13,7 @@
"File '/node_modules/some-library/lib/index.ios.ts' does not exist.",
"File '/node_modules/some-library/lib/index.ios.tsx' does not exist.",
"File '/node_modules/some-library/lib/index.ios.d.ts' exists - use it as a name resolution result.",
"Resolving real path for '/node_modules/some-library/lib/index.ios.d.ts', result '/node_modules/some-library/lib/index.ios.d.ts'.",
"======== Module name 'some-library' was successfully resolved to '/node_modules/some-library/lib/index.ios.d.ts'. ========",
"======== Resolving module 'some-library/index' from '/test.ts'. ========",
"Explicitly specified module resolution kind: 'Node10'.",
@ -25,6 +26,7 @@
"File '/node_modules/some-library/lib/index.ios.tsx' does not exist.",
"File '/node_modules/some-library/lib/index.ios.d.ts' exists - use it as a name resolution result.",
"File '/node_modules/some-library/package.json' does not exist.",
"Resolving real path for '/node_modules/some-library/lib/index.ios.d.ts', result '/node_modules/some-library/lib/index.ios.d.ts'.",
"======== Module name 'some-library/index' was successfully resolved to '/node_modules/some-library/lib/index.ios.d.ts'. ========",
"======== Resolving module 'some-library/index.js' from '/test.ts'. ========",
"Explicitly specified module resolution kind: 'Node10'.",
@ -38,5 +40,6 @@
"File '/node_modules/some-library/lib/index.ios.tsx' does not exist.",
"File '/node_modules/some-library/lib/index.ios.d.ts' exists - use it as a name resolution result.",
"File '/node_modules/some-library/package.json' does not exist according to earlier cached lookups.",
"Resolving real path for '/node_modules/some-library/lib/index.ios.d.ts', result '/node_modules/some-library/lib/index.ios.d.ts'.",
"======== Module name 'some-library/index.js' was successfully resolved to '/node_modules/some-library/lib/index.ios.d.ts'. ========"
]

View File

@ -98,7 +98,6 @@
"Using 'exports' subpath './foo' with target './dist/foo.js'.",
"File name '/node_modules/exports-and-types-versions/dist/foo.js' has a '.js' extension - stripping it.",
"File '/node_modules/exports-and-types-versions/dist/foo.js' exists - use it as a name resolution result.",
"Resolving real path for '/node_modules/exports-and-types-versions/dist/foo.js', result '/node_modules/exports-and-types-versions/dist/foo.js'.",
"Resolution of non-relative name failed; trying with modern Node resolution features disabled to see if npm library needs configuration update.",
"File '/package.json' does not exist according to earlier cached lookups.",
"Loading module 'exports-and-types-versions/foo' from 'node_modules' folder, target file types: TypeScript, Declaration.",
@ -108,7 +107,7 @@
"Module name 'foo', matched pattern 'foo'.",
"Trying substitution './types/foo.d.ts', candidate module location: './types/foo.d.ts'.",
"File '/node_modules/exports-and-types-versions/types/foo.d.ts' exists - use it as a name resolution result.",
"Resolving real path for '/node_modules/exports-and-types-versions/types/foo.d.ts', result '/node_modules/exports-and-types-versions/types/foo.d.ts'.",
"Resolving real path for '/node_modules/exports-and-types-versions/dist/foo.js', result '/node_modules/exports-and-types-versions/dist/foo.js'.",
"======== Module name 'exports-and-types-versions/foo' was successfully resolved to '/node_modules/exports-and-types-versions/dist/foo.js' with Package ID 'exports-and-types-versions/dist/foo.js@1.0.0'. ========",
"======== Resolving module 'exports-and-types-versions/nope' from '/main.mts'. ========",
"Module resolution kind is not specified, using 'Node16'.",

View File

@ -98,7 +98,6 @@
"Using 'exports' subpath './foo' with target './dist/foo.js'.",
"File name '/node_modules/exports-and-types-versions/dist/foo.js' has a '.js' extension - stripping it.",
"File '/node_modules/exports-and-types-versions/dist/foo.js' exists - use it as a name resolution result.",
"Resolving real path for '/node_modules/exports-and-types-versions/dist/foo.js', result '/node_modules/exports-and-types-versions/dist/foo.js'.",
"Resolution of non-relative name failed; trying with modern Node resolution features disabled to see if npm library needs configuration update.",
"File '/package.json' does not exist according to earlier cached lookups.",
"Loading module 'exports-and-types-versions/foo' from 'node_modules' folder, target file types: TypeScript, Declaration.",
@ -108,7 +107,7 @@
"Module name 'foo', matched pattern 'foo'.",
"Trying substitution './types/foo.d.ts', candidate module location: './types/foo.d.ts'.",
"File '/node_modules/exports-and-types-versions/types/foo.d.ts' exists - use it as a name resolution result.",
"Resolving real path for '/node_modules/exports-and-types-versions/types/foo.d.ts', result '/node_modules/exports-and-types-versions/types/foo.d.ts'.",
"Resolving real path for '/node_modules/exports-and-types-versions/dist/foo.js', result '/node_modules/exports-and-types-versions/dist/foo.js'.",
"======== Module name 'exports-and-types-versions/foo' was successfully resolved to '/node_modules/exports-and-types-versions/dist/foo.js' with Package ID 'exports-and-types-versions/dist/foo.js@1.0.0'. ========",
"======== Resolving module 'exports-and-types-versions/nope' from '/main.mts'. ========",
"Module resolution kind is not specified, using 'NodeNext'.",

View File

@ -35,5 +35,6 @@
"File name '/node_modules/foo/bar/foobar.js' has a '.js' extension - stripping it.",
"File '/node_modules/foo/bar/foobar.js' exists - use it as a name resolution result.",
"File '/node_modules/foo/package.json' does not exist according to earlier cached lookups.",
"Resolving real path for '/node_modules/foo/bar/foobar.js', result '/node_modules/foo/bar/foobar.js'.",
"======== Module name 'foo/bar/foobar.js' was successfully resolved to '/node_modules/foo/bar/foobar.js'. ========"
]

View File

@ -31,5 +31,6 @@
"File name '/node_modules/foo/bar/foobar.json' has a '.json' extension - stripping it.",
"File '/node_modules/foo/bar/foobar.json' exists - use it as a name resolution result.",
"File '/node_modules/foo/package.json' does not exist according to earlier cached lookups.",
"Resolving real path for '/node_modules/foo/bar/foobar.json', result '/node_modules/foo/bar/foobar.json'.",
"======== Module name 'foo/bar/foobar.json' was successfully resolved to '/node_modules/foo/bar/foobar.json'. ========"
]

View File

@ -16,7 +16,6 @@
"Using 'exports' subpath '.' with target './index.mjs'.",
"File name '/node_modules/foo/index.mjs' has a '.mjs' extension - stripping it.",
"File '/node_modules/foo/index.mjs' exists - use it as a name resolution result.",
"Resolving real path for '/node_modules/foo/index.mjs', result '/node_modules/foo/index.mjs'.",
"Resolution of non-relative name failed; trying with modern Node resolution features disabled to see if npm library needs configuration update.",
"File '/package.json' does not exist according to earlier cached lookups.",
"Loading module 'foo' from 'node_modules' folder, target file types: TypeScript, Declaration.",
@ -28,7 +27,7 @@
"'package.json' does not have a 'typings' field.",
"'package.json' has 'types' field 'index.d.ts' that references '/node_modules/foo/index.d.ts'.",
"File '/node_modules/foo/index.d.ts' exists - use it as a name resolution result.",
"Resolving real path for '/node_modules/foo/index.d.ts', result '/node_modules/foo/index.d.ts'.",
"Resolving real path for '/node_modules/foo/index.mjs', result '/node_modules/foo/index.mjs'.",
"======== Module name 'foo' was successfully resolved to '/node_modules/foo/index.mjs' with Package ID 'foo/index.mjs@1.0.0'. ========",
"======== Resolving module 'bar' from '/index.mts'. ========",
"Explicitly specified module resolution kind: 'Bundler'.",
@ -48,7 +47,6 @@
"Using 'exports' subpath '.' with target './index.mjs'.",
"File name '/node_modules/bar/index.mjs' has a '.mjs' extension - stripping it.",
"File '/node_modules/bar/index.mjs' exists - use it as a name resolution result.",
"Resolving real path for '/node_modules/bar/index.mjs', result '/node_modules/bar/index.mjs'.",
"Resolution of non-relative name failed; trying with modern Node resolution features disabled to see if npm library needs configuration update.",
"File '/package.json' does not exist according to earlier cached lookups.",
"Loading module 'bar' from 'node_modules' folder, target file types: TypeScript, Declaration.",
@ -80,7 +78,7 @@
"'package.json' does not have a 'typings' field.",
"'package.json' has 'types' field 'index.d.ts' that references '/node_modules/@types/bar/index.d.ts'.",
"File '/node_modules/@types/bar/index.d.ts' exists - use it as a name resolution result.",
"Resolving real path for '/node_modules/@types/bar/index.d.ts', result '/node_modules/@types/bar/index.d.ts'.",
"Resolving real path for '/node_modules/bar/index.mjs', result '/node_modules/bar/index.mjs'.",
"======== Module name 'bar' was successfully resolved to '/node_modules/bar/index.mjs' with Package ID 'bar/index.mjs@1.0.0'. ========",
"======== Resolving type reference directive 'bar', containing file '__inferred type names__.ts', root directory '/node_modules/@types'. ========",
"Resolving with primary search path '/node_modules/@types'.",

View File

@ -18,7 +18,6 @@
"Using 'exports' subpath '.' with target './index.mjs'.",
"File name '/node_modules/foo/index.mjs' has a '.mjs' extension - stripping it.",
"File '/node_modules/foo/index.mjs' exists - use it as a name resolution result.",
"Resolving real path for '/node_modules/foo/index.mjs', result '/node_modules/foo/index.mjs'.",
"Resolution of non-relative name failed; trying with modern Node resolution features disabled to see if npm library needs configuration update.",
"File '/package.json' does not exist according to earlier cached lookups.",
"Loading module 'foo' from 'node_modules' folder, target file types: TypeScript, Declaration.",
@ -27,7 +26,7 @@
"'package.json' does not have a 'typings' field.",
"'package.json' has 'types' field 'index.d.ts' that references '/node_modules/foo/index.d.ts'.",
"File '/node_modules/foo/index.d.ts' exists - use it as a name resolution result.",
"Resolving real path for '/node_modules/foo/index.d.ts', result '/node_modules/foo/index.d.ts'.",
"Resolving real path for '/node_modules/foo/index.mjs', result '/node_modules/foo/index.mjs'.",
"======== Module name 'foo' was successfully resolved to '/node_modules/foo/index.mjs' with Package ID 'foo/index.mjs@1.0.0'. ========",
"======== Resolving module 'bar' from '/index.mts'. ========",
"Explicitly specified module resolution kind: 'Node16'.",
@ -48,7 +47,6 @@
"Using 'exports' subpath '.' with target './index.mjs'.",
"File name '/node_modules/bar/index.mjs' has a '.mjs' extension - stripping it.",
"File '/node_modules/bar/index.mjs' exists - use it as a name resolution result.",
"Resolving real path for '/node_modules/bar/index.mjs', result '/node_modules/bar/index.mjs'.",
"Resolution of non-relative name failed; trying with modern Node resolution features disabled to see if npm library needs configuration update.",
"File '/package.json' does not exist according to earlier cached lookups.",
"Loading module 'bar' from 'node_modules' folder, target file types: TypeScript, Declaration.",
@ -73,7 +71,7 @@
"'package.json' does not have a 'typings' field.",
"'package.json' has 'types' field 'index.d.ts' that references '/node_modules/@types/bar/index.d.ts'.",
"File '/node_modules/@types/bar/index.d.ts' exists - use it as a name resolution result.",
"Resolving real path for '/node_modules/@types/bar/index.d.ts', result '/node_modules/@types/bar/index.d.ts'.",
"Resolving real path for '/node_modules/bar/index.mjs', result '/node_modules/bar/index.mjs'.",
"======== Module name 'bar' was successfully resolved to '/node_modules/bar/index.mjs' with Package ID 'bar/index.mjs@1.0.0'. ========",
"======== Resolving type reference directive 'bar', containing file '__inferred type names__.ts', root directory '/node_modules/@types'. ========",
"Resolving with primary search path '/node_modules/@types'.",

View File

@ -5,5 +5,6 @@
"File '/node_modules/@types/see__saw/package.json' does not exist.",
"File '/node_modules/@types/see__saw.d.ts' does not exist.",
"File '/node_modules/@types/see__saw/index.d.ts' exists - use it as a name resolution result.",
"Resolving real path for '/node_modules/@types/see__saw/index.d.ts', result '/node_modules/@types/see__saw/index.d.ts'.",
"======== Module name '@see/saw' was successfully resolved to '/node_modules/@types/see__saw/index.d.ts'. ========"
]

View File

@ -14,6 +14,7 @@
"File '/x/node_modules/@types/b/package.json' does not exist.",
"File '/x/node_modules/@types/b.d.ts' does not exist.",
"File '/x/node_modules/@types/b/index.d.ts' exists - use it as a name resolution result.",
"Resolving real path for '/x/node_modules/@types/b/index.d.ts', result '/x/node_modules/@types/b/index.d.ts'.",
"======== Module name 'b' was successfully resolved to '/x/node_modules/@types/b/index.d.ts'. ========",
"======== Resolving module 'a' from '/x/node_modules/@types/b/index.d.ts'. ========",
"Module resolution kind is not specified, using 'Classic'.",
@ -38,6 +39,7 @@
"File '/node_modules/@types/a/package.json' does not exist.",
"File '/node_modules/@types/a.d.ts' does not exist.",
"File '/node_modules/@types/a/index.d.ts' exists - use it as a name resolution result.",
"Resolving real path for '/node_modules/@types/a/index.d.ts', result '/node_modules/@types/a/index.d.ts'.",
"======== Module name 'a' was successfully resolved to '/node_modules/@types/a/index.d.ts'. ========",
"======== Resolving type reference directive 'a', containing file '/__inferred type names__.ts', root directory '/node_modules/@types'. ========",
"Resolving with primary search path '/node_modules/@types'.",