diff --git a/src/glob/glob_walker.cc b/src/glob/glob_walker.cc index dda6e7c1c1f2..0ebe29cd0ac1 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,13 +1463,20 @@ 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"..") { 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-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')]); + }); +} 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`,