From 085ba68cc2849f1e2ea2b990ca3f1934f0eef322 Mon Sep 17 00:00:00 2001 From: Daniel Wagner-Hall Date: Thu, 3 Mar 2022 17:25:36 +0000 Subject: [PATCH] Treat rustfmt as optional 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. --- rust/private/repository_utils.bzl | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/rust/private/repository_utils.bzl b/rust/private/repository_utils.bzl index a5f0a5a80c..633e885bd7 100644 --- a/rust/private/repository_utils.bzl +++ b/rust/private/repository_utils.bzl @@ -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", @@ -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. @@ -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. @@ -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, @@ -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 = """\ @@ -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