Skip to content

Commit 87a7112

Browse files
committed
If we are updating dts of any of the file and it affects global scope, everything needs update in signature and dts emit
Fixes #42769
1 parent e4bf58a commit 87a7112

5 files changed

+105
-249
lines changed

Diff for: src/compiler/builder.ts

+47-11
Original file line numberDiff line numberDiff line change
@@ -543,18 +543,48 @@ namespace ts {
543543
return newSignature !== oldSignature;
544544
}
545545

546-
function forEachKeyOfExportedModulesMap(state: BuilderProgramState, filePath: Path, fn: (exportedFromPath: Path) => void) {
546+
function forEachKeyOfExportedModulesMap<T>(
547+
state: BuilderProgramState,
548+
filePath: Path,
549+
fn: (exportedFromPath: Path) => T | undefined,
550+
): T | undefined {
547551
// Go through exported modules from cache first
548-
state.currentAffectedFilesExportedModulesMap!.getKeys(filePath)?.forEach(fn);
552+
let keys = state.currentAffectedFilesExportedModulesMap!.getKeys(filePath);
553+
const result = keys && forEachKey(keys, fn);
554+
if (result) return result;
555+
549556
// If exported from path is not from cache and exported modules has path, all files referencing file exported from are affected
550-
state.exportedModulesMap!.getKeys(filePath)?.forEach(exportedFromPath =>
557+
keys = state.exportedModulesMap!.getKeys(filePath);
558+
return keys && forEachKey(keys, exportedFromPath =>
551559
// If the cache had an updated value, skip
552560
!state.currentAffectedFilesExportedModulesMap!.hasKey(exportedFromPath) &&
553-
!state.currentAffectedFilesExportedModulesMap!.deletedKeys()?.has(exportedFromPath) &&
554-
fn(exportedFromPath)
561+
!state.currentAffectedFilesExportedModulesMap!.deletedKeys()?.has(exportedFromPath) ?
562+
fn(exportedFromPath) :
563+
undefined
555564
);
556565
}
557566

567+
function handleDtsMayChangeOfGlobalScope(
568+
state: BuilderProgramState,
569+
filePath: Path,
570+
cancellationToken: CancellationToken | undefined,
571+
computeHash: BuilderState.ComputeHash,
572+
host: BuilderProgramHost,
573+
): boolean {
574+
if (!state.fileInfos.get(filePath)?.affectsGlobalScope) return false;
575+
// Every file needs to be handled
576+
BuilderState.getAllFilesExcludingDefaultLibraryFile(state, state.program!, /*firstSourceFile*/ undefined)
577+
.forEach(file => handleDtsMayChangeOf(
578+
state,
579+
file.resolvedPath,
580+
cancellationToken,
581+
computeHash,
582+
host,
583+
));
584+
removeDiagnosticsOfLibraryFiles(state);
585+
return true;
586+
}
587+
558588
/**
559589
* Iterate on referencing modules that export entities from affected file and delete diagnostics and add pending emit
560590
*/
@@ -580,6 +610,7 @@ namespace ts {
580610
const currentPath = queue.pop()!;
581611
if (!seenFileNamesMap.has(currentPath)) {
582612
seenFileNamesMap.set(currentPath, true);
613+
if (handleDtsMayChangeOfGlobalScope(state, currentPath, cancellationToken, computeHash, host)) return;
583614
handleDtsMayChangeOf(state, currentPath, cancellationToken, computeHash, host);
584615
if (isChangedSignature(state, currentPath)) {
585616
const currentSourceFile = Debug.checkDefined(state.program).getSourceFileByPath(currentPath)!;
@@ -593,8 +624,10 @@ namespace ts {
593624
const seenFileAndExportsOfFile = new Set<string>();
594625
// Go through exported modules from cache first
595626
// If exported modules has path, all files referencing file exported from are affected
596-
forEachKeyOfExportedModulesMap(state, affectedFile.resolvedPath, exportedFromPath =>
597-
state.referencedMap!.getKeys(exportedFromPath)?.forEach(filePath =>
627+
forEachKeyOfExportedModulesMap(state, affectedFile.resolvedPath, exportedFromPath => {
628+
if (handleDtsMayChangeOfGlobalScope(state, exportedFromPath, cancellationToken, computeHash, host)) return true;
629+
const references = state.referencedMap!.getKeys(exportedFromPath);
630+
return references && forEachKey(references, filePath =>
598631
handleDtsMayChangeOfFileAndExportsOfFile(
599632
state,
600633
filePath,
@@ -603,12 +636,13 @@ namespace ts {
603636
computeHash,
604637
host,
605638
)
606-
)
607-
);
639+
);
640+
});
608641
}
609642

610643
/**
611644
* handle dts and semantic diagnostics on file and iterate on anything that exports this file
645+
* return true when all work is done and we can exit handling dts emit and semantic diagnostics
612646
*/
613647
function handleDtsMayChangeOfFileAndExportsOfFile(
614648
state: BuilderProgramState,
@@ -617,9 +651,10 @@ namespace ts {
617651
cancellationToken: CancellationToken | undefined,
618652
computeHash: BuilderState.ComputeHash,
619653
host: BuilderProgramHost,
620-
): void {
621-
if (!tryAddToSet(seenFileAndExportsOfFile, filePath)) return;
654+
): boolean | undefined {
655+
if (!tryAddToSet(seenFileAndExportsOfFile, filePath)) return undefined;
622656

657+
if (handleDtsMayChangeOfGlobalScope(state, filePath, cancellationToken, computeHash, host)) return true;
623658
handleDtsMayChangeOf(state, filePath, cancellationToken, computeHash, host);
624659
Debug.assert(!!state.currentAffectedFilesExportedModulesMap);
625660

@@ -646,6 +681,7 @@ namespace ts {
646681
host,
647682
)
648683
);
684+
return undefined;
649685
}
650686

651687
/**

Diff for: tests/baselines/reference/tsc/incremental/change-to-type-that-gets-used-as-global-through-export-in-another-file-discrepancies.js

-107
This file was deleted.

Diff for: tests/baselines/reference/tsc/incremental/change-to-type-that-gets-used-as-global-through-export-in-another-file-through-indirect-import-discrepancies.js

-121
This file was deleted.

0 commit comments

Comments
 (0)