Skip to content

When using glob can't exclude all directs #56260

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
babakfp opened this issue Dec 15, 2024 · 6 comments · Fixed by #57420
Closed

When using glob can't exclude all directs #56260

babakfp opened this issue Dec 15, 2024 · 6 comments · Fixed by #57420
Labels
fs Issues and PRs related to the fs subsystem / file system.

Comments

@babakfp
Copy link

babakfp commented Dec 15, 2024

Version

v23.4.0

Platform

Microsoft Windows NT 10.0.22631.0 x64

Subsystem

No response

What steps will reproduce the bug?

  1. Create a folder and open it in your code editor (we use this as current working directory).

  2. Make/create the files/folders shown below:

    src
    src\a
    src\a\b
    src\a\c
    src\index.ts
    src\zip.ts
    
  3. Run the code below:

    console.log(
        fs.globSync("src/**", {
            withFileTypes: true,
            cwd: process.cwd(),
            exclude: (dirent) => {
                console.log(dirent)
            },
        }),
    )

How often does it reproduce? Is there a required condition?

.

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

I expect the exclude function to receive all the output directs, so that they can be excluded. (See "Additional information" part).

Even with just returning true in exclude function, you can't exclude DirentFromStats from the result.

What do you see instead?

The first direct from output is missing in exclude. (See "Additional information" part).

Additional information

Exclude:

Dirent {
  name: 'a',
  parentPath: 'C:\\Users\\Babak\\Documents\\Code\\tosk\\src',
  path: 'C:\\Users\\Babak\\Documents\\Code\\tosk\\src',
  [Symbol(type)]: 2
}
Dirent {
  name: 'index.ts',
  parentPath: 'C:\\Users\\Babak\\Documents\\Code\\tosk\\src',
  path: 'C:\\Users\\Babak\\Documents\\Code\\tosk\\src',
  [Symbol(type)]: 1
}
Dirent {
  name: 'zip.ts',
  parentPath: 'C:\\Users\\Babak\\Documents\\Code\\tosk\\src',
  path: 'C:\\Users\\Babak\\Documents\\Code\\tosk\\src',
  [Symbol(type)]: 1
}
Dirent {
  name: 'b',
  parentPath: 'C:\\Users\\Babak\\Documents\\Code\\tosk\\src\\a',
  path: 'C:\\Users\\Babak\\Documents\\Code\\tosk\\src\\a',
  [Symbol(type)]: 2
}
Dirent {
  name: 'c',
  parentPath: 'C:\\Users\\Babak\\Documents\\Code\\tosk\\src\\a',
  path: 'C:\\Users\\Babak\\Documents\\Code\\tosk\\src\\a',
  [Symbol(type)]: 2
}

Output:

[
  DirentFromStats {
    name: 'src',
    parentPath: 'C:\\Users\\Babak\\Documents\\Code\\tosk',
    path: 'C:\\Users\\Babak\\Documents\\Code\\tosk',
    [Symbol(type)]: null,
    [Symbol(stats)]: Stats {
      dev: 1247631908,
      mode: 16822,
      nlink: 1,
      uid: 0,
      gid: 0,
      rdev: 0,
      blksize: 4096,
      ino: 1970324837878618,
      size: 0,
      blocks: 0,
      atimeMs: 1734234059936.9617,
      mtimeMs: 1734231659493.8164,
      ctimeMs: 1734231659493.8164,
      birthtimeMs: 1734197097735.2148
    }
  },
  Dirent {
    name: 'index.ts',
    parentPath: 'C:\\Users\\Babak\\Documents\\Code\\tosk\\src',
    path: 'C:\\Users\\Babak\\Documents\\Code\\tosk\\src',
    [Symbol(type)]: 1
  },
  Dirent {
    name: 'zip.ts',
    parentPath: 'C:\\Users\\Babak\\Documents\\Code\\tosk\\src',
    path: 'C:\\Users\\Babak\\Documents\\Code\\tosk\\src',
    [Symbol(type)]: 1
  },
  Dirent {
    name: 'a',
    parentPath: 'C:\\Users\\Babak\\Documents\\Code\\tosk\\src',
    path: 'C:\\Users\\Babak\\Documents\\Code\\tosk\\src',
    [Symbol(type)]: 2
  },
  Dirent {
    name: 'c',
    parentPath: 'C:\\Users\\Babak\\Documents\\Code\\tosk\\src\\a',
    path: 'C:\\Users\\Babak\\Documents\\Code\\tosk\\src\\a',
    [Symbol(type)]: 2
  },
  Dirent {
    name: 'b',
    parentPath: 'C:\\Users\\Babak\\Documents\\Code\\tosk\\src\\a',
    path: 'C:\\Users\\Babak\\Documents\\Code\\tosk\\src\\a',
    [Symbol(type)]: 2
  }
]
@mertcanaltin mertcanaltin added the fs Issues and PRs related to the fs subsystem / file system. label Dec 15, 2024
@Trott
Copy link
Member

