Repository navigation
Conversation
|
Warning Review limit reachedYou've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository. Next included review available in 33 seconds. View limit details
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
LGTM — focused Node-compat fix with the full sync/callback/promises matrix covered.
What was reviewed:
rmSync/promises.rmlstat gate:!force || !recursivematches Node'svalidateRmOptions; non-ENOENT rethrow and!recursive-gated EISDIR are correct; callbackfs.rmroutes throughpromises.rmso all three stay in sync.map_rm_errno_narrowdeletion: only call site removed in the same hunk; raw errno now surfaces with thermtag.- Stream path handling:
getValidatedFsPathaccepts string/Buffer/URL with a NUL check and no resolve; thefastPathbypass is preserved (and the old cwd-deletion caveat is moot withoutpath.resolve). - Tests: added to the existing file, use
tempDir, await open/error events (no sleeps), assert{code, syscall, path}, and Windows-gate the../trailing-/cases with a stated reason.
Extended reasoning...
Overview
This PR fixes two node:fs compatibility divergences. For rm/rmSync/promises.rm, it widens the JS-side pre-removal lstat to run whenever !force || !recursive (matching Node's validateRmOptions), rethrows non-ENOENT lstat errors as-is instead of swallowing them, and deletes the native map_rm_errno_narrow table that collapsed most unlink errnos to EFAULT. For createReadStream/createWriteStream, it swaps a local getValidatedPath (which ran path.resolve() and rejected Buffers) for the shared getValidatedFsPath validator, so .path preserves the input verbatim and open() sees the literal path. Five files touched: src/js/internal/fs/streams.ts, src/js/node/fs.ts, src/js/node/fs.promises.ts, src/runtime/node/node_fs.rs, and test/js/node/fs/fs.test.ts.
Security risks
None identified. The change surfaces more accurate errnos rather than hiding them, and stops normalizing user paths before open() — which is actually the safer direction (a nope/../file.txt path now fails at the kernel walk of nope instead of silently collapsing). No auth, crypto, or permission logic is involved; input validation is delegated to the existing shared getValidatedFsPath which retains the NUL-byte check.
Level of scrutiny
Moderate. This is Node-compat work in src/js/node/ and src/runtime/node/, so the bar is "match Node's full error contract across every sibling form." I verified: the fs.ts and fs.promises.ts guards are byte-for-byte parallel; the callback fs.rm delegates to promises.rm so it inherits the fix; the EISDIR branch is correctly re-gated on !recursive now that the outer condition is wider; the deleted Rust helper has no remaining callers; and getValidatedFsPath in internal/validators.ts handles URL→path, string/Buffer passthrough, and NUL rejection as described. The removed fastPath comment about cwd deletion is no longer needed because path.resolve() is no longer called.
Other factors
Tests land in the existing test/js/node/fs/fs.test.ts, use tempDir from harness, iterate the full option matrix (undefined, {force}, {recursive}, {recursive, force}) across all three rm entry points, assert the exact {code, syscall, path} shape, and wire both open and error to resolve the awaited promise (no sleeps). Windows-specific kernel-normalization behavior is gated with isWindows and explained inline. No CODEOWNERS cover these paths. The bug hunt ran to a dry streak with no findings and no ruled-out candidates. The PR description states these tests fail on released Bun and that the relevant Node parallel tests still pass.
|
Overlap note. This PR now owns the #41480 reworks the recursive walker and also deletes |
|
Updated 6:27 AM PT - Oct 27th, 2026
❌ @robobun, your commit 5cf0198 has 2 failures in
🧪 To try this PR locally: bunx bun-pr 41486That installs a local version of the PR into your bun-41486 --bun |
…paths fs.rm and fs.rmSync mapped every unlink errno outside a short list to EFAULT, so a path that goes through a regular file reported EFAULT instead of ENOTDIR. Pass the errno through. The JS-side lstat that runs before the removal now rethrows every error except ENOENT, which is what node does, so the ENOTDIR is tagged lstat like in node. fs.ReadStream and fs.WriteStream ran path.resolve() on the path before open(). A ".." through a missing directory then opened the collapsed path, and stream.path was absolute instead of the given value. Buffer paths were rejected. Validate the path like node does and hand it to open() as given.
The src/js type check (`tsc --noEmit -p src/js/tsconfig.json`) runs in CI since #43649. This branch was last checked before that. - `FSStream.path` accepts a `Uint8Array`. The streams keep a Buffer path as it is now, so the old `string` type was wrong. - The fast-path `Bun.file(this.path)` call casts to `string`. Only internal callers reach it, and they pass a string. - The `rm` and `rmSync` lstat catch blocks read `err.code`, so the catch variable is `any`, like the other catch blocks in fs.ts. - The three new tests in fs.test.ts no longer add type errors to `test/`. Types only. The bundled JS is byte-identical.
705b816 to
5cf0198
Compare
|
Status: draft. Not ready to merge. Correction, Oct 27. The description said the
The overlap note above says the test matches Node for this case. The error code matches. The The description now also states what the first version left out:
Not checked before the PR opened: whether the lstat belongs in JS before the removal, or in Earlier status (Oct 27, before the correction)Status: ready for review. CI runs on 5cf0198. Oct 27: rebased onto main (c643ea8), with one commit on top. The previous head was 705b816. CI did not type-check 5cf0198 fixes them with types only. Reproduced again on the rebased branch with a debug build: the three new tests in |
There was a problem hiding this comment.
Nothing blocking. The comments below are optional suggestions. There is no need to push a fix for them before merging.
Beyond the inline finding, I also checked whether widening FSStream.path to Uint8Array can reach Bun.file(this.path as string) in underscoreWriteFast with a Buffer — it cannot: that branch only runs when the internal $fastPath option is set, and every such caller (writableFromFileSink, process stdio, tty) passes ""/null plus an fd, so a user-supplied Buffer path never takes the fast path.
Extended reasoning...
The change touches the node:fs rm pre-check in both fs.ts and fs.promises.ts, the non-recursive unlink errno mapping in src/runtime/node/node_fs.rs, and path validation in fs ReadStream/WriteStream; no auth, crypto or injection surface. A confirmed finding is posted inline and the hunt stopped at its bug cap rather than running dry, so approval is not on the table; this note only records the one additional candidate ruled out from reading the fast-path wiring in streams.ts.
|
Closing. The stream half is now #47968, on its own and rebased on main. The rm half should not land in this form. Why the rm half stops here. A design review compared it with the other open rm work. Its conclusion: the lstat does not belong in the JS wrappers before the removal. State of main today (debug build of b1007f1, Linux x64). For
The other 36 cells (a missing path, a directory) agree with Node. Open work in the same function.
No open PR does these two things yet: report the real errno from the non-recursive unlink (delete The status comment above has the earlier analysis and the correction of my first description. |
Draft. An earlier version said the
rmlstat rule matches Node in all three forms. It does not. See Downsides.Problem
fs.rmSync("file.txt/child"),fs.rmandfs.promises.rmthrowEFAULT: bad address in system call argument, rm 'file.txt/child'. Node throwsENOTDIRfromlstat.map_rm_errno_narrow(src/runtime/node/node_fs.rs:9605on main) maps most unlink errnos to EFAULT. The JS wrapper drops every lstat error.fs.createReadStream("nope/../file.txt")opensfile.txt. Node fails with ENOENT.getValidatedPath(src/js/internal/fs/streams.ts:96on main) runspath.resolve()beforeopen(). A Buffer path throwsERR_INVALID_ARG_TYPE.Fix
NodeFS::rm, non-recursive branch: return the unlink errno. Deletemap_rm_errno_narrow.rmSyncandpromises.rm: lstat when!force || !recursive, and rethrow every lstat error except ENOENT.ReadStreamandWriteStream: pass the path toopen()as given, aftergetValidatedFsPath.test/js/node/fs/fs.test.tsfail on bun 1.4.3. No test reaches the native errno change.Background
rmremoves anything.validateRmOptionsSyncskips the lstat whenforceandrecursiveare both set.validateRmOptions, whichfs.rmandfs.promises.rmuse, always runs it.forcewhen unlink reports ENOTDIR #44588 lstats inNodeFS::rmonly after a failed removal. That comparison is open.Downsides
{ recursive: true, force: true }on a path through a file. Node'sfs.rmandfs.promises.rmreportENOTDIRfromlstat. This branch reports it fromrm, and the new test asserts that. Node'srmSyncreturns with no error. This branch throws.rmwith{ recursive: true }and noforceruns one morelstatper call. Counted from the diff, not measured.stream.pathis the given value, not an absolute path. A relative path resolves at open time.Notes
Correction log. The first description said
rmSyncandpromises.rmrun the lstat when!force || !recursive, "like node". The{ recursive: true, force: true }case was not run against Node before the PR opened. Node v26.3.0 source, read from the binary with--expose-internals:validateRmOptionsSynchas the gate!options.force || expectDir || !options.recursive.validateRmOptionscallslstatwith no gate.rm("file.txt/child", options), wherefile.txtis a regular file. Each cell iscode syscall.{force}{recursive}{recursive, force}rmSync{recursive, force}fs.promises.rm{recursive, force}fs.rmThe node and bun 1.4.3 columns are from a run on Linux x64. In the branch column, the first two rows and the
fs.promises.rmcell are from a debug build of the first commit of this PR before the rebase (a6bfdb0). ThermSyncandfs.rmcells for{recursive, force}are read from the diff, which does not touch that path. They were not run on a build of this branch.Limits of the proof.
ERR_INVALID_ARG_TYPE). They do not get to the..andstream.pathassertions. A manual run showed those:createReadStream("nope/../file.txt")opens the file on 1.4.3 and fails with ENOENT on a6bfdb0, as in Node...case and the rm ENOTDIR case there. The reasons for the skips (Win32 collapses..before the filesystem sees it, and libuv reports a path through a file with a different errno) come from reading, not from a run.Runs on a6bfdb0 (debug build).
test/js/node/fs/fs.test.ts(568 pass, 8 skip),test/js/node/fs/fs-leak.test.js,test-fs-rm.js,test-fs-rmSync-special-char.js,test-fs-rmdir-throws-on-file.js,test-fs-rmdir-throws-not-found.js, and the 34 files that matchtest-fs-read-stream*,test-fs-write-stream*ortest-file-write-stream*. The first description listedtest-fs-rmdir*.js.test-fs-rmdir-type-check.jswas not in that run. The status comment lists the runs on the rebased head.Costs counted from the diff.
recursive. After: it also runs for{ recursive: true }withoutforce. That is one morelstatforrmSync, and one morefs.lstaton the thread pool forfs.rmandfs.promises.rm. Calls with{ recursive: true, force: true }and non-recursive calls run the same syscalls as before.stream.pathwas an absolute string. It is now the value passed in (a string, a Buffer, or the path of a URL). A relative path resolves against the cwd when the stream opens. Aprocess.chdir()between the constructor and the open changes the file, as in Node.Background detail. The EFAULT table is a port of the Zig source before the Rust rewrite (
src/runtime/node/node_fs.zigat the parent of 23427db):std.posix.unlinkZ(dest) catch |err1|followed by aswitchthat ends inelse => .FAULT.bun_sys::unlinkreturns the raw errno, so the table can only hide information.Related PRs.
map_rm_errno_narrowremoval. It also made{ recursive: true, force: true }succeed on ENOTDIR, which matches Node'srmSync.forcewhen unlink reports ENOTDIR #44588 (open) handles ENOTDIR from a failed removal with a lazy lstat inNodeFS::rm. It edits the same branch of that function.promises.rmwrapper andNodeFS::rm. node:fs: non-recursive rm treats a file that is already being deleted as gone #38550 and node:fs: recursive rm keeps walking when a concurrent deleter wins the race #39710 (open) changermerror handling in the same file.no test proof · iteration 1 · platform-specific test(s) that do not run on this machine, deferring to CI, which covers all platforms: test/js/node/fs/fs.test.ts