Skip to content

Commit 8dede43

Browse files
authored
Reset hasChangedAutomaticTypeDirectiveNames once new program is created (#37266)
* Allow passing watch to the change as parameter * Reset hasChangedAutomaticTypeDirectiveNames once new program is created Also dont invoke afterProgramCreate if the program is not new
1 parent c0c5760 commit 8dede43

File tree

7 files changed

+64
-33
lines changed

7 files changed

+64
-33
lines changed

src/compiler/watchPublic.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ namespace ts {
380380
createNewProgram(hasInvalidatedResolution);
381381
}
382382

383-
if (host.afterProgramCreate) {
383+
if (host.afterProgramCreate && program !== builderProgram) {
384384
host.afterProgramCreate(builderProgram);
385385
}
386386

@@ -399,6 +399,7 @@ namespace ts {
399399
resolutionCache.startCachingPerDirectoryResolution();
400400
compilerHost.hasInvalidatedResolution = hasInvalidatedResolution;
401401
compilerHost.hasChangedAutomaticTypeDirectiveNames = hasChangedAutomaticTypeDirectiveNames;
402+
hasChangedAutomaticTypeDirectiveNames = false;
402403
builderProgram = createProgram(rootFileNames, compilerOptions, compilerHost, builderProgram, configFileParsingDiagnostics, projectReferences);
403404
resolutionCache.finishCachingPerDirectoryResolution();
404405

src/testRunner/unittests/tsbuild/watchMode.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -598,7 +598,7 @@ let x: string = 10;`);
598598
}
599599

600600
function verifyDependencies(watch: Watch, filePath: string, expected: readonly string[]) {
601-
checkArray(`${filePath} dependencies`, watch.getBuilderProgram().getAllDependencies(watch().getSourceFile(filePath)!), expected);
601+
checkArray(`${filePath} dependencies`, watch.getCurrentProgram().getAllDependencies(watch.getCurrentProgram().getSourceFile(filePath)!), expected);
602602
}
603603

604604
describe("on sample project", () => {
@@ -639,7 +639,7 @@ let x: string = 10;`);
639639

640640
host.checkTimeoutQueueLengthAndRun(1);
641641
checkOutputErrorsIncremental(host, emptyArray);
642-
checkProgramActualFiles(watch(), expectedProgramFilesAfterEdit());
642+
checkProgramActualFiles(watch.getCurrentProgram().getProgram(), expectedProgramFilesAfterEdit());
643643

644644
});
645645

