nodejs / nodejs/node

fs.globSync() uses process.cwd() when building the root exclude Dirent with withFileTypes

未关闭
#63,535 1 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

stale
主要语言
JavaScript
星标
122k
派生
37.4k
平均合并
4 天 3 小时
30 天内合并 PR
272

描述

Version

v25.8.2

Platform
Darwin arm64
Subsystem

fs

What steps will reproduce the bug?
const { mkdtempSync, mkdirSync, writeFileSync, globSync } = require('node:fs');
const { tmpdir } = require('node:os');
const { join } = require('node:path');
const { chdir, cwd } = require('node:process');

const base = mkdtempSync(join(tmpdir(), 'glob-root-'));
const ambient = join(base, 'ambient');
const root = join(base, 'root');

mkdirSync(ambient, { recursive: true });
mkdirSync(join(root, 'a'), { recursive: true });
writeFileSync(join(ambient, 'a'), 'shadow-file');
writeFileSync(join(root, 'a', 'real.txt'), 'real');

chdir(ambient);

const seen = [];
const result = globSync('a/**', {
  cwd: root,
  withFileTypes: true,
  exclude: (dirent) => {
    seen.push({
      name: dirent.name,
      parentPath: dirent.parentPath,
      isDirectory: dirent.isDirectory(),
      isFile: dirent.isFile(),
    });
    return dirent.isDirectory();
  },
});

console.log(JSON.stringify({
  processCwd: cwd(),
  globCwd: root,
  seen,
  result: result.map((dirent) => ({
    name: dirent.name,
    parentPath: dirent.parentPath,
    isDirectory: dirent.isDirectory(),
    isFile: dirent.isFile(),
  })),
}, null, 2));
How often does it reproduce? Is there a required condition?

It reproduces consistently in globSync() when all of the following are true:

  • withFileTypes: true
  • cwd !== process.cwd()
  • the pattern goes through the root-entry #addSubpattern() path (for example a/**)

If the ambient process.cwd() also contains the same relative path, the callback receives a Dirent for the ambient path instead of the glob cwd path. If the ambient cwd does not contain that path, the root entry can skip the callback entirely because statSync(path) returns null.

The async glob path does not seem affected.

What is the expected behavior? Why is that the expected behavior?

The exclude callback should receive a Dirent describing the candidate entry under options.cwd.

For the repro above, the callback should receive a directory dirent for <globCwd>/a, so dirent.isDirectory() should be true and the result should be an empty array because the callback returns true for directories.

What do you see instead?

The callback receives a Dirent for the ambient process.cwd() path instead:

{
  "processCwd": "/tmp/.../ambient",
  "globCwd": "/tmp/.../root",
  "seen": [
    {
      "name": "a",
      "parentPath": ".",
      "isDirectory": false,
      "isFile": true
    },
    {
      "name": "real.txt",
      "parentPath": "/tmp/.../root/a",
      "isDirectory": false,
      "isFile": true
    }
  ],
  "result": [
    {
      "name": "a",
      "parentPath": "/tmp/.../root",
      "isDirectory": true,
      "isFile": false
    },
    {
      "name": "real.txt",
      "parentPath": "/tmp/.../root/a",
      "isDirectory": false,
      "isFile": true
    }
  ]
}

So the root a entry is not excluded even though the callback logic is meant to exclude directories.

Additional information

This looks like a sync-only regression in the root-path exclude handling added for #56260 / #57420.

In lib/internal/fs/glob.js, #addSubpattern() computes const fullpath = resolve(this.#root, path), but in the withFileTypes + exclude branch it does:

const stat = this.#cache.statSync(path);

That appears to stat a path relative to process.cwd() instead of options.cwd. The async path already uses await this.#cache.stat(fullpath).

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

从 lib/internal/fs/glob.js 中的 #addSubpattern() 开始,将同步的 withFileTypes 和 exclude 分支与异步路径进行比较。使用提供的脚本复现该问题,然后验证 exclude callback 接收到的是 glob cwd 的 Dirent,而不是 process.cwd() 的 Dirent,并且该目录已被排除。

由索引模型根据 Issue 内容生成。

评估

技术栈
javascript
领域
operating-systems
Issue 类型
缺陷
难度
3/5
预计耗时
1-2 天
活跃度
活跃
描述清晰度
描述清楚
新手友好度
76/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。