Skip to content

Commit 35eff19

Browse files
committed
Merge pull request microsoft#7039 from Microsoft/fixFileExists
fix 'fileExists' check by using stat directly
2 parents 20f7b18 + b18cbc6 commit 35eff19

1 file changed

Lines changed: 29 additions & 7 deletions

File tree

src/compiler/sys.ts

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,7 @@ namespace ts {
411411
const useCaseSensitiveFileNames = platform !== "win32" && platform !== "win64" && platform !== "darwin";
412412

413413
function readFile(fileName: string, encoding?: string): string {
414-
if (!_fs.existsSync(fileName)) {
414+
if (!fileExists(fileName)) {
415415
return undefined;
416416
}
417417
const buffer = _fs.readFileSync(fileName);
@@ -462,6 +462,32 @@ namespace ts {
462462
return useCaseSensitiveFileNames ? path : path.toLowerCase();
463463
}
464464

465+
const enum FileSystemEntryKind {
466+
File,
467+
Directory
468+
}
469+
470+
function fileSystemEntryExists(path: string, entryKind: FileSystemEntryKind): boolean {
471+
try {
472+
const stat = _fs.statSync(path);
473+
switch (entryKind) {
474+
case FileSystemEntryKind.File: return stat.isFile();
475+
case FileSystemEntryKind.Directory: return stat.isDirectory();
476+
}
477+
}
478+
catch (e) {
479+
return false;
480+
}
481+
}
482+
483+
function fileExists(path: string): boolean {
484+
return fileSystemEntryExists(path, FileSystemEntryKind.File);
485+
}
486+
487+
function directoryExists(path: string): boolean {
488+
return fileSystemEntryExists(path, FileSystemEntryKind.Directory);
489+
}
490+
465491
function readDirectory(path: string, extension?: string, exclude?: string[]): string[] {
466492
const result: string[] = [];
467493
exclude = map(exclude, s => getCanonicalPath(combinePaths(path, s)));
@@ -538,12 +564,8 @@ namespace ts {
538564
resolvePath: function (path: string): string {
539565
return _path.resolve(path);
540566
},
541-
fileExists(path: string): boolean {
542-
return _fs.existsSync(path);
543-
},
544-
directoryExists(path: string) {
545-
return _fs.existsSync(path) && _fs.statSync(path).isDirectory();
546-
},
567+
fileExists,
568+
directoryExists,
547569
createDirectory(directoryName: string) {
548570
if (!this.directoryExists(directoryName)) {
549571
_fs.mkdirSync(directoryName);

0 commit comments

Comments
 (0)