Skip to content

fs: add encoding option to glob and globSync#62951

Open
maruthang wants to merge 1 commit intonodejs:mainfrom
maruthang:fix-59202-glob-encoding
Open

fs: add encoding option to glob and globSync#62951
maruthang wants to merge 1 commit intonodejs:mainfrom
maruthang:fix-59202-glob-encoding

Conversation

@maruthang
Copy link
Copy Markdown

fs: add encoding option to glob and globSync

fs.glob, fs.globSync, and fsPromises.glob always decoded
directory entries as UTF-8, so on POSIX any non-UTF-8 bytes in
file names were silently replaced with U+FFFD and the original
byte sequence could not be recovered.

Add an encoding option mirroring fs.readdir. When set to
'buffer', glob returns Buffer paths and (when withFileTypes is
true) Dirents whose name and parentPath are Buffers. The
internal walker continues to operate on strings (latin1-encoded
so each byte maps 1:1 to a code unit, preserving arbitrary
bytes), and only the fs binding boundary and the result-emission
boundary round-trip through Buffer; minimatch, path joining, and
the seen-path cache are unchanged.

Fixes: #59202


Note: I was unable to run the test suite locally (no built out/Release/node on this Windows host; the system Node is v20 which predates fs.glob). Both touched JS files lint clean and pass node --check; doc edits follow existing fs.md patterns but tools/lint-md is not installed locally. Looking forward to CI verification.

fs.glob, fs.globSync, and fsPromises.glob always decoded
directory entries as UTF-8, so on POSIX any non-UTF-8 bytes in
file names were silently replaced with U+FFFD and the original
byte sequence could not be recovered.

Add an `encoding` option mirroring fs.readdir. When set to
'buffer', glob returns Buffer paths and (when withFileTypes is
true) Dirents whose `name` and `parentPath` are Buffers. The
internal walker continues to operate on strings (latin1-encoded
so each byte maps 1:1 to a code unit, preserving arbitrary
bytes), and only the fs binding boundary and the result-emission
boundary round-trip through Buffer; minimatch, path joining, and
the seen-path cache are unchanged.

Fixes: nodejs#59202
Signed-off-by: Maruthan G <[email protected]>
@nodejs-github-bot nodejs-github-bot added fs Issues and PRs related to the fs subsystem / file system. needs-ci PRs that need a full CI run. labels Apr 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

fs Issues and PRs related to the fs subsystem / file system. needs-ci PRs that need a full CI run.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fs.globSync returns incorrect results with non-UTF8 encoded file names.

2 participants