tweag/rules_nixpkgs

Can't build rust wasm targets

snapbug opened this issue · 1 comments

Describe the bug
Can't build rust wasm targets.

To Reproduce
I used the following changes to the examples/toolchains/rust:

diff --git i/examples/toolchains/rust/BUILD w/examples/toolchains/rust/BUILD
index 092e9a2..66aebdf 100644
--- i/examples/toolchains/rust/BUILD
+++ w/examples/toolchains/rust/BUILD
@@ -1,4 +1,5 @@
 load("@rules_rust//rust:defs.bzl", "rust_binary")
+load("@rules_rust//wasm_bindgen:defs.bzl", "rust_wasm_bindgen")
 load("@crate_index//:defs.bzl", "all_crate_deps")

 exports_files([
@@ -12,3 +13,9 @@ rust_binary(
     srcs = ["hello.rs"],
     deps = all_crate_deps(normal = True),
 )
+
+rust_wasm_bindgen(
+    name = "wasm",
+    target = "web",
+    wasm_file = ":hello",
+)
diff --git i/examples/toolchains/rust/WORKSPACE w/examples/toolchains/rust/WORKSPACE
index fa6e669..8430ff3 100644
--- i/examples/toolchains/rust/WORKSPACE
+++ w/examples/toolchains/rust/WORKSPACE
@@ -66,6 +66,13 @@ load("@rules_rust//rust:repositories.bzl", "rust_repositories")

 rust_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()
+
+
 # crate_universe as a way of governing deps
 load("@rules_rust//crate_universe:repositories.bzl", "crate_universe_dependencies")

trying to build bazel build --config=nix //:wasm fails with:

❯ bazel build --config=nix //:wasm --verbose_failures
/nix/store/k7cyp2af28k8505kfdhcyyjnhv2xl8n0-bazel-rust-toolchain
INFO: Analyzed target //:wasm (2 packages loaded, 317 targets configured).
INFO: Found 1 target...
ERROR: /home/grawlix/.cache/bazel/_bazel_grawlix/e607e8e55d4b1c2cd07980276d90b4b7/external/crate_index__bitflags-1.3.2/BUILD.bazel:20:13: Compiling Rust rlib bitflags v1.3.2 (20 files) failed: (Exit 1): process_wrapper failed: error executing command (from target @crate_index__bitflags-1.3.2//:bitflags)
  (cd /home/grawlix/.cache/bazel/_bazel_grawlix/e607e8e55d4b1c2cd07980276d90b4b7/sandbox/linux-sandbox/550/execroot/__main__ && \
  exec env - \
    CARGO_CFG_TARGET_ARCH=wasm32 \
    CARGO_CFG_TARGET_OS=unknown \
    CARGO_CRATE_NAME=bitflags \
    CARGO_MANIFEST_DIR='${pwd}/external/crate_index__bitflags-1.3.2' \
    CARGO_PKG_AUTHORS='' \
    CARGO_PKG_DESCRIPTION='' \
    CARGO_PKG_HOMEPAGE='' \
    CARGO_PKG_NAME=bitflags \
    CARGO_PKG_VERSION=1.3.2 \
    CARGO_PKG_VERSION_MAJOR=1 \
    CARGO_PKG_VERSION_MINOR=3 \
    CARGO_PKG_VERSION_PATCH=2 \
    CARGO_PKG_VERSION_PRE='' \
    SYSROOT=bazel-out/k8-fastbuild-ST-38b09129ed05/bin/external/rust_linux_x86_64__wasm32-unknown-unknown__stable_tools/rust_toolchain \
  bazel-out/k8-opt-exec-C7777A24-ST-da94b34a892a/bin/external/rules_rust/util/process_wrapper/process_wrapper --subst 'pwd=${pwd}' -- bazel-out/k8-fastbuild-ST-38b09129ed05/bin/external/rust_linux_x86_64__wasm32-unknown-unknown__stable_tools/rust_toolchain/bin/rustc external/crate_index__bitflags-1.3.2/src/lib.rs '--crate-name=bitflags' '--crate-type=rlib' '--error-format=human' '--codegen=metadata=-146562053' '--c
odegen=extra-filename=-146562053' '--out-dir=bazel-out/k8-fastbuild-ST-38b09129ed05/bin/external/crate_index__bitflags-1.3.2' '--codegen=opt-level=0' '--codegen=debuginfo=0' '--remap-path-prefix=${pwd}=' '--emit=dep-info,link' '--color=always' '--target=wasm32-unknown-unknown' --cfg 'feature="default"' -L bazel-out/k8-fastbuild-ST-38b09129ed05/bin/external/rust_linux_x86_64__wasm32-unknown-unknown__stable_tools/rus
t_toolchain/lib/rustlib/wasm32-unknown-unknown/lib '--cap-lints=allow' '--edition=2018' '--sysroot=bazel-out/k8-fastbuild-ST-38b09129ed05/bin/external/rust_linux_x86_64__wasm32-unknown-unknown__stable_tools/rust_toolchain')
# Configuration: 0590131d7658b76a08b19d13b2b4ddbfb70bcfab1a21c0745038366e0d666ffe
# Execution platform: @rules_nixpkgs_core//platforms:host

Use --sandbox_debug to see verbose messages from the sandbox and retain the sandbox build root for debugging
Error: ProcessWrapperError("failed to spawn child process: No such file or directory (os error 2)")
Target //:wasm failed to build

where it's (I think?) trying to use the downloaded toolchain to execute rather than the nix-provided toolchain.

I tried to ensure that the wasm32-unknown-unknown target was available to the rustc package installed with the following:

diff --git i/toolchains/rust/rust.bzl w/toolchains/rust/rust.bzl
index f555697..31b5fce 100644
--- i/toolchains/rust/rust.bzl
+++ w/toolchains/rust/rust.bzl
@@ -15,7 +15,26 @@ load("@rules_nixpkgs_core//:util.bzl", "ensure_constraints")
 # Nix generation is used to dynamically compute both Linux and Darwin environments
 _rust_nix_contents = """\
 let
-    pkgs = import <nixpkgs> {{ config = {{}}; overrides = []; }};
+    pkgs = import <nixpkgs> {{ config = {{}}; overrides = self: super:
+      {{
+        rustc = super.rustc.override {{
+          targets = super.rustc.targets // {{
+            wasm32-unknown-unknown = super.rustc.targets.wasm32-unknown-unknown // {{
+              stdenv = super.stdenv.override {{
+                targetPlatform = {{
+                  parsed = {{
+                    cpu = {{ name = "wasm32"; }};
+                    vendor = {{ name = "unknown"; }};
+                    kernel = {{ name = "unknown"; }};
+                    abi = {{ name = "unknown"; }};
+                  }};
+                }};
+              }};
+            }};
+          }};
+        }};
+      }};
+    }};
     rust = pkgs.rust;
     os = rust.toTargetOs pkgs.stdenv.targetPlatform;
     build-triple = rust.toRustTargetSpec pkgs.stdenv.buildPlatform;

fails with the same error (although crate_index__foreign-types-shared-0.1.1 rather than crate_index__bitflags-1.3.2, but I think this is due to parallel building rather than any actual difference.)

Removing the rust_wasm_bindgen_register_toolchains() call from WORKSPACE (in a last ditch kinda attempt) yielded:

ERROR: /home/grawlix/repos/github.com/tweag/rules_nixpkgs/examples/toolchains/rust/BUILD:18:18: While resolving toolchains for target //:wasm: No matching toolchains found for types @rules_rust//wasm_bindgen:toolchain_type.
To debug, rerun with --toolchain_resolution_debug='@rules_rust//wasm_bindgen:toolchain_type'

and doing so results in:

❯ bazel build --config=nix //:wasm --verbose_failures --toolchain_resolution_debug='@rules_rust//wasm_bindgen:toolchain_type'
INFO: Build option --toolchain_resolution_debug has changed, discarding analysis cache.
INFO: ToolchainResolution:   Type @rules_rust//wasm_bindgen:toolchain_type: target platform @rules_nixpkgs_core//platforms:host: No toolchains found.
ERROR: /home/grawlix/repos/github.com/tweag/rules_nixpkgs/examples/toolchains/rust/BUILD:18:18: While resolving toolchains for target //:wasm: No matching toolchains found for types @rules_rust//wasm_bindgen:toolchain_type.
To debug, rerun with --toolchain_resolution_debug='@rules_rust//wasm_bindgen:toolchain_type'
If platforms or toolchains are a new concept for you, we'd encourage reading https://bazel.build/concepts/platforms-intro.

Expected behavior
Expect to be able to build wasm libraries using the tooling provided by rules_rust.

Environment

  • OS name + version: NixOS 23.11.5541.56528ee42526 (Tapir) x86_64
  • Version of the code: 65a48d2

Additional context

Created #496 to track updates/attempts at addressing this issue. I can only work part time on this, and making a best effort to help resolve my own issue 🙂