@@ -739,7 +739,7 @@ export function gfoo() {
739739
expectedWatchedDirectoriesRecursive: readonly string[],
740740
dependencies: readonly [string, readonly string[]][],
741741
expectedWatchedDirectories?: readonly string[]) {
742-
checkProgramActualFiles(watch(), expectedProgramFiles);
742+
checkProgramActualFiles(watch.getCurrentProgram().getProgram(), expectedProgramFiles);
743743
verifyWatchesOfProject(host, expectedWatchedFiles, expectedWatchedDirectoriesRecursive, expectedWatchedDirectories);
744744
for (const [file, deps] of dependencies) {
745745
verifyDependencies(watch, file, deps);

src/testRunner/unittests/tscWatch/consoleClearing.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,11 @@ namespace ts.tscWatch {
4949
subScenario: "when preserveWatchOutput is true in config file/createWatchOfConfigFile",
5050
commandLineArgs: ["--w", "-p", configFile.path],
5151
sys,
52-
getPrograms: () => [[watch(), watch.getBuilderProgram()]],
52+
getPrograms: () => [[watch.getCurrentProgram().getProgram(), watch.getCurrentProgram()]],
5353
changes: [
5454
makeChangeToFile
55-
]
55+
],
56+
watchOrSolution: watch
5657
});
5758
});
5859
verifyTscWatch({

src/testRunner/unittests/tscWatch/helpers.ts

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -31,27 +31,18 @@ namespace ts.tscWatch {
3131
checkArray(`Program rootFileNames`, program.getRootFileNames(), expectedFiles);
3232
}
3333

34-
export interface Watch {
35-
(): Program;
36-
getBuilderProgram(): EmitAndSemanticDiagnosticsBuilderProgram;
37-
close(): void;
38-
}
34+
export type Watch = WatchOfConfigFile<EmitAndSemanticDiagnosticsBuilderProgram> | WatchOfFilesAndCompilerOptions<EmitAndSemanticDiagnosticsBuilderProgram>;
3935

4036
export function createWatchOfConfigFile(configFileName: string, host: WatchedSystem, optionsToExtend?: CompilerOptions, watchOptionsToExtend?: WatchOptions, maxNumberOfFilesToIterateForInvalidation?: number) {
4137
const compilerHost = createWatchCompilerHostOfConfigFile(configFileName, optionsToExtend || {}, watchOptionsToExtend, host);
4238
compilerHost.maxNumberOfFilesToIterateForInvalidation = maxNumberOfFilesToIterateForInvalidation;
43-
const watch = createWatchProgram(compilerHost);
44-
const result = (() => watch.getCurrentProgram().getProgram()) as Watch;
45-
result.getBuilderProgram = () => watch.getCurrentProgram();
46-
result.close = () => watch.close();
47-
return result;
39+
return createWatchProgram(compilerHost);
4840
}
4941

5042
export function createWatchOfFilesAndCompilerOptions(rootFiles: string[], host: WatchedSystem, options: CompilerOptions = {}, watchOptions?: WatchOptions, maxNumberOfFilesToIterateForInvalidation?: number) {
5143
const compilerHost = createWatchCompilerHostOfFilesAndCompilerOptions(rootFiles, options, watchOptions, host);
5244
compilerHost.maxNumberOfFilesToIterateForInvalidation = maxNumberOfFilesToIterateForInvalidation;
53-
const watch = createWatchProgram(compilerHost);
54-
return () => watch.getCurrentProgram().getProgram();
45+
return createWatchProgram(compilerHost);
5546
}
5647

5748
const elapsedRegex = /^Elapsed:: [0-9]+ms/;
@@ -281,7 +272,11 @@ namespace ts.tscWatch {
281272
return getDiagnosticOfFileFromProgram(program, file.path, file.content.indexOf(quotedModuleName), quotedModuleName.length, Diagnostics.Cannot_find_module_0, moduleName);
282273
}
283274

284-
export type TscWatchCompileChange = (sys: TestFSWithWatch.TestServerHostTrackingWrittenFiles, programs: readonly CommandLineProgram[]) => string;
275+
export type TscWatchCompileChange = (
276+
sys: TestFSWithWatch.TestServerHostTrackingWrittenFiles,
277+
programs: readonly CommandLineProgram[],
278+
watchOrSolution: ReturnType<typeof executeCommandLine>
279+
) => string;
285280
export interface TscWatchCheckOptions {
286281
baselineSourceMap?: boolean;
287282
}
@@ -309,7 +304,7 @@ namespace ts.tscWatch {
309304
} = input;
310305

311306
const { cb, getPrograms } = commandLineCallbacks(sys);
312-
executeCommandLine(
307+
const watchOrSolution = executeCommandLine(
313308
sys,
314309
cb,
315310
commandLineArgs,
@@ -322,20 +317,22 @@ namespace ts.tscWatch {
322317
sys,
323318
getPrograms,
324319
baselineSourceMap,
325-
changes
320+
changes,
321+
watchOrSolution
326322
});
327323
});
328324
}
329325

