diff --git a/nix-js/src/ir/downgrade.rs b/nix-js/src/ir/downgrade.rs index 8f381b9..551d0df 100644 --- a/nix-js/src/ir/downgrade.rs +++ b/nix-js/src/ir/downgrade.rs @@ -2,11 +2,28 @@ #![allow(clippy::unwrap_used)] use rnix::ast::{self, Expr, HasEntry}; +use std::path::{Component, Path as StdPath, PathBuf}; use crate::error::{Error, Result}; use super::*; +fn normalize_path(path: &StdPath) -> String { + let mut normalized = PathBuf::new(); + for component in path.components() { + match component { + Component::Prefix(p) => normalized.push(p.as_os_str()), + Component::RootDir => normalized.push("/"), + Component::CurDir => {} + Component::ParentDir => { + normalized.pop(); + } + Component::Normal(c) => normalized.push(c), + } + } + normalized.to_string_lossy().to_string() +} + pub trait Downgrade { fn downgrade(self, ctx: &mut Ctx) -> Result; } @@ -74,21 +91,10 @@ impl Downgrade for ast::Path { .collect(); let resolved_path = if path_str.starts_with('/') { - path_str + normalize_path(&std::path::PathBuf::from(&path_str)) } else { let current_dir = ctx.get_current_dir(); - - current_dir - .join(&path_str) - .canonicalize() - .map_err(|e| { - crate::error::Error::downgrade_error(format!( - "Failed to resolve path {}: {}", - path_str, e - )) - })? - .to_string_lossy() - .to_string() + normalize_path(¤t_dir.join(&path_str)) }; vec![ctx.new_expr(Str { val: resolved_path }.to_ir())]