google / cargo-raze

Generate Bazel BUILD from Cargo dependencies!
Apache License 2.0
480 stars 104 forks source link

Alias mistakenly gets applied to library instead of build.rs #477

Open maghoff opened 2 years ago

maghoff commented 2 years ago

When pulling in value-bag as a dependency with cargo-raze-0.15.0, an alias from Cargo.toml gets included in the generated rust_library rule, but it should apply to the generated cargo_build_script rule.

The dependency is declared in value-bag's Cargo.toml: https://github.com/sval-rs/value-bag/blob/v1.0.0-alpha.8/Cargo.toml#L100-L102

[build-dependencies.rustc]
version = "0.9"
package = "version_check"

The generated bazel rules:

cargo_build_script(
    name = "value_bag_build_script",
    srcs = glob(["**/*.rs"]),
    build_script_env = {
    },
    crate_features = [
    ],
    crate_root = "build.rs",
    data = glob(["**"]),
    edition = "2018",
    rustc_flags = [
        "--cap-lints=allow",
    ],
    tags = [
        "cargo-raze",
        "manual",
    ],
    version = "1.0.0-alpha.8",
    visibility = ["//visibility:private"],
    deps = [
        "@raze__version_check__0_9_4//:version_check",
    ],
)

rust_library(
    name = "value_bag",
    srcs = glob(["**/*.rs"]),
    aliases = {
        "@raze__version_check__0_9_4//:version_check": "rustc",
    },
    crate_features = [
    ],
    crate_root = "src/lib.rs",
    data = [],
    edition = "2018",
    proc_macro_deps = [
        "@raze__ctor__0_1_22//:ctor",
    ],
    rustc_flags = [
        "--cap-lints=allow",
    ],
    tags = [
        "cargo-raze",
        "crate-name=value-bag",
        "manual",
    ],
    version = "1.0.0-alpha.8",
    # buildifier: leave-alone
    deps = [
        ":value_bag_build_script",
    ],
)

This does not build correctly, as build.rs expects a crate named rustc, which is an alias of version_check. Moving the aliases section from the rust_library rule to the cargo_build_script rule fixes this:

cargo_build_script(
    name = "value_bag_build_script",
    srcs = glob(["**/*.rs"]),
    aliases = {
        "@raze__version_check__0_9_4//:version_check": "rustc",
    },
    build_script_env = {
    },
    crate_features = [
    ],
    crate_root = "build.rs",
    data = glob(["**"]),
    edition = "2018",
    rustc_flags = [
        "--cap-lints=allow",
    ],
    tags = [
        "cargo-raze",
        "manual",
    ],
    version = "1.0.0-alpha.8",
    visibility = ["//visibility:private"],
    deps = [
        "@raze__version_check__0_9_4//:version_check",
    ],
)

rust_library(
    name = "value_bag",
    srcs = glob(["**/*.rs"]),
    crate_features = [
    ],
    crate_root = "src/lib.rs",
    data = [],
    edition = "2018",
    proc_macro_deps = [
        "@raze__ctor__0_1_22//:ctor",
    ],
    rustc_flags = [
        "--cap-lints=allow",
    ],
    tags = [
        "cargo-raze",
        "crate-name=value-bag",
        "manual",
    ],
    version = "1.0.0-alpha.8",
    # buildifier: leave-alone
    deps = [
        ":value_bag_build_script",
    ],
)

Cargo-raze should generate aliases for build-dependencies in the cargo_build_script, but they are currently generated in the rust_library rule.

dfreese commented 2 years ago

This makes #475 more clear for me. Thanks. It looks like we'll need to be a little more careful about which aliases get applied where.