fix: Path::canonicalize -> normalize_path

* Nix doesn't require path to exist
This commit is contained in:
2026-01-11 14:20:18 +08:00
parent 621d4ea5c0
commit 0538463bf0

View File

@@ -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<Ctx: DowngradeContext> {
fn downgrade(self, ctx: &mut Ctx) -> Result<ExprId>;
}
@@ -74,21 +91,10 @@ impl<Ctx: DowngradeContext> Downgrade<Ctx> 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(&current_dir.join(&path_str))
};
vec![ctx.new_expr(Str { val: resolved_path }.to_ir())]