Skip to content

Commit 4c5c704

Browse files
committed
Add assert that redirectTarget file is already seen before redirect file is created and handle it accordingly
1 parent 819172c commit 4c5c704

File tree

1 file changed

+26
-11
lines changed

1 file changed

+26
-11
lines changed

src/compiler/program.ts

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1369,7 +1369,7 @@ namespace ts {
13691369
const seenPackageNames = new Map<string, SeenPackageName>();
13701370

13711371
for (const oldSourceFile of oldProgram.getSourceFiles()) {
1372-
let newSourceFile = host.getSourceFileByPath
1372+
const newSourceFile = host.getSourceFileByPath
13731373
? host.getSourceFileByPath(oldSourceFile.fileName, oldSourceFile.resolvedPath, options.target!, /*onError*/ undefined, shouldCreateNewSourceFile)
13741374
: host.getSourceFile(oldSourceFile.fileName, options.target!, /*onError*/ undefined, shouldCreateNewSourceFile); // TODO: GH#18217
13751375

@@ -1387,12 +1387,15 @@ namespace ts {
13871387
// Underlying file has changed. Might not redirect anymore. Must rebuild program.
13881388
return StructureIsReused.Not;
13891389
}
1390-
fileChanged = false;
1391-
newSourceFile = oldSourceFile; // Use the redirect.
1390+
// redirect target should already be present
1391+
Debug.checkDefined(find(newSourceFiles, f => f.path === oldSourceFile.redirectInfo?.redirectTarget.path));
1392+
// Add to the newSourceFiles for now and handle redirect if program is used completely
1393+
newSourceFiles.push(newSourceFile);
1394+
continue;
13921395
}
13931396
else if (oldProgram.redirectTargetsMap.has(oldSourceFile.path)) {
13941397
// If a redirected-to source file changes, the redirect may be broken.
1395-
if (newSourceFile !== oldSourceFile) {
1398+
if (newSourceFile.version !== oldSourceFile.version) {
13961399
return StructureIsReused.Not;
13971400
}
13981401
fileChanged = false;
@@ -1527,11 +1530,24 @@ namespace ts {
15271530

15281531
// update fileName -> file mapping
15291532
Debug.assert(newSourceFiles.length === oldProgram.getSourceFiles().length);
1530-
for (const newSourceFile of newSourceFiles) {
1531-
filesByName.set(newSourceFile.path, newSourceFile);
1532-
// Ensure imports are calculated if the file version didnt change so but its different instance of file
1533-
collectExternalModuleReferences(newSourceFile);
1534-
}
1533+
newSourceFiles.forEach((newSourceFile, index) => {
1534+
// Update file if its redirecting to different file
1535+
const oldSourceFile = oldProgram!.getSourceFiles()[index];
1536+
if (oldSourceFile.redirectInfo) {
1537+
const newRedirectTarget = filesByName.get(oldSourceFile.redirectInfo.redirectTarget.path) as SourceFile;
1538+
newSourceFile = newRedirectTarget === oldSourceFile.redirectInfo.redirectTarget ?
1539+
oldSourceFile :
1540+
// Create new redirect file
1541+
createRedirectSourceFile(newRedirectTarget, newSourceFile, oldSourceFile.fileName, oldSourceFile.path, oldSourceFile.resolvedPath, oldSourceFile.originalFileName);
1542+
newSourceFiles[index] = newSourceFile;
1543+
filesByName.set(newSourceFile.path, newSourceFile);
1544+
}
1545+
else {
1546+
filesByName.set(newSourceFile.path, newSourceFile);
1547+
// Ensure imports are calculated if the file version didnt change so but its different instance of file
1548+
collectExternalModuleReferences(newSourceFile);
1549+
}
1550+
});
15351551
const oldFilesByNameMap = oldProgram.getFilesByNameMap();
15361552
oldFilesByNameMap.forEach((oldFile, path) => {
15371553
if (!oldFile) {
@@ -2417,7 +2433,6 @@ namespace ts {
24172433
redirect.resolvedPath = resolvedPath;
24182434
redirect.originalFileName = originalFileName;
24192435
redirect.redirectInfo = { redirectTarget, unredirected };
2420-
sourceFilesFoundSearchingNodeModules.set(path, currentNodeModulesDepth > 0);
24212436
Object.defineProperties(redirect, {
24222437
id: {
24232438
get(this: SourceFile) { return this.redirectInfo!.redirectTarget.id; },
@@ -2542,7 +2557,7 @@ namespace ts {
25422557
const dupFile = createRedirectSourceFile(fileFromPackageId, file!, fileName, path, toPath(fileName), originalFileName); // TODO: GH#18217
25432558
redirectTargetsMap.add(fileFromPackageId.path, fileName);
25442559
addFileToFilesByName(dupFile, path, redirectedPath);
2545-
sourceFileToPackageName.set(path, packageId.name);
2560+
sourceFilesFoundSearchingNodeModules.set(path, currentNodeModulesDepth > 0);
25462561
processingOtherFiles!.push(dupFile);
25472562
return dupFile;
25482563
}

0 commit comments

Comments
 (0)