Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support conditional compilation dependencies #1950

Open
tthebst opened this issue Apr 29, 2023 Discussed in #1947 · 2 comments
Open

Support conditional compilation dependencies #1950

tthebst opened this issue Apr 29, 2023 Discussed in #1947 · 2 comments

Comments

@tthebst
Copy link

tthebst commented Apr 29, 2023

Enable support for the selection of conditional compilation dependencies.

Discussed in #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
@UebelAndre
Copy link
Collaborator

From #1947 (reply in thread)

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 😄

@ericmcbride
Copy link
Contributor

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants