Skip to content

Commit e0f7793

Browse files
committed
Guard realpath usage against use on nonexistant paths
1 parent 5423501 commit e0f7793

File tree

1 file changed

+5
-0
lines changed

1 file changed

+5
-0
lines changed

src/compiler/sys.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -780,6 +780,7 @@ namespace ts {
780780
export const enum FileSystemEntryKind {
781781
File,
782782
Directory,
783+
Any,
783784
}
784785

785786
/*@internal*/
@@ -1821,6 +1822,7 @@ namespace ts {
18211822
switch (entryKind) {
18221823
case FileSystemEntryKind.File: return stat.isFile();
18231824
case FileSystemEntryKind.Directory: return stat.isDirectory();
1825+
case FileSystemEntryKind.Any: return true;
18241826
default: return false;
18251827
}
18261828
}
@@ -1845,6 +1847,9 @@ namespace ts {
18451847
}
18461848

18471849
function realpath(path: string): string {
1850+
// `realpathSync` is slow on non-existent files, since it throws an exception - guard against calls on paths that don't exist with the
1851+
// more optimized exists call (which attempts to avoid throwing an error if the runtime supports it)
1852+
if (!fileSystemEntryExists(path, FileSystemEntryKind.Any)) return path;
18481853
try {
18491854
return realpathSync(path);
18501855
}

0 commit comments

Comments
 (0)