Skip to content

Remove dead code from four platform shims, bun_core, and bun_resolver - #43121

Open
robobun wants to merge 1 commit into
mainfrom
robobun/2358ab64/dead-code-sweep
Open

robobun wants to merge 1 commit into
mainfrom
robobun/2358ab64/dead-code-sweep

Conversation

@robobun

@robobun robobun commented Sep 17, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

  • Four C++ platform shims define do-nothing stubs on the platforms they do not support: Bun__noOrphans_* (NoOrphansTracker.cpp), bun_coregraphics_* (image_coregraphics_shim.cpp), bun_wic_propbag_write_* (image_wic_shim.cpp) and io_darwin_* (io_darwin.cpp). The comments say the linker needs them. It does not: every Rust caller is behind #[cfg(target_os = "macos")] or #![cfg(windows)], so no other target references the symbols.
  • Two trait impls have no caller: OptionsEnvArg for Box<ZStr> (src/bun_core/util.rs) and From<Box<[u8]>> for cache::Contents (src/resolver/lib.rs). The dead_code lint of rustc does not report trait impls.

Fix

  • Delete the 16 stub functions and the two impls. 6 files, 68 lines removed, no behavior change.
  • Correct because nothing names them. rg finds each symbol only at its definition and at the cfg-gated call sites. The Linux debug link does not use --gc-sections, so a reference that is left fails the link. The link passes.
  • Verified: bun bd, bun run rust:check-all (12 targets), cargo check --workspace --release, and bun bd test on test/cli/env/bun-options.test.ts, test/js/bun/image/image.test.ts, test/cli/run/no-orphans.test.ts and two test/js/bun/resolve/ files.

Background

  • A platform shim here is a C++ file whose body sits inside #if OS(DARWIN), #if defined(__APPLE__) or #if defined(_WIN32). The #else branch held empty definitions of the same extern "C" names.
  • An extern "C" declaration in Rust makes a link-time reference only where code calls it. A call site under #[cfg(...)] does not exist on the other targets.
  • The candidates come from a relink of the debug build with --gc-sections --print-gc-sections. That link lists every function that nothing references.
Notes

Removed symbols:

  • src/jsc/bindings/NoOrphansTracker.cpp (non-Darwin stubs): Bun__noOrphans_begin, Bun__noOrphans_releaseKq, Bun__noOrphans_onFork, Bun__noOrphans_onExit, Bun__noOrphans_killTracked. Callers: src/spawn/process.rs and src/io/ParentDeathWatchdog.rs, all under #[cfg(target_os = "macos")].
  • src/jsc/bindings/image_coregraphics_shim.cpp (non-Apple stubs): bun_coregraphics_decode, bun_coregraphics_encode, bun_coregraphics_scale, bun_coregraphics_rotate90, bun_coregraphics_reflect, bun_coregraphics_clipboard, bun_coregraphics_clipboard_change_count. Only caller: src/runtime/image/backend_coregraphics.rs, mounted with #[cfg(target_os = "macos")] in src/runtime/image/mod.rs.
  • src/jsc/bindings/image_wic_shim.cpp (non-Windows stubs): bun_wic_propbag_write_f32, bun_wic_propbag_write_u8. Only caller: src/runtime/image/backend_wic.rs, which starts with #![cfg(windows)].
  • src/io/io_darwin.cpp (non-Apple stubs): io_darwin_create_machport, io_darwin_schedule_wakeup. Only caller: the macOS KEventWaker in src/io/lib.rs, declared and used under #[cfg(target_os = "macos")].
  • src/bun_core/util.rs: impl OptionsEnvArg for Box<ZStr>. The three callers of append_options_env use &'static ZStr or bun_core::String. The trait doc comment now names the two impls that exist.
  • src/resolver/lib.rs: impl From<Box<[u8]>> for Contents. From<Vec<u8>> stays, it has callers.

Verification details:

  • No test is added. The change only deletes code that nothing references, so there is no behavior to assert. test/internal/source-lints/CLAUDE.md asks not to add tests for dead symbols.
  • The one failure in test/cli/run/no-orphans.test.ts ("a non-Bun grandchild spawned with uid/gid is reaped") is not from this change. It fails the same way with a release build of main in the same container, and the Linux code path does not touch the removed macOS stubs.
  • cargo check --workspace --all-targets stops in the bun_bundler unit tests on main already (src/bundler/options.rs:2328, Some(0) where Option<ContentHash> is expected). This PR does not touch that crate.
  • no-orphans(macOS): close the fast-exit-intermediate race with an inherited-fd sentinel #33965 adds one more stub to the #else block of NoOrphansTracker.cpp. If it lands first, that hunk needs a small rebase.

Scope of this sweep and what came back clean, so the next run can skip it:

  • 32 dead-code PRs are open and hold about 55,000 deleted lines. This PR deletes no line that any of them deletes. One candidate was dropped for that reason: Bun__napi_get_version in napi.cpp is the renamed napi_internal_get_version that Remove dead code from the JSC bindings, headers.h, the console builtin, and bun_jsc #40232 removes.
  • Link-level: the --gc-sections relink lists 427 unreferenced functions in Bun's own C and C++. 247 are already removed by open PRs. Most of the rest are called from Rust code that is compiled on another platform only, or are macro-generated (*_getter in ZigGlobalObject.cpp), or belong to the bindgen integer converter matrix that src/codegen/bindgen.ts can emit.
  • Rust: dead_code and unreachable_pub are deny-level, so a flip of every pub in bun_runtime to pub(crate) on Linux, Windows and macOS reports zero dead functions. It reports about 20 never-read fields and never-built variants, nearly all wire-protocol or FFI tables. A strip of every #[allow(dead_code)] escape on the same three targets reports nothing that is dead on all of them.
  • Trait impls from the link list: Display for FilePoll (with FlagsFormatter) and Default for store::Bytes look dead on Linux but macOS and Windows use them, so they stay. The rust:check-all run caught both.
  • Clean: no #if 0, no cfg(any()), no orphan .rs file, no header that nothing includes, no macro_rules! without an invocation, no unused #define, no Rust extern declaration without a caller (two exist, both in open PRs), no unused BunBuiltinNames.h entry, no unused $intrinsic declaration outside open PRs. oxlint no-unused-vars and an unused-export scan over src/js, src/codegen, scripts and packages only find items that open PRs already remove.
  • The new cargo::unused_dependencies lint reports five dependency edges that are unused on all 12 targets. Three are removed by Remove dead code from bun-uws, bun-usockets, the require.extensions FFI, and unused Cargo dependencies #40294. bun_zstd in bun_resolver is used under cfg(bun_codegen_embed), and bstr in bun_wyhash is used by a unit test, so both stay.

The C++ shims for NoOrphansTracker, the CoreGraphics and WIC image
backends, and the io_darwin waker defined empty stubs on the platforms
they do not support. Every Rust caller of those symbols is behind a
target_os cfg, so nothing references the stubs on any target.

Also remove two trait impls that nothing calls:
OptionsEnvArg for Box<ZStr> and From<Box<[u8]>> for cache::Contents.
@robobun

robobun commented Sep 17, 2026 •

Copy link
Copy Markdown
Collaborator Author

Status: ready for review.

  • This PR only deletes code. It removes 16 stub functions from four C++ platform shims and two Rust trait impls that nothing calls.
  • CI build #117148 is finished: 180 of 181 jobs pass. All 13 build-bun jobs pass (Windows x64 and aarch64, macOS x64 and aarch64, Linux glibc, musl, ASAN and Android, FreeBSD x64 and aarch64), so no platform needs the removed stubs to link. 159 of 160 test-bun jobs pass.
  • The one red job is the x64-asan test job, with test/js/bun/spawn/spawn.test.ts. The child prints the LeakSanitizer warning ptrace appears to be blocked to stderr, and the test expects an empty stderr. That failure is on main since the LLVM 23 update, spawn: install the waiter thread's SIGCHLD handler with SA_RESTART #42911 has the fix, and this diff does not touch that code path.
  • Checked locally: bun bd links on Linux, bun run rust:check-all passes on all 12 targets, cargo check --workspace --release passes, and the tests for BUN_OPTIONS, Bun.Image, no-orphans and the resolver pass (one no-orphans uid/gid test fails the same way on main in this container).

