From 1855a0758056082176fb36d5be5fa490df8835f4 Mon Sep 17 00:00:00 2001 From: "shyyko.serhiy@gmail.com" Date: Wed, 8 Jul 2015 13:26:44 +0300 Subject: [PATCH 1/2] Fix issue https://github.com/Microsoft/TypeScript/issues/3277 --- src/compiler/sys.ts | 11 ++++++++--- src/compiler/tsc.ts | 16 +++++++++++----- src/server/server.ts | 20 ++++++++++---------- 3 files changed, 29 insertions(+), 18 deletions(-) diff --git a/src/compiler/sys.ts b/src/compiler/sys.ts index a47d6959ba8..d5619163647 100644 --- a/src/compiler/sys.ts +++ b/src/compiler/sys.ts @@ -8,7 +8,7 @@ namespace ts { write(s: string): void; readFile(path: string, encoding?: string): string; writeFile(path: string, data: string, writeByteOrderMark?: boolean): void; - watchFile?(path: string, callback: (path: string) => void): FileWatcher; + watchFile?(path: string, callback: (path: string, removed: boolean) => void): FileWatcher; resolvePath(path: string): string; fileExists(path: string): boolean; directoryExists(path: string): boolean; @@ -282,11 +282,16 @@ namespace ts { }; function fileChanged(curr: any, prev: any) { + // mtime.getTime() equals 0 if file was removed + if (curr.mtime.getTime() === 0) { + callback(fileName, true); + return; + } if (+curr.mtime <= +prev.mtime) { return; } - callback(fileName); + callback(fileName, false); }; }, resolvePath: function (path: string): string { @@ -331,4 +336,4 @@ namespace ts { return undefined; // Unsupported host } })(); -} \ No newline at end of file +} diff --git a/src/compiler/tsc.ts b/src/compiler/tsc.ts index 2a01774fbca..d0ad9c0c438 100644 --- a/src/compiler/tsc.ts +++ b/src/compiler/tsc.ts @@ -82,7 +82,7 @@ namespace ts { function reportDiagnostic(diagnostic: Diagnostic) { var output = ""; - + if (diagnostic.file) { var loc = getLineAndCharacterOfPosition(diagnostic.file, diagnostic.start); @@ -257,7 +257,7 @@ namespace ts { var sourceFile = hostGetSourceFile(fileName, languageVersion, onError); if (sourceFile && compilerOptions.watch) { // Attach a file watcher - sourceFile.fileWatcher = sys.watchFile(sourceFile.fileName, () => sourceFileChanged(sourceFile)); + sourceFile.fileWatcher = sys.watchFile(sourceFile.fileName, (fileName, removed) => sourceFileChanged(sourceFile, removed)); } return sourceFile; } @@ -279,9 +279,15 @@ namespace ts { } // If a source file changes, mark it as unwatched and start the recompilation timer - function sourceFileChanged(sourceFile: SourceFile) { + function sourceFileChanged(sourceFile: SourceFile, removed: boolean) { sourceFile.fileWatcher.close(); sourceFile.fileWatcher = undefined; + if (removed){ + var index = rootFileNames.indexOf(sourceFile.fileName); + if (index !== -1){ + rootFileNames.splice(index, 1); + } + } startTimer(); } @@ -354,11 +360,11 @@ namespace ts { return { program, exitStatus }; function compileProgram(): ExitStatus { - // First get any syntactic errors. + // First get any syntactic errors. var diagnostics = program.getSyntacticDiagnostics(); reportDiagnostics(diagnostics); - // If we didn't have any syntactic errors, then also try getting the global and + // If we didn't have any syntactic errors, then also try getting the global and // semantic errors. if (diagnostics.length === 0) { var diagnostics = program.getGlobalDiagnostics(); diff --git a/src/server/server.ts b/src/server/server.ts index 843197b918a..842995cbad8 100644 --- a/src/server/server.ts +++ b/src/server/server.ts @@ -11,7 +11,7 @@ namespace ts.server { input: process.stdin, output: process.stdout, terminal: false, - }); + }); class Logger implements ts.server.Logger { fd = -1; @@ -58,7 +58,7 @@ namespace ts.server { isVerbose() { return this.loggingEnabled() && (this.level == "verbose"); } - + msg(s: string, type = "Err") { if (this.fd < 0) { @@ -85,7 +85,7 @@ namespace ts.server { interface WatchedFile { fileName: string; - callback: (fileName: string) => void; + callback: (fileName: string, removed: boolean) => void; mtime: Date; } @@ -121,11 +121,11 @@ namespace ts.server { fs.stat(watchedFile.fileName,(err, stats) => { if (err) { - watchedFile.callback(watchedFile.fileName); + watchedFile.callback(watchedFile.fileName, false); } else if (watchedFile.mtime.getTime() !== stats.mtime.getTime()) { watchedFile.mtime = WatchedFileSet.getModifiedTime(watchedFile.fileName); - watchedFile.callback(watchedFile.fileName); + watchedFile.callback(watchedFile.fileName, watchedFile.mtime.getTime() === 0); } }); } @@ -153,7 +153,7 @@ namespace ts.server { }, this.interval); } - addFile(fileName: string, callback: (fileName: string) => void ): WatchedFile { + addFile(fileName: string, callback: (fileName: string, removed: boolean) => void ): WatchedFile { var file: WatchedFile = { fileName, callback, @@ -170,7 +170,7 @@ namespace ts.server { removeFile(file: WatchedFile) { this.watchedFiles = WatchedFileSet.copyListRemovingItem(file, this.watchedFiles); } - } + } class IOSession extends Session { constructor(host: ServerHost, logger: ts.server.Logger) { @@ -243,11 +243,11 @@ namespace ts.server { // TODO: check that this location is writable var logger = createLoggerFromEnv(); - + // REVIEW: for now this implementation uses polling. // The advantage of polling is that it works reliably // on all os and with network mounted files. - // For 90 referenced files, the average time to detect + // For 90 referenced files, the average time to detect // changes is 2*msInterval (by default 5 seconds). // The overhead of this is .04 percent (1/2500) with // average pause of < 1 millisecond (and max @@ -271,4 +271,4 @@ namespace ts.server { }); // Start listening ioSession.listen(); -} \ No newline at end of file +} From 30888da8fa212ecc59d01fd9999e2edce57917cc Mon Sep 17 00:00:00 2001 From: "shyyko.serhiy@gmail.com" Date: Thu, 9 Jul 2015 13:12:50 +0300 Subject: [PATCH 2/2] fixed indent issues --- src/compiler/sys.ts | 4 ++-- src/compiler/tsc.ts | 4 ++-- src/server/server.ts | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/compiler/sys.ts b/src/compiler/sys.ts index d5619163647..10586d0d9b3 100644 --- a/src/compiler/sys.ts +++ b/src/compiler/sys.ts @@ -284,14 +284,14 @@ namespace ts { function fileChanged(curr: any, prev: any) { // mtime.getTime() equals 0 if file was removed if (curr.mtime.getTime() === 0) { - callback(fileName, true); + callback(fileName, /* removed */ true); return; } if (+curr.mtime <= +prev.mtime) { return; } - callback(fileName, false); + callback(fileName, /* removed */ false); }; }, resolvePath: function (path: string): string { diff --git a/src/compiler/tsc.ts b/src/compiler/tsc.ts index d0ad9c0c438..5f008eda2c2 100644 --- a/src/compiler/tsc.ts +++ b/src/compiler/tsc.ts @@ -282,9 +282,9 @@ namespace ts { function sourceFileChanged(sourceFile: SourceFile, removed: boolean) { sourceFile.fileWatcher.close(); sourceFile.fileWatcher = undefined; - if (removed){ + if (removed) { var index = rootFileNames.indexOf(sourceFile.fileName); - if (index !== -1){ + if (index >= 0) { rootFileNames.splice(index, 1); } } diff --git a/src/server/server.ts b/src/server/server.ts index 842995cbad8..c3942b01a39 100644 --- a/src/server/server.ts +++ b/src/server/server.ts @@ -121,7 +121,7 @@ namespace ts.server { fs.stat(watchedFile.fileName,(err, stats) => { if (err) { - watchedFile.callback(watchedFile.fileName, false); + watchedFile.callback(watchedFile.fileName, /* removed */ false); } else if (watchedFile.mtime.getTime() !== stats.mtime.getTime()) { watchedFile.mtime = WatchedFileSet.getModifiedTime(watchedFile.fileName);