Skip to content

Commit 72eaadc

Browse files
Pass throwIfNoEntry to fs.statSync (#41604) (#44583)
Future versions of node will be able to return undefined, rather than allocating and throwing an exception, when a file is not found. See nodejs/node#33716 Co-authored-by: Andrew Casey <[email protected]>
1 parent 13651e8 commit 72eaadc

File tree

2 files changed

+23
-6
lines changed

2 files changed

+23
-6
lines changed

src/compiler/sys.ts

+22-6
Original file line numberDiff line numberDiff line change
@@ -1235,8 +1235,8 @@ namespace ts {
12351235
},
12361236
getFileSize(path) {
12371237
try {
1238-
const stat = _fs.statSync(path);
1239-
if (stat.isFile()) {
1238+
const stat = statSync(path);
1239+
if (stat?.isFile()) {
12401240
return stat.size;
12411241
}
12421242
}
@@ -1283,6 +1283,16 @@ namespace ts {
12831283
};
12841284
return nodeSystem;
12851285

1286+
/**
1287+
* `throwIfNoEntry` was added so recently that it's not in the node types.
1288+
* This helper encapsulates the mitigating usage of `any`.
1289+
* See https://github.com/nodejs/node/pull/33716
1290+
*/
1291+
function statSync(path: string): import("fs").Stats | undefined {
1292+
// throwIfNoEntry will be ignored by older versions of node
1293+
return (_fs as any).statSync(path, { throwIfNoEntry: false });
1294+
}
1295+
12861296
/**
12871297
* Uses the builtin inspector APIs to capture a CPU profile
12881298
* See https://nodejs.org/api/inspector.html#inspector_example_usage for details
@@ -1341,7 +1351,7 @@ namespace ts {
13411351
activeSession.post("Profiler.stop", (err, { profile }) => {
13421352
if (!err) {
13431353
try {
1344-
if (_fs.statSync(profilePath).isDirectory()) {
1354+
if (statSync(profilePath)?.isDirectory()) {
13451355
profilePath = _path.join(profilePath, `${(new Date()).toISOString().replace(/:/g, "-")}+P${process.pid}.cpuprofile`);
13461356
}
13471357
}
@@ -1631,7 +1641,10 @@ namespace ts {
16311641
const name = combinePaths(path, entry);
16321642

16331643
try {
1634-
stat = _fs.statSync(name);
1644+
stat = statSync(name);
1645+
if (!stat) {
1646+
continue;
1647+
}
16351648
}
16361649
catch (e) {
16371650
continue;
@@ -1668,7 +1681,10 @@ namespace ts {
16681681
Error.stackTraceLimit = 0;
16691682

16701683
try {
1671-
const stat = _fs.statSync(path);
1684+
const stat = statSync(path);
1685+
if (!stat) {
1686+
return false;
1687+
}
16721688
switch (entryKind) {
16731689
case FileSystemEntryKind.File: return stat.isFile();
16741690
case FileSystemEntryKind.Directory: return stat.isDirectory();
@@ -1706,7 +1722,7 @@ namespace ts {
17061722

17071723
function getModifiedTime(path: string) {
17081724
try {
1709-
return _fs.statSync(path).mtime;
1725+
return statSync(path)?.mtime;
17101726
}
17111727
catch (e) {
17121728
return undefined;

src/tsserver/server.ts

+1
Original file line numberDiff line numberDiff line change
@@ -680,6 +680,7 @@ namespace ts.server {
680680
return { getModifiedTime, poll, startWatchTimer, addFile, removeFile };
681681

682682
function getModifiedTime(fileName: string): Date {
683+
// Caller guarantees that `fileName` exists, so there'd be no benefit from throwIfNoEntry
683684
return fs.statSync(fileName).mtime;
684685
}
685686

0 commit comments

Comments
 (0)