How to use trunk crate/cli using rules_rust
I've got rules_rust all setup and working for my personal website here that id like to b build it as a static site and deploy the built dist/ folder - built with trunk build.
I'm using the latest rules_rust release I think and bzlmod too using the from_cargo approach to pull in crates.
The problem im getting to is if I try and add the trunk crate to the Cargo.toml file and run bazel query @crates//......its not listed for some reason.
I think its because the crate only provides an "installable" CLI binary and not an actual library.
Is there a way to somehow get this trunk CLI executable as a Bazel target, without having to break hermicity/still pull it in using rules_rust? I couldn't find an example of this situation anywhere in the rules_rust docs unfortunately...thats why im asking here.
Thanks in advanced 🙏🏼
You can sort of see what im trying to do in the latest CI run in the deploy job
did you try to enable generate_binaries in from_cargo?
I have found a similar issue. With cargo-audit it was working but not cargo-machete. I went to debugging and found out that if the binary dependency has a lib type the above solution will work. However, if it doesn't then it will filter out.
To make it work it requires a unstable feature from cargo artifact-dependencies.
If the parser allows it you might be able to add it to Cargo.toml.
Alternatively you can define the spec in MODULE.bazel.
The below code is working on latest main:
bindeps = use_extension("@rules_rust//crate_universe:extension.bzl", "crate")
bindeps.spec(package = "trunk", version = "=0.21.5", artifact = "bin")
bindeps.annotation(crate = "trunk", gen_all_binaries = True)
bindeps.from_specs(
name = "bindeps",
host_tools_repo = "rust_host_tools_nightly",
)
use_repo(bindeps, "bindeps")
You can do
bazel run @bindeps//:trunk__trunk -- --help
I'm following this example and it's failing :/
MODULE.bazel
bazel_dep(name = "rules_rust", version = "0.57.1+git-overriden", dev_dependency = True)
git_override(
module_name="rules_rust",
commit="b13b15fe27e7571d655edf9ec910d08c12020315", # Latest commit in main (2025/02/17)
remote="https://github.com/bazelbuild/rules_rust.git"
)
rust = use_extension("@rules_rust//rust:extensions.bzl", "rust")
rust.toolchain(
edition = "2021",
versions = ["1.84.1"],
)
use_repo(rust, "rust_toolchains")
register_toolchains("@rust_toolchains//:all")
bindeps = use_extension("@rules_rust//crate_universe:extension.bzl", "crate")
bindeps.spec(artifact = "bin", package = "diesel_cli", version = "2.2.7")
bindeps.annotation(crate = "diesel_cli", gen_all_binaries = True)
bindeps.from_specs(
name = "bindeps",
host_tools_repo = "rust_host_tools_nightly"
)
use_repo(bindeps, "bindeps")
And then, it fails:
bazel run @bindeps//:diesel_cli-diesel -- --help
error: failed to parse manifest at `/var/folders/jj/j4d2plrj28z03nwl751rr_2c0000gn/T/.tmpLNQfeQ/Cargo.toml`
Caused by:
`artifact = …` requires `-Z bindeps` (diesel_cli)
Error: Failed to generate lockfile
Caused by:
Failed to fetch crates for lockfile: exit status: 101
Here, I read a similar issue and they workaround it by using the following idea, but IDK if here we could apply the same fix or not.
# .cargo/config.toml
[unstable]
bindeps = true
Thanks!
Im having this same issue. It looks like its trying to use the stable channel and not nightly channel.
--cargo", "/private/var/tmp/_bazel_eric.mcbride/79b277eea1629c6c0e66b29d7b082943/external/rules_rust++rust_host_tools+rust_host_tools/bin/cargo", "--rustc", "/private/var/tmp/_bazel_eric.mcbride/79b277eea1629c6c0e66b29d7b082943/external/rules_rust++rust_host_tools+rust_host_tools/bin/rustc
If i invoke that directly I get
cargo 1.86.0 (adf9b6ad1 2025-02-28)
If i look in my external folder i also have a nightly host
private/var/tmp/_bazel_eric.mcbride/79b277eea1629c6c0e66b29d7b082943/external/rules_rust++rust_host_tools+rust_host_tools_nightly/bin/cargo --version
cargo 1.88.0-nightly (a6c604d1b 2025-03-26)
So it looks like rules_rust isnt using the correct cargo / rust. This is my implementation
rust_host_tools = use_extension("@rules_rust//rust:extensions.bzl", "rust_host_tools")
rust_host_tools.host_tools(
name = "rust_host_tools_nightly",
version = "nightly",
)
use_repo(
rust_host_tools,
"rust_host_tools_nightly",
)
bindeps = use_extension("@rules_rust//crate_universe:extension.bzl", "crate")
bindeps.spec(package = "cargo-machete", version = "=0.9.0", artifact = "bin")
bindeps.annotation(crate = "cargo-machete", gen_all_binaries = True)
bindeps.from_specs(
name = "bindeps",
host_tools = "@rust_host_tools_nightly",
)
use_repo(bindeps, "bindeps")