diff --git a/lib/internal/fs/glob.js b/lib/internal/fs/glob.js index 1bfa39150e5196..526efd4c010d7c 100644 --- a/lib/internal/fs/glob.js +++ b/lib/internal/fs/glob.js @@ -65,8 +65,10 @@ async function getDirent(path) { * @returns {DirentFromStats|null} */ function getDirentSync(path) { - const stat = lstatSync(path, { throwIfNoEntry: false }); - if (stat === undefined) { + let stat; + try { + stat = lstatSync(path); + } catch { return null; } return new DirentFromStats(basename(path), stat, dirname(path)); diff --git a/test/parallel/test-fs-glob.mjs b/test/parallel/test-fs-glob.mjs index 54523219cc2d97..74791deba373e3 100644 --- a/test/parallel/test-fs-glob.mjs +++ b/test/parallel/test-fs-glob.mjs @@ -2,7 +2,7 @@ import * as common from '../common/index.mjs'; import tmpdir from '../common/tmpdir.js'; import { resolve, dirname, sep, relative, join, isAbsolute } from 'node:path'; import { mkdir, writeFile, symlink, glob as asyncGlob } from 'node:fs/promises'; -import { glob, globSync, Dirent, chmodSync } from 'node:fs'; +import { glob, globSync, Dirent, chmodSync, writeFileSync, rmSync } from 'node:fs'; import { test, describe } from 'node:test'; import { pathToFileURL } from 'node:url'; import { promisify } from 'node:util'; @@ -543,3 +543,21 @@ describe('glob - with restricted directory', function() { } }); }); + +describe('globSync - ENOTDIR', function() { + test('should return empty array when a file is treated as a directory', () => { + const file = tmpdir.resolve('foo'); + writeFileSync(file, ''); + try { + const pattern = 'foo{,/bar}'; + const actual = globSync(pattern, { cwd: tmpdir.path }).sort(); + assert.deepStrictEqual(actual, ['foo']); + } finally { + try { + rmSync(file); + } catch { + // ignore + } + } + }); +});