Skip to content

Commit 533ff93

Browse files
authored
Merge pull request #401 from tschaub/exists
Ignore errors in fs.exists
2 parents 76de528 + 9e4f1b2 commit 533ff93

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

lib/binding.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1485,7 +1485,14 @@ Binding.prototype.exists = function (filepath, callback, ctx) {
14851485

14861486
return maybeCallback(normalizeCallback(callback), ctx, this, function () {
14871487
filepath = deBuffer(filepath);
1488-
const item = this._system.getItem(filepath);
1488+
let item;
1489+
try {
1490+
item = this._system.getItem(filepath);
1491+
} catch {
1492+
// ignore errors
1493+
// see https://github.com/nodejs/node/blob/v22.11.0/lib/fs.js#L255-L257
1494+
return false;
1495+
}
14891496

14901497
if (item) {
14911498
if (item instanceof SymbolicLink) {

test/lib/fs.exists.spec.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,13 @@ describe('fs.exists(path, callback)', function () {
7676
done();
7777
});
7878
});
79+
80+
it('calls with false for bogus path (III)', function (done) {
81+
fs.exists(path.join('path', 'to', 'a.bin', 'foo'), function (exists) {
82+
assert.isFalse(exists);
83+
done();
84+
});
85+
});
7986
});
8087

8188
describe('fs.existsSync(path)', function () {
@@ -123,4 +130,8 @@ describe('fs.existsSync(path)', function () {
123130
it('returns false for bogus path (II)', function () {
124131
assert.isFalse(fs.existsSync(path.join('nested', 'dir', 'none')));
125132
});
133+
134+
it('returns false for a path beyond a file', function () {
135+
assert.isFalse(fs.existsSync(path.join('path', 'to', 'a.bin', 'foo')));
136+
});
126137
});

0 commit comments

Comments
 (0)