Skip to content

Commit

Permalink
Ignore errors in fs.exists
Browse files Browse the repository at this point in the history
  • Loading branch information
tschaub committed Oct 31, 2024
1 parent 76de528 commit 9e4f1b2
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
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')));
});
});

0 comments on commit 9e4f1b2

Please sign in to comment.