I am starting out with a Bazel project in Rust, without Cargo. I am trying to set up a feature-gated, optional dependency.
I have a crates_vendor
set up:
crates_vendor(
name = "crates_io",
cargo_lockfile = ":Cargo.Bazel.lock",
mode = "remote",
packages = {
"tonic": crate.spec(version = "*"),
"serde": crate.spec(version = "1.0"),
"serde_json": crate.spec(version = "1.0"),
},
repository_name = "crates_io",
tags = ["manual"],
)
Now my Rust library is defined as:
rust_library(
name = "foo",
srcs = glob("**/*.rs"),
deps = [
"@crates_io//:serde",
"@crates_io//:serde_json",
],
)
It is @crates_io//:serde_json
that I want to feature-gate.
The Cargo.toml
code for that would be:
[dependencies]
serde_json = { version = "1.0", optional = true }
[features]
json = ["dep:serde_json"]
However, as I mentioned, this is a non-Cargo project.
What would be the correct way of achieving this? Tried diggint through the docs, but can’t figure out.