Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

build-dependencies are not added to crates_repository #3194

Closed
bradzacher opened this issue Jan 22, 2025 · 6 comments
Closed

build-dependencies are not added to crates_repository #3194

bradzacher opened this issue Jan 22, 2025 · 6 comments

Comments

@bradzacher
Copy link

I'm attempting to update our repo from rules_rust 0.49 to 0.57 and I'm finding that build-dependencies aren't emitted in the generated crates_repository -- causing the build to fail.

In our repo we use crates_repository like so:

crates_repository(
    name = "crate_index",
    cargo_lockfile = "//:Cargo.lock",
    generate_binaries = True,
    manifests = [
        "//:Cargo.lock",
        "//path/to/unzip:Cargo.toml",
    ]
    lockfile = "//:Cargo.Bazel.lock",
)
One of the mainfests is a `Cargo.toml` and `BUILD.bazel` that looks like this:
[package]
name = "unzip"
version = "0.0.1"
edition = { workspace = true }
description = "..."

[dependencies]
anyhow = { workspace = true }
clap = { workspace = true }
napi = { workspace = true, features = ["async"] }
napi-derive = { workspace = true }
tokio = { workspace = true, features = ["fs", "macros", "rt-multi-thread"] }
zip = { workspace = true }

[build-dependencies]
napi-build = { workspace = true }
load("@crate_index//:defs.bzl", "aliases", "all_crate_deps")
load("@rules_rust//rust:defs.bzl", "rust_shared_library")
load("@rules_rust//cargo:defs.bzl", "cargo_build_script")

cargo_build_script(
    name = "build_script",
    srcs = ["build.rs"],
    deps = all_crate_deps(build = True),
)

rust_shared_library(
    name = "unzip_shared",
    srcs = glob(["src/**/*.rs"]),
    aliases = aliases(),
    proc_macro_deps = all_crate_deps(proc_macro = True),
    visibility = ["//visibility:public"],
    deps = all_crate_deps() + [
        ":build_script",
    ],
)
And in the root `Cargo.toml` we declare the dependencies like so:
[workspace]
resolver = "2"
members = [
  "path/to/unzip",
]

[workspace.package]
edition = "2021"

[workspace.dependencies]
anyhow = "1.0.86"
clap = "4.5.9"
napi = "2"
napi-build = "2"
napi-derive = "2"
tokio = "1.38.1"
zip = "2.2.2"

It's all pretty straightforward, as far as I understand, at least.

When we repin we get a lockfile which declares all of the dependencies with their correct versions.

For example I can see `napi-build`'s entry is this:
"napi-build 2.1.4": {
  "name": "napi-build",
  "version": "2.1.4",
  "package_url": "https://github.com/napi-rs/napi-rs",
  "repository": {
    "Http": {
      "url": "https://static.crates.io/crates/napi-build/2.1.4/download",
      "sha256": "db836caddef23662b94e16bf1f26c40eceb09d6aee5d5b06a7ac199320b69b19"
    }
  },
  "targets": [
    {
      "Library": {
        "crate_name": "napi_build",
        "crate_root": "src/lib.rs",
        "srcs": {
          "allow_empty": true,
          "include": [
            "**/*.rs"
          ]
        }
      }
    }
  ],
  "library_target_name": "napi_build",
  "common_attrs": {
    "compile_data_glob": [
      "**"
    ],
    "edition": "2021",
    "version": "2.1.4"
  },
  "license": "MIT",
  "license_ids": [
    "MIT"
  ],
  "license_file": null
},

However when we do a build we get an error like:

/private/var/tmp/bazel/a8584ebfb3d6ff0dfe61abfbfa5bb4d3/external/crate_index/BUILD.bazel: no such target '@@crate_index//:napi-build-2.1.4': target 'napi-build-2.1.4' not declared in package '' defined by /private/var/tmp/bazel/a8584ebfb3d6ff0dfe61abfbfa5bb4d3/external/crate_index/BUILD.bazel
If I hop into the specified file I can see it has the following `napi-**` aliases:
alias(
    name = "napi-2.16.13",
    actual = "@crate_index__napi-2.16.13//:napi",
    tags = ["manual"],
)

alias(
    name = "napi",
    actual = "@crate_index__napi-2.16.13//:napi",
    tags = ["manual"],
)

alias(
    name = "napi-derive-2.16.13",
    actual = "@crate_index__napi-derive-2.16.13//:napi_derive",
    tags = ["manual"],
)

alias(
    name = "napi-derive",
    actual = "@crate_index__napi-derive-2.16.13//:napi_derive",
    tags = ["manual"],
)

Note that an alias for napi-build is not defined.

Note that this isn't only happening for napi et al -- it's also happening for a few other build-dependencies.

I've done some playing around and at least I've figured out that if I declare an empty, dummy crate (with an associated rust_binary target) in the repo and I list the build-dependencies as normal dependencies, then this causes the aliases to be output and thus everything works correctly from there.

Is this a bug in rules_rust? Or have I misconfigured something somewhere?

@bradzacher
Copy link
Author

I tried all versions up to 0.54 and they worked just fine and did not exhibit this behaviour
Unfortunately I can't try 0.55 or 0.56 due to this bug #3166 which was fixed in 0.57

@realtimetodie
Copy link
Contributor

I'm experiencing the same issue

@realtimetodie
Copy link
Contributor

I downgraded to 0.56.0 again, 0.57.0 and 0.57.1 seem to be broken

@gferon
Copy link
Contributor

gferon commented Jan 23, 2025

I also tried to migrate to v0.57.0 from v0.54.0 and am seeing the same problem.

@HackAttack
Copy link
Contributor

Duplicated by #3227, but since that one has a PR attached, maybe we should close in favor of that one.

@bradzacher
Copy link
Author

bradzacher commented Mar 11, 2025

This appears to have been fixed in 0.58.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants