Skip to content

Commit

Permalink
Deduplicate expand_location targets in rust-project.json crate creation
Browse files Browse the repository at this point in the history
There's a bug in bazel where the server crashes if it encounters
duplicate entries in expand_location's targets field.
See bazelbuild/bazel#16664
  • Loading branch information
googleson78 committed Nov 8, 2022
1 parent 03a0b24 commit da4c87c
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion rust/private/rust_analyzer.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -182,9 +182,13 @@ 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 = getattr(ctx.rule.attr, "data", []) + getattr(ctx.rule.attr, "compile_data", [])
expand_targets = _deduplicate(_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()})

# Omit when a crate appears to depend on itself (e.g. foo_test crates).
Expand All @@ -204,6 +208,12 @@ 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

0 comments on commit da4c87c

Please sign in to comment.