Fix: canonicalize paths to ensure hashmap lookups behave as expected

This commit is contained in:
Luca Palmieri 2024-12-15 12:08:39 +01:00
parent f0ac41df30
commit 2cf65d18cc

View File

@ -182,27 +182,40 @@ impl Config {
start_at: Option<&Path>, start_at: Option<&Path>,
file_info: Option<FileInfo<'_>>, file_info: Option<FileInfo<'_>>,
) -> Result<Arc<Path>, CompileError> { ) -> Result<Arc<Path>, CompileError> {
if let Some(root) = start_at { fn _find_template(config: &Config, path: &str, start_at: Option<&Path>) -> Option<PathBuf> {
let relative = root.with_file_name(path); if let Some(root) = start_at {
if relative.exists() { let relative = root.with_file_name(path);
return Ok(relative.into()); 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 Some(path) = _find_template(self, path, start_at) else {
let rooted = dir.join(path); return Err(CompileError::new(
if rooted.exists() { format!(
return Ok(rooted.into()); "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,
))
} }
} }