@@ -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 ,
@@ -1049,6 +1052,12 @@ export function loadWithModeAwareCache<Entry, SourceFile, ResolutionCache, Resol
1049
1052
return resolutions ;
1050
1053
}
1051
1054
1055
+ function getLibFileName ( libReference : FileReference ) {
1056
+ const libName = toFileNameLowerCase ( libReference . fileName ) ;
1057
+ const libFileName = libMap . get ( libName ) ;
1058
+ return { libName, libFileName } ;
1059
+ }
1060
+
1052
1061
/** @internal */
1053
1062
export function forEachResolvedProjectReference < T > (
1054
1063
resolvedProjectReferences : readonly ( ResolvedProjectReference | undefined ) [ ] | undefined ,
@@ -1097,6 +1106,11 @@ function forEachProjectReference<T>(
1097
1106
/** @internal */
1098
1107
export const inferredTypesContainingFile = "__inferred type names__.ts" ;
1099
1108
1109
+ /** @internal */
1110
+ export function getInferredLibrarayNameResolveFrom ( currentDirectory : string , libFileName : string ) {
1111
+ return combinePaths ( currentDirectory , `__lib_node_modules_lookup_${ libFileName } __.ts` ) ;
1112
+ }
1113
+
1100
1114
interface DiagnosticCache < T extends Diagnostic > {
1101
1115
perFile ?: Map < Path , readonly T [ ] > ;
1102
1116
allDiagnostics ?: readonly T [ ] ;
@@ -1176,6 +1190,7 @@ export function isProgramUptoDate(
1176
1190
getSourceVersion : ( path : Path , fileName : string ) => string | undefined ,
1177
1191
fileExists : ( fileName : string ) => boolean ,
1178
1192
hasInvalidatedResolutions : HasInvalidatedResolutions ,
1193
+ hasInvalidatedLibResolutions : HasInvalidatedLibResolutions ,
1179
1194
hasChangedAutomaticTypeDirectiveNames : HasChangedAutomaticTypeDirectiveNames | undefined ,
1180
1195
getParsedCommandLine : ( fileName : string ) => ParsedCommandLine | undefined ,
1181
1196
projectReferences : readonly ProjectReference [ ] | undefined
@@ -1201,6 +1216,8 @@ export function isProgramUptoDate(
1201
1216
// If the compilation settings do no match, then the program is not up-to-date
1202
1217
if ( ! compareDataObjects ( currentOptions , newOptions ) ) return false ;
1203
1218
1219
+ if ( some ( newOptions . lib , hasInvalidatedLibResolutions ) ) return false ;
1220
+
1204
1221
// If everything matches but the text of config file is changed,
1205
1222
// error locations can change for program options, so update the program
1206
1223
if ( currentOptions . configFile && newOptions . configFile ) return currentOptions . configFile . text === newOptions . configFile . text ;
@@ -1209,7 +1226,11 @@ export function isProgramUptoDate(
1209
1226
1210
1227
function sourceFileNotUptoDate ( sourceFile : SourceFile ) {
1211
1228
return ! sourceFileVersionUptoDate ( sourceFile ) ||
1212
- hasInvalidatedResolutions ( sourceFile . path ) ;
1229
+ hasInvalidatedResolutions ( sourceFile . path ) ||
1230
+ some ( sourceFile . libReferenceDirectives , libRef => {
1231
+ const { libFileName } = getLibFileName ( libRef ) ;
1232
+ return ! ! libFileName && hasInvalidatedLibResolutions ( libFileName ) ;
1233
+ } ) ;
1213
1234
}
1214
1235
1215
1236
function sourceFileVersionUptoDate ( sourceFile : SourceFile ) {
@@ -1469,7 +1490,8 @@ export function createProgram(rootNamesOrOptions: readonly string[] | CreateProg
1469
1490
let automaticTypeDirectiveNames : string [ ] | undefined ;
1470
1491
let automaticTypeDirectiveResolutions : ModeAwareCache < ResolvedTypeReferenceDirectiveWithFailedLookupLocations > ;
1471
1492
1472
- let resolvedLibReferences : Map < string , string > | undefined ;
1493
+ let resolvedLibReferences : Map < string , LibResolution > | undefined ;
1494
+ let resolvedLibProcessing : Map < string , LibResolution > | undefined ;
1473
1495
1474
1496
// The below settings are to track if a .js file should be add to the program if loaded via searching under node_modules.
1475
1497
// This works as imported modules are discovered recursively in a depth first manner, specifically:
@@ -1594,6 +1616,17 @@ export function createProgram(rootNamesOrOptions: readonly string[] | CreateProg
1594
1616
) ;
1595
1617
}
1596
1618
1619
+ const hasInvalidatedLibResolutions = host . hasInvalidatedLibResolutions || returnFalse ;
1620
+ let actualResolveLibrary : ( libraryName : string , resolveFrom : string , options : CompilerOptions , libFileName : string ) => ResolvedModuleWithFailedLookupLocations ;
1621
+ if ( host . resolveLibrary ) {
1622
+ actualResolveLibrary = host . resolveLibrary . bind ( host ) ;
1623
+ }
1624
+ else {
1625
+ const libraryResolutionCache = createModuleResolutionCache ( currentDirectory , getCanonicalFileName , options , moduleResolutionCache ?. getPackageJsonInfoCache ( ) ) ;
1626
+ actualResolveLibrary = ( libraryName , resolveFrom , options ) =>
1627
+ resolveLibrary ( libraryName , resolveFrom , options , host , libraryResolutionCache ) ;
1628
+ }
1629
+
1597
1630
// Map from a stringified PackageId to the source file with that id.
1598
1631
// Only one source file may have a given packageId. Others become redirects (see createRedirectSourceFile).
1599
1632
// `packageIdToSourceFile` is only used while building the program, while `sourceFileToPackageName` and `isSourceFileTargetOfRedirect` are kept around.
@@ -1774,6 +1807,7 @@ export function createProgram(rootNamesOrOptions: readonly string[] | CreateProg
1774
1807
1775
1808
// unconditionally set oldProgram to undefined to prevent it from being captured in closure
1776
1809
oldProgram = undefined ;
1810
+ resolvedLibProcessing = undefined ;
1777
1811
1778
1812
const program : Program = {
1779
1813
getRootFileNames : ( ) => rootNames ,
@@ -2434,6 +2468,15 @@ export function createProgram(rootNamesOrOptions: readonly string[] | CreateProg
2434
2468
else {
2435
2469
newSourceFile . resolvedTypeReferenceDirectiveNames = oldSourceFile . resolvedTypeReferenceDirectiveNames ;
2436
2470
}
2471
+ // Do this resolution if necessary to determine reconstruction of program
2472
+ if ( structureIsReused !== StructureIsReused . Completely &&
2473
+ some ( newSourceFile . libReferenceDirectives , libReference => {
2474
+ const { libFileName } = getLibFileName ( libReference ) ;
2475
+ return ! ! libFileName &&
2476
+ pathForLibFileWorker ( libFileName ) . actual !== oldProgram ?. resolvedLibReferences ?. get ( libFileName ) ?. actual ;
2477
+ } ) ) {
2478
+ structureIsReused = StructureIsReused . SafeModules ;
2479
+ }
2437
2480
}
2438
2481
2439
2482
if ( structureIsReused !== StructureIsReused . Completely ) {
@@ -2444,6 +2487,10 @@ export function createProgram(rootNamesOrOptions: readonly string[] | CreateProg
2444
2487
return StructureIsReused . SafeModules ;
2445
2488
}
2446
2489
2490
+ if ( some ( options . lib , libFileName => pathForLibFileWorker ( libFileName ) . actual !== oldProgram ?. resolvedLibReferences ?. get ( libFileName ) ?. actual ) ) {
2491
+ return StructureIsReused . SafeModules ;
2492
+ }
2493
+
2447
2494
if ( host . hasChangedAutomaticTypeDirectiveNames ) {
2448
2495
if ( host . hasChangedAutomaticTypeDirectiveNames ( ) ) return StructureIsReused . SafeModules ;
2449
2496
}
@@ -2600,7 +2647,7 @@ export function createProgram(rootNamesOrOptions: readonly string[] | CreateProg
2600
2647
return equalityComparer ( file . fileName , getDefaultLibraryFileName ( ) ) ;
2601
2648
}
2602
2649
else {
2603
- return some ( options . lib , libFileName => equalityComparer ( file . fileName , resolvedLibReferences ! . get ( libFileName ) ! ) ) ;
2650
+ return some ( options . lib , libFileName => equalityComparer ( file . fileName , resolvedLibReferences ! . get ( libFileName ) ! . actual ) ) ;
2604
2651
}
2605
2652
}
2606
2653
@@ -3314,11 +3361,9 @@ export function createProgram(rootNamesOrOptions: readonly string[] | CreateProg
3314
3361
}
3315
3362
3316
3363
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
- }
3364
+ const { libFileName } = getLibFileName ( ref ) ;
3365
+ const actualFileName = libFileName && resolvedLibReferences ?. get ( libFileName ) ?. actual ;
3366
+ return actualFileName !== undefined ? getSourceFile ( actualFileName ) : undefined ;
3322
3367
}
3323
3368
3324
3369
/** This should have similar behavior to 'processSourceFile' without diagnostics or mutation. */
@@ -3815,7 +3860,13 @@ export function createProgram(rootNamesOrOptions: readonly string[] | CreateProg
3815
3860
3816
3861
function pathForLibFile ( libFileName : string ) : string {
3817
3862
const existing = resolvedLibReferences ?. get ( libFileName ) ;
3818
- if ( existing ) return existing ;
3863
+ if ( existing ) return existing . actual ;
3864
+ const result = pathForLibFileWorker ( libFileName ) ;
3865
+ ( resolvedLibReferences ??= new Map ( ) ) . set ( libFileName , result ) ;
3866
+ return result . actual ;
3867
+ }
3868
+
3869
+ function getLibraryName ( libFileName : string ) {
3819
3870
// Support resolving to lib.dom.d.ts -> @typescript/lib-dom, and
3820
3871
// lib.dom.iterable.d.ts -> @typescript/lib-dom/iterable
3821
3872
// lib.es2015.symbol.wellknown.d.ts -> @typescript/lib-es2015/symbol-wellknown
@@ -3826,19 +3877,52 @@ export function createProgram(rootNamesOrOptions: readonly string[] | CreateProg
3826
3877
path += ( i === 2 ? "/" : "-" ) + components [ i ] ;
3827
3878
i ++ ;
3828
3879
}
3829
- const resolveFrom = combinePaths ( currentDirectory , `__lib_node_modules_lookup_${ libFileName } __.ts` ) ;
3830
- const localOverrideModuleResult = resolveModuleName ( "@typescript/lib-" + path , resolveFrom , { moduleResolution : ModuleResolutionKind . Node10 , traceResolution : options . traceResolution } , host , moduleResolutionCache ) ;
3831
- const result = localOverrideModuleResult ?. resolvedModule ?
3832
- localOverrideModuleResult . resolvedModule . resolvedFileName :
3833
- combinePaths ( defaultLibraryPath , libFileName ) ;
3834
- ( resolvedLibReferences ??= new Map ( ) ) . set ( libFileName , result ) ;
3880
+ return "@typescript/lib-" + path ;
3881
+ }
3882
+
3883
+ function pathForLibFileWorker ( libFileName : string ) : LibResolution {
3884
+ const existing = resolvedLibProcessing ?. get ( libFileName ) ;
3885
+ if ( existing ) return existing ;
3886
+
3887
+ if ( structureIsReused !== StructureIsReused . Not && oldProgram && ! hasInvalidatedLibResolutions ( libFileName ) ) {
3888
+ const oldResolution = oldProgram . resolvedLibReferences ?. get ( libFileName ) ;
3889
+ if ( oldResolution ) {
3890
+ if ( oldResolution . resolution && isTraceEnabled ( options , host ) ) {
3891
+ const libraryName = getLibraryName ( libFileName ) ;
3892
+ const resolveFrom = getInferredLibrarayNameResolveFrom ( currentDirectory , libFileName ) ;
3893
+ trace ( host ,
3894
+ oldResolution . resolution . resolvedModule ?
3895
+ oldResolution . resolution . resolvedModule . packageId ?
3896
+ Diagnostics . Reusing_resolution_of_module_0_from_1_of_old_program_it_was_successfully_resolved_to_2_with_Package_ID_3 :
3897
+ Diagnostics . Reusing_resolution_of_module_0_from_1_of_old_program_it_was_successfully_resolved_to_2 :
3898
+ Diagnostics . Reusing_resolution_of_module_0_from_1_of_old_program_it_was_not_resolved ,
3899
+ libraryName ,
3900
+ getNormalizedAbsolutePath ( resolveFrom , currentDirectory ) ,
3901
+ oldResolution . resolution . resolvedModule ?. resolvedFileName ,
3902
+ oldResolution . resolution . resolvedModule ?. packageId && packageIdToString ( oldResolution . resolution . resolvedModule . packageId )
3903
+ ) ;
3904
+ }
3905
+ ( resolvedLibProcessing ??= new Map ( ) ) . set ( libFileName , oldResolution ) ;
3906
+ return oldResolution ;
3907
+ }
3908
+ }
3909
+
3910
+ const libraryName = getLibraryName ( libFileName ) ;
3911
+ const resolveFrom = getInferredLibrarayNameResolveFrom ( currentDirectory , libFileName ) ;
3912
+ const resolution = actualResolveLibrary ( libraryName , resolveFrom , options , libFileName ) ;
3913
+ const result : LibResolution = {
3914
+ resolution,
3915
+ actual : resolution . resolvedModule ?
3916
+ resolution . resolvedModule . resolvedFileName :
3917
+ combinePaths ( defaultLibraryPath , libFileName )
3918
+ } ;
3919
+ ( resolvedLibProcessing ??= new Map ( ) ) . set ( libFileName , result ) ;
3835
3920
return result ;
3836
3921
}
3837
3922
3838
3923
function processLibReferenceDirectives ( file : SourceFile ) {
3839
3924
forEach ( file . libReferenceDirectives , ( libReference , index ) => {
3840
- const libName = toFileNameLowerCase ( libReference . fileName ) ;
3841
- const libFileName = libMap . get ( libName ) ;
3925
+ const { libName, libFileName } = getLibFileName ( libReference ) ;
3842
3926
if ( libFileName ) {
3843
3927
// we ignore any 'no-default-lib' reference set on this file.
3844
3928
processRootFile ( pathForLibFile ( libFileName ) , /*isDefaultLib*/ true , /*ignoreNoDefaultLib*/ true , { kind : FileIncludeKind . LibReferenceDirective , file : file . path , index, } ) ;
0 commit comments