From 78d3d9150b1eb76e0820a254c97976e10a8614d6 Mon Sep 17 00:00:00 2001 From: Adam Joseph Date: Wed, 12 Oct 2022 23:10:58 -0700 Subject: fix(tvix/eval): src/compiler: ensure root_dir is absolute Cppnix immediately absolutizes pathnames at parse time; if you write `./foo`, it is immediately converted to `$(pwd)/foo` and manipulated as an absolute path at all times. To avoid having to introduce filesystem access operations in the implementation of otherwise-pure builtins, let's guarantee that the `root_dir` of the VM is always an absolute path. Signed-off-by: Adam Joseph Change-Id: I7cbbae2cba4b2716ff3f5ff7c9ce0ad529358c8a Reviewed-on: https://cl.tvl.fyi/c/depot/+/6995 Reviewed-by: grfn Tested-by: BuildkiteCI --- tvix/eval/src/compiler/mod.rs | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) (limited to 'tvix/eval/src') diff --git a/tvix/eval/src/compiler/mod.rs b/tvix/eval/src/compiler/mod.rs index e4c2c159da..bf59027c8b 100644 --- a/tvix/eval/src/compiler/mod.rs +++ b/tvix/eval/src/compiler/mod.rs @@ -112,14 +112,21 @@ impl<'observer> Compiler<'observer> { observer: &'observer mut dyn CompilerObserver, ) -> EvalResult { let mut root_dir = match location { - Some(dir) => Ok(dir), - None => std::env::current_dir().map_err(|e| Error { - kind: ErrorKind::PathResolution(format!( - "could not determine current directory: {}", - e - )), - span: file.span, - }), + Some(dir) if dir.is_absolute() => Ok(dir), + _ => { + let current_dir = std::env::current_dir().map_err(|e| Error { + kind: ErrorKind::PathResolution(format!( + "could not determine current directory: {}", + e + )), + span: file.span, + })?; + if let Some(dir) = location { + Ok(current_dir.join(dir)) + } else { + Ok(current_dir) + } + } }?; // If the path passed from the caller points to a file, the @@ -131,6 +138,7 @@ impl<'observer> Compiler<'observer> { let globals = globals.borrow(); + debug_assert!(root_dir.is_absolute()); Ok(Self { root_dir, file, -- cgit 1.4.1