Skip to content

Commit 080fca7

Browse files
committed
Fix CARGO_MANIFEST_DIR when transform_sources moves crate root to bazel-out
1 parent 2bad175 commit 080fca7

1 file changed

Lines changed: 15 additions & 1 deletion

File tree

rust/private/rustc.bzl

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -997,7 +997,21 @@ def construct_arguments(
997997

998998
# Both ctx.label.workspace_root and ctx.label.package are relative paths
999999
# and either can be empty strings. Avoid trailing/double slashes in the path.
1000-
components = "${{pwd}}/{}/{}".format(ctx.label.workspace_root, ctx.label.package).split("/")
1000+
#
1001+
# When transform_sources moves source files into bazel-out (due to mixed
1002+
# generated/non-generated sources), the crate root ends up under a different
1003+
# directory than the original source package. We must derive CARGO_MANIFEST_DIR
1004+
# from the actual crate root path so proc-macros can find Cargo.toml in the
1005+
# sandbox.
1006+
_package_path = "/".join([c for c in [ctx.label.workspace_root, ctx.label.package] if c])
1007+
if crate_info.root.is_source:
1008+
components = "${{pwd}}/{}/{}".format(ctx.label.workspace_root, ctx.label.package).split("/")
1009+
else:
1010+
# Crate root was transformed (symlinked into bazel-out). The file is at
1011+
# <root.root.path>/<workspace_root>/<package>/<relative_path>, so the
1012+
# manifest directory is <root.root.path>/<workspace_root>/<package>.
1013+
_manifest_dir = "/".join([c for c in [crate_info.root.root.path, _package_path] if c])
1014+
components = "${{pwd}}/{}".format(_manifest_dir).split("/")
10011015
env["CARGO_MANIFEST_DIR"] = "/".join([c for c in components if c])
10021016

10031017
if out_dir != None:

0 commit comments

Comments
 (0)