Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compiling Rocket in Bazel

I'm attempting to get a working prototype of the following scenario:

  • Language: Rust (rustc 1.45.0-nightly (ad4bc3323 2020-06-01))
  • Framework: Rocket v0.4.4
  • Build Tool: Bazel
  • Platform: Mac OS X / Darwin x64

Running bazel build //web-api yields the below error. I believe, based on looking at the Cargo.lock file it is because Rocket's dependency on the hyper library specifies a dependency on the log 0.3.9 library. For whatever reason it is not using the more recent log=0.4.x. That said, I don't know why it's pulling this library since, if I build it manually, it works fine.

ERROR: /private/var/tmp/_bazel_nathanielford/2a39169ea9f6eb02fe788b12f9eae88f/external/raze__log__0_3_9/BUILD.bazel:27:1: error executing shell command: '/bin/bash -c CARGO_MANIFEST_DIR=$(pwd)/external/raze__log__0_3_9 external/rust_darwin_x86_64/bin/rustc "$@" --remap-path-prefix="$(pwd)"=__bazel_redacted_pwd  external/raze__log__0_3_9/src/lib.rs -...' failed (Exit 1) bash failed: error executing command /bin/bash -c 'CARGO_MANIFEST_DIR=$(pwd)/external/raze__log__0_3_9 external/rust_darwin_x86_64/bin/rustc "$@" --remap-path-prefix="$(pwd)"=__bazel_redacted_pwd' '' external/raze__log__0_3_9/src/lib.rs ... (remaining 24 argument(s) skipped)

Use --sandbox_debug to see verbose messages from the sandbox
error[E0425]: cannot find function `set_logger` in crate `log`
   --> external/raze__log__0_3_9/src/lib.rs:731:16
    |
731 |     match log::set_logger(&ADAPTOR) {
    |                ^^^^^^^^^^ not found in `log`
    |
help: consider importing this function
    |
204 | use set_logger;
    |

The following is my directory structure:

/
|-WORKSPACE
|-BUILD      # Empty
|-web-api/
| |-BUILD
| |-src/
| | |-main.rs
| |-cargo/
|   |-Cargo.toml
|   |-Cargo.lock
|   |-BUILD.bazel
|   |-remote/
|     |-... (Cargo-raze files)

In order to set up the cargo-raze I did the following, following instructions from the github page.:

$ cd web-api/cargo
$ cargo generate-lockfile  
$ cargo vendor --versioned-dirs --locked
$ cargo raze

(The generate-lockfile is what creates the Cargo.lock file, and the cargo raze is what creates the BUILD.bazel file and all the contents of the remote sub directory.)

And then to execute the bazel build I go back to the root and run bazel build //web-api, which produces the above error.

This is my WORKSPACE file:

workspace(name = "rocket-bazel")

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

http_archive(
    name = "io_bazel_rules_rust",
    sha256 = "f21c67fc2fef9d57fa3c81fde1defd9e57d451883388c0a469ec1c470fd30dcb",
    strip_prefix = "rules_rust-master",
    urls = [
        "https://github.com/bazelbuild/rules_rust/archive/master.tar.gz"
    ],
)

http_archive(
    name = "bazel_skylib",
    sha256 = "9a737999532daca978a158f94e77e9af6a6a169709c0cee274f0a4c3359519bd",
    strip_prefix = "bazel-skylib-1.0.0",
    url = "https://github.com/bazelbuild/bazel-skylib/archive/1.0.0.tar.gz",
)

load("@io_bazel_rules_rust//rust:repositories.bzl", "rust_repositories")
rust_repositories(version="nightly", iso_date="2020-06-02")

load("@io_bazel_rules_rust//:workspace.bzl", "bazel_version")
bazel_version(name = "bazel_version")

load("//web-api/cargo:crates.bzl", "raze_fetch_remote_crates")
raze_fetch_remote_crates()

This is my web-api/BUILD file:

load("@io_bazel_rules_rust//rust:rust.bzl", "rust_binary")

rust_binary(
    name = "web-api",
    srcs = ["src/main.rs"],
    deps = [
        "//web-api/cargo:rocket",
    ],
)

And my web-api/cargo/Cargo.toml file:

load("@io_bazel_rules_rust//rust:rust.bzl", "rust_binary")

rust_binary(
    name = "web-api",
    srcs = ["src/main.rs"],
    deps = [
        "//web-api/cargo:rocket",
    ],
)

I've run out of ideas as to what to try. I can get this to compile without Bazel, just using rust (though obviously the files are in slightly different places). I can get it to compile inside a Docker container. I just can't get Bazel (necessarily with cargo raze, either in vendor or remote mode) to run successfully: I assume that there is some mismatch in compile target or the nightly build that is not being properly set - but I'm not sure how to diagnose or get past that.

Here is a link to a repository with the files/structure I tried.

like image 373
Nathaniel Ford Avatar asked Oct 27 '22 21:10

Nathaniel Ford


1 Answers

I had a similar issue when I made a minimal Bazel workspace with rust and the log crate together with env_logger crate. I found a similar issue when you try to compile without features = ["std"]. I then tried to enable that in Cargo.toml on the log dependency without success.

My solution is that in Cargo.toml under [raze] I added:

default_gen_buildrs = true

I could trace it down to that when default_gen_buildrs flag is not set in the generated log crate the BUILD.bazel file did not have a cargo_build_script definition or this:

crate_features = [
      "std",
    ],
like image 140
Anders.L Avatar answered Oct 29 '22 15:10

Anders.L