Repository navigation
Description
Problem
Every wheel ships two executables that each statically link nearly the whole workspace (build engine, packages, deploy, zccache, tree-sitter, …):
dist profile (#1846), Linux x86_64, --strip-debug |
size |
|---|---|
fbuild |
35.7 MB |
fbuild-daemon |
38.1 MB |
fbuild-cli uses the build stack in-process (fbuild_build:: 52 refs, fbuild_packages:: 36, fbuild_deploy:: 13 in crates/fbuild-cli/src, re-counted against main @ bebd679c), so the CLI can't simply become a thin HTTP client without a large refactor. The two binaries are mostly the same code twice.
Strategy: one binary, argv[0] multicall, self-relocated daemon
Ship one native executable, fbuild. fbuild-daemon stops being a separate link and becomes a name for the same image, selected by argv[0] — busybox-style, the way soldr already does it.
- One link.
fbuild-daemon'smainbody moves behind a library entry (fbuild_daemon::entry::run() -> i32);fbuild-clidepends onfbuild-daemonas a library. Thefbuild-daemon[[bin]]is dropped from the release build. - argv[0] dispatch before clap/tokio. A small
multicallmodule infbuild-clitakes the file stem ofargv[0](strip dir, strip.exe, case-insensitive on Windows) and, if it isfbuild-daemon, calls straight into the daemon entry. Anything else falls through to today's CLI path unchanged. fbuild-daemonis a hardlink tofbuild(same inode). Not a symlink (Windows symlinks needSeCreateSymbolicLinkPrivilege/developer mode, and argv[0] through a symlink is launcher-dependent), and not a copy (doubles disk again — the thing we're removing).- The running daemon executes from a relocated copy under the user data dir, not from the installed image. This, not the hardlink, is what fixes Windows upgrade locking — see below.
soldr reference (read the code, it is the template)
~/dev/soldr/crates/soldr-cli/src/multicall.rs:1-7— module doc: the release archive no longer shipssoldr-shim,soldr-clang-shim, orzccache-soldrsidecars; installers hardlink/copy the mainsoldrbinary under each shim name and this module "selects the tiny fast path before clap/tokio startup".multicall.rs:104-119—maybe_dispatch();:116isShimIdentity::SoldrDaemon => Some(MulticallDispatch::Exit(crate::daemon_entry::run())).multicall.rs:143-158—classify_argv0()(stem → identity,SOLDR_DAEMON = "soldr-daemon"at:23);:184argv0_stem(); tests at:605(classify_argv0_recognizes_runtime_aliases).multicall.rs:62-81—SOLDR_ARGV0_SHIM/apply_shim_argv0_override(): an env carrier for argv[0] identity when anshtrampoline stands in for a hardlink (can't set argv[0] portably). fbuild needs the equivalent only if we ever fall back to a trampoline; note it, don't build it up front.~/dev/soldr/crates/soldr-core/src/self_relocate.rs:102-117—ensure_daemon_relocated(): copies the daemon image into~/.soldr/runtime/soldr-daemon/<hash>/and runs from there so "uninstall/upgrade/rm of the source no longer needs to wait for the daemon to exit". Returns the source unchanged if it already lives under the runtime root (:155). Content-hashed dir, a runtime-root lock around the copy (:224,:478), and periodic GC of stale copies (:344-348,:427refresh_running_image_ledgerso GC never reaps the running image).- Caller:
~/dev/soldr/crates/soldr-cli/src/broker_launcher.rsrelocates before spawning.
Why argv[0] and not fbuild __daemon …
The previous version of this issue proposed a hidden subcommand. argv[0] is strictly better here:
- External callers keep working because the name still exists. FastLED, scripts, docs, service definitions and the running-process broker all exec
fbuild-daemonby name. With a hidden subcommand every one of them has to change tofbuild __daemon, in lockstep with a release. With argv[0] they don't change at all —fbuild-daemonis still on disk and still behaves like the daemon. - Process identity is preserved. fbuild identifies its daemon by exe stem in several places:
fbuild_paths::daemon_ownership::DAEMON_EXE_STEM = "fbuild-daemon"(crates/fbuild-paths/src/daemon_ownership.rs:36, used byfbuild-paths/src/lib.rs:475,507,662),running_process::DAEMON_BINARY_NAME(crates/fbuild-paths/src/running_process.rs:49),port_holders.rs:117in fbuild-serial, andbroker/service.rs:233in the daemon. Afbuild __daemonprocess has stemfbuildand is indistinguishable from a CLI invocation; an argv[0] daemon's stem is stillfbuild-daemon(it is the alias path that's executed — or the relocated copy, which is namedfbuild-daemon[.exe]). - No clap cost and no second CLI surface. The dispatch happens before
clapparses fbuild's large subcommand tree and before any tokio runtime is built, so the daemon starts exactly as it does today (its ownArgs::parse(), its own#[tokio::main]-equivalent runtime). There's no hidden subcommand to keep out of--help, document, or guard from users.
What has to change (in order)
crates/fbuild-daemon/src/main.rs— move the body ofasync fn main()(:42-44, carrying thetoo_many_linesexpect, which must not grow) into a library fnfbuild_daemon::entry::run() -> i32that builds its own multi-thread runtime. Keepfbuild_daemon::heap_profile::start_from_env()as the first statement (perf: adopt the mimalloc-pprof allocator for heap/leak profiling; verify on-CPU, off-CPU, and symbolization from running-process #1361).main.rsbecomes a 3-line shim (kept for dev/cargo run -p fbuild-daemonwhile the alias rolls out).crates/fbuild-cli/Cargo.toml— depend onfbuild-daemonas a library (no new crate; this fits the modules-first rule).- New module
crates/fbuild-cli/src/multicall.rs—classify_argv0/maybe_dispatchmodeled on soldr's, recognising onlyfbuild-daemon. Unit tests for:/x/fbuild-daemon,C:\x\FBUILD-DAEMON.EXE, relocated path,fbuild(falls through), empty argv. crates/fbuild-cli/src/main.rs:9— the hook goes at the very top offn main(), before the 8 MBfbuild-mainthread trampoline and beforeasync_main_entry()(which derives the dev daemon namespace and builds the tokio runtime at:51, thencli::async_main()runs clap). The daemon path should not pay for either; it sets its own env exactly as the standalone binary does today. If the daemon needs the large stack too, spawn it on its own thread insideentry::run().- Alias creation — see Packaging.
fbuild(the CLI's daemon-spawn path incrates/fbuild-cli/src/daemon_client.rs) ensuresfbuild-daemon[.exe]exists next to itself as a hardlink before spawning, falling back to spawning a relocated copy directly. - Self-relocation — port
ensure_daemon_relocatedintofbuild-paths(shared, dependency-free; both CLI and daemon already depend on it). Target:<fbuild cache root>/runtime/fbuild-daemon/<content-hash>/fbuild-daemon[.exe], respectingFBUILD_DEV_MODE(~/.fbuild/dev/…). Copy under a runtime-root lock, atomic rename into place, content hash as dir name, and GC of stale copies that never touches the running image's dir. The CLI relocates, then spawns the relocated path; on Windows the relocated copy must carry any adjacent DLLs/PDB it needs (check the wheel layout — soldr'sexe_depends_on_bundled_wheel_libsatself_relocate.rs:282exists for exactly this).- Lock policy: the runtime-copy lock is a new file lock. CLAUDE.md's "no file-based locks" rule has one sanctioned exception (daemon startup/lifetime ownership + spawn election, handoff(#1154): finish cold-cache reset with soldr-style daemon ownership #1159); the relocation copy sits inside that spawn-election path, so it should be done under the existing
spawn.lockrather than adding a new lock. If that doesn't fit, it needs explicit maintainer sign-off and a CLAUDE.md update.
- Lock policy: the runtime-copy lock is a new file lock. CLAUDE.md's "no file-based locks" rule has one sanctioned exception (daemon startup/lifetime ownership + spawn election, handoff(#1154): finish cold-cache reset with soldr-style daemon ownership #1159); the relocation copy sits inside that spawn-election path, so it should be done under the existing
- Identity/diagnostics —
crates/fbuild-core/src/platform/executable.rs:87current_image_sibling_candidates("fbuild-daemon")keeps resolving: the alias is a real file next tofbuild, so[fbuild-daemon, fbuild-daemon.exe]both still work with no code change.crates/fbuild-cli/src/daemon_client/restart_diag.rs:12-39compares the siblingfbuild-daemonmtime against the image the daemon reports (health.source_mtime, exe). With relocation, the daemon reports the relocated exe; the health payload must also carry the original (source) exe path + mtime (soldr carries this asSOLDR_ORIGINAL_EXE,self_relocate.rs:32), andrestart_diagmust compare sibling-vs-source and print both paths. Otherwise every relocated daemon looks like a "second launcher serving".- Note a hardlink shares mtime with
fbuild— that's correct (same image) and makes the "sibling newer than running" rebuild check still work. - Endpoint/version key (
fbuild_paths::default_daemon_port/daemon_endpoint_key) is derived from backend version + cache identity, not exe path — relocation must not change it. Add a test that a relocated and a non-relocated daemon of the same version derive the same endpoint, and two versions still derive different ones (daemon: endpoint isolation follow-up to #940 — key the default endpoint by cache-identity/checkout + deterministic version arbitration #1009). - Root-owner / spawn locks (handoff(#1154): finish cold-cache reset with soldr-style daemon ownership #1159) are under the cache root, not next to the exe, so they're unaffected — verify, don't assume.
- Broker —
crates/fbuild-daemon/src/broker/service.rs:233builds the service definition with namefbuild-daemon; the executable path it passes becomes the relocated path.DAEMON_BINARY_NAMEstays. - Packaging —
ci/build_backend.py,.github/workflows/template_native_build.yml,ci/wheel_check.py(below).
Windows upgrade locking — the precise mechanism
Today a running daemon maps fbuild-daemon.exe, and Windows denies delete/overwrite of a mapped image, so only that one file is pinned during pip install -U / uv sync (and fbuild.exe is free unless a CLI is mid-run).
A hardlink alone does not fix this — it makes it worse. fbuild.exe and fbuild-daemon.exe would be two directory entries for one file; the loader's section lock is on the file, not the name. A daemon started as fbuild-daemon.exe therefore pins fbuild.exe too, and the installer's attempt to replace fbuild.exe fails for as long as any daemon is up — which is "always" for a long-lived daemon.
Relocation is what fixes it. The daemon never executes from the install directory: the CLI copies the image to <cache root>/runtime/fbuild-daemon/<hash>/fbuild-daemon.exe and spawns that. The only mapped file is under the user data dir, so the installer can freely replace Scripts\fbuild.exe and its alias. The next CLI invocation of the new version hashes a different image, relocates it to a new <hash> dir, and its version-keyed endpoint starts a new daemon; the old daemon keeps running from its own copy until it idles out, and GC removes its dir only once it's no longer the running image. This is the same contract soldr documents at self_relocate.rs:102-108.
Residual: a CLI process (fbuild.exe) running during the upgrade still pins the install image for its lifetime — same as today, not in scope.
Packaging and measurement
- Wheels can't carry hardlinks. A wheel is a zip; it would store
fbuild-daemonas a second full copy. So the wheel ships onlyfbuild, and the alias is created at runtime by the CLI on first daemon spawn (step 5) — wheels have no post-install hook. If the scripts dir is read-only, the CLI skips the alias and spawns the relocated copy directly (which is namedfbuild-daemon, so stem identity holds). External callers that execfbuild-daemonbefore anyfbuildrun would not find it on a fresh install — that is the compatibility gap the rollout below covers. ci/build_backend.py— stop building/stagingfbuild-daemon(TARGET_DAEMON_NAME/STAGED_DAEMON_PATHat:84-90,_find_fbuild_daemon_from_json:240,_find_fbuild_daemon_by_search:303, the-p fbuild-daemonbuild at:450, the error at:496)..github/workflows/template_native_build.yml— drop-p fbuild-daemon(:261,:292-295), the daemoncpinto staging (:342), the daemon entry in the strip loop (:409), and the daemon debug sidecarsfbuild_daemon.pdb/fbuild-daemon.dSYM/fbuild-daemon.dwp(:432-434).ci/wheel_check.py:31-38— rebase the comment and budgets on one artifact. Current note: ~30 MiB per wheel (fbuild 13.5 + fbuild-daemon 14.5 MiB gzip -9),MAX_WHEEL_BYTES = 40 MiB,MAX_RELEASE_BYTES = 200 MiB. Add an assertion that the wheel contains exactly one native executable (nofbuild-daemon).- Expectation (from the table above; to be measured on the release
distbuild after the change): one ~40 MB binary instead of ~74 MB (≈ 35.7 + 38.1), roughly 30+ MB uncompressed / ~12 MB compressed saved per wheel, ×6 platforms per release. The merged binary's exact size and the new per-wheel/compressed numbers are not yet measured — record them in the PR and set the newwheel_check.pybudgets from them with the same ~30% headroom.
Acceptance
- One native executable per wheel;
wheel_check.pyenforces it. All existing commands, daemon behaviour and HTTP API unchanged. fbuild-daemon …(the alias) behaves identically to today's standalone binary, including--port,--dev,--spawner-cwd, and heap-profile startup ordering (perf: adopt the mimalloc-pprof allocator for heap/leak profiling; verify on-CPU, off-CPU, and symbolization from running-process #1361).- argv[0] dispatch happens before clap and tokio init in
fbuild-cli/src/main.rs; unit tests cover stem classification on Windows and Unix paths. - Windows: with a daemon running,
uv pip install -U fbuild(anduv sync) succeeds; the running daemon's image is under the cache-rootruntime/dir, not the scripts dir. Test this on the Windows CI lane with a real daemon, not a mock. restart_diagreports both the relocated exe and the source exe; sibling-newer-than-running detection still fires after a rebuild.- Endpoint key unchanged by relocation; different versions still isolate (daemon: endpoint isolation follow-up to #940 — key the default endpoint by cache-identity/checkout + deterministic version arbitration #1009); handoff(#1154): finish cold-cache reset with soldr-style daemon ownership #1159 locks unchanged.
- Size measured before/after on the release
distbuild and recorded in the PR.
Phases / rollout
- Library entry + multicall, two binaries still shipped. Refactor the daemon
mainintoentry::run(), addmulticall.rsand themain.rshook. Both binaries still built; provesfbuildrenamed tofbuild-daemonruns the daemon. No packaging change. - Self-relocation behind the spawn path, still two binaries. Lands the Windows-locking fix and the
restart_diag/health changes independently of the size change, and gets a release of soak. - One binary. Drop the
fbuild-daemonbin from build/staging/wheel_check; CLI creates the alias. Release notes tell external callers thatfbuild-daemonnow exists after the firstfbuildinvocation. - Compatibility window (a release or two). Keep the
fbuild-daemonalias indefinitely if cheap (it costs nothing on disk), but audit FastLED and docs for directfbuild-daemonexec on a fresh install and switch them to "spawn viafbuild". Then remove the standalonefbuild-daemon[[bin]]/main.rsshim if dev workflows no longer need it.
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsTriage
Activity
zackees commentedon Nov 2, 2026
Phase 1 of this is up as #1852:
fbuild_daemon::entry::run()+argv[0]multicall infbuild-cli, both binaries still shipped (no packaging change). 4137 unit tests, workspace clippy, both crates' dylint sets, boundary/layout/crate gates all green; e2e proof that a renamedfbuildimage boots the real daemon (health payload + profiler-first ordering preserved). Phases 2-4 (self-relocation, one binary, alias creation) intentionally untouched.