Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deduplicate expand_location targets in rust-project.json crate creation to avoid a bazel crash #1639

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 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 @@ -184,8 +190,9 @@ def _create_single_crate(ctx, info):

# 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 = getattr(ctx.rule.attr, "data", []) + getattr(ctx.rule.attr, "compile_data", [])
crate["env"].update({k: ctx.expand_location(v, expand_targets) for k, v in info.env.items()})
expand_targets = concat([getattr(ctx.rule.attr, attr, []) for attr in ["data", "compile_data"]])

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 Down
20 changes: 16 additions & 4 deletions rust/private/utils.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,19 @@ def get_preferred_artifact(library_to_link, use_pic):
library_to_link.dynamic_library
)

def _expand_location(ctx, env, data):
# 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`

Args:
Expand All @@ -240,7 +252,7 @@ def _expand_location(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 Expand Up @@ -273,7 +285,7 @@ def expand_dict_value_locations(ctx, env, data):
Returns:
dict: A dict of environment variables with expanded location macros
"""
return dict([(k, _expand_location(ctx, v, data)) for (k, v) in env.items()])
return dict([(k, _expand_location_for_build_script_runner(ctx, v, data)) for (k, v) in env.items()])

def expand_list_element_locations(ctx, args, data):
"""Performs location-macro expansion on a list of string values.
Expand All @@ -296,7 +308,7 @@ def expand_list_element_locations(ctx, args, data):
Returns:
list: A list of arguments with expanded location macros
"""
return [_expand_location(ctx, arg, data) for arg in args]
return [_expand_location_for_build_script_runner(ctx, arg, data) for arg in args]

def name_to_crate_name(name):
"""Converts a build target's name into the name of its associated crate.
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