Skip to content

Commit 892978f

Browse files
authored
Pass throwIfNoEntry to fs.statSync (#41604)
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
1 parent 9b778b3 commit 892978f

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
@@ -1217,8 +1217,8 @@ namespace ts {
12171217
},
12181218
getFileSize(path) {
12191219
try {
1220-
const stat = _fs.statSync(path);
1221-
if (stat.isFile()) {
1220+
const stat = statSync(path);
1221+
if (stat?.isFile()) {
12221222
return stat.size;
12231223
}
12241224
}
@@ -1265,6 +1265,16 @@ namespace ts {
12651265
};
12661266
return nodeSystem;
12671267

1268+
/**
1269+
* `throwIfNoEntry` was added so recently that it's not in the node types.
1270+
* This helper encapsulates the mitigating usage of `any`.
1271+
* See https://github.com./nodejs/node/pull/33716
1272+
*/
1273+
function statSync(path: string): import("fs").Stats | undefined {
1274+
// throwIfNoEntry will be ignored by older versions of node
1275+
return (_fs as any).statSync(path, { throwIfNoEntry: false });
1276+
}
1277+
12681278
/**
12691279
* Uses the builtin inspector APIs to capture a CPU profile
12701280
* See https://nodejs.org/api/inspector.html#inspector_example_usage for details
@@ -1323,7 +1333,7 @@ namespace ts {
13231333
activeSession.post("Profiler.stop", (err, { profile }) => {
13241334
if (!err) {
13251335
try {
1326-
if (_fs.statSync(profilePath).isDirectory()) {
1336+
if (statSync(profilePath)?.isDirectory()) {
13271337
profilePath = _path.join(profilePath, `${(new Date()).toISOString().replace(/:/g, "-")}+P${process.pid}.cpuprofile`);
13281338
}
13291339
}
@@ -1613,7 +1623,10 @@ namespace ts {
16131623
const name = combinePaths(path, entry);
16141624

16151625
try {
1616-
stat = _fs.statSync(name);
1626+
stat = statSync(name);
1627+
if (!stat) {
1628+
continue;
1629+
}
16171630
}
16181631
catch (e) {
16191632
continue;
@@ -1645,7 +1658,10 @@ namespace ts {
16451658

16461659
function fileSystemEntryExists(path: string, entryKind: FileSystemEntryKind): boolean {
16471660
try {
1648-
const stat = _fs.statSync(path);
1661+
const stat = statSync(path);
1662+
if (!stat) {
1663+
return false;
1664+
}
16491665
switch (entryKind) {
16501666
case FileSystemEntryKind.File: return stat.isFile();
16511667
case FileSystemEntryKind.Directory: return stat.isDirectory();
@@ -1680,7 +1696,7 @@ namespace ts {
16801696

16811697
function getModifiedTime(path: string) {
16821698
try {
1683-
return _fs.statSync(path).mtime;
1699+
return statSync(path)?.mtime;
16841700
}
16851701
catch (e) {
16861702
return undefined;

src/tsserver/server.ts

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

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

0 commit comments

Comments
 (0)