Skip to content

Commit

Permalink
Pass throwIfNoEntry to fs.statSync (#41604) (#44584)
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
DanielRosenwasser and amcasey authored Jun 15, 2021
1 parent aace53f commit 13e7ae7
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
28 changes: 22 additions & 6 deletions src/compiler/sys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1235,8 +1235,8 @@ namespace ts {
},
getFileSize(path) {
try {
const stat = _fs.statSync(path);
if (stat.isFile()) {
const stat = statSync(path);
if (stat?.isFile()) {
return stat.size;
}
}
Expand Down Expand Up @@ -1283,6 +1283,16 @@ namespace ts {
};
return nodeSystem;

/**
* `throwIfNoEntry` was added so recently that it's not in the node types.
* This helper encapsulates the mitigating usage of `any`.
* See https://github.com/nodejs/node/pull/33716
*/
function statSync(path: string): import("fs").Stats | undefined {
// throwIfNoEntry will be ignored by older versions of node
return (_fs as any).statSync(path, { throwIfNoEntry: false });
}

/**
* Uses the builtin inspector APIs to capture a CPU profile
* See https://nodejs.org/api/inspector.html#inspector_example_usage for details
Expand Down Expand Up @@ -1341,7 +1351,7 @@ namespace ts {
activeSession.post("Profiler.stop", (err, { profile }) => {
if (!err) {
try {
if (_fs.statSync(profilePath).isDirectory()) {
if (statSync(profilePath)?.isDirectory()) {
profilePath = _path.join(profilePath, `${(new Date()).toISOString().replace(/:/g, "-")}+P${process.pid}.cpuprofile`);
}
}
Expand Down Expand Up @@ -1631,7 +1641,10 @@ namespace ts {
const name = combinePaths(path, entry);

try {
stat = _fs.statSync(name);
stat = statSync(name);
if (!stat) {
continue;
}
}
catch (e) {
continue;
Expand Down Expand Up @@ -1668,7 +1681,10 @@ namespace ts {
Error.stackTraceLimit = 0;

try {
const stat = _fs.statSync(path);
const stat = statSync(path);
if (!stat) {
return false;
}
switch (entryKind) {
case FileSystemEntryKind.File: return stat.isFile();
case FileSystemEntryKind.Directory: return stat.isDirectory();
Expand Down Expand Up @@ -1706,7 +1722,7 @@ namespace ts {

function getModifiedTime(path: string) {
try {
return _fs.statSync(path).mtime;
return statSync(path)?.mtime;
}
catch (e) {
return undefined;
Expand Down
1 change: 1 addition & 0 deletions src/tsserver/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -675,6 +675,7 @@ namespace ts.server {
return { getModifiedTime, poll, startWatchTimer, addFile, removeFile };

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

Expand Down

0 comments on commit 13e7ae7

Please sign in to comment.