Refactor CrateInfo construction (#2161)

## Description

This PR addresses https://github.com/bazelbuild/rules_rust/issues/2013
to ensure rust-analyzer can access all the env vars the rust rules pass
to `rustc` at compile time. This is a large refactoring in almost all
the rules and aspects so I aim this PR to just fix `rust_library` rule.
The follow-up PR will address the remaining rules and aspects.

## Summary
* Create `create_crate_info_dict` function in `rust/private/utils.bzl`
to create a mutable dict repsenting CrateInfo
* Move `_determine_lib_name`, `get_edition`, `_transform_sources`
functions to `rust/private/utils.bzl` to avoid cyclic dependency when
`create_crate_info_dict` function use these
* Introduce optional `create_crate_info_callback` attribute to
`rustc_compile_action` to allow creating `CrateInfo` inside
`rustc_compile_action`. This optional attribute allows scoping the
refactoring in this PR to just `rust_library` rule.
* Introduce optional `skip_expanding_rustc_env` attribute to
`rustc_compile_action` to skip expanding `rustc_env` attr. This is
useful for `clippy` aspect and `rust_test` rule because the `CrateInfo`
provided from the depended `rust_library` already expands all the env
vars before returning the provider downstream.

## Notes
* `rustc_env_attr` is a temporary field in `CrateInfo` and needed by the
rules and aspects not migrated to create `CrateInfo` in
`rustc_compile_action` yet. It will be remove in the next PR after
`CrateInfo.rustc_env` is always expanded.
* `create_crate_info_callback` will be removed from
`rustc_compile_action` after all `CrateInfo`s are created inside
`rustc_compile_action`.

---------

Co-authored-by: scentini <rosica@google.com>
15 files changed
tree: aa0501642e785d285aed75f904a360b998a7145c
  1. .bazelci/
  2. .github/
  3. bindgen/
  4. cargo/
  5. crate_universe/
  6. docs/
  7. examples/
  8. ffi/
  9. proto/
  10. rust/
  11. test/
  12. tools/
  13. util/
  14. wasm_bindgen/
  15. .bazelignore
  16. .bazelrc
  17. .clang-format
  18. .gitattributes
  19. .gitignore
  20. .rustfmt.toml
  21. ARCHITECTURE.md
  22. AUTHORS
  23. BUILD.bazel
  24. CODEOWNERS
  25. COMPATIBILITY.md
  26. CONTRIBUTING.md
  27. CONTRIBUTORS
  28. LICENSE.txt
  29. MODULE.bazel
  30. README.md
  31. version.bzl
  32. WORKSPACE.bazel
README.md

Rust Rules

  • Postsubmit Build status

Overview

This repository provides rules for building Rust projects with Bazel.

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.