Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 38 additions & 1 deletion crates/chat-cli/src/util/directories.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,44 @@ pub fn canonicalizes_path(os: &Os, path_as_str: &str) -> Result<String> {
let context = |input: &str| Ok(os.env.get(input).ok());
let home_dir = || os.env.home().map(|p| p.to_string_lossy().to_string());

Ok(shellexpand::full_with_context(path_as_str, home_dir, context)?.to_string())
let expanded = shellexpand::full_with_context(path_as_str, home_dir, context)?;

// Convert all relative paths to absolute paths (including glob patterns)
if !expanded.starts_with("/") && !expanded.starts_with("~") {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

~ is already expanded I think?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider using Path::new(&expanded).is_relative

let current_dir = os.env.current_dir()?;
let absolute_path = current_dir.join(expanded.as_ref());
// Normalize the path to remove ./ and ../ components
match absolute_path.canonicalize() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am wondering should we just always use the normalize_path() method for code simplicity and consistency?

Ok(canonical) => Ok(canonical.to_string_lossy().to_string()),
Err(_) => {
// If canonicalize fails (e.g., path doesn't exist), do manual normalization
let normalized = normalize_path(&absolute_path);
Ok(normalized.to_string_lossy().to_string())
}
}
} else {
Ok(expanded.to_string())
}
}

/// Manually normalize a path by resolving . and .. components
fn normalize_path(path: &std::path::Path) -> std::path::PathBuf {
let mut components = Vec::new();
for component in path.components() {
match component {
std::path::Component::CurDir => {
// Skip current directory components
}
std::path::Component::ParentDir => {
// Pop the last component for parent directory
components.pop();
}
_ => {
components.push(component);
}
}
}
components.iter().collect()
}

/// Given a globset builder and a path, build globs for both the file and directory patterns
Expand Down