From aaa50df186dc5ed120c8bc3036fe8c18d78039fa Mon Sep 17 00:00:00 2001 From: Marco Farrugia Date: Fri, 19 Oct 2018 00:19:26 -0400 Subject: [PATCH 1/9] Move runfiles library to tools/ --- examples/hello_runfiles/BUILD | 9 ++------- tools/BUILD | 10 ++++++++++ .../hello_runfiles/src/lib.rs => tools/runfiles.rs | 0 3 files changed, 12 insertions(+), 7 deletions(-) create mode 100644 tools/BUILD rename examples/hello_runfiles/src/lib.rs => tools/runfiles.rs (100%) diff --git a/examples/hello_runfiles/BUILD b/examples/hello_runfiles/BUILD index e6426b1a90..4166952f58 100644 --- a/examples/hello_runfiles/BUILD +++ b/examples/hello_runfiles/BUILD @@ -7,20 +7,15 @@ load( "rust_test", ) -rust_library( - name = "runfiles", - srcs = ["src/lib.rs"], -) - rust_binary( name = "hello_runfiles", srcs = ["src/main.rs"], data = ["data/sample.txt"], - deps = [":runfiles"], + deps = ["@io_bazel_rules_rust//tools:runfiles"], ) rust_test( name = "hello_runfiles_test", data = ["data/sample.txt"], - deps = [":runfiles"], + deps = ["@io_bazel_rules_rust//tools:runfiles"], ) diff --git a/tools/BUILD b/tools/BUILD new file mode 100644 index 0000000000..facffa071f --- /dev/null +++ b/tools/BUILD @@ -0,0 +1,10 @@ +load( + "@io_bazel_rules_rust//rust:rust.bzl", + "rust_library", +) + +rust_library( + name = "runfiles", + srcs = ["runfiles.rs"], + visibility = ["//visibility:public"], +) diff --git a/examples/hello_runfiles/src/lib.rs b/tools/runfiles.rs similarity index 100% rename from examples/hello_runfiles/src/lib.rs rename to tools/runfiles.rs From 04b699e35c73200781063d29fc75ed424f143e53 Mon Sep 17 00:00:00 2001 From: Marco Farrugia Date: Fri, 19 Oct 2018 00:19:26 -0400 Subject: [PATCH 2/9] Implement directory based runfiles lookup. --- examples/hello_runfiles/BUILD | 4 +- examples/hello_runfiles/src/main.rs | 9 ++- tools/runfiles.rs | 116 +++++++++++++++++++++----- tools/{ => runfiles}/BUILD | 0 tools/runfiles/runfiles.rs | 121 ++++++++++++++++++++++++++++ 5 files changed, 225 insertions(+), 25 deletions(-) rename tools/{ => runfiles}/BUILD (100%) create mode 100644 tools/runfiles/runfiles.rs diff --git a/examples/hello_runfiles/BUILD b/examples/hello_runfiles/BUILD index 4166952f58..71ce450878 100644 --- a/examples/hello_runfiles/BUILD +++ b/examples/hello_runfiles/BUILD @@ -11,11 +11,11 @@ rust_binary( name = "hello_runfiles", srcs = ["src/main.rs"], data = ["data/sample.txt"], - deps = ["@io_bazel_rules_rust//tools:runfiles"], + deps = ["@io_bazel_rules_rust//tools/runfiles"], ) rust_test( name = "hello_runfiles_test", data = ["data/sample.txt"], - deps = ["@io_bazel_rules_rust//tools:runfiles"], + deps = ["@io_bazel_rules_rust//tools/runfiles"], ) diff --git a/examples/hello_runfiles/src/main.rs b/examples/hello_runfiles/src/main.rs index ae0f641f34..698d006947 100644 --- a/examples/hello_runfiles/src/main.rs +++ b/examples/hello_runfiles/src/main.rs @@ -3,14 +3,15 @@ extern crate runfiles; use std::io::prelude::*; use std::fs::File; +use runfiles::Runfiles; fn main() { - let runfiles = runfiles::get_runfiles_dir().unwrap(); + let r = Runfiles::create().unwrap(); - let mut f = File::open(runfiles.join("examples/hello_runfiles/data/sample.txt")).unwrap(); - let mut buffer = String::new(); + let mut f = File::open(r.rlocation("examples/hello_runfiles/data/sample.txt")).unwrap(); + let mut buffer = String::new(); f.read_to_string(&mut buffer).unwrap(); println!("{}", buffer); -} \ No newline at end of file +} diff --git a/tools/runfiles.rs b/tools/runfiles.rs index 3e8aa63cfc..944557aebf 100644 --- a/tools/runfiles.rs +++ b/tools/runfiles.rs @@ -1,22 +1,104 @@ +//! Runfiles lookup library for Bazel-built Rust binaries and tests. +//! +//! USAGE: +//! +//! 1. Depend on this runfiles library from your build rule: +//! ``` +//! rust_binary( +//! name = "my_binary", +//! ... +//! deps = ["@io_bazel_rules_rust//tools/runfiles"], +//! ) +//! ``` +//! +//! 2. Import the runfiles library. +//! ``` +//! extern crate runfiles; +//! +//! use runfiles::Runfiles; +//! ``` +//! +//! 3. Create a Runfiles object and use rlocation to look up runfile paths: +//! ``` +//! let r = Runfiles::create(); +//! let f = File::open(r.rlocation("my_workspace/path/to/my/data.txt")); +//! ``` + use std::io; +use std::fs; use std::path::PathBuf; +use std::path::Path; +use std::env; -/// Returns the .runfiles directory for the currently executing binary. -pub fn get_runfiles_dir() -> io::Result { - let mut path = std::env::current_exe()?; - - if cfg!(target_os = "macos") { - path.pop(); - } else { - let mut name = path.file_name().unwrap().to_owned(); - name.push(".runfiles"); - path.pop(); - path.push(name); +pub struct Runfiles { + runfiles_dir: PathBuf, +} + +impl Runfiles { + /// Creates a directory based Runfiles object. + /// + /// Manifest based creation is not currently supported. + pub fn create() -> io::Result { + Ok(Runfiles { runfiles_dir: find_runfiles_dir()? }) } - Ok(path) + /// Returns the runtime path of a runfile. + /// + /// Runfiles are data-dependencies of Bazel-built binaries and tests. + /// The returned path may not be valid. The caller should check the path's + /// validity and that the path exists. + pub fn rlocation(&self, path: impl AsRef) -> PathBuf { + let path = path.as_ref(); + if path.is_absolute() { + return path.to_path_buf(); + } + self.runfiles_dir.join(path) + } } +/// Returns the .runfiles directory for the currently executing binary. +fn find_runfiles_dir() -> io::Result { + let exec_path = std::env::args().nth(0).expect("arg 0 was not set"); + let exec_path = PathBuf::from(&exec_path); + + let mut binary_path = exec_path; + loop { + // Check for our neighboring $binary.runfiles directory. + let mut runfiles_name = binary_path.file_name().unwrap().to_owned(); + runfiles_name.push(".runfiles"); + + let runfiles_path = binary_path.with_file_name(&runfiles_name); + if runfiles_path.is_dir() { + return Ok(runfiles_path); + } + + // Check if we're already under a *.runfiles directory. + { + // TODO: 1.28 adds Path::ancestors() which is a little simpler. + let mut next = binary_path.parent(); + while let Some(ancestor) = next { + if ancestor.file_name().map_or(false, |f| { + f.to_string_lossy().ends_with(".runfiles") + }) + { + return Ok(ancestor.to_path_buf()); + } + next = ancestor.parent(); + } + } + + // Follow symlinks and keep looking. + if !fs::symlink_metadata(&binary_path)?.file_type().is_symlink() { + break; + } + binary_path = binary_path.read_link()?; + if binary_path.is_relative() { + binary_path = env::current_dir()?.join(binary_path) + } + } + + panic!("Failed to find .runfiles directory."); +} #[cfg(test)] mod test { @@ -27,15 +109,11 @@ mod test { #[test] fn test_can_read_data_from_runfiles() { - let runfiles = get_runfiles_dir().unwrap(); + let r = Runfiles::create().unwrap(); - let mut f = if cfg!(target_os = "macos") { - File::open(runfiles.join("data/sample.txt")).unwrap() - } else { - File::open(runfiles.join("examples/hello_runfiles/data/sample.txt")).unwrap() - }; - let mut buffer = String::new(); + let mut f = File::open(r.rlocation("examples/hello_runfiles/data/sample.txt")).unwrap(); + let mut buffer = String::new(); f.read_to_string(&mut buffer).unwrap(); assert_eq!("Example Text!", buffer); diff --git a/tools/BUILD b/tools/runfiles/BUILD similarity index 100% rename from tools/BUILD rename to tools/runfiles/BUILD diff --git a/tools/runfiles/runfiles.rs b/tools/runfiles/runfiles.rs new file mode 100644 index 0000000000..33e2225671 --- /dev/null +++ b/tools/runfiles/runfiles.rs @@ -0,0 +1,121 @@ +//! Runfiles lookup library for Bazel-built Rust binaries and tests. +//! +//! USAGE: +//! +//! 1. Depend on this runfiles library from your build rule: +//! ``` +//! rust_binary( +//! name = "my_binary", +//! ... +//! deps = ["@io_bazel_rules_rust//tools/runfiles"], +//! ) +//! ``` +//! +//! 2. Import the runfiles library. +//! ``` +//! extern crate runfiles; +//! +//! use runfiles::Runfiles; +//! ``` +//! +//! 3. Create a Runfiles object and use rlocation to look up runfile paths: +//! ``` +//! let r = Runfiles::create(); +//! let f = File::open(r.rlocation("my_workspace/path/to/my/data.txt")); +//! ``` + +use std::io; +use std::fs; +use std::path::PathBuf; +use std::path::Path; +use std::env; + +pub struct Runfiles { + runfiles_dir: PathBuf, +} + +impl Runfiles { + /// Creates a directory based Runfiles object. + /// + /// Manifest based creation is not currently supported. + pub fn create() -> io::Result { + Ok(Runfiles { runfiles_dir: find_runfiles_dir()? }) + } + + /// Returns the runtime path of a runfile. + /// + /// Runfiles are data-dependencies of Bazel-built binaries and tests. + /// The returned path may not be valid. The caller should check the path's + /// validity and that the path exists. + pub fn rlocation(&self, path: impl AsRef) -> PathBuf { + let path = path.as_ref(); + if path.is_absolute() { + return path.to_path_buf(); + } + self.runfiles_dir.join(path) + } +} + +/// Returns the .runfiles directory for the currently executing binary. +fn find_runfiles_dir() -> io::Result { + let exec_path = std::env::args().nth(0).expect("arg 0 was not set"); + let exec_path = PathBuf::from(&exec_path); + + let mut binary_path = exec_path; + loop { + // Check for our neighboring $binary.runfiles directory. + let mut runfiles_name = binary_path.file_name().unwrap().to_owned(); + runfiles_name.push(".runfiles"); + + let runfiles_path = binary_path.with_file_name(&runfiles_name); + if runfiles_path.is_dir() { + return Ok(runfiles_path); + } + + // Check if we're already under a *.runfiles directory. + { + // TODO: 1.28 adds Path::ancestors() which is a little simpler. + let mut next = binary_path.parent(); + while let Some(ancestor) = next { + if ancestor.file_name().map_or(false, |f| { + f.to_string_lossy().ends_with(".runfiles") + }) + { + return Ok(ancestor.to_path_buf()); + } + next = ancestor.parent(); + } + } + + // Follow symlinks and keep looking. + if !fs::symlink_metadata(&binary_path)?.file_type().is_symlink() { + break; + } + binary_path = binary_path.read_link()?; + if binary_path.is_relative() { + binary_path = env::current_dir()?.join(binary_path) + } + } + + panic!("Failed to find .runfiles directory."); +} + +#[cfg(test)] +mod test { + use super::*; + + use std::io::prelude::*; + use std::fs::File; + + #[test] + fn test_can_read_data_from_runfiles() { + let r = Runfiles::create().unwrap(); + + let mut f = File::open(r.rlocation("examples/hello_runfiles/data/sample.txt")).unwrap(); + + let mut buffer = String::new(); + f.read_to_string(&mut buffer).unwrap(); + + assert_eq!("Example Text!", buffer); + } +} From 22794b0c68c5807f5aee38a83fbd310efc5407b4 Mon Sep 17 00:00:00 2001 From: Marco Farrugia Date: Fri, 19 Oct 2018 00:19:26 -0400 Subject: [PATCH 3/9] Add doc_test for runfiles. --- tools/runfiles/BUILD | 7 +++++++ tools/runfiles/runfiles.rs | 15 ++++++++++----- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/tools/runfiles/BUILD b/tools/runfiles/BUILD index facffa071f..3f486f3f66 100644 --- a/tools/runfiles/BUILD +++ b/tools/runfiles/BUILD @@ -1,6 +1,7 @@ load( "@io_bazel_rules_rust//rust:rust.bzl", "rust_library", + "rust_doc_test", ) rust_library( @@ -8,3 +9,9 @@ rust_library( srcs = ["runfiles.rs"], visibility = ["//visibility:public"], ) + +rust_doc_test( + name = "runfiles_doc_test", + dep = ":runfiles", +) + diff --git a/tools/runfiles/runfiles.rs b/tools/runfiles/runfiles.rs index 33e2225671..a4ee50db6a 100644 --- a/tools/runfiles/runfiles.rs +++ b/tools/runfiles/runfiles.rs @@ -3,10 +3,11 @@ //! USAGE: //! //! 1. Depend on this runfiles library from your build rule: -//! ``` +//! ```python //! rust_binary( //! name = "my_binary", //! ... +//! data = ["@my_workspace//path/to/my/data.txt"], //! deps = ["@io_bazel_rules_rust//tools/runfiles"], //! ) //! ``` @@ -19,9 +20,13 @@ //! ``` //! //! 3. Create a Runfiles object and use rlocation to look up runfile paths: -//! ``` -//! let r = Runfiles::create(); -//! let f = File::open(r.rlocation("my_workspace/path/to/my/data.txt")); +//! ```ignore -- This doesn't work under rust_doc_test because argv[0] is not what we expect. +//! use runfiles::Runfiles; +//! +//! let r = Runfiles::create().unwrap(); +//! let path = r.rlocation("my_workspace/path/to/my/data.txt"); +//! +//! // let f = File::open(path); //! ``` use std::io; @@ -112,7 +117,7 @@ mod test { let r = Runfiles::create().unwrap(); let mut f = File::open(r.rlocation("examples/hello_runfiles/data/sample.txt")).unwrap(); - + let mut buffer = String::new(); f.read_to_string(&mut buffer).unwrap(); From 614e912933d48fbb1366fa07729b1a73e815ed60 Mon Sep 17 00:00:00 2001 From: Marco Farrugia Date: Fri, 19 Oct 2018 00:19:26 -0400 Subject: [PATCH 4/9] Merge hello_runfiles into tools/runfiles --- .gitignore | 6 + examples/hello_runfiles/BUILD | 21 --- examples/hello_runfiles/src/main.rs | 17 --- tools/runfiles.rs | 121 ------------------ tools/runfiles/BUILD | 10 +- .../runfiles}/data/sample.txt | 0 tools/runfiles/runfiles.rs | 10 +- 7 files changed, 21 insertions(+), 164 deletions(-) delete mode 100644 examples/hello_runfiles/BUILD delete mode 100644 examples/hello_runfiles/src/main.rs delete mode 100644 tools/runfiles.rs rename {examples/hello_runfiles => tools/runfiles}/data/sample.txt (100%) diff --git a/.gitignore b/.gitignore index 5a105330db..0d43ef1f0f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,8 @@ +# vim *.swp + +# bazel /bazel-* + +# rustfmt +*.rs.bk diff --git a/examples/hello_runfiles/BUILD b/examples/hello_runfiles/BUILD deleted file mode 100644 index 71ce450878..0000000000 --- a/examples/hello_runfiles/BUILD +++ /dev/null @@ -1,21 +0,0 @@ -package(default_visibility = ["//visibility:public"]) - -load( - "@io_bazel_rules_rust//rust:rust.bzl", - "rust_library", - "rust_binary", - "rust_test", -) - -rust_binary( - name = "hello_runfiles", - srcs = ["src/main.rs"], - data = ["data/sample.txt"], - deps = ["@io_bazel_rules_rust//tools/runfiles"], -) - -rust_test( - name = "hello_runfiles_test", - data = ["data/sample.txt"], - deps = ["@io_bazel_rules_rust//tools/runfiles"], -) diff --git a/examples/hello_runfiles/src/main.rs b/examples/hello_runfiles/src/main.rs deleted file mode 100644 index 698d006947..0000000000 --- a/examples/hello_runfiles/src/main.rs +++ /dev/null @@ -1,17 +0,0 @@ -extern crate runfiles; - -use std::io::prelude::*; -use std::fs::File; - -use runfiles::Runfiles; - -fn main() { - let r = Runfiles::create().unwrap(); - - let mut f = File::open(r.rlocation("examples/hello_runfiles/data/sample.txt")).unwrap(); - - let mut buffer = String::new(); - f.read_to_string(&mut buffer).unwrap(); - - println!("{}", buffer); -} diff --git a/tools/runfiles.rs b/tools/runfiles.rs deleted file mode 100644 index 944557aebf..0000000000 --- a/tools/runfiles.rs +++ /dev/null @@ -1,121 +0,0 @@ -//! Runfiles lookup library for Bazel-built Rust binaries and tests. -//! -//! USAGE: -//! -//! 1. Depend on this runfiles library from your build rule: -//! ``` -//! rust_binary( -//! name = "my_binary", -//! ... -//! deps = ["@io_bazel_rules_rust//tools/runfiles"], -//! ) -//! ``` -//! -//! 2. Import the runfiles library. -//! ``` -//! extern crate runfiles; -//! -//! use runfiles::Runfiles; -//! ``` -//! -//! 3. Create a Runfiles object and use rlocation to look up runfile paths: -//! ``` -//! let r = Runfiles::create(); -//! let f = File::open(r.rlocation("my_workspace/path/to/my/data.txt")); -//! ``` - -use std::io; -use std::fs; -use std::path::PathBuf; -use std::path::Path; -use std::env; - -pub struct Runfiles { - runfiles_dir: PathBuf, -} - -impl Runfiles { - /// Creates a directory based Runfiles object. - /// - /// Manifest based creation is not currently supported. - pub fn create() -> io::Result { - Ok(Runfiles { runfiles_dir: find_runfiles_dir()? }) - } - - /// Returns the runtime path of a runfile. - /// - /// Runfiles are data-dependencies of Bazel-built binaries and tests. - /// The returned path may not be valid. The caller should check the path's - /// validity and that the path exists. - pub fn rlocation(&self, path: impl AsRef) -> PathBuf { - let path = path.as_ref(); - if path.is_absolute() { - return path.to_path_buf(); - } - self.runfiles_dir.join(path) - } -} - -/// Returns the .runfiles directory for the currently executing binary. -fn find_runfiles_dir() -> io::Result { - let exec_path = std::env::args().nth(0).expect("arg 0 was not set"); - let exec_path = PathBuf::from(&exec_path); - - let mut binary_path = exec_path; - loop { - // Check for our neighboring $binary.runfiles directory. - let mut runfiles_name = binary_path.file_name().unwrap().to_owned(); - runfiles_name.push(".runfiles"); - - let runfiles_path = binary_path.with_file_name(&runfiles_name); - if runfiles_path.is_dir() { - return Ok(runfiles_path); - } - - // Check if we're already under a *.runfiles directory. - { - // TODO: 1.28 adds Path::ancestors() which is a little simpler. - let mut next = binary_path.parent(); - while let Some(ancestor) = next { - if ancestor.file_name().map_or(false, |f| { - f.to_string_lossy().ends_with(".runfiles") - }) - { - return Ok(ancestor.to_path_buf()); - } - next = ancestor.parent(); - } - } - - // Follow symlinks and keep looking. - if !fs::symlink_metadata(&binary_path)?.file_type().is_symlink() { - break; - } - binary_path = binary_path.read_link()?; - if binary_path.is_relative() { - binary_path = env::current_dir()?.join(binary_path) - } - } - - panic!("Failed to find .runfiles directory."); -} - -#[cfg(test)] -mod test { - use super::*; - - use std::io::prelude::*; - use std::fs::File; - - #[test] - fn test_can_read_data_from_runfiles() { - let r = Runfiles::create().unwrap(); - - let mut f = File::open(r.rlocation("examples/hello_runfiles/data/sample.txt")).unwrap(); - - let mut buffer = String::new(); - f.read_to_string(&mut buffer).unwrap(); - - assert_eq!("Example Text!", buffer); - } -} diff --git a/tools/runfiles/BUILD b/tools/runfiles/BUILD index 3f486f3f66..e9be3e5f26 100644 --- a/tools/runfiles/BUILD +++ b/tools/runfiles/BUILD @@ -1,7 +1,8 @@ load( "@io_bazel_rules_rust//rust:rust.bzl", - "rust_library", "rust_doc_test", + "rust_library", + "rust_test", ) rust_library( @@ -10,8 +11,13 @@ rust_library( visibility = ["//visibility:public"], ) +rust_test( + name = "runfiles_test", + data = ["data/sample.txt"], + deps = [":runfiles"], +) + rust_doc_test( name = "runfiles_doc_test", dep = ":runfiles", ) - diff --git a/examples/hello_runfiles/data/sample.txt b/tools/runfiles/data/sample.txt similarity index 100% rename from examples/hello_runfiles/data/sample.txt rename to tools/runfiles/data/sample.txt diff --git a/tools/runfiles/runfiles.rs b/tools/runfiles/runfiles.rs index a4ee50db6a..54c32ef07f 100644 --- a/tools/runfiles/runfiles.rs +++ b/tools/runfiles/runfiles.rs @@ -7,7 +7,7 @@ //! rust_binary( //! name = "my_binary", //! ... -//! data = ["@my_workspace//path/to/my/data.txt"], +//! data = ["//path/to/my/data.txt"], //! deps = ["@io_bazel_rules_rust//tools/runfiles"], //! ) //! ``` @@ -21,12 +21,14 @@ //! //! 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; //! //! let r = Runfiles::create().unwrap(); //! let path = r.rlocation("my_workspace/path/to/my/data.txt"); //! -//! // let f = File::open(path); +//! let f = File::open(path).unwrap(); +//! // ... //! ``` use std::io; @@ -116,7 +118,9 @@ mod test { fn test_can_read_data_from_runfiles() { let r = Runfiles::create().unwrap(); - let mut f = File::open(r.rlocation("examples/hello_runfiles/data/sample.txt")).unwrap(); + let mut f = File::open(r.rlocation( + "io_bazel_rules_rust/tools/runfiles/data/sample.txt", + )).unwrap(); let mut buffer = String::new(); f.read_to_string(&mut buffer).unwrap(); From 68924fe83a267988a6830fbe5ab33f868507284e Mon Sep 17 00:00:00 2001 From: Marco Farrugia Date: Fri, 19 Oct 2018 00:19:26 -0400 Subject: [PATCH 5/9] Remove unnecessary public visibilities. --- examples/ffi/c_calling_rust/BUILD | 2 -- examples/fibonacci/BUILD | 2 -- examples/hello_out_dir/BUILD | 2 -- 3 files changed, 6 deletions(-) diff --git a/examples/ffi/c_calling_rust/BUILD b/examples/ffi/c_calling_rust/BUILD index 940af5f62b..47960e4ab2 100644 --- a/examples/ffi/c_calling_rust/BUILD +++ b/examples/ffi/c_calling_rust/BUILD @@ -1,5 +1,3 @@ -package(default_visibility = ["//visibility:private"]) - load("@//rust:rust.bzl", "rust_library", "rust_test", "rust_binary") rust_library( diff --git a/examples/fibonacci/BUILD b/examples/fibonacci/BUILD index 8a3dc4ed3e..21bb74b2e5 100644 --- a/examples/fibonacci/BUILD +++ b/examples/fibonacci/BUILD @@ -1,5 +1,3 @@ -package(default_visibility = ["//visibility:public"]) - load( "@io_bazel_rules_rust//rust:rust.bzl", "rust_library", diff --git a/examples/hello_out_dir/BUILD b/examples/hello_out_dir/BUILD index 6e3904e392..e92e287a32 100644 --- a/examples/hello_out_dir/BUILD +++ b/examples/hello_out_dir/BUILD @@ -1,5 +1,3 @@ -package(default_visibility = ["//visibility:public"]) - load( "@io_bazel_rules_rust//rust:rust.bzl", "rust_binary", From bffc8c5536c9faea2a032ec9d379bd9c3bf8890d Mon Sep 17 00:00:00 2001 From: Marco Farrugia Date: Fri, 19 Oct 2018 00:19:26 -0400 Subject: [PATCH 6/9] Update CI config. --- .bazelci/presubmit.yml | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/.bazelci/presubmit.yml b/.bazelci/presubmit.yml index d57caf1743..91fa6aec85 100644 --- a/.bazelci/presubmit.yml +++ b/.bazelci/presubmit.yml @@ -30,9 +30,7 @@ platforms: - "//test/..." - "@examples//..." - "-//test/conflicting_deps:conflicting_deps_test" - # Runfiles currently not working properly with remote execution - # https://github.com/bazelbuild/rules_rust/issues/112 - - "-@examples//hello_runfiles:hello_runfiles_test" # rust_doc_test is likely not fully sandboxed - "-@examples//fibonacci:fibonacci_doc_test" - "-@examples//hello_lib:hello_lib_doc_test" + - "-//tools/runfiles:runfiles_doc_test" From e2b99c19e70f2d83085918b8e4d1f4fa5d8cb060 Mon Sep 17 00:00:00 2001 From: Marco Farrugia Date: Fri, 19 Oct 2018 20:01:48 -0400 Subject: [PATCH 7/9] Revert removal of hello_runfiles example. --- examples/hello_runfiles/BUILD | 21 +++++++++++++++++++++ examples/hello_runfiles/data/sample.txt | 1 + examples/hello_runfiles/src/main.rs | 17 +++++++++++++++++ 3 files changed, 39 insertions(+) create mode 100644 examples/hello_runfiles/BUILD create mode 100644 examples/hello_runfiles/data/sample.txt create mode 100644 examples/hello_runfiles/src/main.rs diff --git a/examples/hello_runfiles/BUILD b/examples/hello_runfiles/BUILD new file mode 100644 index 0000000000..71ce450878 --- /dev/null +++ b/examples/hello_runfiles/BUILD @@ -0,0 +1,21 @@ +package(default_visibility = ["//visibility:public"]) + +load( + "@io_bazel_rules_rust//rust:rust.bzl", + "rust_library", + "rust_binary", + "rust_test", +) + +rust_binary( + name = "hello_runfiles", + srcs = ["src/main.rs"], + data = ["data/sample.txt"], + deps = ["@io_bazel_rules_rust//tools/runfiles"], +) + +rust_test( + name = "hello_runfiles_test", + data = ["data/sample.txt"], + deps = ["@io_bazel_rules_rust//tools/runfiles"], +) diff --git a/examples/hello_runfiles/data/sample.txt b/examples/hello_runfiles/data/sample.txt new file mode 100644 index 0000000000..398ec97d8a --- /dev/null +++ b/examples/hello_runfiles/data/sample.txt @@ -0,0 +1 @@ +Example Text! \ No newline at end of file diff --git a/examples/hello_runfiles/src/main.rs b/examples/hello_runfiles/src/main.rs new file mode 100644 index 0000000000..698d006947 --- /dev/null +++ b/examples/hello_runfiles/src/main.rs @@ -0,0 +1,17 @@ +extern crate runfiles; + +use std::io::prelude::*; +use std::fs::File; + +use runfiles::Runfiles; + +fn main() { + let r = Runfiles::create().unwrap(); + + let mut f = File::open(r.rlocation("examples/hello_runfiles/data/sample.txt")).unwrap(); + + let mut buffer = String::new(); + f.read_to_string(&mut buffer).unwrap(); + + println!("{}", buffer); +} From 6873ab8550e402f7993f28fa290f562db854dd01 Mon Sep 17 00:00:00 2001 From: Marco Farrugia Date: Fri, 19 Oct 2018 20:18:36 -0400 Subject: [PATCH 8/9] Spice up runfiles example. --- examples/hello_runfiles/BUILD | 12 +++--------- examples/hello_runfiles/data/sample.txt | 1 - .../{src/main.rs => hello_runfiles.rs} | 5 +++-- 3 files changed, 6 insertions(+), 12 deletions(-) delete mode 100644 examples/hello_runfiles/data/sample.txt rename examples/hello_runfiles/{src/main.rs => hello_runfiles.rs} (69%) diff --git a/examples/hello_runfiles/BUILD b/examples/hello_runfiles/BUILD index 71ce450878..55032515c5 100644 --- a/examples/hello_runfiles/BUILD +++ b/examples/hello_runfiles/BUILD @@ -2,20 +2,14 @@ package(default_visibility = ["//visibility:public"]) load( "@io_bazel_rules_rust//rust:rust.bzl", - "rust_library", "rust_binary", + "rust_library", "rust_test", ) rust_binary( name = "hello_runfiles", - srcs = ["src/main.rs"], - data = ["data/sample.txt"], - deps = ["@io_bazel_rules_rust//tools/runfiles"], -) - -rust_test( - name = "hello_runfiles_test", - data = ["data/sample.txt"], + srcs = ["hello_runfiles.rs"], + data = ["hello_runfiles.rs"], # Yes, we're being cute. deps = ["@io_bazel_rules_rust//tools/runfiles"], ) diff --git a/examples/hello_runfiles/data/sample.txt b/examples/hello_runfiles/data/sample.txt deleted file mode 100644 index 398ec97d8a..0000000000 --- a/examples/hello_runfiles/data/sample.txt +++ /dev/null @@ -1 +0,0 @@ -Example Text! \ No newline at end of file diff --git a/examples/hello_runfiles/src/main.rs b/examples/hello_runfiles/hello_runfiles.rs similarity index 69% rename from examples/hello_runfiles/src/main.rs rename to examples/hello_runfiles/hello_runfiles.rs index 698d006947..caeb81a1ba 100644 --- a/examples/hello_runfiles/src/main.rs +++ b/examples/hello_runfiles/hello_runfiles.rs @@ -8,10 +8,11 @@ use runfiles::Runfiles; fn main() { let r = Runfiles::create().unwrap(); - let mut f = File::open(r.rlocation("examples/hello_runfiles/data/sample.txt")).unwrap(); + let mut f = File::open(r.rlocation("examples/hello_runfiles/hello_runfiles.rs")).unwrap(); let mut buffer = String::new(); f.read_to_string(&mut buffer).unwrap(); - println!("{}", buffer); + assert_eq!(buffer.len(), 427); + println!("This program's source is:\n```\n{}\n```", buffer); } From c3647eb127982f1f8e3b5ee85a3c6d72940a1402 Mon Sep 17 00:00:00 2001 From: Marco Farrugia Date: Fri, 19 Oct 2018 20:22:27 -0400 Subject: [PATCH 9/9] Address review comments. --- tools/runfiles/runfiles.rs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/tools/runfiles/runfiles.rs b/tools/runfiles/runfiles.rs index 54c32ef07f..f2b9486079 100644 --- a/tools/runfiles/runfiles.rs +++ b/tools/runfiles/runfiles.rs @@ -66,9 +66,8 @@ impl Runfiles { /// Returns the .runfiles directory for the currently executing binary. fn find_runfiles_dir() -> io::Result { let exec_path = std::env::args().nth(0).expect("arg 0 was not set"); - let exec_path = PathBuf::from(&exec_path); - let mut binary_path = exec_path; + let mut binary_path = PathBuf::from(&exec_path); loop { // Check for our neighboring $binary.runfiles directory. let mut runfiles_name = binary_path.file_name().unwrap().to_owned(); @@ -94,10 +93,10 @@ fn find_runfiles_dir() -> io::Result { } } - // Follow symlinks and keep looking. if !fs::symlink_metadata(&binary_path)?.file_type().is_symlink() { break; } + // Follow symlinks and keep looking. binary_path = binary_path.read_link()?; if binary_path.is_relative() { binary_path = env::current_dir()?.join(binary_path)