@coderabbitai

coderabbitai Bot commented Sep 17, 2026 •

Copy link
Copy Markdown
Contributor

Review Change StackReview Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Essentials

Run ID: b1afd054-7e9e-406b-af96-d652564bbfc5

📥 Commits

Reviewing files that changed from the base of the PR and between b52d513 and 75eccbd.

📒 Files selected for processing (6)
  • src/bun_core/util.rs
  • src/io/io_darwin.cpp
  • src/jsc/bindings/NoOrphansTracker.cpp
  • src/jsc/bindings/image_coregraphics_shim.cpp
  • src/jsc/bindings/image_wic_shim.cpp
  • src/resolver/lib.rs
💤 Files with no reviewable changes (1)
  • src/resolver/lib.rs

Included review availability: Your plan provides up to 10 included reviews per hour; 1 remains after this review.


Walkthrough

The pull request removes unsupported boxed conversion implementations and non-native platform fallback stubs. It also updates OptionsEnvArg documentation to match its remaining implementations.

Changes

Rust API cleanup

Layer / File(s) Summary
Remove boxed conversion implementations
src/bun_core/util.rs, src/resolver/lib.rs
OptionsEnvArg no longer supports Box<ZStr>. Contents no longer implements conversion from Box<[u8]>. Documentation lists the remaining supported argument types.

Platform stub cleanup

Layer / File(s) Summary
Remove unsupported platform stubs
src/io/io_darwin.cpp, src/jsc/bindings/NoOrphansTracker.cpp, src/jsc/bindings/image_coregraphics_shim.cpp, src/jsc/bindings/image_wic_shim.cpp
Non-Apple, non-Darwin, and non-Windows fallback definitions were removed from the platform-specific APIs.

Suggested reviewers: jarred-sumner

Priority: ⬇️ Low

Merge Risk: ⚪ Minimal · up to 75ecc

The cleanup does not leave an inspected supported target or internal conversion path without its required implementation.

🚥 Pre-merge checks | ✅ 4
✅ Passed checks (4 passed)
Check name Status Explanation
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.
Description check ✅ Passed The description clearly explains the problem, fix, scope, verification steps, known test failures, and background. It does not use the template headings exactly, but it provides the required informati…
Title check ✅ Passed The title clearly and concisely identifies the removal of dead code from the platform shims, bun_core, and bun_resolver.

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

@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.

LGTM — pure dead-code removal; every deleted symbol and impl is unreferenced on the platforms that lose it.

What was reviewed:

  • Traced each removed C++ stub to its Rust callers: Bun__noOrphans_* are used only inside #[cfg(target_os = "macos")] items in src/spawn/process.rs (wait_mac_kqueue, gated use/defer!) and src/io/ParentDeathWatchdog.rs; io_darwin_* only in the macOS-gated KEventWaker in src/io/lib.rs; bun_coregraphics_* only in backend_coregraphics.rs (mounted under cfg(target_os = "macos")); bun_wic_propbag_write_* only in backend_wic.rs (#!&#91;cfg(windows)]). The un-gated declarations in src/spawn_sys/lib.rs are declarations only, so no link reference is emitted.
  • No C++ header, export list, or build script names these symbols; the shims compile as standalone TUs (scripts/build/unified.ts noUnify), and an empty TU on non-target platforms matches the existing root_certs_darwin.cpp/root_certs_windows.cpp pattern.
  • The three append_options_env callers use &'static ZStr or BunString, and every Contents::from site passes Vec&lt;u8&gt; or Cow&lt;[u8]&gt;, so the deleted Box&lt;ZStr&gt; and From&lt;Box&lt;[u8]&gt;&gt; impls had no users (a leftover would fail to compile).

