Skip to content

Commit

Permalink
Handle empty string when getting file version (#51689)
Browse files Browse the repository at this point in the history
* Test for empty string change

* Fix empty string for file versioning
  • Loading branch information
sheetalkamat committed Nov 30, 2022
1 parent 86019fa commit 16edc29
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 22 deletions.
2 changes: 1 addition & 1 deletion src/compiler/tsbuildPublic.ts
Expand Up @@ -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;
}

Expand Down
2 changes: 1 addition & 1 deletion src/compiler/watchPublic.ts
Expand Up @@ -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) {
Expand Down
10 changes: 10 additions & 0 deletions src/testRunner/unittests/tscWatch/watchEnvironment.ts
Expand Up @@ -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),
Expand Down
Expand Up @@ -136,25 +136,11 @@ Output::
>> Screen clear
[12:00:36 AM] File change detected. Starting incremental compilation...

[[90m12:00:37 AM[0m] Project 'tsconfig.json' is out of date because output 'tsconfig.tsbuildinfo' is older than input 'a.js'
[[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

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

[12:00:39 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::

Expand Down Expand Up @@ -182,13 +168,13 @@ const x = 10;

Output::
>> Screen clear
[[90m12:00:43 AM[0m] File change detected. Starting incremental compilation...
[[90m12:00:42 AM[0m] File change detected. Starting incremental compilation...

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

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

[[90m12:00:53 AM[0m] Found 0 errors. Watching for file changes.
[[90m12:00:52 AM[0m] Found 0 errors. Watching for file changes.



Expand Down
Expand Up @@ -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::
Expand Down

0 comments on commit 16edc29

Please sign in to comment.