Title: Inconsistency in fs.cp operation · Issue #58947 · nodejs/node · GitHub
Open Graph Title: Inconsistency in fs.cp operation · Issue #58947 · nodejs/node
X Title: Inconsistency in fs.cp operation · Issue #58947 · nodejs/node
Description: In the fs.cp... API, we have options such as errorOnExist and force that are intended to control what happens when the destination already exists. There is, however, a discrepancy in how the options are implemented depending on whether t...
Open Graph Description: In the fs.cp... API, we have options such as errorOnExist and force that are intended to control what happens when the destination already exists. There is, however, a discrepancy in how the option...
X Description: In the fs.cp... API, we have options such as errorOnExist and force that are intended to control what happens when the destination already exists. There is, however, a discrepancy in how the option...
Opengraph URL: https://github.com/nodejs/node/issues/58947
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Inconsistency in fs.cp operation","articleBody":"In the `fs.cp...` API, we have options such as `errorOnExist` and `force` that are intended to control what happens when the destination already exists. There is, however, a discrepancy in how the options are implemented depending on whether the src/dest are files or directories.\n\nFor instance, let's suppose we have the following directory layout:\n\n```\n~/tmp\n | -- ~/tmp/a (file)\n | -- ~/tmp/b (file)\n | -- ~/tmp/c (directory)\n | | -- /tmp/c/1 (file)\n | -- ~/tmp/d (directory)\n | -- /tmp/d/2 (file)\n```\n\nThen, in the `fs.cp` operation, let's first try copying a to b, with options `errorOnExist: true` and `force: false` we get the appropriate error indicating that the destination exists and we're not going to overwrite it:\n\n```js\n\u003e fs.cp('a', 'b', { errorOnExist: true, force: false }, console.log)\nundefined\n\u003e SystemError [ERR_FS_CP_EEXIST]: Target already exists: cp returned EEXIST (b already exists) b\n at mayCopyFile (node:internal/fs/cp/cp:250:11)\n at onFile (node:internal/fs/cp/cp:242:10)\n at getStatsForCopy (node:internal/fs/cp/cp:211:12) {\n code: 'ERR_FS_CP_EEXIST',\n info: {\n message: 'b already exists',\n path: 'b',\n syscall: 'cp',\n errno: 17,\n code: 'EEXIST'\n },\n errno: [Getter/Setter: 17],\n syscall: [Getter/Setter: 'cp'],\n path: [Getter/Setter: 'b']\n}\n```\n\nHowever, if we try copying c to d, with options `errorOnExist: true` and `force: false`, we happily go ahead and copy contents of 'c' over to 'd', erroring only if one of the individual files in d overlaps with a file in c\n\n```js\n\u003e fs.cp('c', 'd', { recursive: true, errorOnExist: true, force: false }, console.log)\nundefined\n\u003e null undefined\n```\n\nThis just feels wrong. `d` already exists and we're being told not to force. I would expect the behavior to be the same as copying the files. If `d` exists, this operation should fail.\n\nI don't think we can change this behavior now without it being semver-major and potentially breaking a lot of things but we really ought to have an option of enforcing that no copy is performed if `d` exists.\n\n@nodejs/fs @anonrig @dario-piotrowicz ","author":{"url":"https://github.com/jasnell","@type":"Person","name":"jasnell"},"datePublished":"2025-07-03T16:02:20.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":2},"url":"https://github.com/58947/node/issues/58947"}
| 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:d6d52471-8704-e20c-62cf-d9a74abfae41 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 889A:348CFA:23CD44:3289DE:6A4C9A82 |
| html-safe-nonce | 7bfe57b81cb68f45de19c3aa65ed56c2ea429241aeaf4da457df041ad0762d78 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4ODlBOjM0OENGQToyM0NENDQ6MzI4OURFOjZBNEM5QTgyIiwidmlzaXRvcl9pZCI6IjE5Mzg1OTgxMTI5MTA5NDA4MDIiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 4f4da2d62004b660b51e9079081d3eecf98bda2440c25e4974ecb234ded84340 |
| hovercard-subject-tag | issue:3199885326 |
| 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/58947/issue_layout |
| twitter:image | https://opengraph.githubassets.com/2164368c3da6c0767be4e03cb06db67439e0ab2e31af3346c299dca54d2cb02a/nodejs/node/issues/58947 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/2164368c3da6c0767be4e03cb06db67439e0ab2e31af3346c299dca54d2cb02a/nodejs/node/issues/58947 |
| og:image:alt | In the fs.cp... API, we have options such as errorOnExist and force that are intended to control what happens when the destination already exists. There is, however, a discrepancy in how the option... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | jasnell |
| hostname | github.com |
| expected-hostname | github.com |
| None | 3d11bb817438277de2a940854450e83a7d32b6aeb5014e9e6b00a6423900251c |
| 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 | 7a2dede45637df6b92ddcfe2a557e67d4b5854ae |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width