From c88ba10b51a7bc9a4c4f2a316ac8221d0612ee98 Mon Sep 17 00:00:00 2001 From: yuzhy8701 <18453608+yuzhy8701@users.noreply.github.com> Date: Mon, 13 May 2024 06:34:12 -0700 Subject: [PATCH] Fix cc_common_link when using sibling repository layout (#2643) Linking with cc_common is broken for external repositories if you also specify `--experimental_sibling_repository_layout`. The rule would complain `The package dir path should be a prefix of the crate_info.output.path`. It happens because the package path derived from `bin_dir`, `workspace_root` and `package` did not match how sibling layout handles external repositories. This change ignores the `workspace_root` component if the path signifies the usage of sibling layout, as it is not needed. --- rust/private/rustc.bzl | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/rust/private/rustc.bzl b/rust/private/rustc.bzl index 474d1a6141..98806f1ae9 100644 --- a/rust/private/rustc.bzl +++ b/rust/private/rustc.bzl @@ -1391,13 +1391,18 @@ def rustc_compile_action( # The path to the package dir, including a trailing "/". package_dir = ctx.bin_dir.path + "/" - if ctx.label.workspace_root: + + # For external repositories, workspace root is not part of the output + # path when sibling repository layout is used (the repository name is + # part of the bin_dir). This scenario happens when the workspace root + # starts with "../" + if ctx.label.workspace_root and not ctx.label.workspace_root.startswith("../"): package_dir = package_dir + ctx.label.workspace_root + "/" if ctx.label.package: package_dir = package_dir + ctx.label.package + "/" if not crate_info.output.path.startswith(package_dir): - fail("The package dir path {} should be a prefix of the crate_info.output.path {}", package_dir, crate_info.output.path) + fail("The package dir path", package_dir, "should be a prefix of the crate_info.output.path", crate_info.output.path) output_relative_to_package = crate_info.output.path[len(package_dir):]