use anyhow::{Result, anyhow};
use std::path::{Path, PathBuf};
use tracing::warn;
pub fn canonicalize(path: impl AsRef<Path>) -> std::io::Result<PathBuf> {
dunce::canonicalize(path)
}
pub async fn canonicalize_async(path: impl AsRef<Path> + Send) -> std::io::Result<PathBuf> {
let path = path.as_ref().to_path_buf();
match tokio::task::spawn_blocking(move || dunce::canonicalize(&path)).await {
Ok(inner) => inner,
Err(join_error) => Err(std::io::Error::other(join_error)),
}
}
pub fn canonicalize_workspace(workspace_root: &Path) -> PathBuf {
canonicalize(workspace_root).unwrap_or_else(|error| {
warn!(
path = %workspace_root.display(),
%error,
"Failed to canonicalize workspace root; falling back to provided path"
);
workspace_root.to_path_buf()
})
}
pub async fn canonicalize_allow_missing(normalized: &Path) -> Result<PathBuf> {
if tokio::fs::try_exists(normalized).await.unwrap_or(false) {
return canonicalize_async(normalized)
.await
.map_err(|e| anyhow!("Failed to resolve canonical path for '{}': {}", normalized.display(), e));
}
let mut current = normalized.to_path_buf();
while let Some(parent) = current.parent() {
if tokio::fs::try_exists(parent).await.unwrap_or(false) {
let canonical_parent = canonicalize_async(parent)
.await
.map_err(|e| anyhow!("Failed to resolve canonical path for '{}': {}", parent.display(), e))?;
let remainder = normalized.strip_prefix(parent).unwrap_or_else(|_| Path::new(""));
return if remainder.as_os_str().is_empty() {
Ok(canonical_parent)
} else {
Ok(canonical_parent.join(remainder))
};
}
current = parent.to_path_buf();
}
Ok(normalized.to_path_buf())
}
#[cfg(test)]
mod tests {
use super::*;
#[tokio::test]
async fn test_canonicalize_existing_file() {
let temp_dir = std::env::temp_dir();
let test_file = temp_dir.join("vtcode_test_existing.txt");
tokio::fs::write(&test_file, b"test").await.unwrap();
let canonical = canonicalize_allow_missing(&test_file).await.unwrap();
assert!(canonical.is_absolute());
assert!(canonical.exists());
drop(tokio::fs::remove_file(&test_file).await);
}
#[tokio::test]
async fn test_canonicalize_missing_file() {
let temp_dir = std::env::temp_dir();
let missing_file = temp_dir.join("vtcode_test_missing_dir/missing_file.txt");
let canonical = canonicalize_allow_missing(&missing_file).await.unwrap();
assert!(canonical.is_absolute());
assert!(canonical.to_string_lossy().contains("missing_file.txt"));
}
#[tokio::test]
async fn test_canonicalize_deeply_missing_path() {
let temp_dir = std::env::temp_dir();
let deep_missing = temp_dir.join("vtcode_test_a/b/c/d/file.txt");
let canonical = canonicalize_allow_missing(&deep_missing).await.unwrap();
assert!(canonical.is_absolute());
assert!(canonical.to_string_lossy().contains("vtcode_test_a"));
}
#[tokio::test]
async fn test_canonicalize_missing_file_with_existing_parent() {
let temp_dir = std::env::temp_dir();
let test_dir = temp_dir.join("vtcode_test_parent");
tokio::fs::create_dir_all(&test_dir).await.unwrap();
let missing_file = test_dir.join("missing.txt");
let canonical = canonicalize_allow_missing(&missing_file).await.unwrap();
assert!(canonical.is_absolute());
assert!(canonical.to_string_lossy().ends_with("missing.txt"));
drop(tokio::fs::remove_dir(&test_dir).await);
}
}