Title: `fs.cp` creates file symlink instead of directory symlink on Windows when `filter` is provided · Issue #62653 · nodejs/node · GitHub
Open Graph Title: `fs.cp` creates file symlink instead of directory symlink on Windows when `filter` is provided · Issue #62653 · nodejs/node
X Title: `fs.cp` creates file symlink instead of directory symlink on Windows when `filter` is provided · Issue #62653 · nodejs/node
Description: Version v24.14.1 (also affects v25.9.0) Platform Windows 11 Pro 10.0.26200 What steps will reproduce the bug? import fs from 'node:fs'; import path from 'node:path'; import os from 'node:os'; const tmp = path.join(os.tmpdir(), 'repro-' +...
Open Graph Description: Version v24.14.1 (also affects v25.9.0) Platform Windows 11 Pro 10.0.26200 What steps will reproduce the bug? import fs from 'node:fs'; import path from 'node:path'; import os from 'node:os'; const...
X Description: Version v24.14.1 (also affects v25.9.0) Platform Windows 11 Pro 10.0.26200 What steps will reproduce the bug? import fs from 'node:fs'; import path from 'node:path'; import os from ...
Opengraph URL: https://github.com/nodejs/node/issues/62653
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"`fs.cp` creates file symlink instead of directory symlink on Windows when `filter` is provided","articleBody":"### Version\n\nv24.14.1 (also affects v25.9.0)\n\n### Platform\n\nWindows 11 Pro 10.0.26200\n\n### What steps will reproduce the bug?\n\n```javascript\nimport fs from 'node:fs';\nimport path from 'node:path';\nimport os from 'node:os';\n\nconst tmp = path.join(os.tmpdir(), 'repro-' + Date.now());\nconst src = path.join(tmp, 'src');\nconst dest1 = path.join(tmp, 'dest-no-filter');\nconst dest2 = path.join(tmp, 'dest-with-filter');\n\n// Create a source directory with a relative directory symlink\nfs.mkdirSync(path.join(src, 'packages', 'my-lib'), { recursive: true });\nfs.writeFileSync(path.join(src, 'packages', 'my-lib', 'index.js'), 'hello');\nfs.mkdirSync(path.join(src, 'linked'), { recursive: true });\nfs.symlinkSync(\n path.join('..', 'packages', 'my-lib'),\n path.join(src, 'linked', 'my-lib'),\n 'dir'\n);\n\n// Test 1: verbatimSymlinks WITHOUT filter — works\nfs.cpSync(src, dest1, { recursive: true, verbatimSymlinks: true });\nconsole.log('without filter:', fs.statSync(path.join(dest1, 'linked', 'my-lib')).isDirectory()); // true\n\n// Test 2: verbatimSymlinks WITH filter — broken\nfs.cpSync(src, dest2, { recursive: true, verbatimSymlinks: true, filter: () => true });\nconsole.log('with filter:', fs.statSync(path.join(dest2, 'linked', 'my-lib')).isDirectory()); // EPERM\n\nfs.rmSync(tmp, { recursive: true, force: true });\n```\n\n### How often does it reproduce? Is there a required condition?\n\nAlways reproducible on Windows. Requires `verbatimSymlinks: true` combined with a `filter` function (even `() => true`).\n\n### What is the expected behavior? Why is that the expected behavior?\n\nBoth cases should produce a working directory symlink. The `filter` option should not affect the symlink type — `filter: () => true` is semantically identical to no filter.\n\n### What do you see instead?\n\nWithout `filter`: the directory symlink is preserved correctly (`Directory, ReparsePoint`).\n\nWith `filter`: a file symlink is created instead (`Archive, ReparsePoint`). Subsequent `stat()`, `readdir()`, and `realpathSync()` on the copied symlink all fail with `EPERM`.\n\n### Root cause\n\n`fs.cp` takes two different code paths depending on whether `filter` is provided:\n\n- **Without `filter`**: Uses the C++ fast path (`fsBinding.cpSyncCopyDir`) which calls `std::filesystem::copy_symlink()` — this automatically preserves the symlink type.\n\n- **With `filter`**: Falls back to the JavaScript path in `lib/internal/fs/cp/cp-sync.js`, where `onLink()` calls `symlinkSync(resolvedSrc, dest)` **without the `type` parameter**.\n\nWhen `type` is omitted, `symlinkSync` tries to auto-detect by calling `statSync(resolve(dest, '..', target))`. However, during a recursive copy, the symlink's target directory may not yet exist at the destination (e.g., `linked/` is copied before `packages/` in alphabetical order). When stat fails, `type` defaults to `null`, which results in a **file symlink** on Windows.\n\nThe same issue exists in the async version (`lib/internal/fs/cp/cp.js`).\n\n### Related Issue\n\n- https://github.com/vitejs/vite/pull/22196#discussion_r3056981191","author":{"url":"https://github.com/shulaoda","@type":"Person","name":"shulaoda"},"datePublished":"2026-04-09T16:15:39Z","url":"https://github.com/nodejs/node/issues/62653"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react-reconciler,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@d5750093facf |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:43787496-cb90-4a6a-8bab-f120d8923011 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | CCFA:1E2715:60ECCB:7D76F4:6AED2ACC |
| html-safe-nonce | 7f70f6eb3de1296ba72b1033daecdcd2e2baf06b4a0441534c97e651da600051 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDQ0ZBOjFFMjcxNTo2MEVDQ0I6N0Q3NkY0OjZBRUQyQUNDIiwidmlzaXRvcl9pZCI6IjcxODgwNjMxNjg2MzAwNzQzNiIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 1d8b5abd41d444f0c657abe711687c5854d90f1f272604ea5a7d0e7231c1412d |
| hovercard-subject-tag | issue:4233541029 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/nodejs/node/62653/issue_layout |
| twitter:image | https://opengraph.githubassets.com/55051d812b488df139e71beec2565c146073381e7a20235900c037ec88bc6870/nodejs/node/issues/62653 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/55051d812b488df139e71beec2565c146073381e7a20235900c037ec88bc6870/nodejs/node/issues/62653 |
| og:image:alt | Version v24.14.1 (also affects v25.9.0) Platform Windows 11 Pro 10.0.26200 What steps will reproduce the bug? import fs from 'node:fs'; import path from 'node:path'; import os from 'node:os'; const... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | shulaoda |
| hostname | github.com |
| expected-hostname | github.com |
| None | 9d95365dda0576efe9b71f45807595e396bd0ca9286fba5367a05048e6ddf8f2 |
| turbo-cache-control | no-preview |
| go-import | github.com/nodejs/node git https://github.com/nodejs/node.git |
| octolytics-dimension-user_id | 9950313 |
| octolytics-dimension-user_login | nodejs |
| octolytics-dimension-repository_id | 27193779 |
| octolytics-dimension-repository_nwo | nodejs/node |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 27193779 |
| octolytics-dimension-repository_network_root_nwo | nodejs/node |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 98b41fa4b5635ceb45a4c54320fced73817ff290 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width