Skip to content

Commit f34d481

Browse files
committed
When the global file is deleted mark all files as changed
Fixes #36728
1 parent 4da9fc5 commit f34d481

File tree

5 files changed

+133
-16
lines changed

5 files changed

+133
-16
lines changed

src/compiler/builder.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,12 @@ namespace ts {
245245
}
246246
});
247247

248-
if (oldCompilerOptions && compilerOptionsAffectEmit(compilerOptions, oldCompilerOptions)) {
248+
// If the global file is removed, add all files as changed
249+
if (useOldState && forEachEntry(oldState!.fileInfos, (info, sourceFilePath) => info.affectsGlobalScope && !state.fileInfos.has(sourceFilePath))) {
250+
BuilderState.getAllFilesExcludingDefaultLibraryFile(state, newProgram, /*firstSourceFile*/ undefined)
251+
.forEach(file => state.changedFilesSet.set(file.resolvedPath, true));
252+
}
253+
else if (oldCompilerOptions && compilerOptionsAffectEmit(compilerOptions, oldCompilerOptions)) {
249254
// Add all files to affectedFilesPendingEmit since emit changed
250255
newProgram.getSourceFiles().forEach(f => addToAffectedFilesPendingEmit(state, f.resolvedPath, BuilderFileEmit.Full));
251256
Debug.assert(!state.seenAffectedFiles || !state.seenAffectedFiles.size);
@@ -701,7 +706,7 @@ namespace ts {
701706
const fileInfos: MapLike<BuilderState.FileInfo> = {};
702707
state.fileInfos.forEach((value, key) => {
703708
const signature = state.currentAffectedFilesSignatures && state.currentAffectedFilesSignatures.get(key);
704-
fileInfos[relativeToBuildInfo(key)] = signature === undefined ? value : { version: value.version, signature };
709+
fileInfos[relativeToBuildInfo(key)] = signature === undefined ? value : { version: value.version, signature, affectsGlobalScope: value.affectsGlobalScope };
705710
});
706711

707712
const result: ProgramBuildInfo = {

src/compiler/builderState.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ namespace ts {
6868
export interface FileInfo {
6969
readonly version: string;
7070
signature: string | undefined;
71+
affectsGlobalScope: boolean;
7172
}
7273
/**
7374
* Referenced files with values for the keys as referenced file's path to be true
@@ -225,7 +226,7 @@ namespace ts {
225226
}
226227
}
227228
}
228-
fileInfos.set(sourceFile.resolvedPath, { version, signature: oldInfo && oldInfo.signature });
229+
fileInfos.set(sourceFile.resolvedPath, { version, signature: oldInfo && oldInfo.signature, affectsGlobalScope: isFileAffectingGlobalScope(sourceFile) });
229230
}
230231

231232
return {
@@ -488,14 +489,14 @@ namespace ts {
488489
/**
489490
* Gets all files of the program excluding the default library file
490491
*/
491-
function getAllFilesExcludingDefaultLibraryFile(state: BuilderState, programOfThisState: Program, firstSourceFile: SourceFile): readonly SourceFile[] {
492+
export function getAllFilesExcludingDefaultLibraryFile(state: BuilderState, programOfThisState: Program, firstSourceFile: SourceFile | undefined): readonly SourceFile[] {
492493
// Use cached result
493494
if (state.allFilesExcludingDefaultLibraryFile) {
494495
return state.allFilesExcludingDefaultLibraryFile;
495496
}
496497

497498
let result: SourceFile[] | undefined;
498-
addSourceFile(firstSourceFile);
499+
if (firstSourceFile) addSourceFile(firstSourceFile);
499500
for (const sourceFile of programOfThisState.getSourceFiles()) {
500501
if (sourceFile !== firstSourceFile) {
501502
addSourceFile(sourceFile);

src/testRunner/unittests/tscWatch/incremental.ts

+6-3
Original file line numberDiff line numberDiff line change
@@ -164,15 +164,18 @@ namespace ts.tscWatch {
164164
assert.equal(state.fileInfos.size, 3, "FileInfo size");
165165
assert.deepEqual(state.fileInfos.get(libFile.path), {
166166
version: system.createHash(libFile.content),
167-
signature: system.createHash(libFile.content)
167+
signature: system.createHash(libFile.content),
168+
affectsGlobalScope: true,
168169
});
169170
assert.deepEqual(state.fileInfos.get(file1.path), {
170171
version: system.createHash(file1.content),
171-
signature: system.createHash(`${file1.content.replace("export ", "export declare ")}\n`)
172+
signature: system.createHash(`${file1.content.replace("export ", "export declare ")}\n`),
173+
affectsGlobalScope: false,
172174
});
173175
assert.deepEqual(state.fileInfos.get(file2.path), {
174176
version: system.createHash(fileModified.content),
175-
signature: system.createHash("export declare const y: string;\n")
177+
signature: system.createHash("export declare const y: string;\n"),
178+
affectsGlobalScope: false,
176179
});
177180

178181
assert.deepEqual(state.compilerOptions, {

tests/baselines/reference/tscWatch/incremental/when-file-with-ambient-global-declaration-file-is-deleted-incremental.js

+59-4
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,18 @@ console.log(Config.value);
3333
"fileInfos": {
3434
"../../../../a/lib/lib.d.ts": {
3535
"version": "3858781397-/// <reference no-default-lib=\"true\"/>\ninterface Boolean {}\ninterface Function {}\ninterface CallableFunction {}\ninterface NewableFunction {}\ninterface IArguments {}\ninterface Number { toExponential: any; }\ninterface Object {}\ninterface RegExp {}\ninterface String { charAt: any; }\ninterface Array<T> { length: number; [n: number]: T; }\ninterface ReadonlyArray<T> {}\ndeclare const console: { log(msg: any): void; };",
36-
"signature": "3858781397-/// <reference no-default-lib=\"true\"/>\ninterface Boolean {}\ninterface Function {}\ninterface CallableFunction {}\ninterface NewableFunction {}\ninterface IArguments {}\ninterface Number { toExponential: any; }\ninterface Object {}\ninterface RegExp {}\ninterface String { charAt: any; }\ninterface Array<T> { length: number; [n: number]: T; }\ninterface ReadonlyArray<T> {}\ndeclare const console: { log(msg: any): void; };"
36+
"signature": "3858781397-/// <reference no-default-lib=\"true\"/>\ninterface Boolean {}\ninterface Function {}\ninterface CallableFunction {}\ninterface NewableFunction {}\ninterface IArguments {}\ninterface Number { toExponential: any; }\ninterface Object {}\ninterface RegExp {}\ninterface String { charAt: any; }\ninterface Array<T> { length: number; [n: number]: T; }\ninterface ReadonlyArray<T> {}\ndeclare const console: { log(msg: any): void; };",
37+
"affectsGlobalScope": true
3738
},
3839
"./globals.d.ts": {
3940
"version": "-6314871648-declare namespace Config { const value: string;} ",
40-
"signature": "-6314871648-declare namespace Config { const value: string;} "
41+
"signature": "-6314871648-declare namespace Config { const value: string;} ",
42+
"affectsGlobalScope": true
4143
},
4244
"./index.ts": {
4345
"version": "5371023861-console.log(Config.value);",
44-
"signature": "5381-"
46+
"signature": "5381-",
47+
"affectsGlobalScope": true
4548
}
4649
},
4750
"options": {
@@ -85,9 +88,60 @@ exitCode:: ExitStatus.Success
8588

8689
Change::
8790

91+
//// [/users/username/projects/project/index.js] file written with same contents
92+
//// [/users/username/projects/project/tsconfig.tsbuildinfo]
93+
{
94+
"program": {
95+
"fileInfos": {
96+
"../../../../a/lib/lib.d.ts": {
97+
"version": "3858781397-/// <reference no-default-lib=\"true\"/>\ninterface Boolean {}\ninterface Function {}\ninterface CallableFunction {}\ninterface NewableFunction {}\ninterface IArguments {}\ninterface Number { toExponential: any; }\ninterface Object {}\ninterface RegExp {}\ninterface String { charAt: any; }\ninterface Array<T> { length: number; [n: number]: T; }\ninterface ReadonlyArray<T> {}\ndeclare const console: { log(msg: any): void; };",
98+
"signature": "3858781397-/// <reference no-default-lib=\"true\"/>\ninterface Boolean {}\ninterface Function {}\ninterface CallableFunction {}\ninterface NewableFunction {}\ninterface IArguments {}\ninterface Number { toExponential: any; }\ninterface Object {}\ninterface RegExp {}\ninterface String { charAt: any; }\ninterface Array<T> { length: number; [n: number]: T; }\ninterface ReadonlyArray<T> {}\ndeclare const console: { log(msg: any): void; };",
99+
"affectsGlobalScope": true
100+
},
101+
"./index.ts": {
102+
"version": "5371023861-console.log(Config.value);",
103+
"signature": "5381-",
104+
"affectsGlobalScope": true
105+
}
106+
},
107+
"options": {
108+
"incremental": true,
109+
"configFilePath": "./tsconfig.json"
110+
},
111+
"referencedMap": {},
112+
"exportedModulesMap": {},
113+
"semanticDiagnosticsPerFile": [
114+
"../../../../a/lib/lib.d.ts",
115+
[
116+
"./index.ts",
117+
[
118+
{
119+
"file": "./index.ts",
120+
"start": 12,
121+
"length": 6,
122+
"messageText": "Cannot find name 'Config'.",
123+
"category": 1,
124+
"code": 2304
125+
}
126+
]
127+
]
128+
]
129+
},
130+
"version": "FakeTSVersion"
131+
}
132+
88133
//// [/users/username/projects/project/globals.d.ts] deleted
89134

90135
Output::
136+
index.ts:1:13 - error TS2304: Cannot find name 'Config'.
137+
138+
1 console.log(Config.value);
139+
   ~~~~~~
140+
141+
142+
143+
Found 1 error.
144+
91145

92146

93147
Program root files: ["/users/username/projects/project/index.ts"]
@@ -97,11 +151,12 @@ Program files::
97151
/users/username/projects/project/index.ts
98152

99153
Semantic diagnostics in builder refreshed for::
154+
/users/username/projects/project/index.ts
100155

101156
WatchedFiles::
102157

103158
FsWatches::
104159

105160
FsWatchesRecursive::
106161

107-
exitCode:: ExitStatus.Success
162+
exitCode:: ExitStatus.DiagnosticsPresent_OutputsGenerated

tests/baselines/reference/tscWatch/incremental/when-file-with-ambient-global-declaration-file-is-deleted-watch.js

+57-4
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,18 @@ console.log(Config.value);
3333
"fileInfos": {
3434
"../../../../a/lib/lib.d.ts": {
3535
"version": "3858781397-/// <reference no-default-lib=\"true\"/>\ninterface Boolean {}\ninterface Function {}\ninterface CallableFunction {}\ninterface NewableFunction {}\ninterface IArguments {}\ninterface Number { toExponential: any; }\ninterface Object {}\ninterface RegExp {}\ninterface String { charAt: any; }\ninterface Array<T> { length: number; [n: number]: T; }\ninterface ReadonlyArray<T> {}\ndeclare const console: { log(msg: any): void; };",
36-
"signature": "3858781397-/// <reference no-default-lib=\"true\"/>\ninterface Boolean {}\ninterface Function {}\ninterface CallableFunction {}\ninterface NewableFunction {}\ninterface IArguments {}\ninterface Number { toExponential: any; }\ninterface Object {}\ninterface RegExp {}\ninterface String { charAt: any; }\ninterface Array<T> { length: number; [n: number]: T; }\ninterface ReadonlyArray<T> {}\ndeclare const console: { log(msg: any): void; };"
36+
"signature": "3858781397-/// <reference no-default-lib=\"true\"/>\ninterface Boolean {}\ninterface Function {}\ninterface CallableFunction {}\ninterface NewableFunction {}\ninterface IArguments {}\ninterface Number { toExponential: any; }\ninterface Object {}\ninterface RegExp {}\ninterface String { charAt: any; }\ninterface Array<T> { length: number; [n: number]: T; }\ninterface ReadonlyArray<T> {}\ndeclare const console: { log(msg: any): void; };",
37+
"affectsGlobalScope": true
3738
},
3839
"./globals.d.ts": {
3940
"version": "-6314871648-declare namespace Config { const value: string;} ",
40-
"signature": "-6314871648-declare namespace Config { const value: string;} "
41+
"signature": "-6314871648-declare namespace Config { const value: string;} ",
42+
"affectsGlobalScope": true
4143
},
4244
"./index.ts": {
4345
"version": "5371023861-console.log(Config.value);",
44-
"signature": "5381-"
46+
"signature": "5381-",
47+
"affectsGlobalScope": true
4548
}
4649
},
4750
"options": {
@@ -104,14 +107,63 @@ exitCode:: ExitStatus.undefined
104107

105108
Change::
106109

110+
//// [/users/username/projects/project/index.js] file written with same contents
111+
//// [/users/username/projects/project/tsconfig.tsbuildinfo]
112+
{
113+
"program": {
114+
"fileInfos": {
115+
"../../../../a/lib/lib.d.ts": {
116+
"version": "3858781397-/// <reference no-default-lib=\"true\"/>\ninterface Boolean {}\ninterface Function {}\ninterface CallableFunction {}\ninterface NewableFunction {}\ninterface IArguments {}\ninterface Number { toExponential: any; }\ninterface Object {}\ninterface RegExp {}\ninterface String { charAt: any; }\ninterface Array<T> { length: number; [n: number]: T; }\ninterface ReadonlyArray<T> {}\ndeclare const console: { log(msg: any): void; };",
117+
"signature": "3858781397-/// <reference no-default-lib=\"true\"/>\ninterface Boolean {}\ninterface Function {}\ninterface CallableFunction {}\ninterface NewableFunction {}\ninterface IArguments {}\ninterface Number { toExponential: any; }\ninterface Object {}\ninterface RegExp {}\ninterface String { charAt: any; }\ninterface Array<T> { length: number; [n: number]: T; }\ninterface ReadonlyArray<T> {}\ndeclare const console: { log(msg: any): void; };",
118+
"affectsGlobalScope": true
119+
},
120+
"./index.ts": {
121+
"version": "5371023861-console.log(Config.value);",
122+
"signature": "5381-",
123+
"affectsGlobalScope": true
124+
}
125+
},
126+
"options": {
127+
"incremental": true,
128+
"watch": true,
129+
"configFilePath": "./tsconfig.json"
130+
},
131+
"referencedMap": {},
132+
"exportedModulesMap": {},
133+
"semanticDiagnosticsPerFile": [
134+
"../../../../a/lib/lib.d.ts",
135+
[
136+
"./index.ts",
137+
[
138+
{
139+
"file": "./index.ts",
140+
"start": 12,
141+
"length": 6,
142+
"messageText": "Cannot find name 'Config'.",
143+
"category": 1,
144+
"code": 2304
145+
}
146+
]
147+
]
148+
]
149+
},
150+
"version": "FakeTSVersion"
151+
}
152+
107153
//// [/users/username/projects/project/globals.d.ts] deleted
108154

109155
Output::
110156
>> Screen clear
111157
[12:00:30 AM] Starting compilation in watch mode...
112158

113159

114-
[12:00:31 AM] Found 0 errors. Watching for file changes.
160+
index.ts:1:13 - error TS2304: Cannot find name 'Config'.
161+
162+
1 console.log(Config.value);
163+
   ~~~~~~
164+
165+
166+
[12:00:37 AM] Found 1 error. Watching for file changes.
115167

116168

117169

@@ -122,6 +174,7 @@ Program files::
122174
/users/username/projects/project/index.ts
123175

124176
Semantic diagnostics in builder refreshed for::
177+
/users/username/projects/project/index.ts
125178

126179
WatchedFiles::
127180
/users/username/projects/project/tsconfig.json:

0 commit comments

Comments
 (0)