Skip to content

fs: support Buffer paths in cp() and cpSync() - #65713

Open
HuzaifaAbdulRehman wants to merge 1 commit into
nodejs:mainfrom
HuzaifaAbdulRehman:fix/58634-cp-buffer-paths-filter
Open

HuzaifaAbdulRehman wants to merge 1 commit into
nodejs:mainfrom
HuzaifaAbdulRehman:fix/58634-cp-buffer-paths-filter

Conversation

@HuzaifaAbdulRehman

Copy link
Copy Markdown

What breaks

fs.cpSync() with a filter, and fs.promises.cp(), throw ERR_INVALID_ARG_TYPE when src or dest is a Buffer, though Buffer paths are accepted elsewhere in fs (they address non-UTF-8 byte file names on POSIX).

const { cpSync, mkdirSync } = require('node:fs');
mkdirSync('a/c', { recursive: true });
cpSync(Buffer.from('a'), Buffer.from('b'), { recursive: true, filter: () => true });
// TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string.
//     at join (node:path)
//     at copyDir (node:internal/fs/cp/cp-sync)

Root cause

The recursive walk builds child paths with path.join(src, name), and the async path also runs path.resolve() / path.dirname() in its structural checks. All reject a Buffer. cpSync without a filter avoids it (that branch runs in C++); cp.promises hits it in isSrcSubdir before copying.

The change

joinPath joins entries onto Buffer paths by concatenating bytes, and copyDir reads entries with encoding: 'buffer' for Buffer sources, so non-UTF-8 names survive rather than being mangled. toPathString decodes 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

@nodejs-github-bot nodejs-github-bot added fs Issues and PRs related to file-system APIs and the fs module. needs-ci PRs that need a full CI run. labels Sep 1, 2026
@codecov

codecov Bot commented Sep 2, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 87.71930% with 7 lines in your changes missing coverage. Please review.
✅ Project coverage is 90.58%. Comparing base (4657394) to head (97e21f7).
⚠️ Report is 3 commits behind head on main.

Files with missing lines Patch % Lines
lib/internal/fs/cp/cp.js 85.71% 7 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #65713      +/-   ##
==========================================
- Coverage   90.59%   90.58%   -0.01%     
==========================================
  Files         802      802              
  Lines      286570   286613      +43     
  Branches    55358    55369      +11     
==========================================
+ Hits       259613   259631      +18     
- Misses      17065    17081      +16     
- Partials     9892     9901       +9     
Files with missing lines Coverage Δ
lib/internal/fs/cp/cp-sync.js 72.30% <100.00%> (+0.32%) ⬆️
lib/internal/fs/cp/cp.js 92.44% <85.71%> (-0.71%) ⬇️

... and 28 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@HuzaifaAbdulRehman
HuzaifaAbdulRehman force-pushed the fix/58634-cp-buffer-paths-filter branch 2 times, most recently from 258a798 to 004d3c3 Compare September 2, 2026 09:17
@HuzaifaAbdulRehman

Copy link
Copy Markdown
Author

@jasnell, could you take a look when you have a chance? This PR also still needs a full CI run.

@MikeMcC399

This comment was marked as resolved.

Buffer paths must preserve byte names during recursion.

Fixes: nodejs#58634
Assisted-by: Claude Code, Codex
Signed-off-by: Huzaifa Abdul Rehman <huzaifarehman897@gmail.com>
@HuzaifaAbdulRehman
HuzaifaAbdulRehman force-pushed the fix/58634-cp-buffer-paths-filter branch from 004d3c3 to 97e21f7 Compare November 29, 2026 16:07
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 file-system APIs and the fs module. needs-ci PRs that need a full CI run.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fs.cpSync / fs.cp / fs.promises.cp fails when src/dest args are Buffer

3 participants