Skip to content

Commit

Permalink
Factor out dedup_expand_location and use it instead of ctx.expand_loc…
Browse files Browse the repository at this point in the history
…ation
  • Loading branch information
googleson78 committed Nov 9, 2022
1 parent 078a07f commit af4e694
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 15 deletions.
21 changes: 9 additions & 12 deletions rust/private/rust_analyzer.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,13 @@ to Cargo.toml files.
load("//rust/platform:triple_mappings.bzl", "system_to_dylib_ext", "triple_to_system")
load("//rust/private:common.bzl", "rust_common")
load("//rust/private:rustc.bzl", "BuildInfo")
load("//rust/private:utils.bzl", "dedent", "find_toolchain")
load(
"//rust/private:utils.bzl",
"concat",
"dedent",
"dedup_expand_location",
"find_toolchain",
)

RustAnalyzerInfo = provider(
doc = "RustAnalyzerInfo holds rust crate metadata for targets",
Expand Down Expand Up @@ -182,14 +188,11 @@ def _create_single_crate(ctx, info):
"include_dirs": [crate_root, _EXEC_ROOT_TEMPLATE + out_dir_path],
}

# We deduplicate the entries from each of the additional targets to work around
# https://github.com/bazelbuild/bazel/issues/16664
#
# TODO: The only imagined use case is an env var holding a filename in the workspace passed to a
# macro like include_bytes!. Other use cases might exist that require more complex logic.
expand_targets = _deduplicate(_concat([getattr(ctx.rule.attr, attr, []) for attr in ["data", "compile_data"]]))
expand_targets = concat([getattr(ctx.rule.attr, attr, []) for attr in ["data", "compile_data"]])

crate["env"].update({k: ctx.expand_location(v, expand_targets) for k, v in info.env.items()})
crate["env"].update({k: dedup_expand_location(ctx, v, expand_targets) for k, v in info.env.items()})

# Omit when a crate appears to depend on itself (e.g. foo_test crates).
# It can happen a single source file is present in multiple crates - there can
Expand All @@ -208,12 +211,6 @@ def _create_single_crate(ctx, info):
crate["proc_macro_dylib_path"] = _EXEC_ROOT_TEMPLATE + info.proc_macro_dylib_path
return crate

def _deduplicate(xs):
return {x: True for x in xs}.keys()

def _concat(xss):
return [x for xs in xss for x in xs]

def _rust_analyzer_toolchain_impl(ctx):
toolchain = platform_common.ToolchainInfo(
rustc_srcs = ctx.attr.rustc_srcs,
Expand Down
14 changes: 13 additions & 1 deletion rust/private/utils.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,18 @@ def get_preferred_artifact(library_to_link, use_pic):
library_to_link.dynamic_library
)

# The normal ctx.expand_location, but with an additional deduplication step.
# We do this to work around a potential crash, see
# https://github.com/bazelbuild/bazel/issues/16664
def dedup_expand_location(ctx, input, targets = []):
return ctx.expand_location(input, _deduplicate(targets))

def _deduplicate(xs):
return {x: True for x in xs}.keys()

def concat(xss):
return [x for xs in xss for x in xs]

def _expand_location_for_build_script_runner(ctx, env, data):
"""A trivial helper for `expand_dict_value_locations` and `expand_list_element_locations`
Expand All @@ -240,7 +252,7 @@ def _expand_location_for_build_script_runner(ctx, env, data):
env = env.replace(directive, "$${pwd}/" + directive)
return ctx.expand_make_variables(
env,
ctx.expand_location(env, data),
dedup_expand_location(ctx, env, data),
{},
)

Expand Down
11 changes: 9 additions & 2 deletions rust/toolchain.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@
load("@bazel_skylib//rules:common_settings.bzl", "BuildSettingInfo")
load("//rust/private:common.bzl", "rust_common")
load("//rust/private:rust_analyzer.bzl", _rust_analyzer_toolchain = "rust_analyzer_toolchain")
load("//rust/private:utils.bzl", "dedent", "find_cc_toolchain", "make_static_lib_symlink")
load(
"//rust/private:utils.bzl",
"dedent",
"dedup_expand_location",
"find_cc_toolchain",
"make_static_lib_symlink",
)

rust_analyzer_toolchain = _rust_analyzer_toolchain

Expand Down Expand Up @@ -456,7 +462,8 @@ def _rust_toolchain_impl(ctx):
expanded_stdlib_linkflags = []
for flag in ctx.attr.stdlib_linkflags:
expanded_stdlib_linkflags.append(
ctx.expand_location(
dedup_expand_location(
ctx,
flag,
targets = rust_std[rust_common.stdlib_info].srcs,
),
Expand Down

0 comments on commit af4e694

Please sign in to comment.