@@ -201,6 +201,37 @@ function resolvedTypeScriptOnly(resolved: Resolved | undefined): PathAndPackageI
201
201
return { fileName : resolved . path , packageId : resolved . packageId } ;
202
202
}
203
203
204
+ function createResolvedModuleWithFailedLookupLocationsHandlingSymlink (
205
+ moduleName : string ,
206
+ resolved : Resolved | undefined ,
207
+ isExternalLibraryImport : boolean | undefined ,
208
+ failedLookupLocations : string [ ] ,
209
+ affectingLocations : string [ ] ,
210
+ diagnostics : Diagnostic [ ] ,
211
+ state : ModuleResolutionState ,
212
+ ) : ResolvedModuleWithFailedLookupLocations {
213
+ // If this is from node_modules for non relative name, always respect preserveSymlinks
214
+ if ( ! state . resultFromCache &&
215
+ ! state . compilerOptions . preserveSymlinks &&
216
+ resolved &&
217
+ isExternalLibraryImport &&
218
+ ! resolved . originalPath &&
219
+ ! isExternalModuleNameRelative ( moduleName )
220
+ ) {
221
+ const { resolvedFileName, originalPath } = getOriginalAndResolvedFileName ( resolved . path , state . host , state . traceEnabled ) ;
222
+ if ( originalPath ) resolved = { ...resolved , path : resolvedFileName , originalPath } ;
223
+ }
224
+ return createResolvedModuleWithFailedLookupLocations (
225
+ state . compilerOptions ,
226
+ resolved ,
227
+ isExternalLibraryImport ,
228
+ failedLookupLocations ,
229
+ affectingLocations ,
230
+ diagnostics ,
231
+ state . resultFromCache ,
232
+ ) ;
233
+ }
234
+
204
235
function createResolvedModuleWithFailedLookupLocations (
205
236
compilerOptions : CompilerOptions ,
206
237
resolved : Resolved | undefined ,
@@ -436,6 +467,16 @@ function arePathsEqual(path1: string, path2: string, host: ModuleResolutionHost)
436
467
return comparePaths ( path1 , path2 , ! useCaseSensitiveFileNames ) === Comparison . EqualTo ;
437
468
}
438
469
470
+ function getOriginalAndResolvedFileName ( fileName : string , host : ModuleResolutionHost , traceEnabled : boolean ) {
471
+ const resolvedFileName = realPath ( fileName , host , traceEnabled ) ;
472
+ const pathsAreEqual = arePathsEqual ( fileName , resolvedFileName , host ) ;
473
+ return {
474
+ // If the fileName and realpath are differing only in casing prefer fileName so that we can issue correct errors for casing under forceConsistentCasingInFileNames
475
+ resolvedFileName : pathsAreEqual ? fileName : resolvedFileName ,
476
+ originalPath : pathsAreEqual ? undefined : fileName ,
477
+ } ;
478
+ }
479
+
439
480
/**
440
481
* @param {string | undefined } containingFile - file that contains type reference directive, can be undefined if containing file is unknown.
441
482
* This is possible in case if resolution is performed for directives specified via 'types' parameter. In this case initial path for secondary lookups
@@ -526,13 +567,12 @@ export function resolveTypeReferenceDirective(typeReferenceDirectiveName: string
526
567
let resolvedTypeReferenceDirective : ResolvedTypeReferenceDirective | undefined ;
527
568
if ( resolved ) {
528
569
const { fileName, packageId } = resolved ;
529
- const resolvedFileName = options . preserveSymlinks ? fileName : realPath ( fileName , host , traceEnabled ) ;
530
- const pathsAreEqual = arePathsEqual ( fileName , resolvedFileName , host ) ;
570
+ let resolvedFileName = fileName , originalPath : string | undefined ;
571
+ if ( ! options . preserveSymlinks ) ( { resolvedFileName , originalPath } = getOriginalAndResolvedFileName ( fileName , host , traceEnabled ) ) ;
531
572
resolvedTypeReferenceDirective = {
532
573
primary,
533
- // If the fileName and realpath are differing only in casing prefer fileName so that we can issue correct errors for casing under forceConsistentCasingInFileNames
534
- resolvedFileName : pathsAreEqual ? fileName : resolvedFileName ,
535
- originalPath : pathsAreEqual ? undefined : fileName ,
574
+ resolvedFileName,
575
+ originalPath,
536
576
packageId,
537
577
isExternalLibraryImport : pathContainsNodeModules ( fileName ) ,
538
578
} ;
@@ -1792,14 +1832,14 @@ function nodeModuleNameResolverWorker(features: NodeResolutionFeatures, moduleNa
1792
1832
result = tryResolve ( extensions ) ;
1793
1833
}
1794
1834
1795
- return createResolvedModuleWithFailedLookupLocations (
1796
- compilerOptions ,
1835
+ return createResolvedModuleWithFailedLookupLocationsHandlingSymlink (
1836
+ moduleName ,
1797
1837
result ?. value ?. resolved ,
1798
1838
result ?. value ?. isExternalLibraryImport ,
1799
1839
failedLookupLocations ,
1800
1840
affectingLocations ,
1801
1841
diagnostics ,
1802
- state . resultFromCache
1842
+ state ,
1803
1843
) ;
1804
1844
1805
1845
function tryResolve ( extensions : Extensions ) : SearchResult < { resolved : Resolved , isExternalLibraryImport : boolean } > {
@@ -1823,18 +1863,8 @@ function nodeModuleNameResolverWorker(features: NodeResolutionFeatures, moduleNa
1823
1863
}
1824
1864
resolved = loadModuleFromNearestNodeModulesDirectory ( extensions , moduleName , containingDirectory , state , cache , redirectedReference ) ;
1825
1865
}
1826
- if ( ! resolved ) return undefined ;
1827
-
1828
- let resolvedValue = resolved . value ;
1829
- if ( ! compilerOptions . preserveSymlinks && resolvedValue && ! resolvedValue . originalPath ) {
1830
- const path = realPath ( resolvedValue . path , host , traceEnabled ) ;
1831
- const pathsAreEqual = arePathsEqual ( path , resolvedValue . path , host ) ;
1832
- const originalPath = pathsAreEqual ? undefined : resolvedValue . path ;
1833
- // If the path and realpath are differing only in casing prefer path so that we can issue correct errors for casing under forceConsistentCasingInFileNames
1834
- resolvedValue = { ...resolvedValue , path : pathsAreEqual ? resolvedValue . path : path , originalPath } ;
1835
- }
1836
1866
// For node_modules lookups, get the real path so that multiple accesses to an `npm link`-ed module do not create duplicate files.
1837
- return { value : resolvedValue && { resolved : resolvedValue , isExternalLibraryImport : true } } ;
1867
+ return resolved && { value : resolved . value && { resolved : resolved . value , isExternalLibraryImport : true } } ;
1838
1868
}
1839
1869
else {
1840
1870
const { path : candidate , parts } = normalizePathForCJSResolution ( containingDirectory , moduleName ) ;
@@ -3061,14 +3091,14 @@ export function classicNameResolver(moduleName: string, containingFile: string,
3061
3091
tryResolve ( Extensions . TypeScript | Extensions . Declaration ) ||
3062
3092
tryResolve ( Extensions . JavaScript | ( compilerOptions . resolveJsonModule ? Extensions . Json : 0 ) ) ;
3063
3093
// No originalPath because classic resolution doesn't resolve realPath
3064
- return createResolvedModuleWithFailedLookupLocations (
3065
- compilerOptions ,
3094
+ return createResolvedModuleWithFailedLookupLocationsHandlingSymlink (
3095
+ moduleName ,
3066
3096
resolved && resolved . value ,
3067
3097
resolved ?. value && pathContainsNodeModules ( resolved . value . path ) ,
3068
3098
failedLookupLocations ,
3069
3099
affectingLocations ,
3070
3100
diagnostics ,
3071
- state . resultFromCache
3101
+ state ,
3072
3102
) ;
3073
3103
3074
3104
function tryResolve ( extensions : Extensions ) : SearchResult < Resolved > {
0 commit comments