Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle external packages using CARGO_MANIFEST_DIR #464

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .bazelignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
docs
examples
examples/hello_cargo_manifest_dir
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
# bazel
/bazel-*
/examples/bazel-*
/examples/*/bazel-*
/docs/bazel-*

# rustfmt
Expand Down
2 changes: 1 addition & 1 deletion WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ examples_deps()

load("@examples//:examples_transitive_deps.bzl", examples_transitive_deps = "transitive_deps")

examples_transitive_deps()
examples_transitive_deps(is_top_level = True)

# Load all dependencies for docs

Expand Down
1 change: 1 addition & 0 deletions examples/.bazelignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
hello_cargo_manifest_dir
25 changes: 23 additions & 2 deletions examples/examples_transitive_deps.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,35 @@ There are some transitive dependencies of the dependencies of the examples'
dependencies. This file contains the required macros to pull these dependencies
"""

load("@bazel_tools//tools/build_defs/repo:utils.bzl", "maybe")
load("@io_bazel_rules_rust//:workspace.bzl", "rust_workspace")
load("@rules_proto//proto:repositories.bzl", "rules_proto_dependencies", "rules_proto_toolchains")

def transitive_deps():
"""Define transitive dependencies for `rules_rust` examples"""
# buildifier: disable=unnamed-macro
def transitive_deps(is_top_level = False):
"""Define transitive dependencies for `rules_rust` examples

Args:
is_top_level (bool, optional): Indicates wheather or not this is being called
from the root WORKSPACE file of `rules_rust`. Defaults to False.
"""

rules_proto_dependencies()

rules_proto_toolchains()

rust_workspace()

# Needed by the hello_uses_cargo_manifest_dir example.
if is_top_level:
maybe(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be

        maybe(
            native.local_repository,
            name = "hello_cargo_manifest_dir",
            path = "hello_cargo_manifest_dir",
        )

(Sorry if this is obvious, just trying to help 😅)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"just one small change, surely I can't mess that one up". Anyway, thanks for the help :-)

Unrelated, but I noticed that if you build from the examples/ dir (which creates all the examples/bazel-* directories, building from the root directory sometimes fails because of infinite recursion, as if the .bazelignore was ignored. I couldn't find how to fix the issue (the issue wasn't introduced in this commit though).

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah... that's what happens when you have nested workspaces that depend on the root workspace like this. local_repository should ignore bazel output paths (or accept a pattern to ignore certain directories).

Hopefully Bazel's proposal for external dependencies resolves this. For now, you'll just have to make sure you bazel clean in ./examples. Or at least that's what I do... 😅

native.local_repository,
name = "hello_cargo_manifest_dir",
path = "examples/hello_cargo_manifest_dir",
)
else:
maybe(
native.local_repository,
name = "hello_cargo_manifest_dir",
path = "hello_cargo_manifest_dir",
)
8 changes: 8 additions & 0 deletions examples/hello_cargo_manifest_dir/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
load("@io_bazel_rules_rust//rust:rust.bzl", "rust_library")

rust_library(
name = "hello_cargo_manifest_dir",
srcs = ["src/lib.rs"],
data = ["include/included_file.rs.inc"],
visibility = ["//visibility:public"],
)
12 changes: 12 additions & 0 deletions examples/hello_cargo_manifest_dir/WORKSPACE
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
local_repository(
name = "io_bazel_rules_rust",
path = "../..",
)

load("@io_bazel_rules_rust//rust:repositories.bzl", "rust_repositories")

rust_repositories()

load("@io_bazel_rules_rust//:workspace.bzl", "rust_workspace")

rust_workspace()
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
I love veggies!
4 changes: 4 additions & 0 deletions examples/hello_cargo_manifest_dir/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

pub fn get_included_str() -> &'static str {
include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/include/included_file.rs.inc"))
}
8 changes: 8 additions & 0 deletions examples/hello_uses_cargo_manifest_dir/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
load("@io_bazel_rules_rust//rust:rust.bzl", "rust_test")

rust_test(
name = "hello_uses_cargo_manifest_dir",
srcs = ["src/lib.rs"],
edition = "2018",
deps = ["@hello_cargo_manifest_dir"],
)
7 changes: 7 additions & 0 deletions examples/hello_uses_cargo_manifest_dir/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

use hello_cargo_manifest_dir::get_included_str;

#[test]
fn test_lib_with_include() {
assert_eq!(get_included_str(), "I love veggies!\n")
}
11 changes: 10 additions & 1 deletion rust/private/rustc.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -442,9 +442,18 @@ def construct_arguments(
# may not follow the `src/lib.rs` convention. As such we use `ctx.build_file_path` mapped into the
# `exec_root`. Since we cannot (seemingly) get the `exec_root` from starlark, we cheat a little
# and use `${pwd}` which resolves the `exec_root` at action execution time.
#
# Unfortunately, ctx.build_file_path isn't relative to the exec_root for external repositories in
# which case we use ctx.label.workspace_root to complete the path.
args.add("--subst", "pwd=${pwd}")

env["CARGO_MANIFEST_DIR"] = "${pwd}/" + ctx.build_file_path[:ctx.build_file_path.rfind("/")]
workspace_root_items = ctx.label.workspace_root.split("/")
if (len(workspace_root_items) >= 2 and
workspace_root_items[0] == "external" and
workspace_root_items[-1] == ctx.build_file_path.split("/")[0]):
workspace_root_items = workspace_root_items[:-1]

env["CARGO_MANIFEST_DIR"] = "${pwd}/" + "/".join(workspace_root_items + ctx.build_file_path.split("/")[:-1])

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