Handle empty string when getting file version (#51689)

* Test for empty string change

* Fix empty string for file versioning
This commit is contained in:
Sheetal Nandi 2022-11-30 10:30:32 -08:00 committed by GitHub
parent 86019fa470
commit 16edc29bc9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 96 additions and 22 deletions

View File

@ -1811,7 +1811,7 @@ function getUpToDateStatusWorker(state: SolutionBuilderState, project: ParsedCom
if (!buildInfoVersionMap) buildInfoVersionMap = getBuildInfoFileVersionMap(buildInfoProgram, buildInfoPath!, host);
version = buildInfoVersionMap.get(toPath(state, inputFile));
const text = version ? state.readFileWithCache(inputFile) : undefined;
currentVersion = text && getSourceFileVersionAsHashFromText(host, text);
currentVersion = text !== undefined ? getSourceFileVersionAsHashFromText(host, text) : undefined;
if (version && version === currentVersion) pseudoInputUpToDate = true;
}

View File

@ -715,7 +715,7 @@ export function createWatchProgram<T extends BuilderProgram>(host: WatchCompiler
if (hostSourceFile.version) return hostSourceFile.version;
// Read file and get new version
const text = readFileWithCache(path);
return text ? getSourceFileVersionAsHashFromText(compilerHost, text) : undefined;
return text !== undefined ? getSourceFileVersionAsHashFromText(compilerHost, text) : undefined;
}
function onReleaseOldSourceFile(oldSourceFile: SourceFile, _oldOptions: CompilerOptions, hasSourceFileByPath: boolean) {

View File

@ -717,6 +717,16 @@ describe("unittests:: tsc-watch:: watchEnvironment:: tsc-watch with different po
change: sys => sys.replaceFileText("/user/username/projects/project/main.ts", "Hello", "Hello World"),
timeouts: sys => sys.runQueuedTimeoutCallbacks(),
},
{
caption: "receive another change event without modifying the file",
change: sys => sys.invokeFsWatches("/user/username/projects/project/main.ts", "change", /*modifiedTime*/ undefined, /*useTildeSuffix*/ undefined),
timeouts: sys => sys.runQueuedTimeoutCallbacks(),
},
{
caption: "change main.ts to empty text",
change: sys => sys.writeFile("/user/username/projects/project/main.ts", ""),
timeouts: sys => sys.runQueuedTimeoutCallbacks(),
},
{
caption: "receive another change event without modifying the file",
change: sys => sys.invokeFsWatches("/user/username/projects/project/main.ts", "change", /*modifiedTime*/ undefined, /*useTildeSuffix*/ undefined),

View File

@ -136,26 +136,12 @@ Output::
>> Screen clear
[12:00:36 AM] File change detected. Starting incremental compilation...
[12:00:37 AM] Project 'tsconfig.json' is out of date because output 'tsconfig.tsbuildinfo' is older than input 'a.js'
[12:00:37 AM] Project 'tsconfig.json' is up to date but needs to update timestamps of output files that are older than input files
[12:00:38 AM] Building project '/user/username/projects/myproject/tsconfig.json'...
[12:00:39 AM] Found 0 errors. Watching for file changes.
[12:00:38 AM] Found 0 errors. Watching for file changes.
Program root files: ["/user/username/projects/myproject/a.js","/user/username/projects/myproject/b.ts"]
Program options: {"allowJs":true,"noEmit":true,"watch":true,"incremental":true,"configFilePath":"/user/username/projects/myproject/tsconfig.json"}
Program structureReused: Not
Program files::
/a/lib/lib.d.ts
/user/username/projects/myproject/a.js
/user/username/projects/myproject/b.ts
Semantic diagnostics in builder refreshed for::
No shapes updated in the builder::
PolledWatches::
FsWatches::
@ -182,13 +168,13 @@ const x = 10;
Output::
>> Screen clear
[12:00:43 AM] File change detected. Starting incremental compilation...
[12:00:42 AM] File change detected. Starting incremental compilation...
[12:00:44 AM] Project 'tsconfig.json' is out of date because output 'tsconfig.tsbuildinfo' is older than input 'a.js'
[12:00:43 AM] Project 'tsconfig.json' is out of date because output 'tsconfig.tsbuildinfo' is older than input 'a.js'
[12:00:45 AM] Building project '/user/username/projects/myproject/tsconfig.json'...
[12:00:44 AM] Building project '/user/username/projects/myproject/tsconfig.json'...
[12:00:53 AM] Found 0 errors. Watching for file changes.
[12:00:52 AM] Found 0 errors. Watching for file changes.

View File

@ -121,6 +121,84 @@ var a = "Hello World";
Change:: receive another change event without modifying the file
Input::
Output::
FileWatcher:: Triggered with main.ts 1:: WatchInfo: main.ts 250 undefined Source file
Scheduling update
Elapsed:: *ms FileWatcher:: Triggered with main.ts 1:: WatchInfo: main.ts 250 undefined Source file
Synchronizing program
PolledWatches::
/user/username/projects/project/node_modules/@types:
{"pollingInterval":500}
FsWatches::
/user/username/projects/project/main.ts:
{}
/a/lib/lib.d.ts:
{}
FsWatchesRecursive::
exitCode:: ExitStatus.undefined
Change:: change main.ts to empty text
Input::
//// [/user/username/projects/project/main.ts]
Output::
FileWatcher:: Triggered with main.ts 1:: WatchInfo: main.ts 250 undefined Source file
Scheduling update
Elapsed:: *ms FileWatcher:: Triggered with main.ts 1:: WatchInfo: main.ts 250 undefined Source file
Synchronizing program
[12:00:34 AM] File change detected. Starting incremental compilation...
CreatingProgramWith::
roots: ["main.ts"]
options: {"watch":true,"extendedDiagnostics":true}
[12:00:38 AM] Found 0 errors. Watching for file changes.
Program root files: ["main.ts"]
Program options: {"watch":true,"extendedDiagnostics":true}
Program structureReused: Completely
Program files::
/a/lib/lib.d.ts
main.ts
Semantic diagnostics in builder refreshed for::
main.ts
Shape signatures in builder refreshed for::
/user/username/projects/project/main.ts (used version)
PolledWatches::
/user/username/projects/project/node_modules/@types:
{"pollingInterval":500}
FsWatches::
/user/username/projects/project/main.ts:
{}
/a/lib/lib.d.ts:
{}
FsWatchesRecursive::
exitCode:: ExitStatus.undefined
//// [/user/username/projects/project/main.js]
Change:: receive another change event without modifying the file
Input::