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

Ignore errors in fs.exists #401

Merged
merged 1 commit into from
Oct 31, 2024
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
9 changes: 8 additions & 1 deletion lib/binding.js
Original file line number Diff line number Diff line change
Expand Up @@ -1485,7 +1485,14 @@ Binding.prototype.exists = function (filepath, callback, ctx) {

return maybeCallback(normalizeCallback(callback), ctx, this, function () {
filepath = deBuffer(filepath);
const item = this._system.getItem(filepath);
let item;
try {
item = this._system.getItem(filepath);
} catch {
// ignore errors
// see https://github.com/nodejs/node/blob/v22.11.0/lib/fs.js#L255-L257
return false;
}

if (item) {
if (item instanceof SymbolicLink) {
Expand Down
11 changes: 11 additions & 0 deletions test/lib/fs.exists.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,13 @@ describe('fs.exists(path, callback)', function () {
done();
});
});

it('calls with false for bogus path (III)', function (done) {
fs.exists(path.join('path', 'to', 'a.bin', 'foo'), function (exists) {
assert.isFalse(exists);
done();
});
});
});

describe('fs.existsSync(path)', function () {
Expand Down Expand Up @@ -123,4 +130,8 @@ describe('fs.existsSync(path)', function () {
it('returns false for bogus path (II)', function () {
assert.isFalse(fs.existsSync(path.join('nested', 'dir', 'none')));
});

it('returns false for a path beyond a file', function () {
assert.isFalse(fs.existsSync(path.join('path', 'to', 'a.bin', 'foo')));
});
});