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