crate_universe: resolve annotation patches against the current module (#4425)

### Problem

`crate.annotation(patches = ...)` labels are stored in
`cargo-bazel-lock.json` as canonical strings, so their form depends on
which module was root when the lockfile was pinned (`@@//…` vs
`@@my_module+//…`). Since #3521 the digest ignores the repo part, so the
lockfile is reused unchanged when the module is consumed by another
root. `_generate_hub_and_spokes` then passes the stored string straight
to `http_archive`/`git_repository`, and `@@//` resolves to the
*consumer's* main repo:

```
Error in patch: Cannot find patch file: /path/to/consumer/bazel/patches/foo.patch
```

Re-pinning from the consumer only moves the breakage: the module then
fails as root. No single lockfile works in both setups.

### Fix

Build an index from the current invocation's annotation tags, `(crate,
repo-stripped label) → label as resolved now`, and use it to rewrite
lockfile patch labels before creating crate repos. The repo-stripping is
the same normalization the digest already uses. Labels with no matching
annotation keep their lockfile value. There's no lockfile format change,
no digest change and no forced re-pin.

### Testing

A module owns a `crate.from_cargo` hub with a patched crate annotation
and its own lockfile pinned as root. A second root module depends on it
through `local_path_override` and `include()`s its `*.MODULE.bazel`.

- Before: the crate fetch fails because the patch path resolves inside
the consumer.
- After: the patch resolves inside the owning module from both roots.

Happy to add a regression test under `test/` (a nested module with a
lockfile and a patch annotation, built as root and as a dependency) if
that's the preferred shape.

### Notes

Other label-valued annotation fields (`deps`, `build_script_data`,
`additive_build_file`, …) may have the same problem when rendered into
spoke BUILD files. This PR only fixes `patches`, which `extensions.bzl`
consumes directly.

Assisted-by: Claude Code:claude-opus-5-5

---------

Co-authored-by: UebelAndre <github@uebelandre.com>
1 file changed
tree: ee710dd5e6885524a43cfb652c7cf11234876cce
  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.