Added `label_injection` to crate_universe annotations (#3755)

The new `label_injection` attribute is used to sanitize "apparent
labels" used in annotations to "canonical labels". A common example of
this is when adding external dependencies to `cargo_build_script`
targets. E.g.

```starlark
bazel_dep(name = "openssl", version = "3.3.1")

crate = use_extension("@rules_rust//crate_universe:extensions.bzl", "crate")
crate.annotation(
    crate = "openssl-sys",
    build_script_data = [
        "@openssl//:openssl",
    ],
    build_script_env = {
        "OPENSSL_BIN": "$(execpath @openssl//:openssl)",
    },
    label_injection = {
        "@openssl//:openssl": "@openssl//:openssl",
    },
)
crate.from_cargo(
    name = "crates_index",
    cargo_lockfile = "//:Cargo.lock",
    manifests = ["//:Cargo.toml"],
)
use_repo(crate, "crates_index")
```

Before this change
[inject_repo](https://bazel.build/rules/lib/globals/module#inject_repo)
would have been needed to get `@openssl` to resolve in the generated
`openssl-sys` crate. After this, labels are remapped after resolving the
canonical label based on the `label_injection` attribute so that even if
the module above is consumed as a transitive dependency, the modules are
still usable (because `inject_repo` is only applied to the root module).
12 files changed
tree: 5f4f681c9365d75e4747600492b9a0a3854a0f79
  1. .bazelci/
  2. .bcr/
  3. .github/
  4. cargo/
  5. crate_universe/
  6. docs/
  7. examples/
  8. extensions/
  9. ffi/
  10. nix/
  11. rust/
  12. test/
  13. tools/
  14. util/
  15. .bazelignore
  16. .bazelrc
  17. .clang-format
  18. .clippy.toml
  19. .envrc
  20. .gitattributes
  21. .gitignore
  22. .pre-commit-config.yaml
  23. .prettierrc.toml
  24. .rustfmt.toml
  25. .typos.toml
  26. ARCHITECTURE.md
  27. AUTHORS
  28. BUILD.bazel
  29. CODEOWNERS
  30. COMPATIBILITY.md
  31. CONTRIBUTING.md
  32. CONTRIBUTORS
  33. LICENSE.txt
  34. MODULE.bazel
  35. README.md
  36. version.bzl
  37. WORKSPACE.bazel
README.md

Rust Rules

  • Postsubmit Build status

Overview

This repository provides rules for building Rust projects with Bazel.

Starter repo

The fastest way to try this in an empty project is to click the green “Use this template” button on https://github.com/bazel-starters/rust.

Community

General discussions and announcements take place in the GitHub Discussions, but there are additional places where community members gather to discuss rules_rust.

Documentation

Please refer to the full documentation.