Skip to content

Commit

Permalink
fix: default rustfmt version to supplied rust version (#2660)
Browse files Browse the repository at this point in the history
Modifies the default of `rustfmt_version` so that it uses the first
version in the `versions` list. If there are multiple versions given
then falls back to the original default of `DEFAULT_NIGHTLY_VERSION`.

We tripped over this where supply a single Rust version (non-nightly),
but started using nightly features in `rustfmt.toml` and could no longer
format directly with `cargo fmt`. This helped align the versions of both
toolchains.

---------

Co-authored-by: Daniel Wagner-Hall <dwagnerhall@apple.com>
  • Loading branch information
mattem and illicitonion committed May 17, 2024
1 parent 0d8d3e0 commit df80ce6
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 4 deletions.
2 changes: 1 addition & 1 deletion docs/flatten.md
Original file line number Diff line number Diff line change
Expand Up @@ -1963,7 +1963,7 @@ See `load_arbitrary_tool` in `@rules_rust//rust:repositories.bzl` for more detai
| <a id="rust_register_toolchains-global_allocator_library"></a>global_allocator_library | Target that provides allocator functions when global allocator is used with cc_common.link. | `None` |
| <a id="rust_register_toolchains-iso_date"></a>iso_date | **Deprecated**: Use <code>versions</code> instead. | `None` |
| <a id="rust_register_toolchains-register_toolchains"></a>register_toolchains | If true, repositories will be generated to produce and register <code>rust_toolchain</code> targets. | `True` |
| <a id="rust_register_toolchains-rustfmt_version"></a>rustfmt_version | The version of rustfmt. | `"nightly/2024-05-02"` |
| <a id="rust_register_toolchains-rustfmt_version"></a>rustfmt_version | The version of rustfmt. If none is supplied and only a single version in <code>versions</code> is given, then this defaults to that version, otherwise will default to the default nightly version. | `None` |
| <a id="rust_register_toolchains-rust_analyzer_version"></a>rust_analyzer_version | The version of Rustc to pair with rust-analyzer. | `None` |
| <a id="rust_register_toolchains-sha256s"></a>sha256s | A dict associating tool subdirectories to sha256 hashes. | `None` |
| <a id="rust_register_toolchains-extra_target_triples"></a>extra_target_triples | Additional rust-style targets that rust toolchains should support. | `["wasm32-unknown-unknown", "wasm32-wasi"]` |
Expand Down
2 changes: 1 addition & 1 deletion docs/rust_repositories.md
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ See `load_arbitrary_tool` in `@rules_rust//rust:repositories.bzl` for more detai
| <a id="rust_register_toolchains-global_allocator_library"></a>global_allocator_library | Target that provides allocator functions when global allocator is used with cc_common.link. | `None` |
| <a id="rust_register_toolchains-iso_date"></a>iso_date | **Deprecated**: Use <code>versions</code> instead. | `None` |
| <a id="rust_register_toolchains-register_toolchains"></a>register_toolchains | If true, repositories will be generated to produce and register <code>rust_toolchain</code> targets. | `True` |
| <a id="rust_register_toolchains-rustfmt_version"></a>rustfmt_version | The version of rustfmt. | `"nightly/2024-05-02"` |
| <a id="rust_register_toolchains-rustfmt_version"></a>rustfmt_version | The version of rustfmt. If none is supplied and only a single version in <code>versions</code> is given, then this defaults to that version, otherwise will default to the default nightly version. | `None` |
| <a id="rust_register_toolchains-rust_analyzer_version"></a>rust_analyzer_version | The version of Rustc to pair with rust-analyzer. | `None` |
| <a id="rust_register_toolchains-sha256s"></a>sha256s | A dict associating tool subdirectories to sha256 hashes. | `None` |
| <a id="rust_register_toolchains-extra_target_triples"></a>extra_target_triples | Additional rust-style targets that rust toolchains should support. | `["wasm32-unknown-unknown", "wasm32-wasi"]` |
Expand Down
10 changes: 8 additions & 2 deletions rust/repositories.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ def rust_register_toolchains(
global_allocator_library = None,
iso_date = None,
register_toolchains = True,
rustfmt_version = DEFAULT_NIGHTLY_VERSION,
rustfmt_version = None,
rust_analyzer_version = None,
sha256s = None,
extra_target_triples = DEFAULT_EXTRA_TARGET_TRIPLES,
Expand Down Expand Up @@ -146,7 +146,7 @@ def rust_register_toolchains(
global_allocator_library (str, optional): Target that provides allocator functions when global allocator is used with cc_common.link.
iso_date (str, optional): **Deprecated**: Use `versions` instead.
register_toolchains (bool): If true, repositories will be generated to produce and register `rust_toolchain` targets.
rustfmt_version (str, optional): The version of rustfmt.
rustfmt_version (str, optional): The version of rustfmt. If none is supplied and only a single version in `versions` is given, then this defaults to that version, otherwise will default to the default nightly version.
rust_analyzer_version (str, optional): The version of Rustc to pair with rust-analyzer.
sha256s (str, optional): A dict associating tool subdirectories to sha256 hashes.
extra_target_triples (list, optional): Additional rust-style targets that rust toolchains should support.
Expand Down Expand Up @@ -176,6 +176,12 @@ def rust_register_toolchains(
else:
versions = _RUST_TOOLCHAIN_VERSIONS

if not rustfmt_version:
if len(versions) == 1:
rustfmt_version = versions[0]
else:
rustfmt_version = DEFAULT_NIGHTLY_VERSION

if dev_components:
has_nightly = False
for ver in versions:
Expand Down

0 comments on commit df80ce6

Please sign in to comment.