bazelbuild / bazelbuild/rules_rust

Prost rules fail to build due to Axum build script failing to build

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

Description

I feel like I may be doing something wrong now, but I can't seem to get the new prost rules to compile my protos. Even a simple, mostly empty workspace containing only a `.proto` files fails to compile, see the error message below.

Minimum repro workspace files:

`WORKSPACE`:

```bzl
workspace(name = "repro")

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

http_archive(
name = "rules_rust",
sha256 = "db89135f4d1eaa047b9f5518ba4037284b43fc87386d08c1d1fe91708e3730ae",
urls = ["https://github.com/bazelbuild/rules_rust/releases/download/0.27.0/rules_rust-v0.27.0.tar.gz"],
)

load("@rules_rust//rust:repositories.bzl", "rules_rust_dependencies", "rust_register_toolchains")

rules_rust_dependencies()

rust_register_toolchains()

load("@rules_rust//proto/prost:repositories.bzl", "rust_prost_dependencies")

rust_prost_dependencies()

load("@rules_rust//proto/prost:transitive_repositories.bzl", "rust_prost_transitive_repositories")

rust_prost_transitive_repositories()

load("@rules_rust//crate_universe:repositories.bzl", "crate_universe_dependencies")

crate_universe_dependencies()

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

crates_repository(
name = "crate_index",
cargo_lockfile = "//:Cargo.lock",
lockfile = "//:cargo-bazel-lock.json",
annotations = {
"protoc-gen-prost": [crate.annotation(
gen_binaries = ["protoc-gen-prost"],
)],
"protoc-gen-tonic": [crate.annotation(
gen_binaries = ["protoc-gen-tonic"],
)],
},
packages = {
"prost": crate.spec(
version = "0.12",
),
"prost-types": crate.spec(
version = "0.12",
),
"protoc-gen-prost": crate.spec(
version = "0.2",
),
"protoc-gen-tonic": crate.spec(
version = "0.3",
),
"tonic": crate.spec(
version = "0.10",
),
},
)

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

crate_repositories()

register_toolchains("//toolchains:prost_toolchain")
```

`BUILD.bazel`:

```bzl
load("@rules_proto//proto:defs.bzl", "proto_library")
load("@rules_rust//proto/prost:defs.bzl", "rust_prost_library")

proto_library(
name = "repro",
srcs = ["repro.proto"],
)

rust_prost_library(
name = "repro_rs",
proto = ":repro",
)
```

`repro.proto`:

```proto
message M {}
```

`toolchains/BUILD.bazel`:

```bzl
load("@rules_rust//proto/prost:defs.bzl", "rust_prost_toolchain")
load("@rules_rust//rust:defs.bzl", "rust_library_group")

rust_library_group(
name = "prost_runtime",
deps = [
"@crate_index//:prost",
],
)

rust_library_group(
name = "tonic_runtime",
deps = [
":prost_runtime",
"@crate_index//:tonic",
],
)

rust_prost_toolchain(
name = "prost_toolchain_impl",
prost_plugin = "@crate_index//:protoc-gen-prost__protoc-gen-prost",
prost_runtime = ":prost_runtime",
prost_types = "@crate_index//:prost-types",
proto_compiler = "@com_google_protobuf//:protoc",
tonic_plugin = "@crate_index//:protoc-gen-tonic__protoc-gen-tonic",
tonic_runtime = ":tonic_runtime",
)

toolchain(
name = "prost_toolchain",
toolchain = "prost_toolchain_impl",
toolchain_type = "@rules_rust//proto/prost:toolchain_type",
)
```

`Cargo.lock` and `cargo-bazel-lock.json` can be empty and generated:

```sh
CARGO_BAZEL_REPIN=1 bazelisk sync --only=crate_index
```

Then trying to build the `repro_rs` rule fails:

```log
error[E0428]: the name `main` is defined multiple times
--> external/crate_index__axum-0.6.20/build.rs:7:1
|
2 | fn main() {
| --------- previous definition of the value `main` here
...
7 | fn main() {}
| ^^^^^^^^^ `main` redefined here
|
= note: `main` must be defined only once in the value namespace of this module

error[E0463]: can't find crate for `rustversion`
--> external/crate_index__axum-0.6.20/build.rs:6:3
|
6 | #[rustversion::not(nightly)]
| ^^^^^^^^^^^ can't find crate

error: cannot determine resolution for the attribute macro `rustversion::not`
--> external/crate_index__axum-0.6.20/build.rs:6:3
|
6 | #[rustversion::not(nightly)]
| ^^^^^^^^^^^^^^^^
|
= note: import resolution is stuck, try simplifying macro imports

error[E0463]: can't find crate for `rustversion`
--> external/crate_index__axum-0.6.20/build.rs:1:3
|
1 | #[rustversion::nightly]
| ^^^^^^^^^^^ can't find crate

error: cannot determine resolution for the attribute macro `rustversion::nightly`
--> external/crate_index__axum-0.6.20/build.rs:1:3
|
1 | #[rustversion::nightly]
| ^^^^^^^^^^^^^^^^^^^^
|
= note: import resolution is stuck, try simplifying macro imports

error: aborting due to 5 previous errors

Some errors have detailed explanations: E0428, E0463.
For more information about an error, try `rustc --explain E0428`.
INFO: Elapsed time: 65.878s, Critical Path: 19.44s
INFO: 222 processes: 134 internal, 88 linux-sandbox.
FAILED: Build did NOT complete successfully
```

---

`bazelisk version` shows:

```
Bazelisk version: development
Build label: 6.3.2
Build target: bazel-out/k8-opt/bin/src/main/java/com/google/devtools/build/lib/bazel/BazelServer_deploy.jar
Build time: Tue Aug 8 15:48:33 2023 (1691509713)
Build timestamp: 1691509713
Build timestamp as int: 1691509713
```

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.