Skip to content

fix: shell rm - #37016

Closed
deepshekhardas wants to merge 2 commits into
oven-sh:mainfrom
deepshekhardas:zack/shell-rm
Closed

deepshekhardas wants to merge 2 commits into
oven-sh:mainfrom
deepshekhardas:zack/shell-rm

Conversation

@deepshekhardas

Copy link
Copy Markdown

fix

@claude claude Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Claude Code Review

This pull request is from a fork — automated review is disabled. A repository maintainer can comment @claude review to run a one-time review.

@coderabbitai

coderabbitai Bot commented Aug 6, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

Walkthrough

Changes

The rm builtin now uses a scoped allocator with ASAN-aware leak diagnostics. Recursive directory cleanup now uses atomic subtask counts and PostRunAction. New tests cover rm errors, concurrency, platform behavior, symlinks, paths, and large directories.

rm cleanup lifecycle

Layer / File(s) Summary
Allocation-scope poison checks
src/allocators/AllocationScope.zig, src/shell/AllocScope.zig
Allocation scopes support ASAN poison checks during deinitialization.
rm scoped allocation lifecycle
src/shell/Builtin.zig, src/shell/builtin/rm.zig
Rm owns a scoped allocator, records exit codes, allocates task data through the scope, and performs scoped cleanup.
Recursive directory post-run flow
src/shell/builtin/rm.zig
Directory tasks use atomic subtask counts and PostRunAction to coordinate child completion and deferred removal.
rm edge-case validation
test/js/bun/shell/commands/rm-edge-cases.test.ts
Tests cover rm errors, recursive and concurrent deletion, platform behavior, symlinks, path separators, special filenames, large directories, and force mode.

Possibly related PRs

  • oven-sh/bun#34906: Both changes modify rm task execution, exit-code handling, recursive cleanup, and rm tests.
🚥 Pre-merge checks | ✅ 3 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Description check ⚠️ Warning The description only says "fix" and omits both required template sections and verification details. Add the required "What does this PR do?" and "How did you verify your code works?" sections with specific implementation and test details.
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly identifies the primary change to the shell rm implementation.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 6

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/allocators/AllocationScope.zig`:
- Around line 17-20: Update the non-ASAN fallback implementation of
checkIsPoisoned in AllocationScope to return false unconditionally, preserving
the ASAN branch’s poisoned-address semantics and ensuring direct callers do not
classify every pointer as poisoned.

In `@src/shell/builtin/rm.zig`:
- Around line 639-710: Add regression tests covering recursive removal errors
across verbose and non-verbose modes, using both buffered output and
file-descriptor output. Exercise each error path and assert the command
completes without hanging and returns status 1. Place the tests alongside the
existing rm behavior tests.
- Around line 657-684: Update DirTask.queueForWrite() to call deinit() before
returning when deleted_entries is empty, ensuring verbose tasks with no output
release the task, path, and list allocation. Preserve the existing write-queue
behavior for non-empty deleted_entries.
- Line 436: Normalize any recorded exec.errno to exit status 1 when assigning
state.done.exit_code, rather than storing the OS errno value. Update next() to
return state.done.exit_code for the completed task path so all output modes
consistently return status 1 after an rm error.
- Around line 572-577: Initialize this.post_run_action to .done before the
Windows-specific CWD lookup and any possible Syscall.getFdPath() failure. Keep
the existing later logic able to replace it with a more specific PostRunAction,
so the deferred switch in the post-run handling always unwraps a value.

In `@test/js/bun/shell/commands/rm-edge-cases.test.ts`:
- Around line 1-21: Remove the unused fileExists helper and the unused
tempDirWithFiles, bunExe, and bunEnv imports from the test module; retain
isPosix from harness and all imports still referenced by the tests.
🪄 Autofix

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: 891c2ce9-dea2-49dc-8616-d3b3933ce170

📥 Commits

Reviewing files that changed from the base of the PR and between 618f2f6 and a030fbe.

📒 Files selected for processing (5)
  • src/allocators/AllocationScope.zig
  • src/shell/AllocScope.zig
  • src/shell/Builtin.zig
  • src/shell/builtin/rm.zig
  • test/js/bun/shell/commands/rm-edge-cases.test.ts

Comment on lines +17 to +20
struct {
pub fn checkIsPoisoned(_: *const anyopaque) bool {
return true;
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win

Return false from the non-ASAN fallback.

The ASAN branch returns true only for poisoned addresses. The fallback currently returns true for every address, so direct callers in non-ASAN builds classify every pointer as poisoned. deinitImpl guards its current call with bun.Environment.enableAsan, but the public helper still has incorrect semantics.

Proposed fix
     struct {
         pub fn checkIsPoisoned(_: *const anyopaque) bool {
-            return true;
+            return false;
         }
     }.checkIsPoisoned;
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
struct {
pub fn checkIsPoisoned(_: *const anyopaque) bool {
return true;
}
struct {
pub fn checkIsPoisoned(_: *const anyopaque) bool {
return false;
}
}.checkIsPoisoned;
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/allocators/AllocationScope.zig` around lines 17 - 20, Update the non-ASAN
fallback implementation of checkIsPoisoned in AllocationScope to return false
unconditionally, preserving the ASAN branch’s poisoned-address semantics and
ensuring direct callers do not classify every pointer as poisoned.

