bazelbuild / rules_rust

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

Unable to build external crate swagger #2252

Open paulfariello opened 12 months ago

paulfariello commented 12 months ago

Current version (0.30) is unable to build a simple swagger external crate.

Step to reproduce

WORKSPACE

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

http_archive(
    name = "rules_rust",
    sha256 = "6357de5982dd32526e02278221bb8d6aa45717ba9bbacf43686b130aa2c72e1e",
    urls = ["https://github.com/bazelbuild/rules_rust/releases/download/0.30.0/rules_rust-v0.30.0.tar.gz"],
)

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

rules_rust_dependencies()

rust_register_toolchains(
    edition = "2021"
)

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

crate_universe_dependencies()

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

crates_repository(
    name = "crate_index",
    cargo_lockfile = "//:Cargo.lock",
    lockfile = "//:Cargo.Bazel.lock",
    packages = {
        "swagger": crate.spec(
            version = "6.4.1",
            features = ["server"],
        ),
    }
)

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

crate_repositories()

and simply run:

CARGO_BAZEL_REPIN=true bazel build @crate_index//:swagger

The following error is raised:

INFO: Analyzed target @crate_index//:swagger (152 packages loaded, 2306 targets configured).
INFO: Found 1 target...
ERROR: [...]/a1d0b31371ba47b46ce907c6b304e7d4/external/crate_index__swagger-6.4.1/BUILD.bazel:20:13: Compiling Rust rlib swagger v6.4.1 (16 files) failed: (Exit 1): process_wrapper failed: error executing command (from target @crate_index__swagger-6.4.1//:swagger) bazel-out/k8-opt-exec-2B5CBBC6/bin/external/rules_rust/util/process_wrapper/process_wrapper --arg-file ... (remaining 96 arguments skipped)

Use --sandbox_debug to see verbose messages from the sandbox and retain the sandbox build root for debugging
error[E0433]: failed to resolve: use of undeclared crate or module `hyper`
 --> external/crate_index__swagger-6.4.1/src/body.rs:3:5
  |
3 | use hyper::body::Bytes;
  |     ^^^^^ use of undeclared crate or module `hyper`
[...]

Details

Swagger crate has a pretty simple Cargo.toml https://github.com/Metaswitch/swagger-rs/blob/master/Cargo.toml

Mainly it depends on hyper (not optional):

[dependencies]
hyper = "0.14"

The corresponding generated BUILD file is:

###############################################################################
# @generated
# DO NOT MODIFY: This file is auto-generated by a crate_universe tool. To 
# regenerate this file, run the following:
#
#     bazel sync --only=crate_index
###############################################################################

load("@rules_rust//rust:defs.bzl", "rust_library")

# buildifier: disable=bzl-visibility
load("@rules_rust//crate_universe/private:selects.bzl", "selects")

package(default_visibility = ["//visibility:public"])

# licenses([
#     "TODO",  # Apache-2.0
# ])

rust_library(
    name = "swagger",
    deps = [
        "@crate_index__base64-0.13.1//:base64",
        "@crate_index__futures-0.3.29//:futures",
        "@crate_index__hyper-old-types-0.11.0//:hyper_old_types",
        "@crate_index__serde-1.0.192//:serde",
        "@crate_index__serde_json-1.0.108//:serde_json",
        "@crate_index__slog-2.7.0//:slog",
        "@crate_index__uuid-0.8.2//:uuid",
    ],
    compile_data = glob(
        include = ["**"],
        exclude = [
            "**/* *",
            ".tmp_git_root/**/*",
            "BUILD",
            "BUILD.bazel",
            "WORKSPACE",
            "WORKSPACE.bazel",
        ],
    ),
    crate_features = [
        "default",
        "serde",
        "serde_json",
        "serdejson",
        "server",
    ],
    crate_root = "src/lib.rs",
    edition = "2018",
    rustc_flags = ["--cap-lints=allow"],
    srcs = glob(["**/*.rs"]),
    tags = [
        "cargo-bazel",
        "crate-name=swagger",
        "manual",
        "noclippy",
        "norustfmt",
    ],
    target_compatible_with = select({
        "@rules_rust//rust/platform:aarch64-apple-darwin": [],
        "@rules_rust//rust/platform:aarch64-apple-ios": [],
        "@rules_rust//rust/platform:aarch64-apple-ios-sim": [],
        "@rules_rust//rust/platform:aarch64-fuchsia": [],
        "@rules_rust//rust/platform:aarch64-linux-android": [],
        "@rules_rust//rust/platform:aarch64-pc-windows-msvc": [],
        "@rules_rust//rust/platform:aarch64-unknown-linux-gnu": [],
        "@rules_rust//rust/platform:arm-unknown-linux-gnueabi": [],
        "@rules_rust//rust/platform:armv7-linux-androideabi": [],
        "@rules_rust//rust/platform:armv7-unknown-linux-gnueabi": [],
        "@rules_rust//rust/platform:i686-apple-darwin": [],
        "@rules_rust//rust/platform:i686-linux-android": [],
        "@rules_rust//rust/platform:i686-pc-windows-msvc": [],
        "@rules_rust//rust/platform:i686-unknown-freebsd": [],
        "@rules_rust//rust/platform:i686-unknown-linux-gnu": [],
        "@rules_rust//rust/platform:powerpc-unknown-linux-gnu": [],
        "@rules_rust//rust/platform:riscv32imc-unknown-none-elf": [],
        "@rules_rust//rust/platform:riscv64gc-unknown-none-elf": [],
        "@rules_rust//rust/platform:s390x-unknown-linux-gnu": [],
        "@rules_rust//rust/platform:thumbv7em-none-eabi": [],
        "@rules_rust//rust/platform:thumbv8m.main-none-eabi": [],
        "@rules_rust//rust/platform:wasm32-unknown-unknown": [],
        "@rules_rust//rust/platform:wasm32-wasi": [],
        "@rules_rust//rust/platform:x86_64-apple-darwin": [],
        "@rules_rust//rust/platform:x86_64-apple-ios": [],
        "@rules_rust//rust/platform:x86_64-fuchsia": [],
        "@rules_rust//rust/platform:x86_64-linux-android": [],
        "@rules_rust//rust/platform:x86_64-pc-windows-msvc": [],
        "@rules_rust//rust/platform:x86_64-unknown-freebsd": [],
        "@rules_rust//rust/platform:x86_64-unknown-linux-gnu": [],
        "@rules_rust//rust/platform:x86_64-unknown-none": [],
        "//conditions:default": ["@platforms//:incompatible"],
    }),
    version = "6.4.1",
)

There is no declared dependencies to hyper.

paulfariello commented 12 months ago

Note that swagger define 2 kind of dependencies for hyper (one being feature-gated):

[features]
multipart_related = ["mime_multipart", "hyper_0_10", "mime_0_2"]

[dependencies]
hyper = "0.14"

# multipart/related
hyper_0_10 = {package = "hyper", version = "0.10", default-features = false, optional=true}

When crate_universe is building the dep list it looks for optional package to check if they should be enabled with regards to features:

https://github.com/bazelbuild/rules_rust/blob/54bef51d8e3458ec60ea2b6a27e15b62502c673d/crate_universe/src/metadata/dependency.rs#L126-L156

But it do so based on crate name and not on crate alias nor crate id (based on name + version).

https://github.com/bazelbuild/rules_rust/blob/54bef51d8e3458ec60ea2b6a27e15b62502c673d/crate_universe/src/metadata/dependency.rs#L150

and thus skip the hyper dependency.

UebelAndre commented 11 months ago

Addressed by https://github.com/bazelbuild/rules_rust/pull/2265