From 3511e328ec67c0481c1412675c1b47025486d453 Mon Sep 17 00:00:00 2001 From: Ilan Joselevich Date: Sun, 4 Aug 2024 01:00:25 +0300 Subject: feat(tvix/eval): Implement builtins.readFileType builtins.readFileType was added to Nix back in version 2.14. The tests were also moved out of notyetpassing in addition to the readDir fixtures they depend on. I caught a bug where we previously used std::fs::metadata (via the .metadata() method on File) which follows symlinks so it would always return false for is_symlink(). Instead we now use std::fs::symlink_metadata directly which does not follow symlinks, so tests now pass. This wasn't an issue for builtins.readDir as it uses walkdir and walkdir doesn't follow symlinks either. Change-Id: I58eb97bdb5ec95df4f6882f495f8c572fe7c6793 Reviewed-on: https://cl.tvl.fyi/c/depot/+/12130 Reviewed-by: flokli Autosubmit: Ilan Joselevich Tested-by: BuildkiteCI --- tvix/eval/src/builtins/impure.rs | 12 ++++++++ tvix/eval/src/io.rs | 4 +-- .../src/tests/nix_tests/eval-okay-readFileType.exp | 1 + .../src/tests/nix_tests/eval-okay-readFileType.nix | 6 ++++ .../notyetpassing/eval-okay-readFileType.exp | 1 - .../notyetpassing/eval-okay-readFileType.nix | 6 ---- .../src/tests/nix_tests/notyetpassing/readDir/bar | 0 .../readDir/foo/git-hates-directories | 0 .../src/tests/nix_tests/notyetpassing/readDir/ldir | 1 - .../tests/nix_tests/notyetpassing/readDir/linked | 1 - tvix/eval/src/tests/nix_tests/readDir/bar | 0 .../nix_tests/readDir/foo/git-hates-directories | 0 tvix/eval/src/tests/nix_tests/readDir/ldir | 1 + tvix/eval/src/tests/nix_tests/readDir/linked | 1 + tvix/eval/src/vm/generators.rs | 32 ++++++++++++++++++++++ 15 files changed, 55 insertions(+), 11 deletions(-) create mode 100644 tvix/eval/src/tests/nix_tests/eval-okay-readFileType.exp create mode 100644 tvix/eval/src/tests/nix_tests/eval-okay-readFileType.nix delete mode 100644 tvix/eval/src/tests/nix_tests/notyetpassing/eval-okay-readFileType.exp delete mode 100644 tvix/eval/src/tests/nix_tests/notyetpassing/eval-okay-readFileType.nix delete mode 100644 tvix/eval/src/tests/nix_tests/notyetpassing/readDir/bar delete mode 100644 tvix/eval/src/tests/nix_tests/notyetpassing/readDir/foo/git-hates-directories delete mode 120000 tvix/eval/src/tests/nix_tests/notyetpassing/readDir/ldir delete mode 120000 tvix/eval/src/tests/nix_tests/notyetpassing/readDir/linked create mode 100644 tvix/eval/src/tests/nix_tests/readDir/bar create mode 100644 tvix/eval/src/tests/nix_tests/readDir/foo/git-hates-directories create mode 120000 tvix/eval/src/tests/nix_tests/readDir/ldir create mode 120000 tvix/eval/src/tests/nix_tests/readDir/linked (limited to 'tvix/eval') diff --git a/tvix/eval/src/builtins/impure.rs b/tvix/eval/src/builtins/impure.rs index 24fe0e7e9ed4..dccb7fbb7d8a 100644 --- a/tvix/eval/src/builtins/impure.rs +++ b/tvix/eval/src/builtins/impure.rs @@ -81,6 +81,18 @@ mod impure_builtins { } } } + + #[builtin("readFileType")] + async fn builtin_read_file_type(co: GenCo, path: Value) -> Result { + match coerce_value_to_path(&co, path).await? { + Err(cek) => Ok(Value::from(cek)), + Ok(path) => Ok(Value::from( + generators::request_read_file_type(&co, path) + .await + .to_string(), + )), + } + } } /// Return all impure builtins, that is all builtins which may perform I/O diff --git a/tvix/eval/src/io.rs b/tvix/eval/src/io.rs index e4bad7b11ce0..7e8b85c87abb 100644 --- a/tvix/eval/src/io.rs +++ b/tvix/eval/src/io.rs @@ -26,7 +26,7 @@ use std::os::unix::ffi::OsStringExt; #[cfg(feature = "impure")] use std::fs::File; -/// Types of files as represented by `builtins.readDir` in Nix. +/// Types of files as represented by `builtins.readFileType` and `builtins.readDir` in Nix. #[derive(Debug)] pub enum FileType { Directory, @@ -120,7 +120,7 @@ impl EvalIO for StdIO { } fn file_type(&self, path: &Path) -> io::Result { - let file_type = File::open(path)?.metadata()?.file_type(); + let file_type = std::fs::symlink_metadata(path)?; Ok(if file_type.is_dir() { FileType::Directory diff --git a/tvix/eval/src/tests/nix_tests/eval-okay-readFileType.exp b/tvix/eval/src/tests/nix_tests/eval-okay-readFileType.exp new file mode 100644 index 000000000000..6413f6d4f9ec --- /dev/null +++ b/tvix/eval/src/tests/nix_tests/eval-okay-readFileType.exp @@ -0,0 +1 @@ +{ bar = "regular"; foo = "directory"; ldir = "symlink"; linked = "symlink"; } diff --git a/tvix/eval/src/tests/nix_tests/eval-okay-readFileType.nix b/tvix/eval/src/tests/nix_tests/eval-okay-readFileType.nix new file mode 100644 index 000000000000..174fb6c3a028 --- /dev/null +++ b/tvix/eval/src/tests/nix_tests/eval-okay-readFileType.nix @@ -0,0 +1,6 @@ +{ + bar = builtins.readFileType ./readDir/bar; + foo = builtins.readFileType ./readDir/foo; + linked = builtins.readFileType ./readDir/linked; + ldir = builtins.readFileType ./readDir/ldir; +} diff --git a/tvix/eval/src/tests/nix_tests/notyetpassing/eval-okay-readFileType.exp b/tvix/eval/src/tests/nix_tests/notyetpassing/eval-okay-readFileType.exp deleted file mode 100644 index 6413f6d4f9ec..000000000000 --- a/tvix/eval/src/tests/nix_tests/notyetpassing/eval-okay-readFileType.exp +++ /dev/null @@ -1 +0,0 @@ -{ bar = "regular"; foo = "directory"; ldir = "symlink"; linked = "symlink"; } diff --git a/tvix/eval/src/tests/nix_tests/notyetpassing/eval-okay-readFileType.nix b/tvix/eval/src/tests/nix_tests/notyetpassing/eval-okay-readFileType.nix deleted file mode 100644 index 174fb6c3a028..000000000000 --- a/tvix/eval/src/tests/nix_tests/notyetpassing/eval-okay-readFileType.nix +++ /dev/null @@ -1,6 +0,0 @@ -{ - bar = builtins.readFileType ./readDir/bar; - foo = builtins.readFileType ./readDir/foo; - linked = builtins.readFileType ./readDir/linked; - ldir = builtins.readFileType ./readDir/ldir; -} diff --git a/tvix/eval/src/tests/nix_tests/notyetpassing/readDir/bar b/tvix/eval/src/tests/nix_tests/notyetpassing/readDir/bar deleted file mode 100644 index e69de29bb2d1..000000000000 diff --git a/tvix/eval/src/tests/nix_tests/notyetpassing/readDir/foo/git-hates-directories b/tvix/eval/src/tests/nix_tests/notyetpassing/readDir/foo/git-hates-directories deleted file mode 100644 index e69de29bb2d1..000000000000 diff --git a/tvix/eval/src/tests/nix_tests/notyetpassing/readDir/ldir b/tvix/eval/src/tests/nix_tests/notyetpassing/readDir/ldir deleted file mode 120000 index 19102815663d..000000000000 --- a/tvix/eval/src/tests/nix_tests/notyetpassing/readDir/ldir +++ /dev/null @@ -1 +0,0 @@ -foo \ No newline at end of file diff --git a/tvix/eval/src/tests/nix_tests/notyetpassing/readDir/linked b/tvix/eval/src/tests/nix_tests/notyetpassing/readDir/linked deleted file mode 120000 index c503f86a0cf7..000000000000 --- a/tvix/eval/src/tests/nix_tests/notyetpassing/readDir/linked +++ /dev/null @@ -1 +0,0 @@ -foo/git-hates-directories \ No newline at end of file diff --git a/tvix/eval/src/tests/nix_tests/readDir/bar b/tvix/eval/src/tests/nix_tests/readDir/bar new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/tvix/eval/src/tests/nix_tests/readDir/foo/git-hates-directories b/tvix/eval/src/tests/nix_tests/readDir/foo/git-hates-directories new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/tvix/eval/src/tests/nix_tests/readDir/ldir b/tvix/eval/src/tests/nix_tests/readDir/ldir new file mode 120000 index 000000000000..19102815663d --- /dev/null +++ b/tvix/eval/src/tests/nix_tests/readDir/ldir @@ -0,0 +1 @@ +foo \ No newline at end of file diff --git a/tvix/eval/src/tests/nix_tests/readDir/linked b/tvix/eval/src/tests/nix_tests/readDir/linked new file mode 120000 index 000000000000..c503f86a0cf7 --- /dev/null +++ b/tvix/eval/src/tests/nix_tests/readDir/linked @@ -0,0 +1 @@ +foo/git-hates-directories \ No newline at end of file diff --git a/tvix/eval/src/vm/generators.rs b/tvix/eval/src/vm/generators.rs index 36b837dbee6e..ae9a8dd6ab51 100644 --- a/tvix/eval/src/vm/generators.rs +++ b/tvix/eval/src/vm/generators.rs @@ -121,6 +121,9 @@ pub enum VMRequest { /// Request serialisation of a value to JSON, according to the /// slightly odd Nix evaluation rules. ToJson(Value), + + /// Request the VM for the file type of the given path. + ReadFileType(PathBuf), } /// Human-readable representation of a generator message, used by observers. @@ -178,6 +181,7 @@ impl Display for VMRequest { VMRequest::Span => write!(f, "span"), VMRequest::TryForce(v) => write!(f, "try_force({})", v.type_of()), VMRequest::ToJson(v) => write!(f, "to_json({})", v.type_of()), + VMRequest::ReadFileType(p) => write!(f, "read_file_type({})", p.to_string_lossy()), } } } @@ -202,6 +206,8 @@ pub enum VMResponse { /// [std::io::Reader] produced by the VM in response to some IO operation. Reader(Box), + + FileType(FileType), } impl Display for VMResponse { @@ -213,6 +219,7 @@ impl Display for VMResponse { VMResponse::Directory(d) => write!(f, "dir(len = {})", d.len()), VMResponse::Span(_) => write!(f, "span"), VMResponse::Reader(_) => write!(f, "reader"), + VMResponse::FileType(t) => write!(f, "file_type({})", t), } } } @@ -497,6 +504,20 @@ where }); return Ok(false); } + + VMRequest::ReadFileType(path) => { + let file_type = self + .io_handle + .as_ref() + .file_type(&path) + .map_err(|e| ErrorKind::IO { + path: Some(path), + error: e.into(), + }) + .with_span(span, self)?; + + message = VMResponse::FileType(file_type); + } } } @@ -791,6 +812,17 @@ pub(crate) async fn request_to_json( } } +#[cfg_attr(not(feature = "impure"), allow(unused))] +pub(crate) async fn request_read_file_type(co: &GenCo, path: PathBuf) -> FileType { + match co.yield_(VMRequest::ReadFileType(path)).await { + VMResponse::FileType(file_type) => file_type, + msg => panic!( + "Tvix bug: VM responded with incorrect generator message: {}", + msg + ), + } +} + /// Call the given value as if it was an attribute set containing a functor. The /// arguments must already be prepared on the stack when a generator frame from /// this function is invoked. -- cgit 1.4.1