GeezerVison is showing github.com — fs: fix ENOTDIR in globSync when file is treated as dir · nodejs/node@bacba16
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: lib/internal/fs/glob.js +4-2Lines changed: 4 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -65,8 +65,10 @@ async function getDirent(path) { | 65 | 65 | * @returns {DirentFromStats|null} | 66 | 66 | */ | 67 | 67 | 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 { | 70 | 72 | return null; | 71 | 73 | } | 72 | 74 | return new DirentFromStats(basename(path), stat, dirname(path)); | |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-fs-glob.mjs +19-1Lines changed: 19 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -2,7 +2,7 @@ import * as common from '../common/index.mjs'; | 2 | 2 | import tmpdir from '../common/tmpdir.js'; | 3 | 3 | import { resolve, dirname, sep, relative, join, isAbsolute } from 'node:path'; | 4 | 4 | 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'; | 6 | 6 | import { test, describe } from 'node:test'; | 7 | 7 | import { pathToFileURL } from 'node:url'; | 8 | 8 | import { promisify } from 'node:util'; | @@ -543,3 +543,21 @@ describe('glob - with restricted directory', function() { | 543 | 543 | } | 544 | 544 | }); | 545 | 545 | }); | | 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 | +}); |
|