rules_rust icon indicating copy to clipboard operation
rules_rust copied to clipboard

Support conditional compilation dependencies

Open tthebst opened this issue 2 years ago • 2 comments

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 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

tthebst avatar Apr 29 '23 16:04 tthebst

From https://github.com/bazelbuild/rules_rust/discussions/1947#discussioncomment-5755216

I don't believe crate_universe knows anything about configurations when it resolves dependencies. So the select statement can never be matched. Similar to @rules_rust//crate_universe/src/context/platforms.rs, I would expect a resolve_cfg_rustc_flags that goes through annotations and finds cfg flags that can be used to enable or disable features. I don't think there will be a way to dynamically enable and disable the dependencies per bazel invocation. Would you be willing to convert this to an Issue? I'd be happy to collaborate on a pull request as well if you'd be open to working on this feature 😄

UebelAndre avatar Apr 29 '23 20:04 UebelAndre

Im gonna try to knock this out. I have a use case for this, for tokio console.

ericmcbride avatar Apr 04 '24 20:04 ericmcbride