Trott commented Mar 11, 2025

I expect the exclude function to receive all the output directs

I would not expect that. That is an internal implementation detail. If you're globbing for src/**, there's no reason for the exclude function to receive the src directory itself (although I suppose it could--again, it's an internal implementation detail).

@Trott Trott closed this as completed Mar 11, 2025
@babakfp
Copy link
Author

babakfp commented Mar 11, 2025

I expect the exclude function to receive all the output directs

I would not expect that. That is an internal implementation detail. If you're globbing for src/**, there's no reason for the exclude function to receive the src directory itself (although I suppose it could--again, it's an internal implementation detail).

Hey 👋

This is an unexpected behavior. It shouldn't work this way.

How is that src/** selects the src folder and shows it in the output but it isn't available in the exclude?

This is just a bug. I don't know what this implementation details is, but it is a bug.

So, can we open this back?

@Trott

@Trott
Copy link
Member

Trott commented Mar 11, 2025

@nodejs/fs Is the behavior here a bug? DirentFromStats doesn't appear to be documented so I'm not sure what to make of it. To me, the key question is this:

How is it that src/** selects the src folder and shows it in the output but it isn't available in the exclude?

@Trott

This comment has been minimized.

@Trott Trott reopened this Mar 11, 2025
@Trott
Copy link
Member

Trott commented Mar 11, 2025

...or maybe returning src makes perfect sense as ** matches nothing. But in that case, it should be passed to exclude for processing....

Trott added a commit to Trott/io.js that referenced this issue Mar 12, 2025
In at least some situations, the root path was
being added to the results of globbing even if
it matched the optional exclude function.

In the relevant test file, a variable was renamed
for consistency. (There was a patterns and pattern2,
rather than patterns2.) The test case is added to
patterns2.

Fixes: nodejs#56260
@Trott
Copy link
Member

Trott commented Mar 12, 2025

I'm not familiar with the globbing code, but I attempted a fix in #57420.

Trott added a commit to Trott/io.js that referenced this issue Mar 13, 2025
In at least some situations, the root path was
being added to the results of globbing even if
it matched the optional exclude function.

In the relevant test file, a variable was renamed
for consistency. (There was a patterns and pattern2,
rather than patterns2.) The test case is added to
patterns2.

Fixes: nodejs#56260
aduh95 pushed a commit that referenced this issue Mar 18, 2025
In at least some situations, the root path was
being added to the results of globbing even if
it matched the optional exclude function.

In the relevant test file, a variable was renamed
for consistency. (There was a patterns and pattern2,
rather than patterns2.) The test case is added to
patterns2.

Fixes: #56260
PR-URL: #57420
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Jason Zhang <xzha4350@gmail.com>
Reviewed-By: LiviaMedeiros <livia@cirno.name>
Reviewed-By: James M Snell <jasnell@gmail.com>
RafaelGSS pushed a commit that referenced this issue Apr 1, 2025
In at least some situations, the root path was
being added to the results of globbing even if
it matched the optional exclude function.

In the relevant test file, a variable was renamed
for consistency. (There was a patterns and pattern2,
rather than patterns2.) The test case is added to
patterns2.

Fixes: #56260
PR-URL: #57420
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Jason Zhang <xzha4350@gmail.com>
Reviewed-By: LiviaMedeiros <livia@cirno.name>
Reviewed-By: James M Snell <jasnell@gmail.com>
RafaelGSS pushed a commit that referenced this issue Apr 1, 2025
In at least some situations, the root path was
being added to the results of globbing even if
it matched the optional exclude function.

In the relevant test file, a variable was renamed
for consistency. (There was a patterns and pattern2,
rather than patterns2.) The test case is added to
patterns2.

Fixes: #56260
PR-URL: #57420
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Jason Zhang <xzha4350@gmail.com>
Reviewed-By: LiviaMedeiros <livia@cirno.name>
Reviewed-By: James M Snell <jasnell@gmail.com>
RafaelGSS pushed a commit to RafaelGSS/node that referenced this issue Apr 8, 2025
In at least some situations, the root path was
being added to the results of globbing even if
it matched the optional exclude function.

In the relevant test file, a variable was renamed
for consistency. (There was a patterns and pattern2,
rather than patterns2.) The test case is added to
patterns2.

Fixes: nodejs#56260
PR-URL: nodejs#57420
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Jason Zhang <xzha4350@gmail.com>
Reviewed-By: LiviaMedeiros <livia@cirno.name>
Reviewed-By: James M Snell <jasnell@gmail.com>
RafaelGSS pushed a commit that referenced this issue Apr 14, 2025
In at least some situations, the root path was
being added to the results of globbing even if
it matched the optional exclude function.

In the relevant test file, a variable was renamed
for consistency. (There was a patterns and pattern2,
rather than patterns2.) The test case is added to
patterns2.

Fixes: #56260
PR-URL: #57420
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Jason Zhang <xzha4350@gmail.com>
Reviewed-By: LiviaMedeiros <livia@cirno.name>
Reviewed-By: James M Snell <jasnell@gmail.com>
RafaelGSS pushed a commit that referenced this issue Apr 14, 2025
In at least some situations, the root path was
being added to the results of globbing even if
it matched the optional exclude function.

In the relevant test file, a variable was renamed
for consistency. (There was a patterns and pattern2,
rather than patterns2.) The test case is added to
patterns2.

Fixes: #56260
PR-URL: #57420
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Jason Zhang <xzha4350@gmail.com>
Reviewed-By: LiviaMedeiros <livia@cirno.name>
Reviewed-By: James M Snell <jasnell@gmail.com>
aduh95 pushed a commit that referenced this issue Apr 14, 2025
In at least some situations, the root path was
being added to the results of globbing even if
it matched the optional exclude function.

In the relevant test file, a variable was renamed
for consistency. (There was a patterns and pattern2,
rather than patterns2.) The test case is added to
patterns2.

Fixes: #56260
PR-URL: #57420
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Jason Zhang <xzha4350@gmail.com>
Reviewed-By: LiviaMedeiros <livia@cirno.name>
Reviewed-By: James M Snell <jasnell@gmail.com>
aduh95 pushed a commit that referenced this issue Apr 14, 2025
In at least some situations, the root path was
being added to the results of globbing even if
it matched the optional exclude function.

In the relevant test file, a variable was renamed
for consistency. (There was a patterns and pattern2,
rather than patterns2.) The test case is added to
patterns2.

Fixes: #56260
PR-URL: #57420
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Jason Zhang <xzha4350@gmail.com>
Reviewed-By: LiviaMedeiros <livia@cirno.name>
Reviewed-By: James M Snell <jasnell@gmail.com>
aduh95 pushed a commit that referenced this issue Apr 15, 2025
In at least some situations, the root path was
being added to the results of globbing even if
it matched the optional exclude function.

In the relevant test file, a variable was renamed
for consistency. (There was a patterns and pattern2,
rather than patterns2.) The test case is added to
patterns2.

Fixes: #56260
PR-URL: #57420
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Jason Zhang <xzha4350@gmail.com>
Reviewed-By: LiviaMedeiros <livia@cirno.name>
Reviewed-By: James M Snell <jasnell@gmail.com>
RafaelGSS pushed a commit that referenced this issue Apr 16, 2025
In at least some situations, the root path was
being added to the results of globbing even if
it matched the optional exclude function.

In the relevant test file, a variable was renamed
for consistency. (There was a patterns and pattern2,
rather than patterns2.) The test case is added to
patterns2.

Fixes: #56260
PR-URL: #57420
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Jason Zhang <xzha4350@gmail.com>
Reviewed-By: LiviaMedeiros <livia@cirno.name>
Reviewed-By: James M Snell <jasnell@gmail.com>
RafaelGSS pushed a commit that referenced this issue Apr 17, 2025
In at least some situations, the root path was
being added to the results of globbing even if
it matched the optional exclude function.

In the relevant test file, a variable was renamed
for consistency. (There was a patterns and pattern2,
rather than patterns2.) The test case is added to
patterns2.

Fixes: #56260
PR-URL: #57420
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Jason Zhang <xzha4350@gmail.com>
Reviewed-By: LiviaMedeiros <livia@cirno.name>
Reviewed-By: James M Snell <jasnell@gmail.com>
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
fs Issues and PRs related to the fs subsystem / file system.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants