Skip to content

Build with Bazel (bzlmod) alongside Cargo - #12

Merged
pawelchcki merged 17 commits into
mainfrom
bazel-migration
Aug 9, 2026
Merged

pawelchcki merged 17 commits into
mainfrom
bazel-migration

Conversation

@pawelchcki

@pawelchcki pawelchcki commented Aug 8, 2026 •

Copy link
Copy Markdown
Owner

Adds a bzlmod build covering the whole repo — the Rust workspace via rules_rs, and the Flutter plugin and example via the rules_flutter fork. The Cargo workflow is untouched and still the path that ships the Android .so through the Gradle shim.

The two ruleset fixes this needs landed in pawelchcki/rules_flutter#1; MODULE.bazel pins the fork by commit via git_override, so no sibling checkout is required.

What is built

Rust: razemify-core in three feature variants (bare for wasm, rembg for the CLI, flutter_ffi for the plugin), razemify-cli, razemify-wasm, xtask, and the plugin's host shared/static libs. Flutter: the plugin with flutter_analyze_test + dart_format_test, and the example with its widget test. No APK — Gradle still owns that, along with the Android .so.

Things worth reviewing

ONNX Runtime is not vendored or linked. razemify-core enables ort's load-dynamic, which turns on ort-sys/disable-linking, so nothing links against it at build time and its build script is skipped entirely. The library is fetched as a pinned per-platform http_archive and handed to the CLI at runtime via ORT_DYLIB_PATH. The pinned version tracks what the resolved ort-sys declares in its build/download/dist.tsv — 1.28.0 for ort 2.0.0-rc.13 — so it must be re-derived whenever ort moves, not assumed.

third_party/wasm_bindgen exists because bindgen schema versions must match exactly. rules_rs bundles a 0.2.105 CLI while the workspace links wasm-bindgen 0.2.126, which fails with a schema-version mismatch. This pins a matching CLI and registers its toolchain ahead of the bundled one. Keep its Cargo.toml in lockstep with the workspace's wasm-bindgen version.

The RBE platform pins BuildBuddy's stock Ubuntu 20.04 image. The default executor image's glibc predates 2.28, which binaries from the hermetic llvm and Rust toolchains require, so --config=remote died with GLIBC_2.28 not found on every remote Rust action. This is a stock published image, not a custom one.

The example's pubspec.lock is deleted. It pinned transitive versions that conflicted with the plugin's own resolution (async 2.11.0 vs 2.13.1), which the pub extension rejects as a hard error. pub_deps.json is the pinned record for the Bazel build, and flutter pub get regenerates the lock for the Gradle path.

Source fixes the dependency bumps forced. Two dead Dart imports and a library name that flutter_lints 6.0 flags; a clippy::unnecessary_sort_by that CI's -D warnings rejects under Rust 1.97 (pre-existing — confirmed by stashing). The example's widget test was still the generated plugin template asserting 'Running on:' text the app never renders, so it failed independently of this change and is rewritten as a real smoke test.

Verification

From a cold bazel clean --expunge, so every hash, download, and toolchain was re-fetched:

  • bazel build //... — 3572 actions, clean
  • bazel test //... with --nocache_test_results so all 4 tests genuinely execute rather than reporting (cached)
  • Remote cache: a second cold build served 2362/3572 actions from BuildBuddy against 23 compiled locally (the first was 2 vs 2383), which demonstrates both reads and writes
  • Remote execution with --noremote_accept_cached to force real work rather than cache replay: 1736 of 2512 actions executed on BuildBuddy. Under --config=remote the Flutter targets correctly stay local, per the ruleset's no-remote-exec tagging
  • The CLI loads a real u2netp ONNX model and writes output, exercising ORT_DYLIB_PATH end to end
  • The wasm glue exports { initSync, __wbg_init as default } — the exact wasm-pack --target web shape
  • cargo check/fmt/clippy -D warnings all clean, and the tree builds against a clean clone of the fork branch, which is the configuration CI uses

Follow-ups

  • Android cdylib via Bazel needs an NDK cc toolchain; Gradle keeps shipping the .so
  • Bazel -c opt is not Cargo's opt-level=z + lto; add rustc flags if wasm size parity matters (5.1 MB opt vs 17 MB fastbuild today)
  • CI needs a BUILDBUDDY_API_KEY secret to use the remote cache; without it the job builds with remote disabled rather than failing

Adds a bzlmod build covering the whole repo: the Rust workspace via
rules_rs, and the Flutter plugin and its example via the rules_flutter
fork. The Cargo workflow is unchanged and still the path that ships the
Android .so through the Gradle shim.

Rust targets are razemify-core (three feature variants: bare for wasm,
rembg for the CLI, flutter_ffi for the plugin), razemify-cli,
razemify-wasm, xtask, and the plugin's host shared/static libs.

ONNX Runtime is fetched as a pinned http_archive per platform and handed
to the CLI at runtime through ORT_DYLIB_PATH. razemify-core enables ort's
load-dynamic, which turns on ort-sys/disable-linking, so nothing links
against it at build time and its build script is skipped. The pinned
version tracks what the resolved ort-sys declares in its dist.tsv --
1.28.0 for ort 2.0.0-rc.13 -- and must be re-derived whenever ort moves.

third_party/wasm_bindgen pins the wasm-bindgen CLI at the same version as
the wasm-bindgen crate and registers its toolchain ahead of the one
bundled with rules_rs. The bindgen schema versions must match exactly, and
the bundled CLI (0.2.105) trails the crate (0.2.126).

BuildBuddy provides the remote cache and build event stream on every
build; remote execution is opt-in via --config=remote. The RBE platform
pins BuildBuddy's stock Ubuntu 20.04 image because the default executor
image's glibc predates 2.28, which binaries from the hermetic llvm and
Rust toolchains require. Credentials stay out of the repo: .bazelrc
try-imports a gitignored user.bazelrc (see user.bazelrc.example).

Dependencies move to current versions -- Bazel 9.2.0, Rust 1.97.1,
Flutter 3.44.7, cargo update across the workspace, and pub upgrades --
which required a few source fixes: two dead Dart imports and a library
name that flutter_lints 6.0 now flags, and a clippy lint that CI's
-D warnings rejects under Rust 1.97.

The example's pubspec.lock is dropped and its widget test rewritten. The
lock pinned transitive versions that conflicted with the plugin's own
resolution, and pub_deps.json is the pinned record for the Bazel build;
the test was still the generated plugin template asserting text the app
never renders, so it failed independently of this change.
Runs bazel build and test on top of the existing Cargo jobs, which is
what covers the Flutter analyze/format gates and the example widget test.

MODULE.bazel reaches the rules_flutter fork through local_path_override
at a path outside $GITHUB_WORKSPACE, where actions/checkout cannot write,
so the fork is cloned there directly. RULES_FLUTTER_REF pins the fork
branch carrying the path-dependency and engine-version fixes; point it at
main once that merges, and delete the arrangement entirely if MODULE.bazel
moves to git_override.

Without a BUILDBUDDY_API_KEY secret -- pull requests from forks never get
one -- the job writes a user.bazelrc that switches the remote cache and
BES off, rather than leaving them to fail against BuildBuddy anonymously.
The WASM Build Check job fails in wasm-opt:

  [wasm-validator error in function 7936] unexpected false: Bulk memory
  operations require bulk memory [--enable-bulk-memory]
  Fatal: error validating input

rustc enables bulk-memory, nontrapping-fptoint, multivalue, reference-types
and sign-ext by default for wasm32-unknown-unknown, while the wasm-opt that
wasm-pack downloads validates with every feature off and rejects the input.

This is toolchain drift rather than fallout from the dependency bump: the
enabled features come from rustc alone, and the failure reproduces
identically against main's untouched Cargo.lock. The job pins no Rust
version -- it uses dtolnay/rust-toolchain@stable -- so it broke whenever
stable crossed the release enabling these, and main last ran in February.

Allow exactly the features rustc emits instead of setting wasm-opt = false,
which would keep the build green only by giving up the size optimization
this crate wants. Verified with wasm-pack 0.15.0 and Rust 1.97.1: the build
completes and wasm-opt takes the module down to 2.5 MB.
The Bazel job used BuildBuddy only as a cache; --config=remote was never
exercised there, so remote execution was verified locally and nowhere else.

--noremote_accept_cached is the substance of the check. Without it a warm
cache serves nearly every action -- a plain --config=remote run here
reported 2 remote actions against 2364 cache hits -- and passing proves
nothing about whether remote execution works. Refusing cached results
forces the actions to actually run on BuildBuddy.

