Skip to content

Commit

Permalink
Remove runfiles.current_repository()
Browse files Browse the repository at this point in the history
  • Loading branch information
dzbarsky committed Mar 21, 2024
1 parent 5c621aa commit cb7a2bf
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 111 deletions.
8 changes: 0 additions & 8 deletions tools/runfiles/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,11 @@ load(
"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 Down
10 changes: 0 additions & 10 deletions tools/runfiles/private/BUILD.bazel

This file was deleted.

26 changes: 0 additions & 26 deletions tools/runfiles/private/runfiles_utils.bzl

This file was deleted.

68 changes: 11 additions & 57 deletions tools/runfiles/runfiles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,33 +19,31 @@
//! use runfiles::Runfiles;
//! ```
//!
//! 3. Create a Runfiles object and use Rlocation! to look up runfile paths:
//! 3. Create a Runfiles object and use `rlocation!`` to look up runfile paths:
//! ```ignore -- This doesn't work under rust_doc_test because argv[0] is not what we expect.
//!
//! use runfiles::{Runfiles, RLocation};
//! use runfiles::{Runfiles, rlocation};
//!
//! let r = Runfiles::create().unwrap();
//! let path = Rlocation!(r, "my_workspace/path/to/my/data.txt");
//! let path = rlocation!(r, "my_workspace/path/to/my/data.txt");
//!
//! let f = File::open(path).unwrap();
//! // ...
//! ```

use std::collections::HashMap;
use std::env;
use std::ffi::OsString;
use std::fs;
use std::io;
use std::path::Path;
use std::path::PathBuf;

const RUNFILES_DIR_ENV_VAR: &str = "RUNFILES_DIR";
const MANIFEST_FILE_ENV_VAR: &str = "RUNFILES_MANIFEST_FILE";
const MANIFEST_ONLY_ENV_VAR: &str = "RUNFILES_MANIFEST_ONLY";
const TEST_SRCDIR_ENV_VAR: &str = "TEST_SRCDIR";

#[macro_export]
macro_rules! Rlocation {
macro_rules! rlocation {
($r:ident, $path:expr) => {
$r.rlocation_from($path, env!("REPOSITORY_NAME"))
};
Expand All @@ -71,23 +69,18 @@ impl Runfiles {
/// RUNFILES_MANIFEST_ONLY environment variable is present,
/// or a directory based Runfiles object otherwise.
pub fn create() -> io::Result<Self> {
let mode = if is_manifest_only() {
Self::create_manifest_based()
let mode = if let Ok(manifest_file) = std::env::var(MANIFEST_FILE_ENV_VAR) {
Self::create_manifest_based(&PathBuf::from(manifest_file))?
} else {
Self::create_directory_based()
}?;
Mode::DirectoryBased(find_runfiles_dir()?)
};

let repo_mapping = parse_repo_mapping(raw_rlocation(&mode, "_repo_mapping"))?;

Ok(Runfiles { mode, repo_mapping })
}

fn create_directory_based() -> io::Result<Mode> {
Ok(Mode::DirectoryBased(find_runfiles_dir()?))
}

fn create_manifest_based() -> io::Result<Mode> {
let manifest_path = find_manifest_path()?;
fn create_manifest_based(manifest_path: &Path) -> io::Result<Mode> {
let manifest_content = std::fs::read_to_string(manifest_path)?;
let path_mapping = manifest_content
.lines()
Expand Down Expand Up @@ -121,7 +114,7 @@ impl Runfiles {
/// The returned path may not be valid. The caller should check the path's
/// validity and that the path exists.
///
/// Typically this should be used via the `RLocation!` macro to properly set source_repo.
/// Typically this should be used via the `rlocation!` macro to properly set source_repo.
pub fn rlocation_from(&self, path: impl AsRef<Path>, source_repo: &str) -> PathBuf {
let path = path.as_ref();
if path.is_absolute() {
Expand All @@ -137,13 +130,6 @@ impl Runfiles {
}
raw_rlocation(&self.mode, path)
}

/// 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")
}
}

fn raw_rlocation(mode: &Mode, path: impl AsRef<Path>) -> PathBuf {
Expand Down Expand Up @@ -171,10 +157,7 @@ fn parse_repo_mapping(path: PathBuf) -> io::Result<RepoMapping> {

/// Returns the .runfiles directory for the currently executing binary.
pub fn find_runfiles_dir() -> io::Result<PathBuf> {
assert_ne!(
std::env::var_os(MANIFEST_ONLY_ENV_VAR).unwrap_or_else(|| OsString::from("0")),
"1"
);
assert!(std::env::var_os(MANIFEST_FILE_ENV_VAR).is_none());

// If bazel told us about the runfiles dir, use that without looking further.
if let Some(runfiles_dir) = std::env::var_os(RUNFILES_DIR_ENV_VAR).map(PathBuf::from) {
Expand Down Expand Up @@ -237,26 +220,6 @@ fn make_io_error(msg: &str) -> io::Error {
io::Error::new(io::ErrorKind::Other, msg)
}

fn is_manifest_only() -> bool {
match std::env::var(MANIFEST_ONLY_ENV_VAR) {
Ok(val) => val == "1",
Err(_) => false,
}
}

fn find_manifest_path() -> io::Result<PathBuf> {
assert_eq!(
std::env::var_os(MANIFEST_ONLY_ENV_VAR).expect("RUNFILES_MANIFEST_ONLY was not set"),
OsString::from("1")
);
match std::env::var_os(MANIFEST_FILE_ENV_VAR) {
Some(path) => Ok(path.into()),
None => Err(
make_io_error(
"RUNFILES_MANIFEST_ONLY was set to '1', but RUNFILES_MANIFEST_FILE was not set. Did Bazel change?"))
}
}

#[cfg(test)]
mod test {
use super::*;
Expand Down Expand Up @@ -336,13 +299,4 @@ 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")
}
}
17 changes: 7 additions & 10 deletions tools/rustfmt/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,16 @@ pub struct RustfmtConfig {
pub fn parse_rustfmt_config() -> RustfmtConfig {
let runfiles = runfiles::Runfiles::create().unwrap();

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

let config = runfiles.rlocation(format!(
"{}/{}",
runfiles.current_repository(),
let config = runfiles::rlocation!(runfiles, format!(
"rules_rust/{}",
env!("RUSTFMT_CONFIG")
));
if !config.exists() {
Expand Down Expand Up @@ -79,7 +77,7 @@ pub fn parse_rustfmt_manifest(manifest: &Path) -> RustfmtManifest {
edition,
sources: lines
.into_iter()
.map(|src| runfiles.rlocation(format!("{}/{}", runfiles.current_repository(), src)))
.map(|src| runfiles::rlocation!(runfiles, format!("rules_rust/{}", src)))
.collect(),
}
}
Expand All @@ -100,9 +98,8 @@ pub fn find_manifests() -> Vec<PathBuf> {
var.split(PATH_ENV_SEP)
.filter_map(|path| match path.is_empty() {
true => None,
false => Some(runfiles.rlocation(format!(
"{}/{}",
runfiles.current_repository(),
false => Some(runfiles::rlocation!(runfiles, format!(
"rules_rust/{}",
path
))),
})
Expand Down

0 comments on commit cb7a2bf

Please sign in to comment.