Skip to content
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
11 changes: 11 additions & 0 deletions .bazelci/presubmit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -777,6 +777,17 @@ tasks:
- "//..."
test_targets:
- "//..."
example_runfiles:
name: Runfiles example
platform: ubuntu2204
working_directory: examples/runfiles
build_targets:
- "//..."
test_targets:
- "//..."
run_targets:
- "//pkg_a:pkg_a_binary"
- "//pkg_b:pkg_b_binary"
ubuntu2204_bzlmod_bcr:
name: bzlmod BCR presubmit
platform: ubuntu2204
Expand Down
2 changes: 2 additions & 0 deletions examples/runfiles/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/bazel-*
user.bazelrc
16 changes: 16 additions & 0 deletions examples/runfiles/MODULE.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module(
name = "runfiles_example",
version = "0.0.0",
)

###############################################################################
# B A Z E L C E N T R A L R E G I S T R Y # https://registry.bazel.build/
###############################################################################
# https://github.com/bazelbuild/rules_rust/releases
bazel_dep(name = "rules_rust", version = "0.0.0")
local_path_override(
module_name = "rules_rust",
path = "../..",
)

bazel_dep(name = "rules_shell", version = "0.6.1")
216 changes: 216 additions & 0 deletions examples/runfiles/MODULE.bazel.lock

Large diffs are not rendered by default.

34 changes: 34 additions & 0 deletions examples/runfiles/pkg_a/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
load("@rules_rust//rust:defs.bzl", "rust_binary", "rust_library", "rust_test")
load("@rules_shell//shell:sh_binary.bzl", "sh_binary")

sh_binary(
name = "say_hello",
srcs = ["src/say_hello.sh"],
visibility = ["//visibility:public"],
)

rust_library(
name = "pkg_a_library",
srcs = ["src/lib.rs"],
data = [":say_hello"],
rustc_env = {
"SAY_HELLO_BIN": "$(rlocationpath :say_hello)",
},
visibility = ["//visibility:public"],
deps = [
"@rules_rust//rust/runfiles",
],
)

rust_binary(
name = "pkg_a_binary",
srcs = ["src/main.rs"],
visibility = ["//visibility:public"],
deps = [":pkg_a_library"],
)

rust_test(
name = "pkg_a_test",
srcs = ["src/test.rs"],
deps = [":pkg_a_library"],
)
50 changes: 50 additions & 0 deletions examples/runfiles/pkg_a/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
use runfiles::{rlocation, Runfiles};
use std::process;
use std::path::PathBuf;

pub fn invoke_binary(bin: PathBuf) {
let child = process::Command::new(bin)
.stdin(process::Stdio::piped())
.stdout(process::Stdio::piped())
.spawn()
.expect("Failed to spawn command");
let output = child.wait_with_output().expect("Failed to read stdout");
let stdout = String::from_utf8(output.stdout).unwrap();
println!("say_hello binary output: {}", stdout.trim());
}

pub fn invoke_say_hello_binary_with_path() {
let r = Runfiles::create().unwrap();
let bin = rlocation!(r, "_main/pkg_a/say_hello").unwrap();
println!("Invoking say_hello binary with path: {}", bin.display());

invoke_binary(bin);
}

pub fn invoke_say_hello_binary_with_env_var() {
let r = Runfiles::create().unwrap();
let bin = rlocation!(r, env!("SAY_HELLO_BIN")).unwrap();
println!("Invoking say_hello binary with env var: {}", bin.display());

invoke_binary(bin);
}

pub fn invoke_say_hello_with_late_join() {
let r = Runfiles::create().unwrap();
let bin_folder = rlocation!(r, "_main/pkg_a").unwrap();

let bin = bin_folder.join("say_hello");
println!("Invoking pkg_a binary with late join: {}", bin.display());

invoke_binary(bin);
}

pub fn invoke_say_hello_with_late_join_parent() {
let r = Runfiles::create().unwrap();
let bin_folder = rlocation!(r, "_main/pkg_a/say_hello").unwrap();

let bin = bin_folder.parent().unwrap().join("say_hello");
println!("Invoking pkg_a binary with late join parent: {}", bin.display());

invoke_binary(bin);
}
8 changes: 8 additions & 0 deletions examples/runfiles/pkg_a/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
use pkg_a_library;

fn main() {
pkg_a_library::invoke_say_hello_binary_with_path();
pkg_a_library::invoke_say_hello_binary_with_env_var();
// pkg_a_library::invoke_say_hello_with_late_join(); // This fails with bazel run, will be added in a follow up PR
pkg_a_library::invoke_say_hello_with_late_join_parent();
}
4 changes: 4 additions & 0 deletions examples/runfiles/pkg_a/src/say_hello.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env bash

echo "Hello, world from $(pwd)"
echo "========================"
21 changes: 21 additions & 0 deletions examples/runfiles/pkg_a/src/test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
use pkg_a_library;

#[test]
fn test_invoke_say_hello_binary_with_path() {
pkg_a_library::invoke_say_hello_binary_with_path();
}

#[test]
fn test_invoke_say_hello_binary_with_env_var() {
pkg_a_library::invoke_say_hello_binary_with_env_var();
}

#[test]
fn test_invoke_say_hello_with_late_join() {
pkg_a_library::invoke_say_hello_with_late_join();
}