Comment thread src/shell/builtin/rm.zig
exec.getOutputCount(.output_done) >= exec.getOutputCount(.output_count))
{
this.state = .{ .done = .{ .exit_code = if (exec.err) |theerr| theerr.errno else 0 } };
this.state = .{ .done = .{ .exit_code = if (exec.errno) |theerr| theerr else 0 } };

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎯 Functional Correctness | 🟠 Major | ⚡ Quick win

Return status 1 after an rm task error.

Line 436 stores an error status, but next() later completes .done with status 0. Line 454 returns the OS errno instead of status 1. As a result, error status depends on the output mode.

Use state.done.exit_code in next(). Normalize every recorded exec.errno to exit status 1.

Proposed fix
-        .done => return this.bltn().done(0),
+        .done => return this.bltn().done(this.state.done.exit_code),

-        this.state = .{ .done = .{ .exit_code = if (exec.errno) |theerr| theerr else 0 } };
+        this.state = .{ .done = .{ .exit_code = if (exec.errno != null) 1 else 0 } };

-        return this.bltn().done(if (this.state.exec.errno) |theerr| theerr else 0);
+        return this.bltn().done(if (this.state.exec.errno != null) 1 else 0);

Also applies to: 454-454

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/shell/builtin/rm.zig` at line 436, Normalize any recorded exec.errno to
exit status 1 when assigning state.done.exit_code, rather than storing the OS
errno value. Update next() to return state.done.exit_code for the completed task
path so all output modes consistently return status 1 after an rm error.

Comment thread src/shell/builtin/rm.zig
Comment on lines +572 to +577
const post_run_action: PostRunAction = bun.take(&this.post_run_action).?;
switch (post_run_action) {
.done => this.postRun(),
.done_no_decrement => this.postRunImpl(false),
.waiting_for_children => {},
.nothing => {},

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🩺 Stability & Availability | 🟠 Major | ⚡ Quick win

Initialize post_run_action before the Windows CWD lookup.

If Syscall.getFdPath() fails at Line 585, the function returns after recording the task error. The defer at Line 572 then unwraps a null post_run_action. This causes a panic instead of completing the root task.

Set this.post_run_action = .done before the Windows-specific block. Later directory logic can replace it with a more specific action.

Proposed fix
 fn runFromThreadPoolImpl(this: *DirTask) void {
+    this.post_run_action = .done;
     defer {
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/shell/builtin/rm.zig` around lines 572 - 577, Initialize
this.post_run_action to .done before the Windows-specific CWD lookup and any
possible Syscall.getFdPath() failure. Keep the existing later logic able to
replace it with a more specific PostRunAction, so the deferred switch in the
post-run handling always unwraps a value.

Comment thread src/shell/builtin/rm.zig
Comment on lines +639 to +710
pub fn postRunImpl(this: *DirTask, check_if_done: bool) void {
debug("DirTask(0x{x}, path={s}) postRun", .{ @intFromPtr(this), this.path });
// // This is true if the directory has subdirectories
// // that need to be deleted
if (this.need_to_wait.load(.seq_cst)) return;

// We have executed all the children of this task
if (this.subtask_count.fetchSub(1, .seq_cst) == 1) {
defer {
if (this.task_manager.opts.verbose)
this.queueForWrite()
else
this.deinit();
}

const all_done = if (!check_if_done) brk: {
bun.assert(this.subtask_count.load(.acquire) == 0);
break :brk true;
} else brk: {

// If this value is:
// 1: The parent DirTask has finished iterating and we are the last child
// >1: We are not the last child, or the parent DirTask is still iterating
const previous_tasks_amount = this.subtask_count.fetchSub(1, .acq_rel);
bun.assert(previous_tasks_amount >= 1);

// We have executed all the children of this task
break :brk previous_tasks_amount == 1;
};

if (all_done) {
const deinit_fn: *const fn (*DirTask) void = if (this.task_manager.opts.verbose)
DirTask.queueForWrite
else
DirTask.deinit;

// If we have a parent and we are the last child, now we can delete the parent
if (this.parent_task != null) {
// It's possible that we queued this subdir task and it finished, while the parent
// was still in the `removeEntryDir` function
const tasks_left_before_decrement = this.parent_task.?.subtask_count.fetchSub(1, .seq_cst);
const parent_still_in_remove_entry_dir = !this.parent_task.?.need_to_wait.load(.monotonic);
if (!parent_still_in_remove_entry_dir and tasks_left_before_decrement == 2) {
this.parent_task.?.deleteAfterWaitingForChildren();
if (this.parent_task) |parent_task| {
// BUT, only if the parent task is done iterating
const parent_tasks_previous_amount = parent_task.subtask_count.fetchSub(1, .acq_rel);
debug("DirTask(0x{x}, path={s}) parent=(0x{x}, path={s}) parent_tasks_previous_amount={d}", .{
@intFromPtr(this),
this.path,
@intFromPtr(parent_task),
parent_task.path,
parent_tasks_previous_amount,
});
bun.assert(parent_tasks_previous_amount >= 1);
deinit_fn(this);
if (parent_tasks_previous_amount == 1) {
parent_task.deleteAfterWaitingForChildren();
}
return;
}

// Otherwise we are root task
this.task_manager.finishConcurrently();
const task_manager = this.task_manager;
deinit_fn(this);
task_manager.finishConcurrently();
}

// Otherwise need to wait
}

pub fn deleteAfterWaitingForChildren(this: *DirTask) void {
debug("DirTask(0x{x}, path={s}) deleteAfterWaitingForChildren", .{ @intFromPtr(this), this.path });
// `runFromMainThreadImpl` has a `defer this.postRun()` so need to set this to true to skip that
this.deleting_after_waiting_for_children.store(true, .seq_cst);
this.need_to_wait.store(false, .seq_cst);
var do_post_run = true;
defer {
if (do_post_run) this.postRun();
if (do_post_run) this.postRunImpl(false);
}
if (this.task_manager.error_signal.load(.seq_cst)) {
return;
}

switch (this.task_manager.removeEntryDirAfterChildren(this)) {
.err => |e| {
.err => |e_| {
var e = e_;
debug("[deleteAfterWaitingForChildren] DirTask({x}) failed: {s}: {s}", .{ @intFromPtr(this), @tagName(e.getErrno()), e.path });
this.task_manager.err_mutex.lock();
defer this.task_manager.err_mutex.unlock();
if (this.task_manager.err == null) {
this.task_manager.err = e;
} else {
bun.default_allocator.free(e.path);
e.deinitWithAllocator(this.task_manager.rm.alloc_scope.allocator());

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎯 Functional Correctness | 🟠 Major | 🏗️ Heavy lift

Add regression coverage for recursive completion.

Add tests for recursive errors with verbose and non-verbose output. Cover buffered output and file-descriptor output. Assert that completion does not hang and that every error path returns status 1.

As per coding guidelines, “Every behavioral change must include an automated regression test in the same change.”

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/shell/builtin/rm.zig` around lines 639 - 710, Add regression tests
covering recursive removal errors across verbose and non-verbose modes, using
both buffered output and file-descriptor output. Exercise each error path and
assert the command completes without hanging and returns status 1. Place the
tests alongside the existing rm behavior tests.

Source: Coding guidelines

Comment thread src/shell/builtin/rm.zig
Comment on lines +657 to +684
if (all_done) {
const deinit_fn: *const fn (*DirTask) void = if (this.task_manager.opts.verbose)
DirTask.queueForWrite
else
DirTask.deinit;

// If we have a parent and we are the last child, now we can delete the parent
if (this.parent_task != null) {
// It's possible that we queued this subdir task and it finished, while the parent
// was still in the `removeEntryDir` function
const tasks_left_before_decrement = this.parent_task.?.subtask_count.fetchSub(1, .seq_cst);
const parent_still_in_remove_entry_dir = !this.parent_task.?.need_to_wait.load(.monotonic);
if (!parent_still_in_remove_entry_dir and tasks_left_before_decrement == 2) {
this.parent_task.?.deleteAfterWaitingForChildren();
if (this.parent_task) |parent_task| {
// BUT, only if the parent task is done iterating
const parent_tasks_previous_amount = parent_task.subtask_count.fetchSub(1, .acq_rel);
debug("DirTask(0x{x}, path={s}) parent=(0x{x}, path={s}) parent_tasks_previous_amount={d}", .{
@intFromPtr(this),
this.path,
@intFromPtr(parent_task),
parent_task.path,
parent_tasks_previous_amount,
});
bun.assert(parent_tasks_previous_amount >= 1);
deinit_fn(this);
if (parent_tasks_previous_amount == 1) {
parent_task.deleteAfterWaitingForChildren();
}
return;
}

// Otherwise we are root task
this.task_manager.finishConcurrently();
const task_manager = this.task_manager;
deinit_fn(this);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🩺 Stability & Availability | 🟠 Major | ⚡ Quick win

Deinitialize verbose tasks that have no output.

When opts.verbose is enabled, postRunImpl() selects queueForWrite() instead of deinit(). queueForWrite() returns at Line 723 when deleted_entries is empty. The DirTask, its path, and its list allocation then remain live. This occurs when a directory task fails before deleting an entry.

Deinitialize the task before returning for an empty list. The allocation scope poison check can otherwise fail during Rm.deinit().

Proposed fix
 pub fn queueForWrite(this: *DirTask) void {
     log("DirTask(0x{x}, path={s}) queueForWrite to_write={d}", .{ `@intFromPtr`(this), this.path, this.deleted_entries.items.len });
-    if (this.deleted_entries.items.len == 0) return;
+    if (this.deleted_entries.items.len == 0) {
+        this.deinit();
+        return;
+    }

Also applies to: 721-729

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/shell/builtin/rm.zig` around lines 657 - 684, Update
DirTask.queueForWrite() to call deinit() before returning when deleted_entries
is empty, ensuring verbose tasks with no output release the task, path, and list
allocation. Preserve the existing write-queue behavior for non-empty
deleted_entries.

Comment on lines +1 to +21
import { $ } from "bun";
import { beforeAll, describe, expect, setDefaultTimeout, test } from "bun:test";
import { tempDirWithFiles, bunExe, bunEnv, tmpdirSync, isWindows, isPosix } from "harness";
import { mkdirSync, writeFileSync, chmodSync, symlinkSync, existsSync } from "node:fs";
import path from "node:path";
import { sortedShellOutput } from "../util";

$.nothrow();

beforeAll(() => {
setDefaultTimeout(1000 * 60 * 5);
});

const fileExists = async (path: string): Promise<boolean> => {
try {
await $`ls -d ${path}`;
return true;
} catch {
return false;
}
};

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
# Verify harness exports used by the new test file and locate existing rm tests.
fd -t f 'harness.ts' test | while read -r f; do
  rg -n 'export (const|function|let) (isPosix|isWindows|tmpdirSync|tempDir|tempDirWithFiles|bunExe|bunEnv)\b' "$f"
done

fd -t f -i 'rm' test/js/bun/shell
rg -n 'sortedShellOutput' test/js/bun/shell -g '*.ts' | head -20

Repository: oven-sh/bun

Length of output: 2186


🏁 Script executed:

#!/bin/bash
set -euo pipefail

# Check whether every imported helper/function is referenced in the target file.
python3 - <<'PY'
from pathlib import Path
import re

file = Path("test/js/bun/shell/commands/rm-edge-cases.test.ts")
src = file.read_text()

imports = re.finditer(r'import\s*\{(?P<body>.*?)\}\s*from\s*["\'](?P<path>.+)["\']', src, re.S)
for m in imports:
    body = m.group("body")
    path = m.group("path")
    names = []
    for part in body.split(","):
        part = part.strip()
        if not part:
            continue
        if " as " in part:
            name = part.split(" as ", 1)[1].strip()
        else:
            name = part.split()[0].strip()
        if name:
            names.append(name)
    references = {name for name in names if re.search(r"\b" + re.escape(name) + r"\b", src)}
    not_refs = sorted(set(names) - references)
    print(f"{m.start()}|{path}|names={names}|referenced={references}|unused_imported={not_refs}")

# Check import-only symbols in the file (simple boundary-aware scan for identifiers).
symbols = {
    "fileExists": re.search(r"\bfileExists\b", src),
    "tempDirWithFiles": re.search(r"\btempDirWithFiles\b", src),
    "bunExe": re.search(r"\bbunExe\b", src),
    "bunEnv": re.search(r"\bbunEnv\b", src),
    "isPosix": re.search(r"\bisPosix\b", src),
    "sortedShellOutput": re.search(r"\bsortedShellOutput\b", src),
}
for name, hit in symbols.items():
    print(f"{name}|reference_count={len(re.findall(r'\\b' + re.escape(name) + r'\\b', src))}|has_reference={bool(hit)}")
PY

# Inspect the import block and usages near the target lines.
sed -n '1,22p;130,150p;240,275p;400,418p;520,543p' test/js/bun/shell/commands/rm-edge-cases.test.ts

Repository: oven-sh/bun

Length of output: 482


🏁 Script executed:

#!/bin/bash
set -euo pipefail

python3 - <<'PY'
from pathlib import Path
import re

file = Path("test/js/bun/shell/commands/rm-edge-cases.test.ts")
src = file.read_text()

for m in re.finditer(r'import\s*\{(?P<body>.*?)HOLDS:0\}\s*from\s*["\'](?P<path>.+)["\']', src, re.S):
    pass

imports = re.finditer(r'import\s*\{(?P<body>.*?)\}\s*from\s*["\'](?P<path>.+)["\']', src, re.S)
for m in imports:
    body = m.group("body")
    path = m.group("path")
    names = []
    for part in body.split(","):
        part = part.strip()
        if not part:
            continue
        if " as " in part:
            name = part.split(" as ", 1)[1].strip()
        else:
            name = part.split()[0].strip()
        if name:
            names.append(name)
    referenced = set()
    for name in names:
        if re.search(r"\b" + re.escape(name) + r"\b", src):
            referenced.add(name)
    unused = sorted(set(names) - referenced)
    print(f"{m.start()}|{path}|names={names}|referenced={referenced}|unused_imported={unused}")

symbols = {
    "fileExists": "fileExists",
    "tempDirWithFiles": "tempDirWithFiles",
    "bunExe": "bunExe",
    "bunEnv": "bunEnv",
    "isPosix": "isPosix",
    "sortedShellOutput": "sortedShellOutput",
}
for name, key in symbols.items():
    count = len(re.findall(r"\b" + re.escape(name) + r"\b", src))
    hit = bool(
        re.search(r"\b" + re.escape(name) + r"\b", src)
    )
    print(f"{key}|reference_count={count}|has_reference={hit}")
PY

sed -n '1,22p;130,150p;240,275p;400,418p;520,543p' test/js/bun/shell/commands/rm-edge-cases.test.ts

Repository: oven-sh/bun

Length of output: 22111


Remove the unused helper and unused imports.

fileExists is never called. Drop tempDirWithFiles, bunExe, and bunEnv; they are no longer used. isPosix is exported by harness.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@test/js/bun/shell/commands/rm-edge-cases.test.ts` around lines 1 - 21, Remove
the unused fileExists helper and the unused tempDirWithFiles, bunExe, and bunEnv
imports from the test module; retain isPosix from harness and all imports still
referenced by the tests.

@deepshekhardas

Copy link
Copy Markdown
Author

Closing as superseded by the shell rewrite. The Zig shell implementation (src/shell/) was rewritten in Rust (src/runtime/shell/builtin/rm.rs), so this branch's changes to src/shell/builtin/rm.zig and the allocation-scope memory fixes no longer apply to the current architecture.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants