This project is intended to demonstrate a Bazel setup for Rust services with Docker support.
It has the following features:
Install Bazelisk
Install Rustup
Install Cargo Raze
$ cargo install cargo-raze
For cross-compilation from MacOS to Linux musl targets, install musl-cross
$ brew install filosottile/musl-cross/musl-cross
For Bazel remote cache, install https://github.com/buchgr/bazel-remote
$ bazel build //...
$ bazel test //...
$ bazel run //service1:app
$ bazel run //service1:image
⚠️ This only works when your host machine is Linux. Look for cross-compiling below when building on MacOS.
$ bazel run //service1:image --//:release
⚠️ This only works when your host machine is Linux. Look for cross-compiling below when building on MacOS.
$ bazel run //service1:image --platforms //build/platform:musl
Can also be combined with --//:release
.
In order for Rust-Analyzer to work, it is necessary to generate a rust-project.json
file. To do so, run
$ bazel run @rules_rust//tools/rust_analyzer:gen_rust_project
When using VS Code as IDE, install the following extension for Rust-Analyzer: https://marketplace.visualstudio.com/items?itemName=matklad.rust-analyzer
In your VS Code settings.json
, make sure to have the following configuration:
{
"rust-analyzer.checkOnSave.overrideCommand": ["cargo", "check" "--message-format=json"]
}
The Rust-Analyzer extension will pick up the rust-project.json
file which was generated earlier.
⚠️ Make sure to generate an updated rust-project.json
file whenever a new folder or dependency is added. It may also be needed to reload the workspace when the updated structure is not immediately recognized by Rust-Analyzer.
For each of the dependencies listed in any of the Cargo.toml
files, a Bazel build should be generated. This is done automatically by running
$ cargo raze
If not already the case, this will add a cargo
folder in the root of the project. Crates can now be referenced fron within Bazel through //cargo/*NAME*
(eg. //cargo/ferris_says
).
After adding a new external dependency in any of the Cargo.toml
files, run the following steps:
cargo raze --generate-lockfile
bazel build //...
rust-project.json
by running bazel run @rules_rust//tools/rust_analyzer:gen_rust_project
This project provides a basic setup for cross-compiling from MacOS to Linux. The setup is inspired by rust-bazel-cross.
⚠️ A known limitation is that dependencies relying on macros fail to compile. More info here.
When applying the Bazel config flag --config=remote_cache
, Bazel will connect to a remote cache running at localhost:9090
. This server will sync files between a local cache and an AWS S3 bucket.
To start the bazel-remote server, run the script ./scripts/start_bazel_remote.sh
. Provide the following environment variables: