Skip to content

Commit 4d413a6

Browse files
committed
Fix the fileByName cache when program is used completely which breaks the getSourceFile not return redirected file by its name
1 parent 6923f2c commit 4d413a6

File tree

2 files changed

+30
-13
lines changed

2 files changed

+30
-13
lines changed

src/compiler/program.ts

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1095,7 +1095,6 @@ namespace ts {
10951095

10961096
// check if program source files has changed in the way that can affect structure of the program
10971097
const newSourceFiles: SourceFile[] = [];
1098-
const filePaths: Path[] = [];
10991098
const modifiedSourceFiles: { oldFile: SourceFile, newFile: SourceFile }[] = [];
11001099
oldProgram.structureIsReused = StructureIsReused.Completely;
11011100

@@ -1148,7 +1147,6 @@ namespace ts {
11481147
newSourceFile.originalFileName = oldSourceFile.originalFileName;
11491148
newSourceFile.resolvedPath = oldSourceFile.resolvedPath;
11501149
newSourceFile.fileName = oldSourceFile.fileName;
1151-
filePaths.push(newSourceFile.path);
11521150

11531151
const packageName = oldProgram.sourceFileToPackageName.get(oldSourceFile.path);
11541152
if (packageName !== undefined) {
@@ -1266,11 +1264,12 @@ namespace ts {
12661264
missingFilePaths = oldProgram.getMissingFilePaths();
12671265

12681266
// update fileName -> file mapping
1269-
for (let i = 0; i < newSourceFiles.length; i++) {
1270-
filesByName.set(filePaths[i], newSourceFiles[i]);
1267+
for (const newSourceFile of newSourceFiles) {
1268+
const filePath = newSourceFile.path;
1269+
addFileToFilesByName(newSourceFile, filePath, newSourceFile.resolvedPath);
12711270
// Set the file as found during node modules search if it was found that way in old progra,
1272-
if (oldProgram.isSourceFileFromExternalLibrary(oldProgram.getSourceFileByPath(filePaths[i])!)) {
1273-
sourceFilesFoundSearchingNodeModules.set(filePaths[i], true);
1271+
if (oldProgram.isSourceFileFromExternalLibrary(oldProgram.getSourceFileByPath(filePath)!)) {
1272+
sourceFilesFoundSearchingNodeModules.set(filePath, true);
12741273
}
12751274
}
12761275

@@ -2113,7 +2112,7 @@ namespace ts {
21132112
return file;
21142113
}
21152114

2116-
let redirectedPath: string | undefined;
2115+
let redirectedPath: Path | undefined;
21172116
if (refFile) {
21182117
const redirect = getProjectReferenceRedirect(fileName);
21192118
if (redirect) {
@@ -2147,7 +2146,7 @@ namespace ts {
21472146
// Instead of creating a duplicate, just redirect to the existing one.
21482147
const dupFile = createRedirectSourceFile(fileFromPackageId, file!, fileName, path, toPath(fileName), originalFileName); // TODO: GH#18217
21492148
redirectTargetsMap.add(fileFromPackageId.path, fileName);
2150-
filesByName.set(path, dupFile);
2149+
addFileToFilesByName(dupFile, path, redirectedPath);
21512150
sourceFileToPackageName.set(path, packageId.name);
21522151
processingOtherFiles!.push(dupFile);
21532152
return dupFile;
@@ -2158,11 +2157,7 @@ namespace ts {
21582157
sourceFileToPackageName.set(path, packageId.name);
21592158
}
21602159
}
2161-
2162-
filesByName.set(path, file);
2163-
if (redirectedPath) {
2164-
filesByName.set(redirectedPath, file);
2165-
}
2160+
addFileToFilesByName(file, path, redirectedPath);
21662161

21672162
if (file) {
21682163
sourceFilesFoundSearchingNodeModules.set(path, currentNodeModulesDepth > 0);
@@ -2205,6 +2200,13 @@ namespace ts {
22052200
return file;
22062201
}
22072202

2203+
function addFileToFilesByName(file: SourceFile | undefined, path: Path, redirectedPath: Path | undefined) {
2204+
filesByName.set(path, file);
2205+
if (redirectedPath) {
2206+
filesByName.set(redirectedPath, file);
2207+
}
2208+
}
2209+
22082210
function getProjectReferenceRedirect(fileName: string): string | undefined {
22092211
// Ignore dts or any of the non ts files
22102212
if (!resolvedProjectReferences || !resolvedProjectReferences.length || fileExtensionIs(fileName, Extension.Dts) || !fileExtensionIsOneOf(fileName, supportedTSExtensions)) {

src/testRunner/unittests/tsbuildWatchMode.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -562,6 +562,21 @@ export function gfoo() {
562562
const { host, watch } = createSolutionAndWatchMode();
563563
verifyProgram(host, watch);
564564
});
565+
566+
it("non local edit updates the program and watch correctly", () => {
567+
const { host, watch, solutionBuilder } = createSolutionAndWatchMode();
568+
569+
// edit
570+
host.writeFile(bTs.path, `${bTs.content}
571+
export function gfoo() {
572+
}`);
573+
solutionBuilder.invalidateProject(bTsconfig.path);
574+
solutionBuilder.buildInvalidatedProject();
575+
576+
host.checkTimeoutQueueLengthAndRun(1);
577+
checkOutputErrorsIncremental(host, emptyArray);
578+
verifyProgram(host, watch);
579+
});
565580
});
566581
});
567582
});

0 commit comments

Comments
 (0)