Skip to content

Commit

Permalink
Establish CARGO_MANIFEST_DIR (#180)
Browse files Browse the repository at this point in the history
See google/cargo-raze#71 (comment) for the rationale as
to why

In brief:

Certain rust build processes expect to find files from the environment variable
`$CARGO_MANIFEST_DIR`. Examples of this include pest, tera, asakuma.

The compiler and by extension proc-macros see the current working directory as the Bazel exec
root. Therefore, in order to fix this without an upstream code change, we have to set
`$CARGO_MANIFEST_DIR`.

As such we attempt to infer `$CARGO_MANIFEST_DIR`.
Inference cannot be derived from `attr.crate_root`, as this points at a source file which may or
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 skylark, we cheat a little
and use `$(pwd)` which resolves the `exec_root` at action execution time.

Fixes: google/cargo-raze#71
  • Loading branch information
GregBowyer authored and acmcarther committed Jan 4, 2019
1 parent ac74cc1 commit f32695d
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 1 deletion.
25 changes: 24 additions & 1 deletion rust/private/rustc.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,30 @@ def rustc_compile_action(
out_dir_env = "OUT_DIR=$(pwd)/{} ".format(out_dir.path)
else:
out_dir_env = ""
command = '{}{} "$@" --remap-path-prefix="$(pwd)"=__bazel_redacted_pwd'.format(out_dir_env, toolchain.rustc.path)

# Similar awkward construction to prepend execroot to the crate-root to set `CARGO_MANIFEST_DIR`
#
# See https://github.com/google/cargo-raze/issues/71#issuecomment-433225853 for the rationale as
# to why
#
# In brief:
#
# Certain rust build processes expect to find files from the environment variable
# `$CARGO_MANIFEST_DIR`. Examples of this include pest, tera, asakuma.
#
# The compiler and by extension proc-macros see the current working directory as the Bazel exec
# root. Therefore, in order to fix this without an upstream code change, we have to set
# `$CARGO_MANIFEST_DIR`.
#
# As such we attempt to infer `$CARGO_MANIFEST_DIR`.
# Inference cannot be derived from `attr.crate_root`, as this points at a source file which may or
# 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 skylark, we cheat a little
# and use `$(pwd)` which resolves the `exec_root` at action execution time.
package_dir = ctx.build_file_path[:ctx.build_file_path.rfind("/")]
manifest_dir_env = "CARGO_MANIFEST_DIR=$(pwd)/{} ".format(package_dir)
command = '{}{}{} "$@" --remap-path-prefix="$(pwd)"=__bazel_redacted_pwd'.format(
manifest_dir_env, out_dir_env, toolchain.rustc.path)

ctx.actions.run_shell(
command = command,
Expand Down
12 changes: 12 additions & 0 deletions test/build_env/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package(default_visibility = ["//visibility:public"])

load(
"//rust:rust.bzl",
"rust_test",
)

rust_test(
name = "conflicting_deps_test",
srcs = ["tests/manifest_dir.rs"],
data = ["src/manifest_dir_file.txt"],
)
1 change: 1 addition & 0 deletions test/build_env/src/manifest_dir_file.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This file tests that CARGO_MANIFEST_DIR is set for the build environment
6 changes: 6 additions & 0 deletions test/build_env/tests/manifest_dir.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#[test]
pub fn test_manifest_dir() {
let actual = include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/src/manifest_dir_file.txt"));
let expected = "This file tests that CARGO_MANIFEST_DIR is set for the build environment\n";
assert_eq!(actual, expected);
}

0 comments on commit f32695d

Please sign in to comment.