Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass throwIfNoEntry to fs.statSync #41604

Merged
merged 1 commit into from
Nov 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 22 additions & 6 deletions src/compiler/sys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1271,8 +1271,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 @@ -1319,6 +1319,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 });
amcasey marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* 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 @@ -1377,7 +1387,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 @@ -1667,7 +1677,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 @@ -1704,7 +1717,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 @@ -1742,7 +1758,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 @@ -678,6 +678,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