diff --git a/rust/private/rustc.bzl b/rust/private/rustc.bzl index b4f03b89be..5c11f762c3 100644 --- a/rust/private/rustc.bzl +++ b/rust/private/rustc.bzl @@ -1018,7 +1018,10 @@ def construct_arguments( env.update(link_env) rustc_flags.add(ld, format = "--codegen=linker=%s") - rustc_flags.add_joined("--codegen", link_args, join_with = " ", format_joined = "link-args=%s") + + # Split link args into individual "--codegen=link-arg=" flags to handle nested spaces. + # Additional context: https://github.com/rust-lang/rust/pull/36574 + rustc_flags.add_all(link_args, format_each = "--codegen=link-arg=%s") _add_native_link_flags(rustc_flags, dep_info, linkstamp_outs, ambiguous_libs, crate_info.type, toolchain, cc_toolchain, feature_configuration, compilation_mode) diff --git a/test/unit/native_deps/native_deps_test.bzl b/test/unit/native_deps/native_deps_test.bzl index a086afb3c1..ee8adf7d63 100644 --- a/test/unit/native_deps/native_deps_test.bzl +++ b/test/unit/native_deps/native_deps_test.bzl @@ -113,13 +113,17 @@ def _bin_has_native_libs_test_impl(ctx): return analysistest.end(env) def _extract_linker_args(argv): - return [a for a in argv if ( - a.startswith("link-arg=") or - a.startswith("link-args=") or - a.startswith("-l") or - a.endswith(".lo") or - a.endswith(".o") - )] + return [ + a.removeprefix("--codegen=link-arg=").removeprefix("--codegen=link-args=") + for a in argv + if ( + a.startswith("--codegen=link-arg=") or + a.startswith("--codegen=link-args=") or + a.startswith("-l") or + a.endswith(".lo") or + a.endswith(".o") + ) + ] def _bin_has_native_dep_and_alwayslink_test_impl(ctx): env = analysistest.begin(ctx) @@ -171,8 +175,7 @@ def _cdylib_has_native_dep_and_alwayslink_test_impl(ctx): tut = analysistest.target_under_test(env) action = tut.actions[0] - # skipping first link-arg since it contains unrelated linker flags - linker_args = _extract_linker_args(action.argv)[1:] + linker_args = _extract_linker_args(action.argv) toolchain = _get_toolchain(ctx) compilation_mode = ctx.var["COMPILATION_MODE"] @@ -204,7 +207,7 @@ def _cdylib_has_native_dep_and_alwayslink_test_impl(ctx): "link-arg=bazel-out/k8-{}/bin/{}test/unit/native_deps/libalwayslink{}.lo".format(compilation_mode, workspace_prefix, pic_suffix), "link-arg=-Wl,--no-whole-archive", ] - asserts.equals(env, want, linker_args) + assert_list_contains_adjacent_elements(env, linker_args, want) return analysistest.end(env) def _get_pic_suffix(ctx, compilation_mode): @@ -334,11 +337,8 @@ def _linkopts_propagate_test_impl(ctx): # Consistently with cc rules, dependency linkopts take precedence over # dependent linkopts (i.e. dependency linkopts appear later in the command # line). - linkopt_args = [ - arg - for arg in _extract_linker_args(action.argv) - if arg.startswith("link-args") - ][0].split(" ") + + linkopt_args = _extract_linker_args(action.argv) assert_list_contains_adjacent_elements( env, linkopt_args,