Skip to content

Commit

Permalink
Allow searching for the rlocation of a module. (#2652)
Browse files Browse the repository at this point in the history
Currently, if the repo mapping maps "foo" to "real_foo", then:
* `rlocation!(r, "foo/bar/baz")` will return "real_foo/bar/baz"
* `rlocation!(r, "foo")` will return `foo`

This PR handles the case where the user attempts to do something like
`rlocation!("rules_rust")` (which is valid for directory based
runfiles).
  • Loading branch information
matts1 authored May 15, 2024
1 parent c88ba10 commit d87eadf
Showing 1 changed file with 23 additions and 19 deletions.
42 changes: 23 additions & 19 deletions tools/runfiles/runfiles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ pub struct Runfiles {

impl Runfiles {
/// Creates a manifest based Runfiles object when
/// RUNFILES_MANIFEST_ONLY environment variable is present,
/// RUNFILES_MANIFEST_FILE environment variable is present,
/// or a directory based Runfiles object otherwise.
pub fn create() -> io::Result<Self> {
let mode = if let Some(manifest_file) = std::env::var_os(MANIFEST_FILE_ENV_VAR) {
Expand Down Expand Up @@ -125,21 +125,22 @@ impl Runfiles {
return path.to_path_buf();
}

let parts: Vec<&str> = path
.to_str()
.expect("Should be valid UTF8")
.splitn(2, '/')
.collect();
if parts.len() == 2 {
let key: (String, String) = (source_repo.into(), parts[0].into());
if let Some(target_repo_directory) = self.repo_mapping.get(&key) {
return raw_rlocation(
&self.mode,
target_repo_directory.to_owned() + "/" + parts[1],
);
};
let path_str = path.to_str().expect("Should be valid UTF8");
let (repo_alias, repo_path): (&str, Option<&str>) = match path_str.split_once('/') {
Some((name, alias)) => (name, Some(alias)),
None => (path_str, None),
};
let key: (String, String) = (source_repo.into(), repo_alias.into());
if let Some(target_repo_directory) = self.repo_mapping.get(&key) {
match repo_path {
Some(repo_path) => {
raw_rlocation(&self.mode, format!("{target_repo_directory}/{repo_path}"))
}
None => raw_rlocation(&self.mode, target_repo_directory),
}
} else {
raw_rlocation(&self.mode, path)
}
raw_rlocation(&self.mode, path)
}
}

Expand Down Expand Up @@ -259,8 +260,11 @@ mod test {
env::remove_var(MANIFEST_FILE_ENV_VAR);
let r = Runfiles::create().unwrap();

let mut f =
File::open(r.rlocation("rules_rust/tools/runfiles/data/sample.txt")).unwrap();
let d = rlocation!(r, "rules_rust");
let f = rlocation!(r, "rules_rust/tools/runfiles/data/sample.txt");
assert_eq!(d.join("tools/runfiles/data/sample.txt"), f);

let mut f = File::open(f).unwrap();

let mut buffer = String::new();
f.read_to_string(&mut buffer).unwrap();
Expand All @@ -276,7 +280,7 @@ mod test {
let r = Runfiles::create().unwrap();

let mut f =
File::open(r.rlocation("rules_rust/tools/runfiles/data/sample.txt")).unwrap();
File::open(rlocation!(r, "rules_rust/tools/runfiles/data/sample.txt")).unwrap();

let mut buffer = String::new();
f.read_to_string(&mut buffer).unwrap();
Expand All @@ -295,7 +299,7 @@ mod test {
let r = Runfiles::create().unwrap();

let mut f =
File::open(r.rlocation("rules_rust/tools/runfiles/data/sample.txt")).unwrap();
File::open(rlocation!(r, "rules_rust/tools/runfiles/data/sample.txt")).unwrap();

let mut buffer = String::new();
f.read_to_string(&mut buffer).unwrap();
Expand Down

0 comments on commit d87eadf

Please sign in to comment.