bazelbuild / bazelbuild/rules_rust

Transitive dependencies don't work when building Prost protos for WASM targets

Open
#2,496 2 comments 1 reaction 0 assignees View on GitHub
awaiting-response proto
Dominant language
Starlark
Stars
843
Forks
651
Avg merge
2d 18h
Merged PRs (30d)
15

Description

Example repo: https://github.com/wmmiii/wasm_prost_test

When using the `rust_prost_library` rule the transitive dependencies work when building for local (Linux, x86) platforms however they do not appear to work when building for `wasm` targets.

See the example repro above for full details, however an abridged version of the project is below:
The following command works,
`bazel build //:rust_bin`
However the following command fails,
`bazel build //:rust_wasm`

BUILD:
```
load("@crate_index//:defs.bzl", "aliases")
load("@rules_proto//proto:defs.bzl", "proto_library")
load("@rules_rust//proto/prost:defs.bzl", "rust_prost_library")
load("@rules_rust//rust:defs.bzl", "rust_binary")
load("@rules_rust//wasm_bindgen/rules_js:defs.bzl", "js_rust_wasm_bindgen")

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

rust_prost_library(
name = "rust_proto",
proto = ":proto_lib",
)

rust_binary(
name = "rust_bin",
srcs = ["main.rs"],
aliases = aliases(),
crate_features = ["js"],
edition = "2021",
deps = [
"//:rust_proto",
"@crate_index//:getrandom",
"@crate_index//:imp",
"@rules_rust//wasm_bindgen/3rdparty:wasm_bindgen",
],
)

js_rust_wasm_bindgen(
name = "rust_wasm",
target = "web",
visibility = ["//editor/src:__pkg__"],
wasm_file = ":rust_bin",
)
```

main.rs:
```
use prost::Message;
use proto_lib::wasm_prost_test::TestProto;
use std::io::Cursor;
use wasm_bindgen::prelude::*;

pub fn main() {
println!("Main");
}

#[wasm_bindgen]
pub fn round_trip() -> String {
let definition = TestProto {
name: "foo".to_string(),
};
let encoded = definition.encode_to_vec();

let decoded = TestProto::decode(&mut Cursor::new(encoded)).unwrap();

return decoded.name;
}
```

Error:
```
$ bazel build //:rust_wasm
INFO: Analyzed target //:rust_wasm (0 packages loaded, 0 targets configured).
ERROR: /home/user/.cache/bazel/_bazel_user/2f06e3936a9a01a1d5e3adde0ef0c906/external/rules_rust_prost__getrandom-0.2.10/BUILD.bazel:13:13: Compiling Rust rlib getrandom v0.2.10 (31 files) failed: (Exit 1): process_wrapper failed: error executing Rustc command (from target @@rules_rust_prost__getrandom-0.2.10//:getrandom) bazel-out/k8-opt-exec-ST-13d3ddad9198/bin/external/rules_rust/util/process_wrapper/process_wrapper --subst 'pwd=${pwd}' -- ... (remaining 23 arguments skipped)

Use --sandbox_debug to see verbose messages from the sandbox and retain the sandbox build root for debugging
error: the wasm*-unknown-unknown targets are not supported by default, you may need to enable the "js" feature. For more information see: https://docs.rs/getrandom/#webassembly-support
--> external/rules_rust_prost__getrandom-0.2.10/src/lib.rs:285:9
|
285 | / compile_error!("the wasm*-unknown-unknown targets are not supported by \
286 | | default, you may need to enable the \"js\" feature. \
287 | | For more information see: \
288 | | https://docs.rs/getrandom/#webassembly-support");
| |________________________________________________________________________^

error[E0433]: failed to resolve: use of undeclared crate or module `imp`
--> external/rules_rust_prost__getrandom-0.2.10/src/lib.rs:341:9
|
341 | imp::getrandom_inner(dest)?;
| ^^^ use of undeclared crate or module `imp`

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0433`.
Target //:rust_wasm failed to build
Use --verbose_failures to see the command lines of failed build steps.
INFO: Elapsed time: 0.373s, Critical Path: 0.17s
INFO: 19 processes: 18 internal, 1 linux-sandbox.
ERROR: Build did NOT complete successfully
```

WORKSPACE

```
workspace(name = "wasm_prost_test")

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

###############################################################################
# R U L E S P R O T O
###############################################################################

http_archive(
name = "rules_proto",
sha256 = "dc3fb206a2cb3441b485eb1e423165b231235a1ea9b031b4433cf7bc1fa460dd",
strip_prefix = "rules_proto-5.3.0-21.7",
urls = [
"https://github.com/bazelbuild/rules_proto/archive/refs/tags/5.3.0-21.7.tar.gz",
],
)

load("@rules_proto//proto:repositories.bzl", "rules_proto_dependencies", "rules_proto_toolchains")

rules_proto_dependencies()

rules_proto_toolchains()

###############################################################################
# R U L E S R U S T
###############################################################################

http_archive(
name = "rules_rust",
integrity = "sha256-GuRaQT0LlDOYcyDfKtQQ22oV+vtsiM8P0b87qsvoJts=",
urls = ["https://github.com/bazelbuild/rules_rust/releases/download/0.39.0/rules_rust-v0.39.0.tar.gz"],
)

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

rust_repositories()

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

rust_prost_dependencies()

rust_prost_register_toolchains()

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

rust_prost_transitive_repositories()

load("@rules_rust//wasm_bindgen:repositories.bzl", "rust_wasm_bindgen_dependencies", "rust_wasm_bindgen_register_toolchains")

rust_wasm_bindgen_dependencies()

rust_wasm_bindgen_register_toolchains()

###############################################################################
# R U S T C R A T E S
###############################################################################

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",
packages = {
"imp": crate.spec(version = "0.1.0"),
"getrandom": crate.spec(
version = "0.2",
features = ["js"],
),
},
render_config = render_config(
default_package_name = "",
),
)

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

crate_repositories()
```

MODULE.bazel

```
module(
name = "wasm_prost_test",
version = "0.1.0",
)

###############################################################################
# R U L E S J S
###############################################################################

bazel_dep(name = "aspect_rules_js", version = "1.34.1")

####### Node.js version #########
# By default you get the node version from DEFAULT_NODE_VERSION in @rules_nodejs//nodejs:repositories.bzl
# Optionally you can pin a different node version:
bazel_dep(name = "rules_nodejs", version = "5.8.2")
node = use_extension("@rules_nodejs//nodejs:extensions.bzl", "node")
node.toolchain(node_version = "16.14.2")
#################################

npm = use_extension("@aspect_rules_js//npm:extensions.bzl", "npm", dev_dependency = True)

npm.npm_translate_lock(
name = "npm",
pnpm_lock = "//:pnpm-lock.yaml",
verify_node_modules_ignored = "//:.bazelignore",
)

use_repo(npm, "npm")
```

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.