crate_universe: use repository_ctx APIs instead of `cp`/`find` in `local_crate_mirror` (#4427)

Fixes #4426.

Replaces `cp -r` and the `find` + `read` loop with a Starlark copy
(`readdir`, `read`, `file`) plus `repository_ctx.watch_tree`, so
`local_crate_mirror` works on Windows.

- Files are copied rather than symlinked, so the mirror stays
self-contained. An earlier revision of this PR used symlinks and failed
`local_path (external)`: that test vendors into `mktemp -d`, and the
Linux sandbox hides `/tmp`.
- `legacy_utf8 = False` keeps the copy byte-exact on Bazel 7.
- The walk passes `watch = "no"` because `watch_tree` already covers the
tree. With the default watches, deleting a subdirectory of the crate
failed the refetch with "Inconsistent filesystem operations".
- `watch_tree` is called before the copy, so an edit made during the
copy still refetches.
- `repository_ctx` cannot read file modes, so every copied file is
executable. `cp -r` kept modes, but this keeps any script a build script
runs working.
- Symlinks inside the crate are followed. `cp -r` kept them as links.

This does what the #3025 review asked for (no shell, `watch_tree`). That
review left `watch` erroring on out-of-workspace paths as an open
concern; it is retested below.

Tested with stock rules_rust 0.75.0 plus this change, using the repro in
#4426:
- The repro builds on Bazel 8.7.0 and 7.4.1. The mirror has no symlinks,
and every file matches its source byte for byte.
- A file of all 256 byte values, a UTF-8 file and nested directories
copy byte-exact on both versions.
- An edit to the linked crate rebuilds it: appending `compile_error!`
fails the build and reverting it builds again. This holds for
in-workspace paths and for absolute out-of-workspace paths.
- Adding a nested directory to the crate shows up in the mirror, and
deleting it removes it (3 cycles, no errors).
- Our monorepo's Windows build, which failed as in #4426, passes with
the symlink revision of this change. The copy revision has not been run
on Windows yet.

`repository_ctx.copy` (Bazel 10) could replace the walk once that is the
minimum version.
1 file changed
tree: 9a704ffe3ac911bf6eb38f164e0cf1040fa43d7d
  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.