Skip to content

Commit ec8eeff

Browse files
committed
Merge pull request #7658 from zhengbli/fixCaseSensitivity
Use fileName instead of Path when dealing with file systems directly
2 parents 730f189 + e9b514c commit ec8eeff

File tree

2 files changed

+51
-55
lines changed

2 files changed

+51
-55
lines changed

src/compiler/sys.ts

Lines changed: 49 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/// <reference path="core.ts"/>
22

33
namespace ts {
4-
export type FileWatcherCallback = (path: string, removed?: boolean) => void;
5-
export type DirectoryWatcherCallback = (path: string) => void;
4+
export type FileWatcherCallback = (fileName: string, removed?: boolean) => void;
5+
export type DirectoryWatcherCallback = (directoryName: string) => void;
66

77
export interface System {
88
args: string[];
@@ -11,7 +11,7 @@ namespace ts {
1111
write(s: string): void;
1212
readFile(path: string, encoding?: string): string;
1313
writeFile(path: string, data: string, writeByteOrderMark?: boolean): void;
14-
watchFile?(path: Path, callback: FileWatcherCallback): FileWatcher;
14+
watchFile?(path: string, callback: FileWatcherCallback): FileWatcher;
1515
watchDirectory?(path: string, callback: DirectoryWatcherCallback, recursive?: boolean): FileWatcher;
1616
resolvePath(path: string): string;
1717
fileExists(path: string): boolean;
@@ -25,7 +25,7 @@ namespace ts {
2525
}
2626

2727
interface WatchedFile {
28-
filePath: Path;
28+
fileName: string;
2929
callback: FileWatcherCallback;
3030
mtime?: Date;
3131
}
@@ -35,7 +35,7 @@ namespace ts {
3535
}
3636

3737
export interface DirectoryWatcher extends FileWatcher {
38-
directoryPath: Path;
38+
directoryName: string;
3939
referenceCount: number;
4040
}
4141

@@ -244,13 +244,13 @@ namespace ts {
244244
return;
245245
}
246246

247-
_fs.stat(watchedFile.filePath, (err: any, stats: any) => {
247+
_fs.stat(watchedFile.fileName, (err: any, stats: any) => {
248248
if (err) {
249-
watchedFile.callback(watchedFile.filePath);
249+
watchedFile.callback(watchedFile.fileName);
250250
}
251251
else if (watchedFile.mtime.getTime() !== stats.mtime.getTime()) {
252-
watchedFile.mtime = getModifiedTime(watchedFile.filePath);
253-
watchedFile.callback(watchedFile.filePath, watchedFile.mtime.getTime() === 0);
252+
watchedFile.mtime = getModifiedTime(watchedFile.fileName);
253+
watchedFile.callback(watchedFile.fileName, watchedFile.mtime.getTime() === 0);
254254
}
255255
});
256256
}
@@ -278,11 +278,11 @@ namespace ts {
278278
}, interval);
279279
}
280280

281-
function addFile(filePath: Path, callback: FileWatcherCallback): WatchedFile {
281+
function addFile(fileName: string, callback: FileWatcherCallback): WatchedFile {
282282
const file: WatchedFile = {
283-
filePath,
283+
fileName,
284284
callback,
285-
mtime: getModifiedTime(filePath)
285+
mtime: getModifiedTime(fileName)
286286
};
287287

288288
watchedFiles.push(file);
@@ -306,26 +306,26 @@ namespace ts {
306306
}
307307

308308
function createWatchedFileSet() {
309-
const dirWatchers = createFileMap<DirectoryWatcher>();
309+
const dirWatchers: Map<DirectoryWatcher> = {};
310310
// One file can have multiple watchers
311-
const fileWatcherCallbacks = createFileMap<FileWatcherCallback[]>();
311+
const fileWatcherCallbacks: Map<FileWatcherCallback[]> = {};
312312
return { addFile, removeFile };
313313

314-
function reduceDirWatcherRefCountForFile(filePath: Path) {
315-
const dirPath = getDirectoryPath(filePath);
316-
if (dirWatchers.contains(dirPath)) {
317-
const watcher = dirWatchers.get(dirPath);
314+
function reduceDirWatcherRefCountForFile(fileName: string) {
315+
const dirName = getDirectoryPath(fileName);
316+
if (hasProperty(dirWatchers, dirName)) {
317+
const watcher = dirWatchers[dirName];
318318
watcher.referenceCount -= 1;
319319
if (watcher.referenceCount <= 0) {
320320
watcher.close();
321-
dirWatchers.remove(dirPath);
321+
delete dirWatchers[dirName];
322322
}
323323
}
324324
}
325325

326-
function addDirWatcher(dirPath: Path): void {
327-
if (dirWatchers.contains(dirPath)) {
328-
const watcher = dirWatchers.get(dirPath);
326+
function addDirWatcher(dirPath: string): void {
327+
if (hasProperty(dirWatchers, dirPath)) {
328+
const watcher = dirWatchers[dirPath];
329329
watcher.referenceCount += 1;
330330
return;
331331
}
@@ -336,52 +336,52 @@ namespace ts {
336336
(eventName: string, relativeFileName: string) => fileEventHandler(eventName, relativeFileName, dirPath)
337337
);
338338
watcher.referenceCount = 1;
339-
dirWatchers.set(dirPath, watcher);
339+
dirWatchers[dirPath] = watcher;
340340
return;
341341
}
342342

343-
function addFileWatcherCallback(filePath: Path, callback: FileWatcherCallback): void {
344-
if (fileWatcherCallbacks.contains(filePath)) {
345-
fileWatcherCallbacks.get(filePath).push(callback);
343+
function addFileWatcherCallback(filePath: string, callback: FileWatcherCallback): void {
344+
if (hasProperty(fileWatcherCallbacks, filePath)) {
345+
fileWatcherCallbacks[filePath].push(callback);
346346
}
347347
else {
348-
fileWatcherCallbacks.set(filePath, [callback]);
348+
fileWatcherCallbacks[filePath] = [callback];
349349
}
350350
}
351351

352-
function addFile(filePath: Path, callback: FileWatcherCallback): WatchedFile {
353-
addFileWatcherCallback(filePath, callback);
354-
addDirWatcher(getDirectoryPath(filePath));
352+
function addFile(fileName: string, callback: FileWatcherCallback): WatchedFile {
353+
addFileWatcherCallback(fileName, callback);
354+
addDirWatcher(getDirectoryPath(fileName));
355355

356-
return { filePath, callback };
356+
return { fileName, callback };
357357
}
358358

359359
function removeFile(watchedFile: WatchedFile) {
360-
removeFileWatcherCallback(watchedFile.filePath, watchedFile.callback);
361-
reduceDirWatcherRefCountForFile(watchedFile.filePath);
360+
removeFileWatcherCallback(watchedFile.fileName, watchedFile.callback);
361+
reduceDirWatcherRefCountForFile(watchedFile.fileName);
362362
}
363363

364-
function removeFileWatcherCallback(filePath: Path, callback: FileWatcherCallback) {
365-
if (fileWatcherCallbacks.contains(filePath)) {
366-
const newCallbacks = copyListRemovingItem(callback, fileWatcherCallbacks.get(filePath));
364+
function removeFileWatcherCallback(filePath: string, callback: FileWatcherCallback) {
365+
if (hasProperty(fileWatcherCallbacks, filePath)) {
366+
const newCallbacks = copyListRemovingItem(callback, fileWatcherCallbacks[filePath]);
367367
if (newCallbacks.length === 0) {
368-
fileWatcherCallbacks.remove(filePath);
368+
delete fileWatcherCallbacks[filePath];
369369
}
370370
else {
371-
fileWatcherCallbacks.set(filePath, newCallbacks);
371+
fileWatcherCallbacks[filePath] = newCallbacks;
372372
}
373373
}
374374
}
375375

376-
function fileEventHandler(eventName: string, relativeFileName: string, baseDirPath: Path) {
376+
function fileEventHandler(eventName: string, relativeFileName: string, baseDirPath: string) {
377377
// When files are deleted from disk, the triggered "rename" event would have a relativefileName of "undefined"
378-
const filePath = typeof relativeFileName !== "string"
378+
const fileName = typeof relativeFileName !== "string"
379379
? undefined
380-
: toPath(relativeFileName, baseDirPath, createGetCanonicalFileName(sys.useCaseSensitiveFileNames));
380+
: ts.getNormalizedAbsolutePath(relativeFileName, baseDirPath);
381381
// Some applications save a working file via rename operations
382-
if ((eventName === "change" || eventName === "rename") && fileWatcherCallbacks.contains(filePath)) {
383-
for (const fileCallback of fileWatcherCallbacks.get(filePath)) {
384-
fileCallback(filePath);
382+
if ((eventName === "change" || eventName === "rename") && hasProperty(fileWatcherCallbacks, fileName)) {
383+
for (const fileCallback of fileWatcherCallbacks[fileName]) {
384+
fileCallback(fileName);
385385
}
386386
}
387387
}
@@ -526,18 +526,18 @@ namespace ts {
526526
},
527527
readFile,
528528
writeFile,
529-
watchFile: (filePath, callback) => {
529+
watchFile: (fileName, callback) => {
530530
// Node 4.0 stabilized the `fs.watch` function on Windows which avoids polling
531531
// and is more efficient than `fs.watchFile` (ref: https://github.com/nodejs/node/pull/2649
532532
// and https://github.com/Microsoft/TypeScript/issues/4643), therefore
533533
// if the current node.js version is newer than 4, use `fs.watch` instead.
534534
const watchSet = isNode4OrLater() ? watchedFileSet : pollingWatchedFileSet;
535-
const watchedFile = watchSet.addFile(filePath, callback);
535+
const watchedFile = watchSet.addFile(fileName, callback);
536536
return {
537537
close: () => watchSet.removeFile(watchedFile)
538538
};
539539
},
540-
watchDirectory: (path, callback, recursive) => {
540+
watchDirectory: (directoryName, callback, recursive) => {
541541
// Node 4.0 `fs.watch` function supports the "recursive" option on both OSX and Windows
542542
// (ref: https://github.com/nodejs/node/pull/2649 and https://github.com/Microsoft/TypeScript/issues/4643)
543543
let options: any;
@@ -549,15 +549,15 @@ namespace ts {
549549
}
550550

551551
return _fs.watch(
552-
path,
552+
directoryName,
553553
options,
554554
(eventName: string, relativeFileName: string) => {
555555
// In watchDirectory we only care about adding and removing files (when event name is
556556
// "rename"); changes made within files are handled by corresponding fileWatchers (when
557557
// event name is "change")
558558
if (eventName === "rename") {
559559
// When deleting a file, the passed baseFileName is null
560-
callback(!relativeFileName ? relativeFileName : normalizePath(combinePaths(path, relativeFileName)));
560+
callback(!relativeFileName ? relativeFileName : normalizePath(combinePaths(directoryName, relativeFileName)));
561561
};
562562
}
563563
);

src/server/editorServices.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1006,9 +1006,7 @@ namespace ts.server {
10061006
info.setFormatOptions(this.getFormatCodeOptions());
10071007
this.filenameToScriptInfo[fileName] = info;
10081008
if (!info.isOpen) {
1009-
info.fileWatcher = this.host.watchFile(
1010-
toPath(fileName, fileName, createGetCanonicalFileName(sys.useCaseSensitiveFileNames)),
1011-
_ => { this.watchedFileChanged(fileName); });
1009+
info.fileWatcher = this.host.watchFile(fileName, _ => { this.watchedFileChanged(fileName); });
10121010
}
10131011
}
10141012
}
@@ -1227,9 +1225,7 @@ namespace ts.server {
12271225
}
12281226
}
12291227
project.finishGraph();
1230-
project.projectFileWatcher = this.host.watchFile(
1231-
toPath(configFilename, configFilename, createGetCanonicalFileName(sys.useCaseSensitiveFileNames)),
1232-
_ => this.watchedProjectConfigFileChanged(project));
1228+
project.projectFileWatcher = this.host.watchFile(configFilename, _ => this.watchedProjectConfigFileChanged(project));
12331229
this.log("Add recursive watcher for: " + ts.getDirectoryPath(configFilename));
12341230
project.directoryWatcher = this.host.watchDirectory(
12351231
ts.getDirectoryPath(configFilename),

0 commit comments

Comments
 (0)