Skip to content

Guard realpath usage against use on nonexistant paths #46208

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/compiler/sys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -780,6 +780,7 @@ namespace ts {
export const enum FileSystemEntryKind {
File,
Directory,
Any,
}

/*@internal*/
Expand Down Expand Up @@ -1821,6 +1822,7 @@ namespace ts {
switch (entryKind) {
case FileSystemEntryKind.File: return stat.isFile();
case FileSystemEntryKind.Directory: return stat.isDirectory();
case FileSystemEntryKind.Any: return true;
default: return false;
}
}
Expand All @@ -1845,6 +1847,9 @@ namespace ts {
}

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