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

fs: reduce throwing unnecessary errors on glob #53632

Merged
merged 1 commit into from
Jul 11, 2024
Merged
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
31 changes: 22 additions & 9 deletions lib/internal/fs/glob.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,30 @@ function lazyMinimatch() {
const isWindows = process.platform === 'win32';
const isOSX = process.platform === 'darwin';

/**
* @param {string} path
* @returns {Promise<DirentFromStats|null>}
*/
async function getDirent(path) {
return new DirentFromStats(basename(path), await lstat(path), path);
let stat;
try {
stat = await lstat(path);
} catch {
return null;
}
return new DirentFromStats(basename(path), stat, path);
}

/**
* @param {string} path
* @returns {DirentFromStats|null}
*/
function getDirentSync(path) {
return new DirentFromStats(basename(path), lstatSync(path), path);
const stat = lstatSync(path, { throwIfNoEntry: false });
if (stat === undefined) {
return null;
}
return new DirentFromStats(basename(path), stat, path);
}

class Cache {
Expand All @@ -56,7 +74,7 @@ class Cache {
if (cached) {
return cached;
}
const promise = PromisePrototypeThen(getDirent(path), null, () => null);
const promise = getDirent(path);
this.#statsCache.set(path, promise);
return promise;
}
Expand All @@ -65,12 +83,7 @@ class Cache {
if (cached) {
return cached;
}
let val;
try {
val = getDirentSync(path);
} catch {
val = null;
}
const val = getDirentSync(path);
this.#statsCache.set(path, val);
return val;
}
Expand Down
Loading