Skip to content

Commit

Permalink
Treat rustfmt as optional
Browse files Browse the repository at this point in the history
All the code above this call treats rustfmt as optional, but then when
we make the BUILD for the rust toolchain, we unconditionally point at
the presumed rustfmt label.

At
https://github.com/bazelbuild/rules_rust/blob/59fab4e79f62bfa13551ac851a40696c24c0c3a4/rust/toolchain.bzl#L331-L335
we conditionally create a symlink based on whether the attribute is set,
but because we unconditionally set the attribute, this causes a hard
error failing to symlink to a non-existent file.

Instead, treat rustfmt as optional in this function too, so that the
code above and below properly convey the optionality to each other.
  • Loading branch information
illicitonion committed Mar 3, 2022
1 parent 59fab4e commit a1c0726
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion rust/private/repository_utils.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ rust_toolchain(
rust_doc = "@{workspace_name}//:rustdoc",
rust_std = "@{workspace_name}//:rust_std-{target_triple}",
rustc = "@{workspace_name}//:rustc",
rustfmt = "@{workspace_name}//:rustfmt_bin",
rustfmt = {rustfmt_label},
cargo = "@{workspace_name}//:cargo",
clippy_driver = "@{workspace_name}//:clippy_driver_bin",
rustc_lib = "@{workspace_name}//:rustc_lib",
Expand All @@ -210,6 +210,7 @@ def BUILD_for_rust_toolchain(
target_triple,
include_rustc_srcs,
default_edition,
include_rustfmt,
stdlib_linkflags = None):
"""Emits a toolchain declaration to match an existing compiler and stdlib.
Expand All @@ -220,6 +221,7 @@ def BUILD_for_rust_toolchain(
target_triple (str): The rust-style target triple of the tool
include_rustc_srcs (bool, optional): Whether to download rustc's src code. This is required in order to use rust-analyzer support. Defaults to False.
default_edition (str): Default Rust edition.
include_rustfmt (bool): Whether rustfmt is present in the toolchain.
stdlib_linkflags (list, optional): Overriden flags needed for linking to rust
stdlib, akin to BAZEL_LINKLIBS. Defaults to
None.
Expand All @@ -235,6 +237,9 @@ def BUILD_for_rust_toolchain(
rustc_srcs = "None"
if include_rustc_srcs:
rustc_srcs = "\"@{workspace_name}//lib/rustlib/src:rustc_srcs\"".format(workspace_name = workspace_name)
rustfmt_label = "None"
if include_rustfmt:
rustfmt_label = "\"@{workspace_name}//:rustfmt_bin\"".format(workspace_name = workspace_name)

return _build_file_for_rust_toolchain_template.format(
toolchain_name = name,
Expand All @@ -248,6 +253,7 @@ def BUILD_for_rust_toolchain(
default_edition = default_edition,
exec_triple = exec_triple,
target_triple = target_triple,
rustfmt_label = rustfmt_label,
)

_build_file_for_toolchain_template = """\
Expand Down Expand Up @@ -402,6 +408,7 @@ def load_rust_stdlib(ctx, target_triple):
stdlib_linkflags = stdlib_linkflags,
workspace_name = ctx.attr.name,
default_edition = ctx.attr.edition,
include_rustfmt = not (not ctx.attr.rustfmt_version),
)

return stdlib_build_file + toolchain_build_file
Expand Down

0 comments on commit a1c0726

Please sign in to comment.