Skip to content

Commit 669305b

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 54f8d38 commit 669305b

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
@@ -1271,8 +1271,8 @@ namespace ts {
12711271
},
12721272
getFileSize(path) {
12731273
try {
1274-
const stat = _fs.statSync(path);
1275-
if (stat.isFile()) {
1274+
const stat = statSync(path);
1275+
if (stat?.isFile()) {
12761276
return stat.size;
12771277
}
12781278
}
@@ -1319,6 +1319,16 @@ namespace ts {
13191319
};
13201320
return nodeSystem;
13211321

1322+
/**
1323+
* `throwIfNoEntry` was added so recently that it's not in the node types.
1324+
* This helper encapsulates the mitigating usage of `any`.
1325+
* See https://github.com/nodejs/node/pull/33716
1326+
*/
1327+
function statSync(path: string): import("fs").Stats | undefined {
1328+
// throwIfNoEntry will be ignored by older versions of node
1329+
return (_fs as any).statSync(path, { throwIfNoEntry: false });
1330+
}
1331+
13221332
/**
13231333
* Uses the builtin inspector APIs to capture a CPU profile
13241334
* See https://nodejs.org/api/inspector.html#inspector_example_usage for details
@@ -1377,7 +1387,7 @@ namespace ts {
13771387
activeSession.post("Profiler.stop", (err, { profile }) => {
13781388
if (!err) {
13791389
try {
1380-
if (_fs.statSync(profilePath).isDirectory()) {
1390+
if (statSync(profilePath)?.isDirectory()) {
13811391
profilePath = _path.join(profilePath, `${(new Date()).toISOString().replace(/:/g, "-")}+P${process.pid}.cpuprofile`);
13821392
}
13831393
}
@@ -1667,7 +1677,10 @@ namespace ts {
16671677
const name = combinePaths(path, entry);
16681678

16691679
try {
1670-
stat = _fs.statSync(name);
1680+
stat = statSync(name);
1681+
if (!stat) {
1682+
continue;
1683+
}
16711684
}
16721685
catch (e) {
16731686
continue;
@@ -1704,7 +1717,10 @@ namespace ts {
17041717
Error.stackTraceLimit = 0;
17051718

17061719
try {
1707-
const stat = _fs.statSync(path);
1720+
const stat = statSync(path);
1721+
if (!stat) {
1722+
return false;
1723+
}
17081724
switch (entryKind) {
17091725
case FileSystemEntryKind.File: return stat.isFile();
17101726
case FileSystemEntryKind.Directory: return stat.isDirectory();
@@ -1742,7 +1758,7 @@ namespace ts {
17421758

17431759
function getModifiedTime(path: string) {
17441760
try {
1745-
return _fs.statSync(path).mtime;
1761+
return statSync(path)?.mtime;
17461762
}
17471763
catch (e) {
17481764
return undefined;

src/tsserver/server.ts

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

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

0 commit comments

Comments
 (0)