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
6 changes: 0 additions & 6 deletions lib/internal/fs/glob.js
Original file line number Diff line number Diff line change
Expand Up @@ -435,9 +435,6 @@ class Glob {
const nSymlinks = new SafeSet();
for (const index of pattern.indexes) {
// For each child, check potential patterns
if (this.#cache.seen(entryPath, pattern, index) || this.#cache.seen(entryPath, pattern, index + 1)) {
return;
}
const current = pattern.at(index);
const nextIndex = index + 1;
const next = pattern.at(nextIndex);
Expand Down Expand Up @@ -645,9 +642,6 @@ class Glob {
const nSymlinks = new SafeSet();
for (const index of pattern.indexes) {
// For each child, check potential patterns
if (this.#cache.seen(entryPath, pattern, index) || this.#cache.seen(entryPath, pattern, index + 1)) {
return;
}
const current = pattern.at(index);
const nextIndex = index + 1;
const next = pattern.at(nextIndex);
Expand Down
71 changes: 71 additions & 0 deletions test/parallel/test-fs-glob.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as common from '../common/index.mjs';
import tmpdir from '../common/tmpdir.js';
import { spawnSync } from 'node:child_process';
import { resolve, dirname, sep, relative, join, isAbsolute } from 'node:path';
import { mkdir, writeFile, symlink, glob as asyncGlob } from 'node:fs/promises';
import { glob, globSync, Dirent, chmodSync, writeFileSync, rmSync } from 'node:fs';
Expand Down Expand Up @@ -377,6 +378,76 @@ describe('fsPromises.glob - with file: URL as cwd', function() {
}
});

test('glob handles seen children without aborting sibling processing', () => {
const script = `
const assert = require('node:assert');
const fs = require('node:fs');
const fsPromises = require('node:fs/promises');
const os = require('node:os');
const path = require('node:path');

const cwd = fs.mkdtempSync(path.join(os.tmpdir(), 'glob-seen-'));
const a = path.join(cwd, 'a');
fs.mkdirSync(path.join(a, 'b', 'c', 'd'), { recursive: true });
fs.mkdirSync(path.join(a, 'c', 'd', 'c'), { recursive: true });
fs.writeFileSync(path.join(a, 'x'), '');
fs.writeFileSync(path.join(a, 'z'), '');

const originalReaddirSync = fs.readdirSync;
const originalReaddir = fsPromises.readdir;

const reorder = (target, entries) => {
if (!Array.isArray(entries)) return entries;
if (target === a) {
const names = ['c', 'b', 'x', 'z'];
return names.map((name) => entries.find((entry) => entry.name === name)).filter(Boolean);
}
if (target === path.join(a, 'c', 'd')) {
const names = ['c'];
return names.map((name) => entries.find((entry) => entry.name === name)).filter(Boolean);
}
return entries;
};

fs.readdirSync = function(target, options) {
return reorder(target, originalReaddirSync.call(this, target, options));
};
fsPromises.readdir = async function(target, options) {
return reorder(target, await originalReaddir.call(this, target, options));
};

const { Glob } = require('internal/fs/glob');
const expected = ['a/b', 'a/c', 'a/x', 'a/z'];

(async () => {
const syncResults = new Glob('a/**/../*', { cwd }).globSync().sort();
for (const item of expected) {
assert.ok(syncResults.includes(item), \`missing \${item} from \${syncResults}\`);
}

const asyncResults = [];
for await (const item of new Glob('a/**/../*', { cwd }).glob()) asyncResults.push(item);
asyncResults.sort();
for (const item of expected) {
assert.ok(asyncResults.includes(item), \`missing \${item} from \${asyncResults}\`);
}

fs.rmSync(cwd, { recursive: true, force: true });
})().catch((err) => {
console.error(err);
fs.rmSync(cwd, { recursive: true, force: true });
process.exitCode = 1;
});
`;

const child = spawnSync(process.execPath, ['--expose-internals', '-e', script], {
cwd: fixtureDir,
encoding: 'utf8',
});

assert.strictEqual(child.status, 0, child.stderr || child.stdout);
});

const normalizeDirent = (dirent) => relative(fixtureDir, join(dirent.parentPath, dirent.name));
// The call to `join()` with only one argument is important, as
// it ensures that the proper path seperators are applied.
Expand Down
Loading