@@ -156,6 +156,7 @@ import {
156
156
HasChangedAutomaticTypeDirectiveNames ,
157
157
hasChangesInResolutions ,
158
158
hasExtension ,
159
+ HasInvalidatedLibResolutions ,
159
160
HasInvalidatedResolutions ,
160
161
hasJSDocNodes ,
161
162
hasJSFileExtension ,
@@ -211,6 +212,7 @@ import {
211
212
JsxEmit ,
212
213
length ,
213
214
libMap ,
215
+ LibResolution ,
214
216
libs ,
215
217
mapDefined ,
216
218
mapDefinedIterator ,
@@ -276,6 +278,7 @@ import {
276
278
ResolvedModuleWithFailedLookupLocations ,
277
279
ResolvedProjectReference ,
278
280
ResolvedTypeReferenceDirectiveWithFailedLookupLocations ,
281
+ resolveLibrary ,
279
282
resolveModuleName ,
280
283
resolveTypeReferenceDirective ,
281
284
returnFalse ,
@@ -1097,6 +1100,32 @@ function forEachProjectReference<T>(
1097
1100
/** @internal */
1098
1101
export const inferredTypesContainingFile = "__inferred type names__.ts" ;
1099
1102
1103
+ /** @internal */
1104
+ export function getInferredLibraryNameResolveFrom ( options : CompilerOptions , currentDirectory : string , libFileName : string ) {
1105
+ const containingDirectory = options . configFilePath ? getDirectoryPath ( options . configFilePath ) : currentDirectory ;
1106
+ return combinePaths ( containingDirectory , `__lib_node_modules_lookup_${ libFileName } __.ts` ) ;
1107
+ }
1108
+
1109
+ function getLibraryNameFromLibFileName ( libFileName : string ) {
1110
+ // Support resolving to lib.dom.d.ts -> @typescript/lib-dom, and
1111
+ // lib.dom.iterable.d.ts -> @typescript/lib-dom/iterable
1112
+ // lib.es2015.symbol.wellknown.d.ts -> @typescript/lib-es2015/symbol-wellknown
1113
+ const components = libFileName . split ( "." ) ;
1114
+ let path = components [ 1 ] ;
1115
+ let i = 2 ;
1116
+ while ( components [ i ] && components [ i ] !== "d" ) {
1117
+ path += ( i === 2 ? "/" : "-" ) + components [ i ] ;
1118
+ i ++ ;
1119
+ }
1120
+ return "@typescript/lib-" + path ;
1121
+ }
1122
+
1123
+ function getLibFileNameFromLibReference ( libReference : FileReference ) {
1124
+ const libName = toFileNameLowerCase ( libReference . fileName ) ;
1125
+ const libFileName = libMap . get ( libName ) ;
1126
+ return { libName, libFileName } ;
1127
+ }
1128
+
1100
1129
interface DiagnosticCache < T extends Diagnostic > {
1101
1130
perFile ?: Map < Path , readonly T [ ] > ;
1102
1131
allDiagnostics ?: readonly T [ ] ;
@@ -1176,6 +1205,7 @@ export function isProgramUptoDate(
1176
1205
getSourceVersion : ( path : Path , fileName : string ) => string | undefined ,
1177
1206
fileExists : ( fileName : string ) => boolean ,
1178
1207
hasInvalidatedResolutions : HasInvalidatedResolutions ,
1208
+ hasInvalidatedLibResolutions : HasInvalidatedLibResolutions ,
1179
1209
hasChangedAutomaticTypeDirectiveNames : HasChangedAutomaticTypeDirectiveNames | undefined ,
1180
1210
getParsedCommandLine : ( fileName : string ) => ParsedCommandLine | undefined ,
1181
1211
projectReferences : readonly ProjectReference [ ] | undefined
@@ -1201,6 +1231,8 @@ export function isProgramUptoDate(
1201
1231
// If the compilation settings do no match, then the program is not up-to-date
1202
1232
if ( ! compareDataObjects ( currentOptions , newOptions ) ) return false ;
1203
1233
1234
+ if ( some ( newOptions . lib , hasInvalidatedLibResolutions ) ) return false ;
1235
+
1204
1236
// If everything matches but the text of config file is changed,
1205
1237
// error locations can change for program options, so update the program
1206
1238
if ( currentOptions . configFile && newOptions . configFile ) return currentOptions . configFile . text === newOptions . configFile . text ;
@@ -1209,7 +1241,11 @@ export function isProgramUptoDate(
1209
1241
1210
1242
function sourceFileNotUptoDate ( sourceFile : SourceFile ) {
1211
1243
return ! sourceFileVersionUptoDate ( sourceFile ) ||
1212
- hasInvalidatedResolutions ( sourceFile . path ) ;
1244
+ hasInvalidatedResolutions ( sourceFile . path ) ||
1245
+ some ( sourceFile . libReferenceDirectives , libRef => {
1246
+ const { libFileName } = getLibFileNameFromLibReference ( libRef ) ;
1247
+ return ! ! libFileName && hasInvalidatedLibResolutions ( libFileName ) ;
1248
+ } ) ;
1213
1249
}
1214
1250
1215
1251
function sourceFileVersionUptoDate ( sourceFile : SourceFile ) {
@@ -1469,7 +1505,8 @@ export function createProgram(rootNamesOrOptions: readonly string[] | CreateProg
1469
1505
let automaticTypeDirectiveNames : string [ ] | undefined ;
1470
1506
let automaticTypeDirectiveResolutions : ModeAwareCache < ResolvedTypeReferenceDirectiveWithFailedLookupLocations > ;
1471
1507
1472
- let resolvedLibReferences : Map < string , string > | undefined ;
1508
+ let resolvedLibReferences : Map < string , LibResolution > | undefined ;
1509
+ let resolvedLibProcessing : Map < string , LibResolution > | undefined ;
1473
1510
1474
1511
// The below settings are to track if a .js file should be add to the program if loaded via searching under node_modules.
1475
1512
// This works as imported modules are discovered recursively in a depth first manner, specifically:
@@ -1594,6 +1631,17 @@ export function createProgram(rootNamesOrOptions: readonly string[] | CreateProg
1594
1631
) ;
1595
1632
}
1596
1633
1634
+ const hasInvalidatedLibResolutions = host . hasInvalidatedLibResolutions || returnFalse ;
1635
+ let actualResolveLibrary : ( libraryName : string , resolveFrom : string , options : CompilerOptions , libFileName : string ) => ResolvedModuleWithFailedLookupLocations ;
1636
+ if ( host . resolveLibrary ) {
1637
+ actualResolveLibrary = host . resolveLibrary . bind ( host ) ;
1638
+ }
1639
+ else {
1640
+ const libraryResolutionCache = createModuleResolutionCache ( currentDirectory , getCanonicalFileName , options , moduleResolutionCache ?. getPackageJsonInfoCache ( ) ) ;
1641
+ actualResolveLibrary = ( libraryName , resolveFrom , options ) =>
1642
+ resolveLibrary ( libraryName , resolveFrom , options , host , libraryResolutionCache ) ;
1643
+ }
1644
+
1597
1645
// Map from a stringified PackageId to the source file with that id.
1598
1646
// Only one source file may have a given packageId. Others become redirects (see createRedirectSourceFile).
1599
1647
// `packageIdToSourceFile` is only used while building the program, while `sourceFileToPackageName` and `isSourceFileTargetOfRedirect` are kept around.
@@ -1774,6 +1822,7 @@ export function createProgram(rootNamesOrOptions: readonly string[] | CreateProg
1774
1822
1775
1823
// unconditionally set oldProgram to undefined to prevent it from being captured in closure
1776
1824
oldProgram = undefined ;
1825
+ resolvedLibProcessing = undefined ;
1777
1826
1778
1827
const program : Program = {
1779
1828
getRootFileNames : ( ) => rootNames ,
@@ -2444,6 +2493,11 @@ export function createProgram(rootNamesOrOptions: readonly string[] | CreateProg
2444
2493
return StructureIsReused . SafeModules ;
2445
2494
}
2446
2495
2496
+ if ( oldProgram . resolvedLibReferences &&
2497
+ forEachEntry ( oldProgram . resolvedLibReferences , ( resolution , libFileName ) => pathForLibFileWorker ( libFileName ) . actual !== resolution . actual ) ) {
2498
+ return StructureIsReused . SafeModules ;
2499
+ }
2500
+
2447
2501
if ( host . hasChangedAutomaticTypeDirectiveNames ) {
2448
2502
if ( host . hasChangedAutomaticTypeDirectiveNames ( ) ) return StructureIsReused . SafeModules ;
2449
2503
}
@@ -2600,7 +2654,7 @@ export function createProgram(rootNamesOrOptions: readonly string[] | CreateProg
2600
2654
return equalityComparer ( file . fileName , getDefaultLibraryFileName ( ) ) ;
2601
2655
}
2602
2656
else {
2603
- return some ( options . lib , libFileName => equalityComparer ( file . fileName , resolvedLibReferences ! . get ( libFileName ) ! ) ) ;
2657
+ return some ( options . lib , libFileName => equalityComparer ( file . fileName , resolvedLibReferences ! . get ( libFileName ) ! . actual ) ) ;
2604
2658
}
2605
2659
}
2606
2660
@@ -3314,11 +3368,9 @@ export function createProgram(rootNamesOrOptions: readonly string[] | CreateProg
3314
3368
}
3315
3369
3316
3370
function getLibFileFromReference ( ref : FileReference ) {
3317
- const libName = toFileNameLowerCase ( ref . fileName ) ;
3318
- const libFileName = libMap . get ( libName ) ;
3319
- if ( libFileName ) {
3320
- return getSourceFile ( resolvedLibReferences ?. get ( libFileName ) ! ) ;
3321
- }
3371
+ const { libFileName } = getLibFileNameFromLibReference ( ref ) ;
3372
+ const actualFileName = libFileName && resolvedLibReferences ?. get ( libFileName ) ?. actual ;
3373
+ return actualFileName !== undefined ? getSourceFile ( actualFileName ) : undefined ;
3322
3374
}
3323
3375
3324
3376
/** This should have similar behavior to 'processSourceFile' without diagnostics or mutation. */
@@ -3815,31 +3867,55 @@ export function createProgram(rootNamesOrOptions: readonly string[] | CreateProg
3815
3867
3816
3868
function pathForLibFile ( libFileName : string ) : string {
3817
3869
const existing = resolvedLibReferences ?. get ( libFileName ) ;
3818
- if ( existing ) return existing ;
3819
- // Support resolving to lib.dom.d.ts -> @typescript/lib-dom, and
3820
- // lib.dom.iterable.d.ts -> @typescript/lib-dom/iterable
3821
- // lib.es2015.symbol.wellknown.d.ts -> @typescript/lib-es2015/symbol-wellknown
3822
- const components = libFileName . split ( "." ) ;
3823
- let path = components [ 1 ] ;
3824
- let i = 2 ;
3825
- while ( components [ i ] && components [ i ] !== "d" ) {
3826
- path += ( i === 2 ? "/" : "-" ) + components [ i ] ;
3827
- i ++ ;
3828
- }
3829
- const containingDirectory = options . configFilePath ? getDirectoryPath ( options . configFilePath ) : currentDirectory ;
3830
- const resolveFrom = combinePaths ( containingDirectory , `__lib_node_modules_lookup_${ libFileName } __.ts` ) ;
3831
- const localOverrideModuleResult = resolveModuleName ( "@typescript/lib-" + path , resolveFrom , { moduleResolution : ModuleResolutionKind . Node10 , traceResolution : options . traceResolution } , host , moduleResolutionCache ) ;
3832
- const result = localOverrideModuleResult ?. resolvedModule ?
3833
- localOverrideModuleResult . resolvedModule . resolvedFileName :
3834
- combinePaths ( defaultLibraryPath , libFileName ) ;
3870
+ if ( existing ) return existing . actual ;
3871
+ const result = pathForLibFileWorker ( libFileName ) ;
3835
3872
( resolvedLibReferences ??= new Map ( ) ) . set ( libFileName , result ) ;
3873
+ return result . actual ;
3874
+ }
3875
+
3876
+ function pathForLibFileWorker ( libFileName : string ) : LibResolution {
3877
+ const existing = resolvedLibProcessing ?. get ( libFileName ) ;
3878
+ if ( existing ) return existing ;
3879
+
3880
+ if ( structureIsReused !== StructureIsReused . Not && oldProgram && ! hasInvalidatedLibResolutions ( libFileName ) ) {
3881
+ const oldResolution = oldProgram . resolvedLibReferences ?. get ( libFileName ) ;
3882
+ if ( oldResolution ) {
3883
+ if ( oldResolution . resolution && isTraceEnabled ( options , host ) ) {
3884
+ const libraryName = getLibraryNameFromLibFileName ( libFileName ) ;
3885
+ const resolveFrom = getInferredLibraryNameResolveFrom ( options , currentDirectory , libFileName ) ;
3886
+ trace ( host ,
3887
+ oldResolution . resolution . resolvedModule ?
3888
+ oldResolution . resolution . resolvedModule . packageId ?
3889
+ Diagnostics . Reusing_resolution_of_module_0_from_1_of_old_program_it_was_successfully_resolved_to_2_with_Package_ID_3 :
3890
+ Diagnostics . Reusing_resolution_of_module_0_from_1_of_old_program_it_was_successfully_resolved_to_2 :
3891
+ Diagnostics . Reusing_resolution_of_module_0_from_1_of_old_program_it_was_not_resolved ,
3892
+ libraryName ,
3893
+ getNormalizedAbsolutePath ( resolveFrom , currentDirectory ) ,
3894
+ oldResolution . resolution . resolvedModule ?. resolvedFileName ,
3895
+ oldResolution . resolution . resolvedModule ?. packageId && packageIdToString ( oldResolution . resolution . resolvedModule . packageId )
3896
+ ) ;
3897
+ }
3898
+ ( resolvedLibProcessing ??= new Map ( ) ) . set ( libFileName , oldResolution ) ;
3899
+ return oldResolution ;
3900
+ }
3901
+ }
3902
+
3903
+ const libraryName = getLibraryNameFromLibFileName ( libFileName ) ;
3904
+ const resolveFrom = getInferredLibraryNameResolveFrom ( options , currentDirectory , libFileName ) ;
3905
+ const resolution = actualResolveLibrary ( libraryName , resolveFrom , options , libFileName ) ;
3906
+ const result : LibResolution = {
3907
+ resolution,
3908
+ actual : resolution . resolvedModule ?
3909
+ resolution . resolvedModule . resolvedFileName :
3910
+ combinePaths ( defaultLibraryPath , libFileName )
3911
+ } ;
3912
+ ( resolvedLibProcessing ??= new Map ( ) ) . set ( libFileName , result ) ;
3836
3913
return result ;
3837
3914
}
3838
3915
3839
3916
function processLibReferenceDirectives ( file : SourceFile ) {
3840
3917
forEach ( file . libReferenceDirectives , ( libReference , index ) => {
3841
- const libName = toFileNameLowerCase ( libReference . fileName ) ;
3842
- const libFileName = libMap . get ( libName ) ;
3918
+ const { libName, libFileName } = getLibFileNameFromLibReference ( libReference ) ;
3843
3919
if ( libFileName ) {
3844
3920
// we ignore any 'no-default-lib' reference set on this file.
3845
3921
processRootFile ( pathForLibFile ( libFileName ) , /*isDefaultLib*/ true , /*ignoreNoDefaultLib*/ true , { kind : FileIncludeKind . LibReferenceDirective , file : file . path , index, } ) ;
0 commit comments