Skip to content

Commit 47e513e

Browse files
authored
Merge pull request microsoft#25627 from Microsoft/preserveOutputInConfig
Fix incorrect handling of preserveWatchOutput flag is in config file
2 parents 3f4412b + ca08380 commit 47e513e

File tree

3 files changed

+63
-30
lines changed

3 files changed

+63
-30
lines changed

src/compiler/watch.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -475,16 +475,15 @@ namespace ts {
475475

476476
// From tsc we want to get already parsed result and hence check for rootFileNames
477477
let newLine = updateNewLine();
478+
if (configFileName && host.configFileParsingResult) {
479+
setConfigFileParsingResult(host.configFileParsingResult);
480+
newLine = updateNewLine();
481+
}
478482
reportWatchDiagnostic(Diagnostics.Starting_compilation_in_watch_mode);
479-
if (configFileName) {
483+
if (configFileName && !host.configFileParsingResult) {
480484
newLine = getNewLineCharacter(optionsToExtendForConfigFile, () => host.getNewLine());
481-
if (host.configFileParsingResult) {
482-
setConfigFileParsingResult(host.configFileParsingResult);
483-
}
484-
else {
485-
Debug.assert(!rootFileNames);
486-
parseConfigFile();
487-
}
485+
Debug.assert(!rootFileNames);
486+
parseConfigFile();
488487
newLine = updateNewLine();
489488
}
490489

src/compiler/watchUtilities.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,7 @@ namespace ts {
434434

435435
function createFileWatcherWithTriggerLogging<H, T, U, V, X, Y>(host: H, file: string, cb: WatchCallback<U, V>, flags: T, passThrough: V | undefined, detailInfo1: X | undefined, detailInfo2: Y | undefined, addWatch: AddWatch<H, T, U, undefined>, log: (s: string) => void, watchCaption: string, getDetailWatchInfo: GetDetailWatchInfo<X, Y> | undefined): FileWatcher {
436436
return addWatch(host, file, (fileName, cbOptional) => {
437-
const triggerredInfo = `${watchCaption}:: Triggered with ${fileName}${cbOptional !== undefined ? cbOptional : ""}:: ${getWatchInfo(file, flags, detailInfo1, detailInfo2, getDetailWatchInfo)}`;
437+
const triggerredInfo = `${watchCaption}:: Triggered with ${fileName} ${cbOptional !== undefined ? cbOptional : ""}:: ${getWatchInfo(file, flags, detailInfo1, detailInfo2, getDetailWatchInfo)}`;
438438
log(triggerredInfo);
439439
const start = timestamp();
440440
cb(fileName, cbOptional, passThrough);

src/testRunner/unittests/tscWatchMode.ts

Lines changed: 55 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2315,63 +2315,97 @@ declare module "fs" {
23152315
describe("tsc-watch console clearing", () => {
23162316
const currentDirectoryLog = "Current directory: / CaseSensitiveFileNames: false\n";
23172317
const fileWatcherAddedLog = [
2318-
"FileWatcher:: Added:: WatchInfo: f.ts 250 Source file\n",
2318+
"FileWatcher:: Added:: WatchInfo: /f.ts 250 Source file\n",
23192319
"FileWatcher:: Added:: WatchInfo: /a/lib/lib.d.ts 250 Source file\n"
23202320
];
23212321

2322+
const file: File = {
2323+
path: "/f.ts",
2324+
content: ""
2325+
};
2326+
23222327
function getProgramSynchronizingLog(options: CompilerOptions) {
23232328
return [
23242329
"Synchronizing program\n",
23252330
"CreatingProgramWith::\n",
2326-
" roots: [\"f.ts\"]\n",
2331+
" roots: [\"/f.ts\"]\n",
23272332
` options: ${JSON.stringify(options)}\n`
23282333
];
23292334
}
23302335

2331-
function checkConsoleClearing(options: CompilerOptions = {}) {
2332-
const file = {
2333-
path: "f.ts",
2334-
content: ""
2335-
};
2336-
const files = [file, libFile];
2337-
const disableConsoleClear = options.diagnostics || options.extendedDiagnostics || options.preserveWatchOutput;
2336+
function isConsoleClearDisabled(options: CompilerOptions) {
2337+
return options.diagnostics || options.extendedDiagnostics || options.preserveWatchOutput;
2338+
}
2339+
2340+
function verifyCompilation(host: WatchedSystem, options: CompilerOptions, initialDisableOptions?: CompilerOptions) {
2341+
const disableConsoleClear = isConsoleClearDisabled(options);
23382342
const hasLog = options.extendedDiagnostics || options.diagnostics;
2339-
const host = createWatchedSystem(files);
2340-
createWatchOfFilesAndCompilerOptions([file.path], host, options);
2341-
checkOutputErrorsInitial(host, emptyArray, disableConsoleClear, hasLog ? [
2343+
checkOutputErrorsInitial(host, emptyArray, initialDisableOptions ? isConsoleClearDisabled(initialDisableOptions) : disableConsoleClear, hasLog ? [
23422344
currentDirectoryLog,
23432345
...getProgramSynchronizingLog(options),
23442346
...(options.extendedDiagnostics ? fileWatcherAddedLog : emptyArray)
23452347
] : undefined);
2346-
2347-
file.content = "//";
2348-
host.reloadFS(files);
2348+
host.modifyFile(file.path, "//");
23492349
host.runQueuedTimeoutCallbacks();
23502350
checkOutputErrorsIncremental(host, emptyArray, disableConsoleClear, hasLog ? [
2351-
"FileWatcher:: Triggered with /f.ts1:: WatchInfo: f.ts 250 Source file\n",
2351+
"FileWatcher:: Triggered with /f.ts 1:: WatchInfo: /f.ts 250 Source file\n",
23522352
"Scheduling update\n",
2353-
"Elapsed:: 0ms FileWatcher:: Triggered with /f.ts1:: WatchInfo: f.ts 250 Source file\n"
2353+
"Elapsed:: 0ms FileWatcher:: Triggered with /f.ts 1:: WatchInfo: /f.ts 250 Source file\n"
23542354
] : undefined, hasLog ? getProgramSynchronizingLog(options) : undefined);
23552355
}
23562356

2357+
function checkConsoleClearingUsingCommandLineOptions(options: CompilerOptions = {}) {
2358+
const files = [file, libFile];
2359+
const host = createWatchedSystem(files);
2360+
createWatchOfFilesAndCompilerOptions([file.path], host, options);
2361+
verifyCompilation(host, options);
2362+
}
2363+
23572364
it("without --diagnostics or --extendedDiagnostics", () => {
2358-
checkConsoleClearing();
2365+
checkConsoleClearingUsingCommandLineOptions();
23592366
});
23602367
it("with --diagnostics", () => {
2361-
checkConsoleClearing({
2368+
checkConsoleClearingUsingCommandLineOptions({
23622369
diagnostics: true,
23632370
});
23642371
});
23652372
it("with --extendedDiagnostics", () => {
2366-
checkConsoleClearing({
2373+
checkConsoleClearingUsingCommandLineOptions({
23672374
extendedDiagnostics: true,
23682375
});
23692376
});
23702377
it("with --preserveWatchOutput", () => {
2371-
checkConsoleClearing({
2378+
checkConsoleClearingUsingCommandLineOptions({
23722379
preserveWatchOutput: true,
23732380
});
23742381
});
2382+
2383+
describe("when preserveWatchOutput is true in config file", () => {
2384+
const compilerOptions: CompilerOptions = {
2385+
preserveWatchOutput: true
2386+
};
2387+
const configFile: File = {
2388+
path: "/tsconfig.json",
2389+
content: JSON.stringify({ compilerOptions })
2390+
};
2391+
const files = [file, configFile, libFile];
2392+
it("using createWatchOfConfigFile ", () => {
2393+
const host = createWatchedSystem(files);
2394+
createWatchOfConfigFile(configFile.path, host);
2395+
// Initially console is cleared if --preserveOutput is not provided since the config file is yet to be parsed
2396+
verifyCompilation(host, compilerOptions, {});
2397+
});
2398+
it("when createWatchProgram is invoked with configFileParseResult on WatchCompilerHostOfConfigFile", () => {
2399+
const host = createWatchedSystem(files);
2400+
const reportDiagnostic = createDiagnosticReporter(host);
2401+
const optionsToExtend: CompilerOptions = {};
2402+
const configParseResult = parseConfigFileWithSystem(configFile.path, optionsToExtend, host, reportDiagnostic)!;
2403+
const watchCompilerHost = createWatchCompilerHostOfConfigFile(configParseResult.options.configFilePath!, optionsToExtend, host, /*createProgram*/ undefined, reportDiagnostic, createWatchStatusReporter(host));
2404+
watchCompilerHost.configFileParsingResult = configParseResult;
2405+
createWatchProgram(watchCompilerHost);
2406+
verifyCompilation(host, compilerOptions);
2407+
});
2408+
});
23752409
});
23762410

23772411
describe("tsc-watch with different polling/non polling options", () => {

0 commit comments

Comments
 (0)