Skip to content

Commit

Permalink
Merge pull request #401 from tschaub/exists
Browse files Browse the repository at this point in the history
Ignore errors in fs.exists
  • Loading branch information
tschaub authored Oct 31, 2024
2 parents 76de528 + 9e4f1b2 commit 533ff93
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 533ff93

Please sign in to comment.