diff --git a/rinja_derive/src/config.rs b/rinja_derive/src/config.rs index ccd90dc4..54476d94 100644 --- a/rinja_derive/src/config.rs +++ b/rinja_derive/src/config.rs @@ -182,27 +182,40 @@ impl Config { start_at: Option<&Path>, file_info: Option>, ) -> Result, CompileError> { - if let Some(root) = start_at { - let relative = root.with_file_name(path); - if relative.exists() { - return Ok(relative.into()); + fn _find_template(config: &Config, path: &str, start_at: Option<&Path>) -> Option { + if let Some(root) = start_at { + let relative = root.with_file_name(path); + if relative.exists() { + return Some(relative); + } } + + for dir in &config.dirs { + let rooted = dir.join(path); + if rooted.exists() { + return Some(rooted); + } + } + + None } - for dir in &self.dirs { - let rooted = dir.join(path); - if rooted.exists() { - return Ok(rooted.into()); - } + let Some(path) = _find_template(self, path, start_at) else { + return Err(CompileError::new( + format!( + "template {:?} not found in directories {:?}", + path, self.dirs + ), + file_info, + )); + }; + match path.canonicalize() { + Ok(p) => Ok(p.into()), + Err(e) => Err(CompileError::no_file_info( + format_args!("could not canonicalize path {path:?}: {e}"), + None, + )), } - - Err(CompileError::new( - format!( - "template {:?} not found in directories {:?}", - path, self.dirs - ), - file_info, - )) } }