Title: fs: rmSync() deletes a symlinked directory's contents then throws ENOTDIR when the path has a trailing slash · Issue #67186 · nodejs/node · GitHub
Open Graph Title: fs: rmSync() deletes a symlinked directory's contents then throws ENOTDIR when the path has a trailing slash · Issue #67186 · nodejs/node
X Title: fs: rmSync() deletes a symlinked directory's contents then throws ENOTDIR when the path has a trailing slash · Issue #67186 · nodejs/node
Description: Version v28.0.0-pre (main, 8d514c7), v22.20.0 for comparison Platform Linux 7.0.0-38-generic x86_64 Subsystem fs What steps will reproduce the bug? link is a symlink to a directory. The path passed to rm ends with a slash. const fs = req...
Open Graph Description: Version v28.0.0-pre (main, 8d514c7), v22.20.0 for comparison Platform Linux 7.0.0-38-generic x86_64 Subsystem fs What steps will reproduce the bug? link is a symlink to a directory. The path passed...
X Description: Version v28.0.0-pre (main, 8d514c7), v22.20.0 for comparison Platform Linux 7.0.0-38-generic x86_64 Subsystem fs What steps will reproduce the bug? link is a symlink to a directory. The path passed...
Opengraph URL: https://github.com/nodejs/node/issues/67186
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"fs: rmSync() deletes a symlinked directory's contents then throws ENOTDIR when the path has a trailing slash","articleBody":"### Version\n\nv28.0.0-pre (main, 8d514c7b), v22.20.0 for comparison\n\n### Platform\n\nLinux 7.0.0-38-generic x86_64\n\n### Subsystem\n\nfs\n\n### What steps will reproduce the bug?\n\n`link` is a symlink to a directory. The path passed to `rm` ends with a slash.\n\n```js\nconst fs = require('node:fs');\nconst path = require('node:path');\nconst os = require('node:os');\n\nfunction setup() {\n const d = fs.mkdtempSync(path.join(os.tmpdir(), 'rm-'));\n fs.mkdirSync(path.join(d, 'dir'));\n fs.writeFileSync(path.join(d, 'dir/keep.txt'), 'x');\n fs.symlinkSync('dir', path.join(d, 'link'));\n return d;\n}\n\nfunction report(label, d, err) {\n console.log(label, err ? err.code : 'ok',\n '| link:', fs.existsSync(path.join(d, 'link')),\n 'dir:', fs.existsSync(path.join(d, 'dir')),\n 'dir/keep.txt:', fs.existsSync(path.join(d, 'dir/keep.txt')));\n}\n\nlet d = setup();\ntry { fs.rmSync(path.join(d, 'link/'), { recursive: true }); report('rmSync', d); }\ncatch (err) { report('rmSync', d, err); }\n\nd = setup();\nfs.rm(path.join(d, 'link/'), { recursive: true }, (err) => report('rm', d, err));\n```\n\n### How often does it reproduce? Is there a required condition?\n\nEvery time. Requires a symlink to a directory, a trailing slash on the path, and `recursive: true`.\n\n### What is the expected behavior?\n\n`rmSync()`, `rm()` and `fsPromises.rm()` should do the same thing. Whatever that is, a call that throws should not leave the tree half deleted.\n\n### What do you see instead?\n\nOn main:\n\n```\nrmSync ENOTDIR | link: true dir: true dir/keep.txt: false\nrm ok | link: true dir: true dir/keep.txt: true\n```\n\n`rmSync()` deletes everything inside the target directory, then throws `ENOTDIR`. The symlink and the directory itself survive.\n\n`rm()` and `fsPromises.rm()` report success and delete nothing.\n\nOn v22.20.0, `rmSync()` throws `ENOTDIR` and deletes nothing, so the sync side regressed when `rmSync` moved to C++ in #53617 (7168295e).\n\n### Additional information\n\nWhy the three behaviors differ:\n\n- `rmSync()` is `std::filesystem::remove_all()`. The trailing slash makes `lstat()` follow the link, so the path reports as a directory. `remove_all` opens the directory, unlinks each entry, then calls `unlink(\"link/\")`, which fails with `ENOTDIR`. By then the entries are gone.\n\n ```\n statx(\"/tmp/rm-x/link/\", AT_SYMLINK_NOFOLLOW) = 0 (S_IFDIR)\n openat(\"/tmp/rm-x/link/\", O_RDONLY|O_NOFOLLOW|O_DIRECTORY) = 20\n unlinkat(20, \"keep.txt\", 0) = 0\n unlink(\"/tmp/rm-x/link/\") = -1 ENOTDIR\n ```\n\n- `rm()` and `fsPromises.rm()` use `internal/fs/rimraf`. `lstat()` also reports a directory, so `_rmdir()` calls `rmdir(\"link/\")`, which fails with `ENOTDIR`. `_rmdir()` turns `ENOTDIR` into `callback(originalErr)`, and `originalErr` is `null` on this path, so the call succeeds without touching anything.\n\nPOSIX `rm -rf link/` deletes the directory contents, leaves the link and the directory, and exits 0. So the sync behavior matches the shell except for the error at the end.\n\n#66673 normalizes dot segments but keeps a trailing slash, so it does not change this case.\n\nI am happy to send a PR once there is agreement on which behavior is wanted: match `rm -rf` (delete contents, succeed), or treat the path as the symlink and refuse with `ENOTDIR` before deleting anything.\n","author":{"url":"https://github.com/yohannesjoya","@type":"Person","name":"yohannesjoya"},"datePublished":"2026-11-01T15:41:00Z","url":"https://github.com/nodejs/node/issues/67186"}
| 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:da706b1b-dec7-aecf-0515-b9f1a620014b |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | C14A:39BC20:98DDED:CC8596:6AED30C9 |
| html-safe-nonce | ac35dc2c472941b1138f7ec6136f1fdbe6727b37fd67cbc4c1881268d9bacd98 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDMTRBOjM5QkMyMDo5OERERUQ6Q0M4NTk2OjZBRUQzMEM5IiwidmlzaXRvcl9pZCI6IjY0Nzc0OTc5Nzc3MTM1MzcyMjUiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 4f7eaa1f9eb4cec69dd74cd377c93526a661fe8abbd69d8992a113ae8e2bf5f2 |
| hovercard-subject-tag | issue:6148774687 |
| 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/67186/issue_layout |
| twitter:image | https://opengraph.githubassets.com/291994dffd140d71c5c0535b420f0d2d0c6a25f36b599e9f5ca1f92bd3095f29/nodejs/node/issues/67186 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/291994dffd140d71c5c0535b420f0d2d0c6a25f36b599e9f5ca1f92bd3095f29/nodejs/node/issues/67186 |
| og:image:alt | Version v28.0.0-pre (main, 8d514c7), v22.20.0 for comparison Platform Linux 7.0.0-38-generic x86_64 Subsystem fs What steps will reproduce the bug? link is a symlink to a directory. The path passed... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | yohannesjoya |
| hostname | github.com |
| expected-hostname | github.com |
| None | e51d0b7b5ab34b1688c05b54c3ed17b8658b2fa9bca973a9c740683ccb12cf4a |
| 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 | 59ebb7b901cc154ecc179a44f266fcf69f343b30 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width