<details>
<summary>Extended reasoning...</summary>

Overview

The PR deletes 68 lines across six files: the #else fallback stubs in four platform-specific C++ shims (src/io/io_darwin.cpp, src/jsc/bindings/NoOrphansTracker.cpp, src/jsc/bindings/image_coregraphics_shim.cpp, src/jsc/bindings/image_wic_shim.cpp), the impl OptionsEnvArg for Box&lt;ZStr&gt; in src/bun_core/util.rs (with a doc-comment update naming the two remaining impls), and impl From&lt;Box&lt;[u8]&gt;&gt; for cache::Contents in src/resolver/lib.rs. No behavior is added or changed on any platform where the code was live.

Security risks

None. The change only removes never-called no-op stubs and two unused trait impls; it does not touch input validation, auth, crypto, or any user-reachable path. Removing the unsafe fat-pointer cast in the Box&lt;ZStr&gt; impl reduces unsafe surface rather than adding it.

Level of scrutiny

The one real risk with this kind of change is link-time symbol resolution on the platforms that lose the definitions, since Rust extern "C" declarations compile cleanly whether or not a definition exists. I grepped the full tree (excluding vendor/build/test) for every removed symbol and confirmed each is referenced only from code gated on the platform that still defines it: Bun__noOrphans_* from wait_mac_kqueue (lines 3463-3703 of process.rs, #[cfg(target_os = "macos")]), a macOS-gated use, defer!, and if block, plus a macOS-gated call and extern block in ParentDeathWatchdog.rs; io_darwin_* from the macOS-gated extern block and impl KEventWaker; bun_coregraphics_* from backend_coregraphics.rs, whose mod is #[cfg(target_os = "macos")] in image/mod.rs; bun_wic_propbag_write_* from backend_wic.rs under #!&#91;cfg(windows)]. The un-gated pub safe fn Bun__noOrphans_* declarations in src/spawn_sys/lib.rs emit no undefined-symbol reference without a call site. No C++ header, .def, export list, or build script names any of them. The shims are in the noUnify list so each compiles as its own TU; an empty TU on other platforms already exists in-tree (root_certs_darwin.cpp, root_certs_windows.cpp), and the build flags do not enable -pedantic/-Wempty-translation-unit.

For the Rust impls, all three append_options_env callers (cli/mod.rs with Vec&lt;&'static ZStr&gt;, node_process.rs with BunString, and the internal &'static ZStr path in util.rs) avoid Box&lt;ZStr&gt;, and the Contents::from sites (transpiler.rs with Vec&lt;u8&gt; from decode_data, lib.rs with Cow/Vec) avoid Box&lt;[u8]&gt;. Any missed user would be a compile error, so the author's reported rust:check-all pass plus CI covers the residual risk.

Other factors

The change is small, mechanical, and self-contained; the changed paths are not covered by .github/CODEOWNERS; no tests are needed for deletion of code that nothing calls (and REVIEW.md explicitly says not to add tests that dead code stays dead). The bug-hunting pass ran dry after ruling out the four link-time candidates, which matches my own reading.

</details>

@robobun

robobun commented Sep 17, 2026

Copy link
Copy Markdown
Collaborator Author

Both automated reviews are in. Neither asks for a change, so the branch stays at 75eccbd.

  • The second review traced each removed stub to its cfg-gated Rust callers. It reached the same result as the PR description: no target references the deleted symbols or the two impls.
  • This PR has no test on purpose. It only deletes code that nothing references, so there is no behavior to assert. REVIEW.md ("Do not add tests to check dead code stays dead") and test/internal/source-lints/CLAUDE.md both ask not to add such tests.
  • The Find duplicate PRs job failed inside the action itself. The run ended after 0 turns with is_error: true, so it did not look at the diff.
  • The Buildkite build (#117148) is the check for the macOS and Windows links. It is still running.

This branch has not been deployed

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants