bazelbuild / rules_rust

Rust rules for Bazel
https://bazelbuild.github.io/rules_rust/
Apache License 2.0
623 stars 398 forks source link

[BUG] 0.46.0 Selects wrong Tokio version #2689

Closed marvin-hansen closed 2 weeks ago

marvin-hansen commented 2 weeks ago

This is something I've encounter while converting a WORKSPACE config to MODULE.

When you run the target with Cargo, it builds and runs:

cargo run --bin server

When you run the same target with Bazel, if fails with an error:

bazel run //crates/grpc_server:server

Error:

Running command line: target-bzl/bin/crates/grpc_server/server GreeterServer listening on [::1]:50051 thread 'main' panicked at external/rules_rust~~i~rules_rust_prost__hyper-0.14.26/src/server/tcp.rs:130:24: there is no reactor running, must be called from the context of a Tokio 1.x runtime note: run withRUST_BACKTRACE=1environment variable to display a backtrace

The error just says that the Tokio version mismatches for some dependencies, meaning some dependencies expecting a version 1 whereas something else is there. The error is well explained on SO. However, I've declared version 1.38 as a dependency.

Versions and features of all deps are identical in Cargo and in Bazel.

Also, strangely enough, the same error occurs regardless of using a WORKSPACE or MODULE config.

Repo: https://github.com/marvin-hansen/bazel_rust_example

illicitonion commented 2 weeks ago

Thanks for the super detailed repro!

I suspect the problem here is that the prost rules themselves have a dependency on tokio, and the version we give by default is probably different from the one you're using. You're pulling this in using the register_toolchains("@rules_rust//proto/prost:default_prost_toolchain") line in your MODULE.bazel.

Take a look at https://bazelbuild.github.io/rules_rust/rust_proto.html#customizing-prost-and-tonic-dependencies which specifies how you can supply your own prost toolchain. Basically, you need to supply each of those crates _from your own crate_universe_ so that the versions get resolved together.

I haven't managed to get this successfully working yet, but I suspect if you can get into a place where your MODULE.bazel pulls in protoc-gen-prost, prost-types, protoc-gen-tonic, prost, and tonic, and can point at your version of them rather than rules_rust's, things will start working.

marvin-hansen commented 2 weeks ago

Alright,

thank you for the detailed insight and link to the setup.

As discussed on Slack, I've added a first draft of a custom toolchain Prost config in a separate branch.

https://github.com/marvin-hansen/bazel_rust_example/tree/rust-proto

Toolchain MODULE

Still working on the proto compiler workaround; I add a comment later once that's solved.

Thank you for all your help and taking a look at the underlying issue.

marvin-hansen commented 2 weeks ago

Solved with a correct custom toolchain thanks to @illicitonion

https://github.com/marvin-hansen/bazel_rust_example/blob/main/build/prost_toolchain/BUILD.bazel

Updating the Bazelmod docs.