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
2 changes: 1 addition & 1 deletion lib/internal/fs/glob.js
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,7 @@ class Glob {

if (this.#exclude) {
if (this.#withFileTypes) {
const stat = this.#cache.statSync(path);
const stat = this.#cache.statSync(fullpath);
if (stat !== null) {
if (this.#exclude(stat)) {
return;
Expand Down
35 changes: 34 additions & 1 deletion test/parallel/test-fs-glob.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as common from '../common/index.mjs';
import tmpdir from '../common/tmpdir.js';
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';
import { glob, globSync, Dirent, chmodSync, mkdirSync, writeFileSync, rmSync } from 'node:fs';
import { test, describe } from 'node:test';
import { pathToFileURL } from 'node:url';
import { promisify } from 'node:util';
Expand Down Expand Up @@ -423,6 +423,39 @@ describe('globSync - withFileTypes', function() {
assert.deepStrictEqual(actual.map(normalizeDirent).sort(), expected.filter(Boolean).map(normalizePath).sort());
});
}

test('exclude receives the root dirent from cwd when cwd differs from process.cwd()', () => {
const cwdMismatchDir = tmpdir.resolve('cwd-mismatch-root-exclude');
const ambientDir = resolve(cwdMismatchDir, 'ambient');
const rootDir = resolve(cwdMismatchDir, 'root');

rmSync(cwdMismatchDir, { recursive: true, force: true });
mkdirSync(ambientDir, { recursive: true });
mkdirSync(resolve(rootDir, 'a'), { recursive: true });
writeFileSync(resolve(ambientDir, 'a'), 'shadow-file');
writeFileSync(resolve(rootDir, 'a', 'real.txt'), 'real');

const originalCwd = process.cwd();
process.chdir(ambientDir);

try {
const actual = globSync('a/**', {
cwd: rootDir,
withFileTypes: true,
exclude: common.mustCall((dirent) => {
assert.ok(dirent instanceof Dirent);
assert.strictEqual(dirent.name, 'a');
assert.strictEqual(dirent.parentPath, rootDir);
assert.ok(dirent.isDirectory());
return true;
}),
});
assert.deepStrictEqual(actual, []);
} finally {
process.chdir(originalCwd);
rmSync(cwdMismatchDir, { recursive: true, force: true });
}
});
});

describe('fsPromises glob - withFileTypes', function() {
Expand Down
Loading