Skip to content

Commit c75ea6f

Browse files
authored
Add Runfiles::current_repository to runfiles library (#1713)
1 parent 2f05117 commit c75ea6f

File tree

8 files changed

+75
-37
lines changed

8 files changed

+75
-37
lines changed

rust/private/BUILD.bazel

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
22
load("//rust/private:rust_analyzer.bzl", "rust_analyzer_detect_sysroot")
3-
load("//rust/private:rustfmt.bzl", "rustfmt_workspace_name")
43
load("//rust/private:stamp.bzl", "stamp_build_setting")
54

65
bzl_library(
@@ -16,8 +15,3 @@ rust_analyzer_detect_sysroot(
1615
name = "rust_analyzer_detect_sysroot",
1716
visibility = ["//visibility:public"],
1817
)
19-
20-
rustfmt_workspace_name(
21-
name = "rustfmt_workspace_name",
22-
visibility = ["//visibility:public"],
23-
)

rust/private/rustfmt.bzl

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -204,22 +204,3 @@ rustfmt_test = rule(
204204
},
205205
test = True,
206206
)
207-
208-
def _rustfmt_workspace_name_impl(ctx):
209-
output = ctx.actions.declare_file(ctx.label.name)
210-
211-
ctx.actions.write(
212-
output = output,
213-
content = "RUSTFMT_WORKSPACE={}".format(
214-
ctx.workspace_name,
215-
),
216-
)
217-
218-
return [DefaultInfo(
219-
files = depset([output]),
220-
)]
221-
222-
rustfmt_workspace_name = rule(
223-
implementation = _rustfmt_workspace_name_impl,
224-
doc = "A rule for detecting the workspace name for Rustfmt runfiles.",
225-
)

tools/runfiles/BUILD.bazel

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,22 @@
11
load(
22
"//rust:defs.bzl",
3-
"rust_doc_test",
3+
"rust_doc",
44
"rust_library",
55
"rust_test",
66
)
7+
load("//tools/runfiles/private:runfiles_utils.bzl", "workspace_name")
8+
9+
workspace_name(
10+
name = "workspace_name.env",
11+
)
712

813
rust_library(
914
name = "runfiles",
1015
srcs = ["runfiles.rs"],
1116
edition = "2018",
17+
rustc_env_files = [
18+
":workspace_name.env",
19+
],
1220
visibility = ["//visibility:public"],
1321
)
1422

@@ -18,7 +26,7 @@ rust_test(
1826
data = ["data/sample.txt"],
1927
)
2028

21-
rust_doc_test(
22-
name = "runfiles_doc_test",
29+
rust_doc(
30+
name = "runfiles_doc",
2331
crate = ":runfiles",
2432
)

tools/runfiles/private/BUILD.bazel

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package(default_visibility = ["//tools/runfiles:__pkg__"])
2+
3+
filegroup(
4+
name = "distro",
5+
srcs = glob([
6+
"**/*.bzl",
7+
]) + [
8+
"BUILD.bazel",
9+
],
10+
)
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
"""Utilities for the `@rules_rust//tools/runfiles` library"""
2+
3+
_RULES_RUST_RUNFILES_WORKSPACE_NAME = "RULES_RUST_RUNFILES_WORKSPACE_NAME"
4+
5+
def _workspace_name_impl(ctx):
6+
output = ctx.actions.declare_file(ctx.label.name)
7+
8+
ctx.actions.write(
9+
output = output,
10+
content = "{}={}\n".format(
11+
_RULES_RUST_RUNFILES_WORKSPACE_NAME,
12+
ctx.workspace_name,
13+
),
14+
)
15+
16+
return [DefaultInfo(
17+
files = depset([output]),
18+
)]
19+
20+
workspace_name = rule(
21+
implementation = _workspace_name_impl,
22+
doc = """\
23+
A rule for detecting the current workspace name and writing it to a file for
24+
for use with `rustc_env_files` attributes on `rust_*` rules. The workspace
25+
name is exposed by the variable `{}`.""".format(_RULES_RUST_RUNFILES_WORKSPACE_NAME),
26+
)

tools/runfiles/runfiles.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,13 @@ impl Runfiles {
105105
.clone(),
106106
}
107107
}
108+
109+
/// Returns the canonical name of the caller's Bazel repository.
110+
pub fn current_repository(&self) -> &str {
111+
// This value must match the value of `_RULES_RUST_RUNFILES_WORKSPACE_NAME`
112+
// which can be found in `@rules_rust//tools/runfiles/private:workspace_name.bzl`
113+
env!("RULES_RUST_RUNFILES_WORKSPACE_NAME")
114+
}
108115
}
109116

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

248255
assert_eq!(r.rlocation("a/b"), PathBuf::from("c/d"));
249256
}
257+
258+
#[test]
259+
fn test_current_repository() {
260+
let r = Runfiles::create().unwrap();
261+
262+
// This check is unique to the rules_rust repository. The name
263+
// here is expected to be different in consumers of this library
264+
assert_eq!(r.current_repository(), "rules_rust")
265+
}
250266
}

tools/rustfmt/BUILD.bazel

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,6 @@ rust_library(
2626
"RUSTFMT": "$(rootpath //rust/toolchain:current_rustfmt_files)",
2727
"RUSTFMT_CONFIG": "$(rootpath //:rustfmt.toml)",
2828
},
29-
rustc_env_files = [
30-
"@rules_rust//rust/private:rustfmt_workspace_name",
31-
],
3229
deps = [
3330
"//tools/runfiles",
3431
],

tools/rustfmt/src/lib.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,18 @@ pub struct RustfmtConfig {
2121
pub fn parse_rustfmt_config() -> RustfmtConfig {
2222
let runfiles = runfiles::Runfiles::create().unwrap();
2323

24-
let rustfmt = runfiles.rlocation(concat!(env!("RUSTFMT_WORKSPACE"), "/", env!("RUSTFMT")));
24+
let rustfmt = runfiles.rlocation(format!(
25+
"{}/{}",
26+
runfiles.current_repository(),
27+
env!("RUSTFMT")
28+
));
2529
if !rustfmt.exists() {
2630
panic!("rustfmt does not exist at: {}", rustfmt.display());
2731
}
2832

29-
let config = runfiles.rlocation(concat!(
30-
env!("RUSTFMT_WORKSPACE"),
31-
"/",
33+
let config = runfiles.rlocation(format!(
34+
"{}/{}",
35+
runfiles.current_repository(),
3236
env!("RUSTFMT_CONFIG")
3337
));
3438
if !config.exists() {
@@ -76,7 +80,7 @@ pub fn parse_rustfmt_manifest(manifest: &Path) -> RustfmtManifest {
7680
edition,
7781
sources: lines
7882
.into_iter()
79-
.map(|src| runfiles.rlocation(format!("{}/{}", env!("RUSTFMT_WORKSPACE"), src)))
83+
.map(|src| runfiles.rlocation(format!("{}/{}", runfiles.current_repository(), src)))
8084
.collect(),
8185
}
8286
}
@@ -95,7 +99,9 @@ pub fn find_manifests() -> Vec<PathBuf> {
9599
std::env::var("RUSTFMT_MANIFESTS")
96100
.map(|var| {
97101
var.split(PATH_ENV_SEP)
98-
.map(|path| runfiles.rlocation(format!("{}/{}", env!("RUSTFMT_WORKSPACE"), path)))
102+
.map(|path| {
103+
runfiles.rlocation(format!("{}/{}", runfiles.current_repository(), path,))
104+
})
99105
.collect()
100106
})
101107
.unwrap_or_default()

0 commit comments

Comments
 (0)