Skip to content

Commit b7d7d5f

Browse files
authored
Merge pull request #29252 from Microsoft/tscWatchIsolatedModules
Do not cache semantic diagnostics with --isolated modules
2 parents bce9e2c + a633f95 commit b7d7d5f

File tree

2 files changed

+65
-7
lines changed

2 files changed

+65
-7
lines changed

src/compiler/builder.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,9 @@ namespace ts {
6464
const state = BuilderState.create(newProgram, getCanonicalFileName, oldState) as BuilderProgramState;
6565
state.program = newProgram;
6666
const compilerOptions = newProgram.getCompilerOptions();
67-
if (!compilerOptions.outFile && !compilerOptions.out) {
67+
// With --out or --outFile, any change affects all semantic diagnostics so no need to cache them
68+
// With --isolatedModules, emitting changed file doesnt emit dependent files so we cant know of dependent files to retrieve errors so dont cache the errors
69+
if (!compilerOptions.outFile && !compilerOptions.out && !compilerOptions.isolatedModules) {
6870
state.semanticDiagnosticsPerFile = createMap<ReadonlyArray<Diagnostic>>();
6971
}
7072
state.changedFilesSet = createMap<true>();
@@ -338,15 +340,19 @@ namespace ts {
338340
*/
339341
function getSemanticDiagnosticsOfFile(state: BuilderProgramState, sourceFile: SourceFile, cancellationToken?: CancellationToken): ReadonlyArray<Diagnostic> {
340342
const path = sourceFile.path;
341-
const cachedDiagnostics = state.semanticDiagnosticsPerFile!.get(path);
342-
// Report the semantic diagnostics from the cache if we already have those diagnostics present
343-
if (cachedDiagnostics) {
344-
return cachedDiagnostics;
343+
if (state.semanticDiagnosticsPerFile) {
344+
const cachedDiagnostics = state.semanticDiagnosticsPerFile.get(path);
345+
// Report the semantic diagnostics from the cache if we already have those diagnostics present
346+
if (cachedDiagnostics) {
347+
return cachedDiagnostics;
348+
}
345349
}
346350

347351
// Diagnostics werent cached, get them from program, and cache the result
348352
const diagnostics = state.program.getSemanticDiagnostics(sourceFile, cancellationToken);
349-
state.semanticDiagnosticsPerFile!.set(path, diagnostics);
353+
if (state.semanticDiagnosticsPerFile) {
354+
state.semanticDiagnosticsPerFile.set(path, diagnostics);
355+
}
350356
return diagnostics;
351357
}
352358

src/testRunner/unittests/tscWatch/programUpdates.ts

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1281,7 +1281,59 @@ interface Document {
12811281
checkProgramActualFiles(watch(), [aFile.path, bFile.path, libFile.path]);
12821282
}
12831283
});
1284-
});
12851284

1285+
it("reports errors correctly with isolatedModules", () => {
1286+
const currentDirectory = "/user/username/projects/myproject";
1287+
const aFile: File = {
1288+
path: `${currentDirectory}/a.ts`,
1289+
content: `export const a: string = "";`
1290+
};
1291+
const bFile: File = {
1292+
path: `${currentDirectory}/b.ts`,
1293+
content: `import { a } from "./a";
1294+
const b: string = a;`
1295+
};
1296+
const configFile: File = {
1297+
path: `${currentDirectory}/tsconfig.json`,
1298+
content: JSON.stringify({
1299+
compilerOptions: {
1300+
isolatedModules: true
1301+
}
1302+
})
1303+
};
1304+
1305+
const files = [aFile, bFile, libFile, configFile];
12861306

1307+
const host = createWatchedSystem(files, { currentDirectory });
1308+
const watch = createWatchOfConfigFile("tsconfig.json", host);
1309+
verifyProgramFiles();
1310+
checkOutputErrorsInitial(host, emptyArray);
1311+
assert.equal(host.readFile(`${currentDirectory}/a.js`), `"use strict";
1312+
exports.__esModule = true;
1313+
exports.a = "";
1314+
`, "Contents of a.js");
1315+
assert.equal(host.readFile(`${currentDirectory}/b.js`), `"use strict";
1316+
exports.__esModule = true;
1317+
var a_1 = require("./a");
1318+
var b = a_1.a;
1319+
`, "Contents of b.js");
1320+
const modifiedTime = host.getModifiedTime(`${currentDirectory}/b.js`);
1321+
1322+
host.writeFile(aFile.path, `export const a: number = 1`);
1323+
host.runQueuedTimeoutCallbacks();
1324+
verifyProgramFiles();
1325+
checkOutputErrorsIncremental(host, [
1326+
getDiagnosticOfFileFromProgram(watch(), bFile.path, bFile.content.indexOf("b"), 1, Diagnostics.Type_0_is_not_assignable_to_type_1, "number", "string")
1327+
]);
1328+
assert.equal(host.readFile(`${currentDirectory}/a.js`), `"use strict";
1329+
exports.__esModule = true;
1330+
exports.a = 1;
1331+
`, "Contents of a.js");
1332+
assert.equal(host.getModifiedTime(`${currentDirectory}/b.js`), modifiedTime, "Timestamp of b.js");
1333+
1334+
function verifyProgramFiles() {
1335+
checkProgramActualFiles(watch(), [aFile.path, bFile.path, libFile.path]);
1336+
}
1337+
});
1338+
});
12871339
}

0 commit comments

Comments
 (0)