fix: normalize preprocessed path #1157
Conversation
There was a problem hiding this comment.
Code Review: pre_process.rs Changes
Issues Identified:
-
Potential Performance Concern 🔴
path.canonicalize()performs filesystem I/O which could be slow for many paths- Consider caching or using a lighter normalization approach if performance is critical
-
Error Handling Gap
⚠️ canonicalize()silently falls back to original path on failure, which might hide real filesystem issues- Consider logging or returning a Result type for better error propagation
-
Redundant URI Conversion 🤔
- Converting to URI and back seems unnecessary for simple path normalization
file_path_to_urianduri_to_file_pathmight introduce platform-specific issues (e.g., Windows drive letters)
Recommendations:
- Simplify normalization if URI conversion isn't strictly required:
fn normalize_path_string(path: String) -> String {
PathBuf::from(path)
.canonicalize()
.unwrap_or_else(|_| PathBuf::from(&path))
.to_string_lossy()
.to_string()
}- Add logging for failed canonicalization:
fn normalize_path_string(path: String) -> String {
let path_buf = PathBuf::from(&path);
match path_buf.canonicalize() {
Ok(canonical) => canonical.to_string_lossy().to_string(),
Err(e) => {
log::warn!("Failed to canonicalize path '{}': {}", path, e);
path
}
}
}- Consider platform compatibility - Test on Windows where path separators and drive letters may behave differently with URI conversion.
Overall Assessment:
The change adds path normalization which is generally good practice, but the implementation could be simplified and made more robust. The URI round-trip appears unnecessary unless there's a specific requirement for URI-encoded paths.
There was a problem hiding this comment.
Code Review
This pull request introduces a path normalization step using a new helper function normalize_path_string in pre_process.rs. Feedback highlights a critical issue on Windows where std::fs::canonicalize returns verbatim paths starting with \\?\ that are unsupported by the url crate, leading to conversion failures. A code suggestion was provided to strip this prefix on Windows to prevent files from being silently ignored.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
| fn normalize_path_string(path: String) -> String { | ||
| let path = PathBuf::from(path); | ||
| let path = path.canonicalize().unwrap_or(path); | ||
| file_path_to_uri(&path) | ||
| .and_then(|uri| uri_to_file_path(&uri)) | ||
| .unwrap_or(path) | ||
| .to_string_lossy() | ||
| .to_string() | ||
| } |
There was a problem hiding this comment.
On Windows, std::fs::canonicalize returns a verbatim path starting with the \\?\ prefix (e.g., \\?\C:\path or \\?\UNC\server\share). The url crate's Url::from_file_path does not support verbatim paths and will return Err(()). This causes file_path_to_uri to return None, meaning normalize_path_string will fall back to returning the verbatim path. Consequently, any subsequent operations attempting to convert this path to a URI (such as when loading or updating files) will fail, causing files to be silently ignored on Windows. To fix this, we should strip the verbatim prefix on Windows before converting the path to a URI.
| fn normalize_path_string(path: String) -> String { | |
| let path = PathBuf::from(path); | |
| let path = path.canonicalize().unwrap_or(path); | |
| file_path_to_uri(&path) | |
| .and_then(|uri| uri_to_file_path(&uri)) | |
| .unwrap_or(path) | |
| .to_string_lossy() | |
| .to_string() | |
| } | |
| fn normalize_path_string(path: String) -> String { | |
| let path = PathBuf::from(path); | |
| let path = path.canonicalize().unwrap_or(path); | |
| #[cfg(windows)] | |
| let path = { | |
| let path_str = path.to_string_lossy(); | |
| if let Some(stripped) = path_str.strip_prefix("\\\\?\\UNC\\") { | |
| PathBuf::from(format!("\\\\{}", stripped)) | |
| } else if let Some(stripped) = path_str.strip_prefix("\\\\?\\") { | |
| PathBuf::from(stripped) | |
| } else { | |
| path | |
| } | |
| }; | |
| file_path_to_uri(&path) | |
| .and_then(|uri| uri_to_file_path(&uri)) | |
| .unwrap_or(path) | |
| .to_string_lossy() | |
| .to_string() | |
| } |
fix this bug: #1156