From a3102bd358da97610bd29018e4615cbb6640c118 Mon Sep 17 00:00:00 2001 From: Surafel Workayehu Date: Wed, 25 Nov 2026 16:28:52 +0300 Subject: [PATCH 1/2] fs: fix glob for drive-less absolute patterns On Windows, an absolute glob pattern without a drive letter, such as '\Users\me\*.js' or '/Users/me/*.js', returned no results as soon as a literal segment followed the root. The walker looks up a literal child with Join(fullpath, name). For the drive-less root '\' that built '\\Users', which Normalize() keeps as a UNC prefix, so the lookup failed. path.win32.join() returns '\Users' here. Skip the separator in Join() when the first part already ends with one. Also queue the root with the platform separator: JoinEntry() appends child names right after it, so a '/' leaked into results ('/Users\me'). Signed-off-by: Surafel Workayehu --- src/glob/glob_walker.cc | 11 +++- .../test-fs-glob-windows-no-drive-letter.mjs | 60 +++++++++++++++++++ 2 files changed, 69 insertions(+), 2 deletions(-) create mode 100644 test/parallel/test-fs-glob-windows-no-drive-letter.mjs diff --git a/src/glob/glob_walker.cc b/src/glob/glob_walker.cc index dda6e7c1c1f2..d80a898e668a 100644 --- a/src/glob/glob_walker.cc +++ b/src/glob/glob_walker.cc @@ -86,7 +86,9 @@ std::string Join(std::string_view a, std::string_view b) { if (a.empty()) return b.empty() ? std::string(".") : Normalize(b); if (b.empty()) return Normalize(a); std::string joined(a); - joined += kWindows ? '\\' : '/'; + // Joining onto a root such as '\' must not produce '\\b', which + // Normalize() keeps as a UNC prefix. path.win32.join() avoids it too. + if (!IsPathSeparator(joined.back())) joined += kWindows ? '\\' : '/'; joined += b; return Normalize(joined); } @@ -1461,7 +1463,12 @@ class WalkerState { if (is_first && IsLiteral(first_part) && first_part->literal.empty()) { IndexSet next; next.Add(1); - ctx.AddChildRedirect("/", pattern.Child(std::move(next))); + // Use the platform separator: JoinEntry() appends child names right + // after a trailing one, so a '/' here would leak into Windows results + // ('/a\b'). The previous JS implementation built child paths with + // path.join(), which normalized it. + ctx.AddChildRedirect(kWindows ? "\\" : "/", + pattern.Child(std::move(next))); return; } if (is_first && IsLiteral(first_part) && first_part->literal == u"..") { diff --git a/test/parallel/test-fs-glob-windows-no-drive-letter.mjs b/test/parallel/test-fs-glob-windows-no-drive-letter.mjs new file mode 100644 index 000000000000..06019e4c922a --- /dev/null +++ b/test/parallel/test-fs-glob-windows-no-drive-letter.mjs @@ -0,0 +1,60 @@ +// On Windows, an absolute glob pattern without a drive letter, such as +// '\foo\*' or '/foo/*', is rooted at the current drive, like +// path.resolve('\\foo'). It must find the same entries as the pattern with +// the drive letter, and results must use '\' throughout. + +import * as common from '../common/index.mjs'; +import tmpdir from '../common/tmpdir.js'; +import assert from 'node:assert'; +import { glob, globSync, mkdirSync, writeFileSync } from 'node:fs'; +import { glob as asyncGlob } from 'node:fs/promises'; +import { join } from 'node:path'; +import { test } from 'node:test'; +import { promisify } from 'node:util'; + +if (!common.isWindows) { + common.skip('this test is Windows-specific.'); +} + +tmpdir.refresh(); + +if (!/^[A-Za-z]:\\/.test(tmpdir.path)) { + common.skip('tmpdir is not on a drive letter'); +} + +// Make the fixture's drive the current one. +process.chdir(tmpdir.path); + +const dir = tmpdir.resolve('no-drive'); +mkdirSync(dir); +writeFileSync(join(dir, 'a.js'), ''); +writeFileSync(join(dir, 'b.js'), ''); + +// 'C:\...\no-drive' -> '\...\no-drive' +const noDrive = dir.slice(2); +const expected = [join(noDrive, 'a.js'), join(noDrive, 'b.js')]; + +const apis = [ + ['glob', promisify(glob)], + ['globSync', globSync], + ['fsPromises glob', (pattern) => Array.fromAsync(asyncGlob(pattern))], +]; + +for (const [name, runGlob] of apis) { + test(`${name} - backslashes`, async () => { + const actual = await runGlob(`${noDrive}\\*.js`); + assert.deepStrictEqual(actual.sort(), expected); + }); + + test(`${name} - forward slashes`, async () => { + const actual = await runGlob(`${noDrive.replaceAll('\\', '/')}/*.js`); + assert.deepStrictEqual(actual.sort(), expected); + }); + + // A literal path is looked up directly instead of through a directory + // listing. + test(`${name} - literal path`, async () => { + const actual = await runGlob(join(noDrive, 'a.js')); + assert.deepStrictEqual(actual, [join(noDrive, 'a.js')]); + }); +} From de27c5c6770e43fea197f8a7d00f142ac592ddbf Mon Sep 17 00:00:00 2001 From: Surafel Workayehu Date: Wed, 25 Nov 2026 16:30:31 +0300 Subject: [PATCH 2/2] fs: fix mixed separators in glob '..' results On Windows, a glob pattern starting with '..' returned paths such as '../sib\a.js'. The walker queued the parent directory as '../', and JoinEntry() appends child names right after a trailing separator, so the '/' stayed in every result. The previous JS implementation built child paths with path.join(), which normalized it. Use the platform separator, as for the drive-less root. POSIX output is unchanged. Signed-off-by: Surafel Workayehu --- src/glob/glob_walker.cc | 4 +++- test/parallel/test-fs-glob.mjs | 12 ++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/glob/glob_walker.cc b/src/glob/glob_walker.cc index d80a898e668a..0ebe29cd0ac1 100644 --- a/src/glob/glob_walker.cc +++ b/src/glob/glob_walker.cc @@ -1474,7 +1474,9 @@ class WalkerState { if (is_first && IsLiteral(first_part) && first_part->literal == u"..") { IndexSet next; next.Add(1); - ctx.AddChildRedirect("../", pattern.Child(std::move(next))); + // Platform separator, as above ('../a\b' otherwise). + ctx.AddChildRedirect(kWindows ? "..\\" : "../", + pattern.Child(std::move(next))); return; } if (is_first && IsLiteral(first_part) && first_part->literal == u".") { diff --git a/test/parallel/test-fs-glob.mjs b/test/parallel/test-fs-glob.mjs index ed7317508087..c337dba8c2ba 100644 --- a/test/parallel/test-fs-glob.mjs +++ b/test/parallel/test-fs-glob.mjs @@ -223,6 +223,18 @@ const patterns = { 'a/symlink/a/b/c/a/b/c/a/b/c/a/b/c/a/b/c/a/b/c/a/b', 'a/symlink/a/b/c/a/b/c/a/b/c/a/b/c/a/b/c/a/b/c/a/b/c', ], + // Leading '..' walks up from cwd; results must use the platform separator + // throughout, not only after the first segment. + '../abs/*': [ + '../abs/asdf', + '../abs/bar', + '../abs/baz', + '../abs/foo', + '../abs/quux', + '../abs/qwer', + '../abs/rewq', + ], + '../abs/foo': ['../abs/foo'], [`{./*/*,${absDir}/*}`]: [ `${absDir}/asdf`, `${absDir}/bar`,