Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit bacba16

Browse filesBrowse files
Han5991aduh95
authored andcommitted
fs: fix ENOTDIR in globSync when file is treated as dir
`fs.globSync` failed with `ENOTDIR` when a path component in a glob pattern was a file but used as a directory (e.g., 'foo{,/bar}' when 'foo' is a file). This change aligns `getDirentSync` with the asynchronous `getDirent` by wrapping the `lstatSync` call in a `try-catch` block to safely return `null` on such errors. Fixes: #61257 PR-URL: #61259 Reviewed-By: René <contact.9a5d6388@renegade334.me.uk>
1 parent 2423ecd commit bacba16
Copy full SHA for bacba16

2 files changed

+23-3Lines changed: 23 additions & 3 deletions

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎lib/internal/fs/glob.js‎

Copy file name to clipboardExpand all lines: lib/internal/fs/glob.js
+4-2Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,10 @@ async function getDirent(path) {
6565
* @returns {DirentFromStats|null}
6666
*/
6767
function getDirentSync(path) {
68-
const stat = lstatSync(path, { throwIfNoEntry: false });
69-
if (stat === undefined) {
68+
let stat;
69+
try {
70+
stat = lstatSync(path);
71+
} catch {
7072
return null;
7173
}
7274
return new DirentFromStats(basename(path), stat, dirname(path));
Collapse file

‎test/parallel/test-fs-glob.mjs‎

Copy file name to clipboardExpand all lines: test/parallel/test-fs-glob.mjs
+19-1Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as common from '../common/index.mjs';
22
import tmpdir from '../common/tmpdir.js';
33
import { resolve, dirname, sep, relative, join, isAbsolute } from 'node:path';
44
import { mkdir, writeFile, symlink, glob as asyncGlob } from 'node:fs/promises';
5-
import { glob, globSync, Dirent, chmodSync } from 'node:fs';
5+
import { glob, globSync, Dirent, chmodSync, writeFileSync, rmSync } from 'node:fs';
66
import { test, describe } from 'node:test';
77
import { pathToFileURL } from 'node:url';
88
import { promisify } from 'node:util';
@@ -543,3 +543,21 @@ describe('glob - with restricted directory', function() {
543543
}
544544
});
545545
});
546+
547+
describe('globSync - ENOTDIR', function() {
548+
test('should return empty array when a file is treated as a directory', () => {
549+
const file = tmpdir.resolve('foo');
550+
writeFileSync(file, '');
551+
try {
552+
const pattern = 'foo{,/bar}';
553+
const actual = globSync(pattern, { cwd: tmpdir.path }).sort();
554+
assert.deepStrictEqual(actual, ['foo']);
555+
} finally {
556+
try {
557+
rmSync(file);
558+
} catch {
559+
// ignore
560+
}
561+
}
562+
});
563+
});

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.