fs: support Buffer paths in cp() and cpSync() - #65713
Open
HuzaifaAbdulRehman wants to merge 1 commit into
Open
Conversation
`fs.cpSync()` with a filter, and `fs.promises.cp()`, threw `ERR_INVALID_ARG_TYPE` when `src` or `dest` was a Buffer. The recursive directory walk passed the Buffer paths to `path.join()`, and the async path additionally to `path.resolve()` and `path.dirname()`, all of which only accept strings. The sync path only reached this on the filter branch because the no-filter branch runs entirely in C++. Join directory entries onto Buffer paths by concatenating bytes, read entries with `encoding: 'buffer'` so non-UTF-8 byte file names on POSIX are preserved, and decode Buffer paths to strings only for the structural subdirectory and parent-directory checks. Fixes: nodejs#58634 Assisted-by: Claude Code Signed-off-by: Huzaifa Abdul Rehman <huzaifarehman897@gmail.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What breaks
fs.cpSync()with afilter, andfs.promises.cp(), throwERR_INVALID_ARG_TYPEwhensrcordestis aBuffer, though Buffer paths are accepted elsewhere infs(they address non-UTF-8 byte file names on POSIX).Root cause
The recursive walk builds child paths with
path.join(src, name), and the async path also runspath.resolve()/path.dirname()in its structural checks. All reject a Buffer.cpSyncwithout a filter avoids it (that branch runs in C++);cp.promiseshits it inisSrcSubdirbefore copying.The change
joinPathjoins entries onto Buffer paths by concatenating bytes, andcopyDirreads entries withencoding: 'buffer'for Buffer sources, so non-UTF-8 names survive rather than being mangled.toPathStringdecodes to a string only for the subdirectory and parent-directory checks; the copy keeps the Buffers.String paths over a non-UTF-8-named entry stay unchanged (a string cannot carry those bytes) and remain a known issue, nearer #58869.
Tests
Promotes the two #58634 known-issue tests into
test/parallel/and adds a Linux-only byte-fidelity test (a Shift-JIS file name copied intact through Buffer paths).Fixes: #58634