Skip to content

Commit

Permalink
Pass throwIfNoEntry to fs.statSync (#41604)
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
  • Loading branch information
amcasey authored Jun 14, 2021
1 parent 9b778b3 commit 892978f
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 @@ -1217,8 +1217,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 @@ -1265,6 +1265,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 @@ -1323,7 +1333,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 @@ -1613,7 +1623,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 @@ -1645,7 +1658,10 @@ namespace ts {

function fileSystemEntryExists(path: string, entryKind: FileSystemEntryKind): boolean {
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 @@ -1680,7 +1696,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 @@ -679,6 +679,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 892978f

Please sign in to comment.