Skip to content

Commit

Permalink
Add Runfiles::current_repository to runfiles library
Browse files Browse the repository at this point in the history
  • Loading branch information
UebelAndre committed Dec 20, 2022
1 parent 2f05117 commit bec16e1
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 37 deletions.
6 changes: 0 additions & 6 deletions rust/private/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
load("//rust/private:rust_analyzer.bzl", "rust_analyzer_detect_sysroot")
load("//rust/private:rustfmt.bzl", "rustfmt_workspace_name")
load("//rust/private:stamp.bzl", "stamp_build_setting")

bzl_library(
Expand All @@ -16,8 +15,3 @@ rust_analyzer_detect_sysroot(
name = "rust_analyzer_detect_sysroot",
visibility = ["//visibility:public"],
)

rustfmt_workspace_name(
name = "rustfmt_workspace_name",
visibility = ["//visibility:public"],
)
19 changes: 0 additions & 19 deletions rust/private/rustfmt.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -204,22 +204,3 @@ rustfmt_test = rule(
},
test = True,
)

def _rustfmt_workspace_name_impl(ctx):
output = ctx.actions.declare_file(ctx.label.name)

ctx.actions.write(
output = output,
content = "RUSTFMT_WORKSPACE={}".format(
ctx.workspace_name,
),
)

return [DefaultInfo(
files = depset([output]),
)]

rustfmt_workspace_name = rule(
implementation = _rustfmt_workspace_name_impl,
doc = "A rule for detecting the workspace name for Rustfmt runfiles.",
)
14 changes: 11 additions & 3 deletions tools/runfiles/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
load(
"//rust:defs.bzl",
"rust_doc_test",
"rust_doc",
"rust_library",
"rust_test",
)
load("//tools/runfiles/private:runfiles_utils.bzl", "workspace_name")

workspace_name(
name = "workspace_name.env",
)

rust_library(
name = "runfiles",
srcs = ["runfiles.rs"],
edition = "2018",
rustc_env_files = [
":workspace_name.env",
],
visibility = ["//visibility:public"],
)

Expand All @@ -18,7 +26,7 @@ rust_test(
data = ["data/sample.txt"],
)

rust_doc_test(
name = "runfiles_doc_test",
rust_doc(
name = "runfiles_doc",
crate = ":runfiles",
)
10 changes: 10 additions & 0 deletions tools/runfiles/private/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package(default_visibility = ["//tools/runfiles:__pkg__"])

filegroup(
name = "distro",
srcs = glob([
"**/*.bzl",
]) + [
"BUILD.bazel",
],
)
26 changes: 26 additions & 0 deletions tools/runfiles/private/runfiles_utils.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
"""Utilities for the `@rules_rust//tools/runfiles` library"""

_RULES_RUST_RUNFILES_WORKSPACE_NAME = "RULES_RUST_RUNFILES_WORKSPACE_NAME"

def _workspace_name_impl(ctx):
output = ctx.actions.declare_file(ctx.label.name)

ctx.actions.write(
output = output,
content = "{}={}\n".format(
_RULES_RUST_RUNFILES_WORKSPACE_NAME,
ctx.workspace_name,
),
)

return [DefaultInfo(
files = depset([output]),
)]

workspace_name = rule(
implementation = _workspace_name_impl,
doc = """\
A rule for detecting the current workspace name and writing it to a file for
for use with `rustc_env_files` attributes on `rust_*` rules. The workspace
name is exposed by the variable `{}`.""".format(_RULES_RUST_RUNFILES_WORKSPACE_NAME),
)
16 changes: 16 additions & 0 deletions tools/runfiles/runfiles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,13 @@ impl Runfiles {
.clone(),
}
}

/// Returns the canonical name of the caller's Bazel repository.
pub fn current_repository(&self) -> &str {
// This value must match the value of `_RULES_RUST_RUNFILES_WORKSPACE_NAME`
// which can be found in `@rules_rust//tools/runfiles/private:workspace_name.bzl`
env!("RULES_RUST_RUNFILES_WORKSPACE_NAME")
}
}

/// Returns the .runfiles directory for the currently executing binary.
Expand Down Expand Up @@ -247,4 +254,13 @@ mod test {

assert_eq!(r.rlocation("a/b"), PathBuf::from("c/d"));
}

#[test]
fn test_current_repository() {
let r = Runfiles::create().unwrap();

// This check is unique to the rules_rust repository. The name
// here is expected to be different in consumers of this library
assert_eq!(r.current_repository(), "rules_rust")
}
}
3 changes: 0 additions & 3 deletions tools/rustfmt/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@ rust_library(
"RUSTFMT": "$(rootpath //rust/toolchain:current_rustfmt_files)",
"RUSTFMT_CONFIG": "$(rootpath //:rustfmt.toml)",
},
rustc_env_files = [
"@rules_rust//rust/private:rustfmt_workspace_name",
],
deps = [
"//tools/runfiles",
],
Expand Down
18 changes: 12 additions & 6 deletions tools/rustfmt/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,18 @@ pub struct RustfmtConfig {
pub fn parse_rustfmt_config() -> RustfmtConfig {
let runfiles = runfiles::Runfiles::create().unwrap();

let rustfmt = runfiles.rlocation(concat!(env!("RUSTFMT_WORKSPACE"), "/", env!("RUSTFMT")));
let rustfmt = runfiles.rlocation(format!(
"{}/{}",
runfiles.current_repository(),
env!("RUSTFMT")
));
if !rustfmt.exists() {
panic!("rustfmt does not exist at: {}", rustfmt.display());
}

let config = runfiles.rlocation(concat!(
env!("RUSTFMT_WORKSPACE"),
"/",
let config = runfiles.rlocation(format!(
"{}/{}",
runfiles.current_repository(),
env!("RUSTFMT_CONFIG")
));
if !config.exists() {
Expand Down Expand Up @@ -76,7 +80,7 @@ pub fn parse_rustfmt_manifest(manifest: &Path) -> RustfmtManifest {
edition,
sources: lines
.into_iter()
.map(|src| runfiles.rlocation(format!("{}/{}", env!("RUSTFMT_WORKSPACE"), src)))
.map(|src| runfiles.rlocation(format!("{}/{}", runfiles.current_repository(), src)))
.collect(),
}
}
Expand All @@ -95,7 +99,9 @@ pub fn find_manifests() -> Vec<PathBuf> {
std::env::var("RUSTFMT_MANIFESTS")
.map(|var| {
var.split(PATH_ENV_SEP)
.map(|path| runfiles.rlocation(format!("{}/{}", env!("RUSTFMT_WORKSPACE"), path)))
.map(|path| {
runfiles.rlocation(format!("{}/{}", runfiles.current_repository(), path,))
})
.collect()
})
.unwrap_or_default()
Expand Down

0 comments on commit bec16e1

Please sign in to comment.