Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions src/glob/glob_walker.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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".") {
Expand Down
60 changes: 60 additions & 0 deletions test/parallel/test-fs-glob-windows-no-drive-letter.mjs
Original file line number Diff line number Diff line change
@@ -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')]);
});
}
12 changes: 12 additions & 0 deletions test/parallel/test-fs-glob.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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`,
Expand Down