#[test]
fn test_invoke_say_hello_with_late_join_parent() {
pkg_a_library::invoke_say_hello_with_late_join_parent();
}
36 changes: 36 additions & 0 deletions examples/runfiles/pkg_b/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
load("@rules_rust//rust:defs.bzl", "rust_binary", "rust_library", "rust_test")

rust_library(
name = "pkg_b_library",
srcs = ["src/lib.rs"],
data = [
"//pkg_a:pkg_a_binary",
"//pkg_a:say_hello",
],
rustc_env = {
"PKG_A_BINARY_BIN": "$(rlocationpath //pkg_a:pkg_a_binary)",
"SAY_HELLO_BIN": "$(rlocationpath //pkg_a:say_hello)",
},
deps = [
"//pkg_a:pkg_a_library",
"@rules_rust//rust/runfiles",
],
)

rust_binary(
name = "pkg_b_binary",
srcs = ["src/main.rs"],
deps = [
":pkg_b_library",
"//pkg_a:pkg_a_library",
],
)

rust_test(
name = "pkg_b_test",
srcs = ["src/test.rs"],
deps = [
":pkg_b_library",
"//pkg_a:pkg_a_library",
],
)
54 changes: 54 additions & 0 deletions examples/runfiles/pkg_b/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
use runfiles::{rlocation, Runfiles};
use pkg_a_library::invoke_binary;

pub fn invoke_say_hello_binary_with_path() {
let r = Runfiles::create().unwrap();
let bin = rlocation!(r, "_main/pkg_a/say_hello").unwrap();
println!("Invoking say_hello binary with path: {}", bin.display());

invoke_binary(bin);
}

pub fn invoke_say_hello_binary_with_env_var() {
let r = Runfiles::create().unwrap();
let bin = rlocation!(r, env!("SAY_HELLO_BIN")).unwrap();
println!("Invoking say_hello binary with env var: {}", bin.display());

invoke_binary(bin);
}

pub fn invoke_pkg_a_binary_with_path() {
let r = Runfiles::create().unwrap();
let bin = rlocation!(r, "_main/pkg_a/pkg_a_binary").unwrap();
println!("Invoking pkg_a binary with path: {}", bin.display());

invoke_binary(bin);
}

pub fn invoke_pkg_a_binary_with_env_var() {
let r = Runfiles::create().unwrap();
let bin = rlocation!(r, env!("PKG_A_BINARY_BIN")).unwrap();
println!("Invoking pkg_a binary with env var: {}", bin.display());

invoke_binary(bin);
}

pub fn invoke_pkg_a_binary_with_late_join() {
let r = Runfiles::create().unwrap();
let bin_folder = rlocation!(r, "_main/pkg_a").unwrap();

let bin = bin_folder.join("pkg_a_binary");
println!("Invoking pkg_a binary with late join: {}", bin.display());

invoke_binary(bin);
}

pub fn invoke_pkg_a_binary_with_late_join_parent() {
let r = Runfiles::create().unwrap();
let bin_folder = rlocation!(r, "_main/pkg_a/pkg_a_binary").unwrap();

let bin = bin_folder.parent().unwrap().join("pkg_a_binary");
println!("Invoking pkg_a binary with late join parent: {}", bin.display());

invoke_binary(bin);
}
16 changes: 16 additions & 0 deletions examples/runfiles/pkg_b/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use pkg_b_library;

fn main() {
pkg_a_library::invoke_say_hello_binary_with_path();
pkg_a_library::invoke_say_hello_binary_with_env_var();
// pkg_a_library::invoke_say_hello_with_late_join(); // This fails with bazel run, will be added in a follow up PR
pkg_a_library::invoke_say_hello_with_late_join_parent();

pkg_b_library::invoke_say_hello_binary_with_path();
pkg_b_library::invoke_say_hello_binary_with_env_var();

pkg_b_library::invoke_pkg_a_binary_with_path();
pkg_b_library::invoke_pkg_a_binary_with_env_var();
// pkg_b_library::invoke_pkg_a_binary_with_late_join(); // This fails with bazel run, will be added in a follow up PR
pkg_b_library::invoke_pkg_a_binary_with_late_join_parent();
}
42 changes: 42 additions & 0 deletions examples/runfiles/pkg_b/src/test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
use pkg_b_library;
use pkg_a_library;

#[test]
fn test_invoke_say_hello_binary_with_path() {
pkg_b_library::invoke_say_hello_binary_with_path();
}

#[test]
fn test_invoke_say_hello_binary_with_env_var() {
pkg_b_library::invoke_say_hello_binary_with_env_var();
}

#[test]
fn test_invoke_pkg_a_say_hello_binary_with_path() {
pkg_a_library::invoke_say_hello_binary_with_path();
}

#[test]
fn test_invoke_pkg_a_say_hello_binary_with_env_var() {
pkg_a_library::invoke_say_hello_binary_with_env_var();
}

#[test]
fn test_invoke_pkg_a_binary_with_path() {
pkg_b_library::invoke_pkg_a_binary_with_path();
}

#[test]
fn test_invoke_pkg_a_binary_with_env_var() {
pkg_b_library::invoke_pkg_a_binary_with_env_var();
}

#[test]
fn test_invoke_pkg_a_binary_with_late_join() {
pkg_b_library::invoke_pkg_a_binary_with_late_join();
}

#[test]
fn test_invoke_pkg_a_binary_with_late_join_parent() {
pkg_b_library::invoke_pkg_a_binary_with_late_join_parent();
}