From e68b6d33b074efa8c745b4ee7c92518cf6dcc1ca Mon Sep 17 00:00:00 2001 From: Vincent Ambo Date: Fri, 12 Aug 2022 18:04:05 +0300 Subject: feat(tvix/eval): support absolute and home-anchored paths These two paths are the easiest to handle, as they are simply built up from the components supplied in the text node and then normalised. Note that the normalisation of fs::canonicalize includes symlink resolution, which Nix does not actually do. We will need to fix that at some point. Change-Id: I54158f0684f197dd2a2583f7d0982d54c7619993 Reviewed-on: https://cl.tvl.fyi/c/depot/+/6183 Tested-by: BuildkiteCI Reviewed-by: sterni --- tvix/eval/src/compiler.rs | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/tvix/eval/src/compiler.rs b/tvix/eval/src/compiler.rs index 3ec7f673eb..c56951e464 100644 --- a/tvix/eval/src/compiler.rs +++ b/tvix/eval/src/compiler.rs @@ -13,6 +13,8 @@ //! the code in this module, `debug_assert!` has been used to catch //! mistakes early during development. +use std::path::Path; + use crate::chunk::Chunk; use crate::errors::EvalResult; use crate::opcode::{CodeIdx, OpCode}; @@ -154,10 +156,39 @@ impl Compiler { Ok(()) } - rnix::NixValue::Path(_, _) => todo!(), + rnix::NixValue::Path(anchor, path) => self.compile_path(anchor, path), } } + fn compile_path(&mut self, anchor: rnix::value::Anchor, path: String) -> EvalResult<()> { + // TODO(tazjin): C++ Nix does not resolve symlinks, but `fs::canonicalize` does. + + let path = match anchor { + rnix::value::Anchor::Absolute => Path::new(&path) + .canonicalize() + .expect("TODO: error variant"), + + rnix::value::Anchor::Home => { + let mut buf = dirs::home_dir().expect("TODO: error variant"); + buf.push(&path); + buf.canonicalize().expect("TODO: error variant") + } + + rnix::value::Anchor::Relative => todo!("resolve relative to file location"), + + // This confusingly named variant is actually + // angle-bracket lookups, which in C++ Nix desugar + // to calls to `__findFile` (implicitly in the + // current scope). + rnix::value::Anchor::Store => todo!("resolve <...> lookups at runtime"), + }; + + let idx = self.chunk.push_constant(Value::Path(path)); + self.chunk.push_op(OpCode::OpConstant(idx)); + + Ok(()) + } + fn compile_string(&mut self, string: rnix::types::Str) -> EvalResult<()> { let mut count = 0; -- cgit 1.4.1