Skip to content

Commit 2e3f50f

Browse files
authored
Merge branch 'master' into location-expansion-tidied
2 parents 31f096e + d1d04eb commit 2e3f50f

File tree

10 files changed

+31
-19
lines changed

10 files changed

+31
-19
lines changed

.bazelignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
docs
22
examples
3-
examples/hello_cargo_manifest_dir
3+
examples/cargo_manifest_dir/external_crate

examples/.bazelignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
hello_cargo_manifest_dir
1+
cargo_manifest_dir/external_crate

examples/hello_cargo_manifest_dir/BUILD renamed to examples/cargo_manifest_dir/external_crate/BUILD.bazel

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
load("@io_bazel_rules_rust//rust:rust.bzl", "rust_library")
22

33
rust_library(
4-
name = "hello_cargo_manifest_dir",
4+
name = "external_crate",
55
srcs = ["src/lib.rs"],
66
data = ["include/included_file.rs.inc"],
77
visibility = ["//visibility:public"],

examples/hello_cargo_manifest_dir/WORKSPACE renamed to examples/cargo_manifest_dir/external_crate/WORKSPACE.bazel

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
workspace(name = "rules_rust_example_cargo_manifest_dir")
2+
13
local_repository(
24
name = "io_bazel_rules_rust",
3-
path = "../..",
5+
path = "../../../",
46
)
57

68
load("@io_bazel_rules_rust//rust:repositories.bzl", "rust_repositories")
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
load("@io_bazel_rules_rust//rust:rust.bzl", "rust_test")
22

33
rust_test(
4-
name = "hello_uses_cargo_manifest_dir",
4+
name = "cargo_manifest_dir_usage",
55
srcs = ["src/lib.rs"],
66
edition = "2018",
7-
deps = ["@hello_cargo_manifest_dir"],
7+
deps = ["@rules_rust_example_cargo_manifest_dir//:external_crate"],
88
)

examples/hello_uses_cargo_manifest_dir/src/lib.rs renamed to examples/cargo_manifest_dir/usage/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
use hello_cargo_manifest_dir::get_included_str;
2+
use external_crate::get_included_str;
33

44
#[test]
55
fn test_lib_with_include() {

examples/examples_transitive_deps.bzl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,12 @@ def transitive_deps(is_top_level = False):
2727
if is_top_level:
2828
maybe(
2929
native.local_repository,
30-
name = "hello_cargo_manifest_dir",
31-
path = "examples/hello_cargo_manifest_dir",
30+
name = "rules_rust_example_cargo_manifest_dir",
31+
path = "examples/cargo_manifest_dir/external_crate",
3232
)
3333
else:
3434
maybe(
3535
native.local_repository,
36-
name = "hello_cargo_manifest_dir",
37-
path = "hello_cargo_manifest_dir",
36+
name = "rules_rust_example_cargo_manifest_dir",
37+
path = "cargo_manifest_dir/external_crate",
3838
)

rust/private/rustc.bzl

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,21 @@ def collect_inputs(
395395
)
396396
return _process_build_scripts(ctx, file, crate_info, build_info, dep_info, compile_inputs)
397397

398+
def _exec_root_relative_path(ctx, filepath):
399+
# Bazel 3.7.0 (commit 301b96b223b0c0) changed how file paths are returned for external packages.
400+
# TODO: use a proper Bazel API to determine exec-root-relative paths.
401+
if len(ctx.label.workspace_root) > 0 and \
402+
(len(BAZEL_VERSION) == 0 or versions.is_at_least("3.7.0", BAZEL_VERSION)):
403+
workspace_root_items = ctx.label.workspace_root.split("/")
404+
if (len(workspace_root_items) >= 2 and
405+
workspace_root_items[0] == "external" and
406+
workspace_root_items[-1] == filepath.split("/")[0]):
407+
workspace_root_items = workspace_root_items[:-1]
408+
409+
return "/".join(workspace_root_items + filepath.split("/"))
410+
else:
411+
return filepath
412+
398413
def construct_arguments(
399414
ctx,
400415
file,
@@ -463,16 +478,11 @@ def construct_arguments(
463478
# and use `${pwd}` which resolves the `exec_root` at action execution time.
464479
#
465480
# Unfortunately, ctx.build_file_path isn't relative to the exec_root for external repositories in
466-
# which case we use ctx.label.workspace_root to complete the path.
481+
# which case we use ctx.label.workspace_root to complete the path in `_exec_root_relative_path()`.
467482
args.add("--subst", "pwd=${pwd}")
468483

469-
workspace_root_items = ctx.label.workspace_root.split("/")
470-
if (len(workspace_root_items) >= 2 and
471-
workspace_root_items[0] == "external" and
472-
workspace_root_items[-1] == ctx.build_file_path.split("/")[0]):
473-
workspace_root_items = workspace_root_items[:-1]
474-
475-
env["CARGO_MANIFEST_DIR"] = "${pwd}/" + "/".join(workspace_root_items + ctx.build_file_path.split("/")[:-1])
484+
relative_build_file_path = _exec_root_relative_path(ctx, ctx.build_file_path)
485+
env["CARGO_MANIFEST_DIR"] = "${pwd}/" + relative_build_file_path[:relative_build_file_path.rfind("/")]
476486

477487
if out_dir != None:
478488
env["OUT_DIR"] = "${pwd}/" + out_dir

0 commit comments

Comments
 (0)