Fix crates_repository cache invalidation on Bazel 8 (#3967)

## Problem
In Bazel 8, `--incompatible_no_implicit_watch_label` defaults to `true`
(bazelbuild/bazel#23861). This means `repository_ctx.path(label)` no
longer implicitly watches the resolved file for changes.

`crates_repository` resolves its `cargo_lockfile`, `lockfile`, and
`manifests` inputs via `repository_ctx.path(label)` but never explicitly
calls `repository_ctx.watch()` on them. As a result, when these files
change on disk, Bazel doesn't know it needs to re-fetch the repository —
the generated `defs.bzl` becomes stale, causing errors like:

```
      Error in fail: Tried to get all_crate_deps for package <name> but that package had no Cargo.toml file
```

This is especially painful in setups where `crates_repository` inputs
come from another external repository (e.g `local_repository`) with a
long-running Bazel server. CI is unaffected because it always starts
with clean state. The only workaround today is `bazel clean` or `bazel
sync --only=<repo>`.

## Fix
Add explicit `repository_ctx.watch()` calls for `cargo_lockfile`,
`lockfile` (optional, guarded by `if lockfiles.bazel`), and each entry
in `manifests`, immediately after `get_lockfiles()` returns in
`_crates_repository_impl`.

## Testing

Verified in a large monorepo (Bazel 8.6.0, rules_rust 0.66.0) with a
nested workspace where `crates_repository` inputs come from a
`local_repository`. Before the fix, changing `Cargo.toml` or lock files
required `bazel clean` to pick up changes. After the fix, Bazel
automatically re-fetches the crate repositories.

## Related
- bazelbuild/rules_rust#2125 — related lockfile hash mismatch in nested
workspaces
- https://github.com/bazelbuild/bazel/issues/23861 — the Bazel flag
change that caused this
1 file changed
tree: e0ee1a9cf8f666ee8d6e7eddb25889f45aaca222
  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
  38. WORKSPACE.bzlmod
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.