bazelbuild / rules_rust

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

Direct Packages support for subcrate #1903

Open kriswuollett opened 1 year ago

kriswuollett commented 1 year ago

Using the following does not result create a @crate_index//:wasmtime-c-api target:

RUST_PACKAGES = {
    "wasmtime": crate.spec(
        version = "7.0.0",
    ),
    "wasmtime-c-api": crate.spec(
        package = "wasmtime-c-api",
        git = "https://github.com/bytecodealliance/wasmtime",
        tag = "v7.0.0",
    ),
}
http_archive(
    name = "rules_rust",
    sha256 = "b4e622a36904b5dd2d2211e40008fc473421c8b51c9efca746ab2ecf11dca08e",
    urls = ["https://github.com/bazelbuild/rules_rust/releases/download/0.19.1/rules_rust-v0.19.1.tar.gz"],
)

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

rules_rust_dependencies()

rust_register_toolchains(
    edition = "2021",
    versions = [
        "nightly/2023-03-24",
    ],
    extra_target_triples = [
        "aarch64-unknown-linux-gnu",
        "x86_64-unknown-linux-gnu",
        "wasm32-unknown-unknown",
        "wasm32-wasi",
    ],
)

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

crate_universe_dependencies()

load("@rules_rust//crate_universe:defs.bzl", "crates_repository", "render_config")
load("//bazel:rust.bzl", "RUST_PACKAGES")

crates_repository(
    name = "crate_index",
    cargo_lockfile = "//:Cargo.lock",
    generate_binaries = True,
    lockfile = "//:Cargo.Bazel.lock",
    packages = RUST_PACKAGES,
    render_config = render_config(
        default_package_name = "",
    ),
    rust_version = "nightly/2023-03-05",
)

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

crate_repositories()

load("@rules_rust//tools/rust_analyzer:deps.bzl", "rust_analyzer_dependencies")

rust_analyzer_dependencies()

I'd expect that it would have created a target that provides headers and library outputs that would be usable from a C/C++ target as shown in c_calling_rust, but it does not:

$ CARGO_BAZEL_REPIN=1 bazel sync --only=crate_index
[REDACTED]
$ bazel query @crate_index//... | grep wasmtime
Loading: 0 packages loaded
@crate_index//:wasmtime
Loading: 0 packages loaded

Is producing subcrate target an unimplemented feature? What would be the workarounds in the meantime if not supported? Vendor in wasmtime-c-api myself? Just write my own C API wrapper that exposes what I need? The Wasmtime C API is the example I'd like to make work.

illicitonion commented 1 year ago

I'm not sure what's going on here, but two things I noticed when looking at the package:

  1. It's crate_types are just staticlib and cdylib, and we probably filter it out in a few places in: https://github.com/bazelbuild/rules_rust/blob/44aec0a793492ec9ffbef1c7ea7697043fd38750/crate_universe/src/metadata/dependency.rs
  2. It sets is target name to wasmtime which conflicts with another crate in the workspace, which plausibly is confusing something somewhere

I'd probably start instrumenting around those two things, to debug this further

illicitonion commented 1 year ago

(To be explicit - we expect subcrates to work fine, but this one seems specially broken in some way)