The step then asserts on the "N remote" figure in Bazel's process
breakdown rather than on the exit code, because a build served entirely
from cache also exits 0. The pattern deliberately does not match
"remote cache hit", so a cache-only run fails the check instead of
passing it.

Rust targets only: rules_flutter tags its host-bound actions
no-remote-exec, so the Flutter targets stay local by design. The step
skips when no API key is present, since pull requests from forks get no
secrets.
local_path_override required the fork to sit at ../../rules_flutter/main on
every machine that builds this repo, and it silently picked up whatever was
in that working tree -- uncommitted edits included -- so no two checkouts
were guaranteed to build the same thing.

git_override pins the fork by commit, which makes the build reproducible
and removes the path requirement entirely. The fixes this repo needs (the
Flutter 3.44 engine-version patch and path-dependency staging) are now on
the fork's main, so the pinned commit carries them.

This also deletes the CI step that cloned the fork to a path outside
$GITHUB_WORKSPACE, which only existed to satisfy local_path_override.

Verified from a cold bazel clean --expunge: all four tests pass, and the
fetched copy matches the pinned commit rather than the local checkout --
the local working tree has uncommitted changes to flutter/defs.bzl that
are absent from what Bazel fetched.

To develop the ruleset itself, swap local_path_override back in.
CI ran two toolchains side by side: a Bazel job plus five cargo jobs that
installed rustup and wasm-pack. Everything the cargo jobs checked now has a
Bazel equivalent, so ci.yml goes away:

  cargo fmt --all --check  -> //:rustfmt
  cargo clippy -D warnings -> //:clippy
  cargo test --workspace   -> //razemify-core:razemify_core{,_rembg}_test
  cargo build --workspace  -> bazel build //...
  wasm-pack build          -> //razemify-wasm:razemify_wasm

rules_rs wraps the rules_rust build rules but does not re-export its
rustfmt/clippy aspects, so MODULE.bazel pulls the same pinned rules_rust
repo in directly. .bazelrc sets clippy_flag=-Dwarnings, without which
//:clippy reports lints and still succeeds -- verified by reintroducing the
unnecessary_sort_by lint and confirming the gate fails, then reverting.

razemify_core_rembg_test is new: the existing test target covered only the
feature-less build, while cargo test --workspace exercises the rembg one
too, since razemify-cli turns that feature on.

The repository cache is cached across runs because it holds the fetched
archives -- Flutter SDK, Rust toolchains, ONNX Runtime, every crate -- that
the BuildBuddy action cache cannot cover: repository rules run outside the
action graph, so their downloads repeat on every cold runner. Keyed on
MODULE.bazel.lock, which pins exactly what gets fetched.

Note this drops CI coverage of the cargo and wasm-pack paths themselves,
which still ship: Gradle builds the Android .so through cargo, and xtask
still bundles releases with wasm-pack.
It was the most expensive step in the job at 142s, because
--noremote_accept_cached deliberately refuses the remote cache that makes
every other step fast. Remote execution is opt-in via --config=remote and
is not what CI builds with, so paying that on every run bought little.

--config=remote is untouched in .bazelrc and still works on demand.
@pawelchcki

Copy link
Copy Markdown
Owner Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: f3e6ba49b1

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread flutter_razemify/android/build.gradle Resolved
Comment thread .bazelrc Resolved
Comment thread third_party/onnxruntime/BUILD.bazel Resolved
Comment thread platforms/BUILD.bazel Resolved
Comment thread BUILD.bazel Resolved
Comment thread .github/workflows/bazel.yml Resolved
Comment thread flutter_razemify/example/pubspec.yaml Resolved
Comment thread flutter_razemify/BUILD.bazel Resolved
@pawelchcki

Copy link
Copy Markdown
Owner Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: c1870dcc15

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread flutter_razemify/example/pubspec.yaml Resolved
@pawelchcki

Copy link
Copy Markdown
Owner Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 5b43b09f67

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread flutter_razemify/BUILD.bazel Resolved
@pawelchcki
pawelchcki merged commit d206876 into main Aug 9, 2026
1 check passed
@pawelchcki
pawelchcki deleted the bazel-migration branch August 9, 2026 04:26
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.

1 participant