diff --git a/src/ast/mod.rs b/src/ast/mod.rs index f8a6939..d5b311a 100644 --- a/src/ast/mod.rs +++ b/src/ast/mod.rs @@ -692,30 +692,23 @@ impl Rewriter { } } +fn normalize_path(p: PathBuf) -> PathBuf { + let mut normalized = PathBuf::new(); + for segment in p.components() { + normalized.push(segment); + } + return normalized; +} + impl walk::Walker for Rewriter { fn visit_expression(&mut self, expr: &mut Expression) { // Rewrite all paths except for stdlib paths to absolute. let main_separator = format!("{}", std::path::MAIN_SEPARATOR); if let Expression::Include(ref mut def) = expr { let path = PathBuf::from(&def.path.fragment); - #[cfg(not(windows))] - { - if path.is_relative() { - def.path.fragment = self - .base - .join(path) - .canonicalize() - .unwrap() - .to_string_lossy() - .to_string(); - } - } - #[cfg(windows)] - { - if path.is_relative() { - def.path.fragment = self.base.join(path).to_string_lossy().to_string(); - } - } + def.path.fragment = normalize_path(self.base.join(path)) + .to_string_lossy() + .to_string(); } if let Expression::Import(ref mut def) = expr { let path = PathBuf::from( @@ -728,24 +721,9 @@ impl walk::Walker for Rewriter { if path.starts_with(format!("std{}", main_separator)) { return; } - #[cfg(not(windows))] - { - if path.is_relative() { - def.path.fragment = self - .base - .join(path) - .canonicalize() - .unwrap() - .to_string_lossy() - .to_string(); - } - } - #[cfg(windows)] - { - if path.is_relative() { - def.path.fragment = self.base.join(path).to_string_lossy().to_string(); - } - } + def.path.fragment = normalize_path(self.base.join(path)) + .to_string_lossy() + .to_string(); } } }