Skip to content

Commit 16edc29

Browse files
authored
Handle empty string when getting file version (#51689)
* Test for empty string change * Fix empty string for file versioning
1 parent 86019fa commit 16edc29

File tree

5 files changed

+96
-22
lines changed

5 files changed

+96
-22
lines changed

src/compiler/tsbuildPublic.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1811,7 +1811,7 @@ function getUpToDateStatusWorker(state: SolutionBuilderState, project: ParsedCom
18111811
if (!buildInfoVersionMap) buildInfoVersionMap = getBuildInfoFileVersionMap(buildInfoProgram, buildInfoPath!, host);
18121812
version = buildInfoVersionMap.get(toPath(state, inputFile));
18131813
const text = version ? state.readFileWithCache(inputFile) : undefined;
1814-
currentVersion = text && getSourceFileVersionAsHashFromText(host, text);
1814+
currentVersion = text !== undefined ? getSourceFileVersionAsHashFromText(host, text) : undefined;
18151815
if (version && version === currentVersion) pseudoInputUpToDate = true;
18161816
}
18171817

src/compiler/watchPublic.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -715,7 +715,7 @@ export function createWatchProgram<T extends BuilderProgram>(host: WatchCompiler
715715
if (hostSourceFile.version) return hostSourceFile.version;
716716
// Read file and get new version
717717
const text = readFileWithCache(path);
718-
return text ? getSourceFileVersionAsHashFromText(compilerHost, text) : undefined;
718+
return text !== undefined ? getSourceFileVersionAsHashFromText(compilerHost, text) : undefined;
719719
}
720720

721721
function onReleaseOldSourceFile(oldSourceFile: SourceFile, _oldOptions: CompilerOptions, hasSourceFileByPath: boolean) {

src/testRunner/unittests/tscWatch/watchEnvironment.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -717,6 +717,16 @@ describe("unittests:: tsc-watch:: watchEnvironment:: tsc-watch with different po
717717
change: sys => sys.replaceFileText("/user/username/projects/project/main.ts", "Hello", "Hello World"),
718718
timeouts: sys => sys.runQueuedTimeoutCallbacks(),
719719
},
720+
{
721+
caption: "receive another change event without modifying the file",
722+
change: sys => sys.invokeFsWatches("/user/username/projects/project/main.ts", "change", /*modifiedTime*/ undefined, /*useTildeSuffix*/ undefined),
723+
timeouts: sys => sys.runQueuedTimeoutCallbacks(),
724+
},
725+
{
726+
caption: "change main.ts to empty text",
727+
change: sys => sys.writeFile("/user/username/projects/project/main.ts", ""),
728+
timeouts: sys => sys.runQueuedTimeoutCallbacks(),
729+
},
720730
{
721731
caption: "receive another change event without modifying the file",
722732
change: sys => sys.invokeFsWatches("/user/username/projects/project/main.ts", "change", /*modifiedTime*/ undefined, /*useTildeSuffix*/ undefined),

tests/baselines/reference/tsbuildWatch/noEmit/does-not-go-in-loop-when-watching-when-no-files-are-emitted-with-incremental.js

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -136,25 +136,11 @@ Output::
136136
>> Screen clear
137137
[12:00:36 AM] File change detected. Starting incremental compilation...
138138

139-
[[90m12:00:37 AM[0m] Project 'tsconfig.json' is out of date because output 'tsconfig.tsbuildinfo' is older than input 'a.js'
139+
[[90m12:00:37 AM[0m] Project 'tsconfig.json' is up to date but needs to update timestamps of output files that are older than input files
140140

141-
[[90m12:00:38 AM[0m] Building project '/user/username/projects/myproject/tsconfig.json'...
141+
[[90m12:00:38 AM[0m] Found 0 errors. Watching for file changes.
142142

143-
[12:00:39 AM] Found 0 errors. Watching for file changes.
144-
145-
146-
147-
Program root files: ["/user/username/projects/myproject/a.js","/user/username/projects/myproject/b.ts"]
148-
Program options: {"allowJs":true,"noEmit":true,"watch":true,"incremental":true,"configFilePath":"/user/username/projects/myproject/tsconfig.json"}
149-
Program structureReused: Not
150-
Program files::
151-
/a/lib/lib.d.ts
152-
/user/username/projects/myproject/a.js
153-
/user/username/projects/myproject/b.ts
154-
155-
Semantic diagnostics in builder refreshed for::
156143

157-
No shapes updated in the builder::
158144

159145
PolledWatches::
160146

@@ -182,13 +168,13 @@ const x = 10;
182168

183169
Output::
184170
>> Screen clear
185-
[[90m12:00:43 AM[0m] File change detected. Starting incremental compilation...
171+
[[90m12:00:42 AM[0m] File change detected. Starting incremental compilation...
186172

187-
[[90m12:00:44 AM[0m] Project 'tsconfig.json' is out of date because output 'tsconfig.tsbuildinfo' is older than input 'a.js'
173+
[[90m12:00:43 AM[0m] Project 'tsconfig.json' is out of date because output 'tsconfig.tsbuildinfo' is older than input 'a.js'
188174

189-
[[90m12:00:45 AM[0m] Building project '/user/username/projects/myproject/tsconfig.json'...
175+
[[90m12:00:44 AM[0m] Building project '/user/username/projects/myproject/tsconfig.json'...
190176

191-
[[90m12:00:53 AM[0m] Found 0 errors. Watching for file changes.
177+
[[90m12:00:52 AM[0m] Found 0 errors. Watching for file changes.
192178

193179

194180

tests/baselines/reference/tscWatch/watchEnvironment/fsEvent-for-change-is-repeated.js

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,84 @@ var a = "Hello World";
121121

122122

123123

124+
Change:: receive another change event without modifying the file
125+
126+
Input::
127+
128+
Output::
129+
FileWatcher:: Triggered with main.ts 1:: WatchInfo: main.ts 250 undefined Source file
130+
Scheduling update
131+
Elapsed:: *ms FileWatcher:: Triggered with main.ts 1:: WatchInfo: main.ts 250 undefined Source file
132+
Synchronizing program
133+
134+
135+
PolledWatches::
136+
/user/username/projects/project/node_modules/@types:
137+
{"pollingInterval":500}
138+
139+
FsWatches::
140+
/user/username/projects/project/main.ts:
141+
{}
142+
/a/lib/lib.d.ts:
143+
{}
144+
145+
FsWatchesRecursive::
146+
147+
exitCode:: ExitStatus.undefined
148+
149+
150+
Change:: change main.ts to empty text
151+
152+
Input::
153+
//// [/user/username/projects/project/main.ts]
154+
155+
156+
157+
Output::
158+
FileWatcher:: Triggered with main.ts 1:: WatchInfo: main.ts 250 undefined Source file
159+
Scheduling update
160+
Elapsed:: *ms FileWatcher:: Triggered with main.ts 1:: WatchInfo: main.ts 250 undefined Source file
161+
Synchronizing program
162+
[12:00:34 AM] File change detected. Starting incremental compilation...
163+
164+
CreatingProgramWith::
165+
roots: ["main.ts"]
166+
options: {"watch":true,"extendedDiagnostics":true}
167+
[12:00:38 AM] Found 0 errors. Watching for file changes.
168+
169+
170+
171+
Program root files: ["main.ts"]
172+
Program options: {"watch":true,"extendedDiagnostics":true}
173+
Program structureReused: Completely
174+
Program files::
175+
/a/lib/lib.d.ts
176+
main.ts
177+
178+
Semantic diagnostics in builder refreshed for::
179+
main.ts
180+
181+
Shape signatures in builder refreshed for::
182+
/user/username/projects/project/main.ts (used version)
183+
184+
PolledWatches::
185+
/user/username/projects/project/node_modules/@types:
186+
{"pollingInterval":500}
187+
188+
FsWatches::
189+
/user/username/projects/project/main.ts:
190+
{}
191+
/a/lib/lib.d.ts:
192+
{}
193+
194+
FsWatchesRecursive::
195+
196+
exitCode:: ExitStatus.undefined
197+
198+
//// [/user/username/projects/project/main.js]
199+
200+
201+
124202
Change:: receive another change event without modifying the file
125203

126204
Input::

0 commit comments

Comments
 (0)