Skip to content

Commit 2074657

Browse files
authored
Simplify statSync (#59276)
1 parent 5119230 commit 2074657

File tree

1 file changed

+29
-62
lines changed

1 file changed

+29
-62
lines changed

src/compiler/sys.ts

Lines changed: 29 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1484,6 +1484,8 @@ export let sys: System = (() => {
14841484
const isMacOs = process.platform === "darwin";
14851485
const isLinuxOrMacOs = process.platform === "linux" || isMacOs;
14861486

1487+
const statSyncOptions = { throwIfNoEntry: false } as const;
1488+
14871489
const platform: string = _os.platform();
14881490
const useCaseSensitiveFileNames = isFileSystemCaseSensitive();
14891491
const fsRealpath = !!_fs.realpathSync.native ? process.platform === "win32" ? fsRealPathHandlingLongPath : _fs.realpathSync.native : _fs.realpathSync;
@@ -1576,13 +1578,10 @@ export let sys: System = (() => {
15761578
return process.memoryUsage().heapUsed;
15771579
},
15781580
getFileSize(path) {
1579-
try {
1580-
const stat = statSync(path);
1581-
if (stat?.isFile()) {
1582-
return stat.size;
1583-
}
1581+
const stat = statSync(path);
1582+
if (stat?.isFile()) {
1583+
return stat.size;
15841584
}
1585-
catch { /*ignore*/ }
15861585
return 0;
15871586
},
15881587
exit(exitCode?: number): void {
@@ -1626,14 +1625,17 @@ export let sys: System = (() => {
16261625
};
16271626
return nodeSystem;
16281627

1629-
/**
1630-
* `throwIfNoEntry` was added so recently that it's not in the node types.
1631-
* This helper encapsulates the mitigating usage of `any`.
1632-
* See https://github.com/nodejs/node/pull/33716
1633-
*/
1628+
/** Calls fs.statSync, returning undefined if any errors are thrown */
16341629
function statSync(path: string): import("fs").Stats | undefined {
1635-
// throwIfNoEntry will be ignored by older versions of node
1636-
return (_fs as any).statSync(path, { throwIfNoEntry: false });
1630+
// throwIfNoEntry is available in Node 14.17 and above, which matches our supported range.
1631+
try {
1632+
return _fs.statSync(path, statSyncOptions);
1633+
}
1634+
catch {
1635+
// This should never happen as we are passing throwIfNoEntry: false,
1636+
// but guard against this just in case (e.g. a polyfill doesn't check this flag).
1637+
return undefined;
1638+
}
16371639
}
16381640

16391641
/**
@@ -1693,13 +1695,8 @@ export let sys: System = (() => {
16931695
const s = activeSession;
16941696
activeSession.post("Profiler.stop", (err, { profile }) => {
16951697
if (!err) {
1696-
try {
1697-
if (statSync(profilePath)?.isDirectory()) {
1698-
profilePath = _path.join(profilePath, `${(new Date()).toISOString().replace(/:/g, "-")}+P${process.pid}.cpuprofile`);
1699-
}
1700-
}
1701-
catch {
1702-
// do nothing and ignore fallible fs operation
1698+
if (statSync(profilePath)?.isDirectory()) {
1699+
profilePath = _path.join(profilePath, `${(new Date()).toISOString().replace(/:/g, "-")}+P${process.pid}.cpuprofile`);
17031700
}
17041701
try {
17051702
_fs.mkdirSync(_path.dirname(profilePath), { recursive: true });
@@ -1857,13 +1854,8 @@ export let sys: System = (() => {
18571854
if (typeof dirent === "string" || dirent.isSymbolicLink()) {
18581855
const name = combinePaths(path, entry);
18591856

1860-
try {
1861-
stat = statSync(name);
1862-
if (!stat) {
1863-
continue;
1864-
}
1865-
}
1866-
catch {
1857+
stat = statSync(name);
1858+
if (!stat) {
18671859
continue;
18681860
}
18691861
}
@@ -1892,30 +1884,17 @@ export let sys: System = (() => {
18921884
}
18931885

18941886
function fileSystemEntryExists(path: string, entryKind: FileSystemEntryKind): boolean {
1895-
// Since the error thrown by fs.statSync isn't used, we can avoid collecting a stack trace to improve
1896-
// the CPU time performance.
1897-
const originalStackTraceLimit = Error.stackTraceLimit;
1898-
Error.stackTraceLimit = 0;
1899-
1900-
try {
1901-
const stat = statSync(path);
1902-
if (!stat) {
1903-
return false;
1904-
}
1905-
switch (entryKind) {
1906-
case FileSystemEntryKind.File:
1907-
return stat.isFile();
1908-
case FileSystemEntryKind.Directory:
1909-
return stat.isDirectory();
1910-
default:
1911-
return false;
1912-
}
1913-
}
1914-
catch {
1887+
const stat = statSync(path);
1888+
if (!stat) {
19151889
return false;
19161890
}
1917-
finally {
1918-
Error.stackTraceLimit = originalStackTraceLimit;
1891+
switch (entryKind) {
1892+
case FileSystemEntryKind.File:
1893+
return stat.isFile();
1894+
case FileSystemEntryKind.Directory:
1895+
return stat.isDirectory();
1896+
default:
1897+
return false;
19191898
}
19201899
}
19211900

@@ -1945,19 +1924,7 @@ export let sys: System = (() => {
19451924
}
19461925

19471926
function getModifiedTime(path: string) {
1948-
// Since the error thrown by fs.statSync isn't used, we can avoid collecting a stack trace to improve
1949-
// the CPU time performance.
1950-
const originalStackTraceLimit = Error.stackTraceLimit;
1951-
Error.stackTraceLimit = 0;
1952-
try {
1953-
return statSync(path)?.mtime;
1954-
}
1955-
catch {
1956-
return undefined;
1957-
}
1958-
finally {
1959-
Error.stackTraceLimit = originalStackTraceLimit;
1960-
}
1927+
return statSync(path)?.mtime;
19611928
}
19621929

19631930
function setModifiedTime(path: string, time: Date) {

0 commit comments

Comments
 (0)