Fix the fileByName cache when program is used completely which breaks the getSourceFile not return redirected file by its name

This commit is contained in:
Sheetal Nandi 2018-10-02 13:11:12 -07:00
parent 6923f2cdb0
commit 4d413a6a55
2 changed files with 30 additions and 13 deletions

View file

@ -1095,7 +1095,6 @@ namespace ts {
// check if program source files has changed in the way that can affect structure of the program // check if program source files has changed in the way that can affect structure of the program
const newSourceFiles: SourceFile[] = []; const newSourceFiles: SourceFile[] = [];
const filePaths: Path[] = [];
const modifiedSourceFiles: { oldFile: SourceFile, newFile: SourceFile }[] = []; const modifiedSourceFiles: { oldFile: SourceFile, newFile: SourceFile }[] = [];
oldProgram.structureIsReused = StructureIsReused.Completely; oldProgram.structureIsReused = StructureIsReused.Completely;
@ -1148,7 +1147,6 @@ namespace ts {
newSourceFile.originalFileName = oldSourceFile.originalFileName; newSourceFile.originalFileName = oldSourceFile.originalFileName;
newSourceFile.resolvedPath = oldSourceFile.resolvedPath; newSourceFile.resolvedPath = oldSourceFile.resolvedPath;
newSourceFile.fileName = oldSourceFile.fileName; newSourceFile.fileName = oldSourceFile.fileName;
filePaths.push(newSourceFile.path);
const packageName = oldProgram.sourceFileToPackageName.get(oldSourceFile.path); const packageName = oldProgram.sourceFileToPackageName.get(oldSourceFile.path);
if (packageName !== undefined) { if (packageName !== undefined) {
@ -1266,11 +1264,12 @@ namespace ts {
missingFilePaths = oldProgram.getMissingFilePaths(); missingFilePaths = oldProgram.getMissingFilePaths();
// update fileName -> file mapping // update fileName -> file mapping
for (let i = 0; i < newSourceFiles.length; i++) { for (const newSourceFile of newSourceFiles) {
filesByName.set(filePaths[i], newSourceFiles[i]); const filePath = newSourceFile.path;
addFileToFilesByName(newSourceFile, filePath, newSourceFile.resolvedPath);
// Set the file as found during node modules search if it was found that way in old progra, // Set the file as found during node modules search if it was found that way in old progra,
if (oldProgram.isSourceFileFromExternalLibrary(oldProgram.getSourceFileByPath(filePaths[i])!)) { if (oldProgram.isSourceFileFromExternalLibrary(oldProgram.getSourceFileByPath(filePath)!)) {
sourceFilesFoundSearchingNodeModules.set(filePaths[i], true); sourceFilesFoundSearchingNodeModules.set(filePath, true);
} }
} }
@ -2113,7 +2112,7 @@ namespace ts {
return file; return file;
} }
let redirectedPath: string | undefined; let redirectedPath: Path | undefined;
if (refFile) { if (refFile) {
const redirect = getProjectReferenceRedirect(fileName); const redirect = getProjectReferenceRedirect(fileName);
if (redirect) { if (redirect) {
@ -2147,7 +2146,7 @@ namespace ts {
// Instead of creating a duplicate, just redirect to the existing one. // Instead of creating a duplicate, just redirect to the existing one.
const dupFile = createRedirectSourceFile(fileFromPackageId, file!, fileName, path, toPath(fileName), originalFileName); // TODO: GH#18217 const dupFile = createRedirectSourceFile(fileFromPackageId, file!, fileName, path, toPath(fileName), originalFileName); // TODO: GH#18217
redirectTargetsMap.add(fileFromPackageId.path, fileName); redirectTargetsMap.add(fileFromPackageId.path, fileName);
filesByName.set(path, dupFile); addFileToFilesByName(dupFile, path, redirectedPath);
sourceFileToPackageName.set(path, packageId.name); sourceFileToPackageName.set(path, packageId.name);
processingOtherFiles!.push(dupFile); processingOtherFiles!.push(dupFile);
return dupFile; return dupFile;
@ -2158,11 +2157,7 @@ namespace ts {
sourceFileToPackageName.set(path, packageId.name); sourceFileToPackageName.set(path, packageId.name);
} }
} }
addFileToFilesByName(file, path, redirectedPath);
filesByName.set(path, file);
if (redirectedPath) {
filesByName.set(redirectedPath, file);
}
if (file) { if (file) {
sourceFilesFoundSearchingNodeModules.set(path, currentNodeModulesDepth > 0); sourceFilesFoundSearchingNodeModules.set(path, currentNodeModulesDepth > 0);
@ -2205,6 +2200,13 @@ namespace ts {
return file; return file;
} }
function addFileToFilesByName(file: SourceFile | undefined, path: Path, redirectedPath: Path | undefined) {
filesByName.set(path, file);
if (redirectedPath) {
filesByName.set(redirectedPath, file);
}
}
function getProjectReferenceRedirect(fileName: string): string | undefined { function getProjectReferenceRedirect(fileName: string): string | undefined {
// Ignore dts or any of the non ts files // Ignore dts or any of the non ts files
if (!resolvedProjectReferences || !resolvedProjectReferences.length || fileExtensionIs(fileName, Extension.Dts) || !fileExtensionIsOneOf(fileName, supportedTSExtensions)) { if (!resolvedProjectReferences || !resolvedProjectReferences.length || fileExtensionIs(fileName, Extension.Dts) || !fileExtensionIsOneOf(fileName, supportedTSExtensions)) {

View file

@ -562,6 +562,21 @@ export function gfoo() {
const { host, watch } = createSolutionAndWatchMode(); const { host, watch } = createSolutionAndWatchMode();
verifyProgram(host, watch); verifyProgram(host, watch);
}); });
it("non local edit updates the program and watch correctly", () => {
const { host, watch, solutionBuilder } = createSolutionAndWatchMode();
// edit
host.writeFile(bTs.path, `${bTs.content}
export function gfoo() {
}`);
solutionBuilder.invalidateProject(bTsconfig.path);
solutionBuilder.buildInvalidatedProject();
host.checkTimeoutQueueLengthAndRun(1);
checkOutputErrorsIncremental(host, emptyArray);
verifyProgram(host, watch);
});
}); });
}); });
}); });