Skip to content

Commit a14b741

Browse files
hofbiUebelAndre
andauthored
Add runfiles example with more levels of indirection (#3628)
#3399 changed the behavior of runfiles. Due to this change a few cases no longer work with `bazel run` that did work before. I tried to create a more complex runfiles example since rules_rust does not have anyone yet + we can use it for discussing what should be the correct behavior. ```shell # Everything with bazel test still works bazel test //... # bazel run no longer works for all cases that work with bazel test bazel run //pkg_a:pkg_a_binary bazel run //pkg_b:pkg_b_binary ``` I added a comment to the few cases that still work with `bazel test` but no longer work with `bazel run`. In particular, this happens when requesting a folder through runfiles instead of an individual file. Co-authored-by: UebelAndre <github@uebelandre.com>
1 parent d492b33 commit a14b741

13 files changed

Lines changed: 510 additions & 0 deletions

File tree

.bazelci/presubmit.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -777,6 +777,17 @@ tasks:
777777
- "//..."
778778
test_targets:
779779
- "//..."
780+
example_runfiles:
781+
name: Runfiles example
782+
platform: ubuntu2204
783+
working_directory: examples/runfiles
784+
build_targets:
785+
- "//..."
786+
test_targets:
787+
- "//..."
788+
run_targets:
789+
- "//pkg_a:pkg_a_binary"
790+
- "//pkg_b:pkg_b_binary"
780791
ubuntu2204_bzlmod_bcr:
781792
name: bzlmod BCR presubmit
782793
platform: ubuntu2204

examples/runfiles/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/bazel-*
2+
user.bazelrc

examples/runfiles/MODULE.bazel

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
module(
2+
name = "runfiles_example",
3+
version = "0.0.0",
4+
)
5+
6+
###############################################################################
7+
# B A Z E L C E N T R A L R E G I S T R Y # https://registry.bazel.build/
8+
###############################################################################
9+
# https://github.com/bazelbuild/rules_rust/releases
10+
bazel_dep(name = "rules_rust", version = "0.0.0")
11+
local_path_override(
12+
module_name = "rules_rust",
13+
path = "../..",
14+
)
15+
16+
bazel_dep(name = "rules_shell", version = "0.6.1")

examples/runfiles/MODULE.bazel.lock

Lines changed: 216 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
load("@rules_rust//rust:defs.bzl", "rust_binary", "rust_library", "rust_test")
2+
load("@rules_shell//shell:sh_binary.bzl", "sh_binary")
3+
4+
sh_binary(
5+
name = "say_hello",
6+
srcs = ["src/say_hello.sh"],
7+
visibility = ["//visibility:public"],
8+
)
9+
10+
rust_library(
11+
name = "pkg_a_library",
12+
srcs = ["src/lib.rs"],
13+
data = [":say_hello"],
14+
rustc_env = {
15+
"SAY_HELLO_BIN": "$(rlocationpath :say_hello)",
16+
},
17+
visibility = ["//visibility:public"],
18+
deps = [
19+
"@rules_rust//rust/runfiles",
20+
],
21+
)
22+
23+
rust_binary(
24+
name = "pkg_a_binary",
25+
srcs = ["src/main.rs"],
26+
visibility = ["//visibility:public"],
27+
deps = [":pkg_a_library"],
28+
)
29+
30+
rust_test(
31+
name = "pkg_a_test",
32+
srcs = ["src/test.rs"],
33+
deps = [":pkg_a_library"],
34+
)

examples/runfiles/pkg_a/src/lib.rs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
use runfiles::{rlocation, Runfiles};
2+
use std::process;
3+
use std::path::PathBuf;
4+
5+
pub fn invoke_binary(bin: PathBuf) {
6+
let child = process::Command::new(bin)
7+
.stdin(process::Stdio::piped())
8+
.stdout(process::Stdio::piped())
9+
.spawn()
10+
.expect("Failed to spawn command");
11+
let output = child.wait_with_output().expect("Failed to read stdout");
12+
let stdout = String::from_utf8(output.stdout).unwrap();
13+
println!("say_hello binary output: {}", stdout.trim());
14+
}
15+
16+
pub fn invoke_say_hello_binary_with_path() {
17+
let r = Runfiles::create().unwrap();
18+
let bin = rlocation!(r, "_main/pkg_a/say_hello").unwrap();
19+
println!("Invoking say_hello binary with path: {}", bin.display());
20+
21+
invoke_binary(bin);
22+
}
23+
24+
pub fn invoke_say_hello_binary_with_env_var() {
25+
let r = Runfiles::create().unwrap();
26+
let bin = rlocation!(r, env!("SAY_HELLO_BIN")).unwrap();
27+
println!("Invoking say_hello binary with env var: {}", bin.display());
28+
29+
invoke_binary(bin);
30+
}
31+
32+
pub fn invoke_say_hello_with_late_join() {
33+
let r = Runfiles::create().unwrap();
34+
let bin_folder = rlocation!(r, "_main/pkg_a").unwrap();
35+
36+
let bin = bin_folder.join("say_hello");
37+
println!("Invoking pkg_a binary with late join: {}", bin.display());
38+
39+
invoke_binary(bin);
40+
}
41+
42+
pub fn invoke_say_hello_with_late_join_parent() {
43+
let r = Runfiles::create().unwrap();
44+
let bin_folder = rlocation!(r, "_main/pkg_a/say_hello").unwrap();
45+
46+
let bin = bin_folder.parent().unwrap().join("say_hello");
47+
println!("Invoking pkg_a binary with late join parent: {}", bin.display());
48+
49+
invoke_binary(bin);
50+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
use pkg_a_library;
2+
3+
fn main() {
4+
pkg_a_library::invoke_say_hello_binary_with_path();
5+
pkg_a_library::invoke_say_hello_binary_with_env_var();
6+
// pkg_a_library::invoke_say_hello_with_late_join(); // This fails with bazel run, will be added in a follow up PR
7+
pkg_a_library::invoke_say_hello_with_late_join_parent();
8+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/usr/bin/env bash
2+
3+
echo "Hello, world from $(pwd)"
4+
echo "========================"
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
use pkg_a_library;
2+
3+
#[test]
4+
fn test_invoke_say_hello_binary_with_path() {
5+
pkg_a_library::invoke_say_hello_binary_with_path();
6+
}
7+
8+
#[test]
9+
fn test_invoke_say_hello_binary_with_env_var() {
10+
pkg_a_library::invoke_say_hello_binary_with_env_var();
11+
}
12+
13+
#[test]
14+
fn test_invoke_say_hello_with_late_join() {
15+
pkg_a_library::invoke_say_hello_with_late_join();
16+
}
17+
18+
#[test]
19+
fn test_invoke_say_hello_with_late_join_parent() {
20+
pkg_a_library::invoke_say_hello_with_late_join_parent();
21+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
load("@rules_rust//rust:defs.bzl", "rust_binary", "rust_library", "rust_test")
2+
3+
rust_library(
4+
name = "pkg_b_library",
5+
srcs = ["src/lib.rs"],
6+
data = [
7+
"//pkg_a:pkg_a_binary",
8+
"//pkg_a:say_hello",
9+
],
10+
rustc_env = {
11+
"PKG_A_BINARY_BIN": "$(rlocationpath //pkg_a:pkg_a_binary)",
12+
"SAY_HELLO_BIN": "$(rlocationpath //pkg_a:say_hello)",
13+
},
14+
deps = [
15+
"//pkg_a:pkg_a_library",
16+
"@rules_rust//rust/runfiles",
17+
],
18+
)
19+
20+
rust_binary(
21+
name = "pkg_b_binary",
22+
srcs = ["src/main.rs"],
23+
deps = [
24+
":pkg_b_library",
25+
"//pkg_a:pkg_a_library",
26+
],
27+
)
28+
29+
rust_test(
30+
name = "pkg_b_test",
31+
srcs = ["src/test.rs"],
32+
deps = [
33+
":pkg_b_library",
34+
"//pkg_a:pkg_a_library",
35+
],
36+
)

0 commit comments

Comments
 (0)