330326
export interface RunWatchBaseline extends TscWatchCompileBase {
331327
sys: TestFSWithWatch.TestServerHostTrackingWrittenFiles;
332328
getPrograms: () => readonly CommandLineProgram[];
329+
watchOrSolution: ReturnType<typeof executeCommandLine>;
333330
}
334331
export function runWatchBaseline({
335332
scenario, subScenario, commandLineArgs,
336333
getPrograms, sys,
337334
baselineSourceMap,
338-
changes
335+
changes, watchOrSolution
339336
}: RunWatchBaseline) {
340337
const baseline: string[] = [];
341338
baseline.push(`${sys.getExecutingFilePath()} ${commandLineArgs.join(" ")}`);
@@ -349,7 +346,7 @@ namespace ts.tscWatch {
349346

350347
for (const change of changes) {
351348
const oldSnap = sys.snap();
352-
const caption = change(sys, programs);
349+
const caption = change(sys, programs, watchOrSolution);
353350
baseline.push(`Change:: ${caption}`, "");
354351
programs = watchBaseline({
355352
baseline,

src/testRunner/unittests/tscWatch/programUpdates.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -497,13 +497,13 @@ export class A {
497497
};
498498
const host = createWatchedSystem([file1, file2, file3]);
499499
const watch = createWatchOfFilesAndCompilerOptions([file2.path, file3.path], host);
500-
checkProgramActualFiles(watch(), [file2.path, file3.path]);
500+
checkProgramActualFiles(watch.getCurrentProgram().getProgram(), [file2.path, file3.path]);
501501

502502
const watch2 = createWatchOfFilesAndCompilerOptions([file1.path], host);
503-
checkProgramActualFiles(watch2(), [file1.path, file2.path, file3.path]);
503+
checkProgramActualFiles(watch2.getCurrentProgram().getProgram(), [file1.path, file2.path, file3.path]);
504504

505505
// Previous program shouldnt be updated
506-
checkProgramActualFiles(watch(), [file2.path, file3.path]);
506+
checkProgramActualFiles(watch.getCurrentProgram().getProgram(), [file2.path, file3.path]);
507507
host.checkTimeoutQueueLength(0);
508508
});
509509

@@ -937,7 +937,7 @@ declare const eval: any`
937937
};
938938
const host = createWatchedSystem([f, libFile]);
939939
const watch = createWatchOfFilesAndCompilerOptions([f.path], host, { allowNonTsExtensions: true });
940-
checkProgramActualFiles(watch(), [f.path, libFile.path]);
940+
checkProgramActualFiles(watch.getCurrentProgram().getProgram(), [f.path, libFile.path]);
941941
});
942942

943943
verifyTscWatch({

src/testRunner/unittests/tscWatch/resolutionCache.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ namespace ts.tscWatch {
1515
const host = createWatchedSystem(files);
1616
const watch = createWatchOfFilesAndCompilerOptions([root.path], host, { module: ModuleKind.AMD });
1717

18-
const f1IsNotModule = getDiagnosticOfFileFromProgram(watch(), root.path, root.content.indexOf('"f1"'), '"f1"'.length, Diagnostics.File_0_is_not_a_module, imported.path);
19-
const cannotFindFoo = getDiagnosticOfFileFromProgram(watch(), imported.path, imported.content.indexOf("foo"), "foo".length, Diagnostics.Cannot_find_name_0, "foo");
18+
const f1IsNotModule = getDiagnosticOfFileFromProgram(watch.getCurrentProgram().getProgram(), root.path, root.content.indexOf('"f1"'), '"f1"'.length, Diagnostics.File_0_is_not_a_module, imported.path);
19+
const cannotFindFoo = getDiagnosticOfFileFromProgram(watch.getCurrentProgram().getProgram(), imported.path, imported.content.indexOf("foo"), "foo".length, Diagnostics.Cannot_find_name_0, "foo");
2020

2121
// ensure that imported file was found
2222
checkOutputErrorsInitial(host, [f1IsNotModule, cannotFindFoo]);
@@ -37,7 +37,7 @@ namespace ts.tscWatch {
3737
// ensure file has correct number of errors after edit
3838
checkOutputErrorsIncremental(host, [
3939
f1IsNotModule,
40-
getDiagnosticOfFileFromProgram(watch(), root.path, newContent.indexOf("var x") + "var ".length, "x".length, Diagnostics.Type_0_is_not_assignable_to_type_1, 1, "string"),
40+
getDiagnosticOfFileFromProgram(watch.getCurrentProgram().getProgram(), root.path, newContent.indexOf("var x") + "var ".length, "x".length, Diagnostics.Type_0_is_not_assignable_to_type_1, 1, "string"),
4141
cannotFindFoo
4242
]);
4343
}
@@ -60,7 +60,7 @@ namespace ts.tscWatch {
6060

6161
// ensure file has correct number of errors after edit
6262
checkOutputErrorsIncremental(host, [
63-
getDiagnosticModuleNotFoundOfFile(watch(), root, "f2")
63+
getDiagnosticModuleNotFoundOfFile(watch.getCurrentProgram().getProgram(), root, "f2")
6464
]);
6565

6666
assert.isTrue(fileExistsIsCalled);
@@ -118,7 +118,7 @@ namespace ts.tscWatch {
118118

119119
assert.isTrue(fileExistsCalledForBar, "'fileExists' should be called");
120120
checkOutputErrorsInitial(host, [
121-
getDiagnosticModuleNotFoundOfFile(watch(), root, "bar")
121+
getDiagnosticModuleNotFoundOfFile(watch.getCurrentProgram().getProgram(), root, "bar")
122122
]);
123123

124124
fileExistsCalledForBar = false;
@@ -166,7 +166,7 @@ namespace ts.tscWatch {
166166
host.runQueuedTimeoutCallbacks();
167167
assert.isTrue(fileExistsCalledForBar, "'fileExists' should be called.");
168168
checkOutputErrorsIncremental(host, [
169-
getDiagnosticModuleNotFoundOfFile(watch(), root, "bar")
169+
getDiagnosticModuleNotFoundOfFile(watch.getCurrentProgram().getProgram(), root, "bar")
170170
]);
171171

172172
fileExistsCalledForBar = false;
@@ -391,6 +391,13 @@ declare namespace myapp {
391391
});
392392
sys.checkTimeoutQueueLengthAndRun(1);
393393
return "npm install ts-types";
394+
},
395+
(sys, [[oldProgram, oldBuilderProgram]], watchorSolution) => {
396+
sys.checkTimeoutQueueLength(0);
397+
const newProgram = (watchorSolution as Watch).getProgram();
398+
assert.strictEqual(newProgram, oldBuilderProgram, "No change so builder program should be same");
399+
assert.strictEqual(newProgram.getProgram(), oldProgram, "No change so program should be same");
400+
return "No change, just check program";
394401
}
395402
]
396403
});

tests/baselines/reference/tscWatch/resolutionCache/when-types-in-compiler-option-are-global-and-installed-at-later-point.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,3 +113,28 @@ FsWatchesRecursive::
113113
{"directoryName":"/user/username/projects/myproject","fallbackPollingInterval":500,"fallbackOptions":{"watchFile":"PriorityPollingInterval"}}
114114

115115
exitCode:: ExitStatus.undefined
116+
117+
Change:: No change, just check program
118+
119+
120+
Output::
121+
122+
WatchedFiles::
123+
/user/username/projects/myproject/tsconfig.json:
124+
{"fileName":"/user/username/projects/myproject/tsconfig.json","pollingInterval":250}
125+
/user/username/projects/myproject/lib/app.ts:
126+
{"fileName":"/user/username/projects/myproject/lib/app.ts","pollingInterval":250}
127+
/a/lib/lib.d.ts:
128+
{"fileName":"/a/lib/lib.d.ts","pollingInterval":250}
129+
/user/username/projects/myproject/node_modules/@myapp/ts-types/types/somefile.define.d.ts:
130+
{"fileName":"/user/username/projects/myproject/node_modules/@myapp/ts-types/types/somefile.define.d.ts","pollingInterval":250}
131+
132+
FsWatches::
133+
134+
FsWatchesRecursive::
135+
/user/username/projects/myproject/node_modules:
136+
{"directoryName":"/user/username/projects/myproject/node_modules","fallbackPollingInterval":500,"fallbackOptions":{"watchFile":"PriorityPollingInterval"}}
137+
/user/username/projects/myproject:
138+
{"directoryName":"/user/username/projects/myproject","fallbackPollingInterval":500,"fallbackOptions":{"watchFile":"PriorityPollingInterval"}}
139+
140+
exitCode:: ExitStatus.undefined

0 commit comments

Comments
 (0)