bazelbuild / bazelbuild/rules_rust

Support conditional compilation dependencies

Open
#1,950 2 comments 0 reactions 0 assignees View on GitHub
crate-universe enhancement needs-triage
Dominant language
Starlark
Stars
843
Forks
651
Avg merge
2d 18h
Merged PRs (30d)
15

Description

Enable support for the selection of conditional compilation dependencies.

### Discussed in https://github.com/bazelbuild/rules_rust/discussions/1947

Originally posted by **tthebst** April 28, 2023
I'm trying to compile tokio with `--cfg tokio_unstable` with rust rules. This works fine by adding it in the `crate.annotations` rustflag options. It also seems to get picked up by when compiling because I see that the compiler invocations have (`bazel build -s --config local @crate_index//:tokio`). But when compiling I get an error `use of undeclared crate or module tracing` indicating that the tracing [dependency](https://github.com/tokio-rs/tokio/blob/master/tokio/Cargo.toml#L116) was not added when compiling. In the bazel lock file I see this:

```
"deps": {
...
"selects" :{
....
"cfg(tokio_unstable)": [
{
"id": "tracing 0.1.38",
"target": "tracing"
}
],
}
}
```

How can I instruct bazel to add the tracing dependency when compiling?

I will try to come up with a small repro for this.

Reproduce:

WORKSPACE.bazel

```
http_archive(
name = "rules_rust",
sha256 = "25209daff2ba21e818801c7b2dab0274c43808982d6aea9f796d899db6319146",
urls = ["https://github.com/bazelbuild/rules_rust/releases/download/0.21.1/rules_rust-v0.21.1.tar.gz"],
)
load("@rules_rust//rust:repositories.bzl", "rules_rust_dependencies", "rust_register_toolchains")

rules_rust_dependencies()

rust_register_toolchains()

load("@rules_rust//crate_universe:defs.bzl", "crate", "crates_repository", "render_config")

crates_repository(
name = "crate_index",
cargo_lockfile = "//:Cargo.lock",
lockfile = "//:Cargo.Bazel.lock",
annotations = {
"tokio": [crate.annotation(
rustc_flags = ["--cfg", "tokio_unstable"],
)],
},
packages = {
"tokio": crate.spec(
version = "1.12.0",
features = ["full", "tracing"],
),
},
)

load("@crate_index//:defs.bzl", "crate_repositories")

crate_repositories()
```

Compile tokio

```
CARGO_BAZEL_REPIN=true bazel build -s @crate_index//:tokio
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.