Skip to content

Commit 1ef4b8b

Browse files
fix: check if path is absolute for create textdoc before canonicaliza… (#515)
* fix: check if path is absolute for create textdoc before canonicalization * fix: check for absolute before canonicalize in all text edit tools
1 parent de36005 commit 1ef4b8b

File tree

5 files changed

+27
-19
lines changed

5 files changed

+27
-19
lines changed

refact-agent/engine/src/files_correction.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ fn _shortify_paths_from_indexed(paths: &Vec<String>, indexed_paths: Arc<HashSet<
338338
/// In Windows, tries to fix the path, permissive about paths like \\?\C:\path, incorrect amount of \ and more.
339339
///
340340
/// Temporarily remove verbatim, to resolve ., .., symlinks if possible, it will be added again later.
341-
fn preprocess_path_for_normalization(p: String) -> String {
341+
pub fn preprocess_path_for_normalization(p: String) -> String {
342342
use itertools::Itertools;
343343

344344
let p = p.replace(r"/", r"\");
@@ -380,7 +380,7 @@ fn preprocess_path_for_normalization(p: String) -> String {
380380

381381
#[cfg(not(windows))]
382382
/// In Unix, do nothing
383-
fn preprocess_path_for_normalization(p: String) -> String {
383+
pub fn preprocess_path_for_normalization(p: String) -> String {
384384
p
385385
}
386386

@@ -447,8 +447,12 @@ fn absolute(path: &Path) -> Result<PathBuf, String> {
447447
pub fn canonical_path<T: Into<String>>(p: T) -> PathBuf {
448448
let p: String = p.into();
449449
let path= PathBuf::from(preprocess_path_for_normalization(p));
450+
canonicalize_normalized_path(path)
451+
}
450452

451-
path.canonicalize().unwrap_or_else(|_| absolute(&path).unwrap_or(path))
453+
/// If you did not call preprocess_path_for_normalization() before, use crate::files_correction::canonical_path() instead
454+
pub fn canonicalize_normalized_path(p: PathBuf) -> PathBuf {
455+
p.canonicalize().unwrap_or_else(|_| absolute(&p).unwrap_or(p))
452456
}
453457

454458
pub fn serialize_path<S: serde::Serializer>(path: &PathBuf, serializer: S) -> Result<S::Ok, S::Error> {

refact-agent/engine/src/tools/file_edit/tool_create_textdoc.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use std::collections::HashMap;
1111
use std::path::PathBuf;
1212
use std::sync::Arc;
1313
use tokio::sync::Mutex as AMutex;
14-
use crate::files_correction::canonical_path;
14+
use crate::files_correction::{canonicalize_normalized_path, preprocess_path_for_normalization};
1515
use crate::global_context::GlobalContext;
1616
use tokio::sync::RwLock as ARwLock;
1717

@@ -25,13 +25,14 @@ pub struct ToolCreateTextDoc;
2525
fn parse_args(args: &HashMap<String, Value>) -> Result<ToolCreateTextDocArgs, String> {
2626
let path = match args.get("path") {
2727
Some(Value::String(s)) => {
28-
let path = canonical_path(&s.trim().to_string());
28+
let path = PathBuf::from(preprocess_path_for_normalization(s.trim().to_string()));
2929
if !path.is_absolute() {
3030
return Err(format!(
31-
"Error: The provided path '{:?}' is not absolute. Please provide a full path starting from the root directory.",
32-
path
31+
"Error: The provided path '{}' is not absolute. Please provide a full path starting from the root directory.",
32+
s.trim()
3333
));
3434
}
35+
let path = canonicalize_normalized_path(path);
3536
if path.exists() {
3637
return Err(format!(
3738
"Error: Cannot create file at '{:?}' because it already exists. Please choose a different path or use update_textdoc/replace_textdoc to modify existing files.",

refact-agent/engine/src/tools/file_edit/tool_replace_textdoc.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::at_commands::at_commands::AtCommandsContext;
22
use crate::call_validation::{ChatContent, ChatMessage, ContextEnum, DiffChunk};
3-
use crate::files_correction::canonical_path;
3+
use crate::files_correction::{canonicalize_normalized_path, preprocess_path_for_normalization};
44
use crate::global_context::GlobalContext;
55
use crate::integrations::integr_abstract::IntegrationConfirmation;
66
use crate::tools::file_edit::auxiliary::{
@@ -25,13 +25,14 @@ pub struct ToolReplaceTextDoc;
2525
fn parse_args(args: &HashMap<String, Value>) -> Result<ToolReplaceTextDocArgs, String> {
2626
let path = match args.get("path") {
2727
Some(Value::String(s)) => {
28-
let path = canonical_path(&s.trim().to_string());
28+
let path = PathBuf::from(preprocess_path_for_normalization(s.trim().to_string()));
2929
if !path.is_absolute() {
3030
return Err(format!(
31-
"Error: The provided path '{:?}' is not absolute. Please provide a full path starting from the root directory.",
32-
path
31+
"Error: The provided path '{}' is not absolute. Please provide a full path starting from the root directory.",
32+
s.trim()
3333
));
3434
}
35+
let path = canonicalize_normalized_path(path);
3536
if !path.exists() {
3637
return Err(format!(
3738
"Error: The file '{:?}' does not exist. Please check if the path is correct and the file exists.",

refact-agent/engine/src/tools/file_edit/tool_update_textdoc.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use std::collections::HashMap;
99
use std::path::PathBuf;
1010
use std::sync::Arc;
1111
use tokio::sync::Mutex as AMutex;
12-
use crate::files_correction::canonical_path;
12+
use crate::files_correction::{canonicalize_normalized_path, preprocess_path_for_normalization};
1313
use tokio::sync::RwLock as ARwLock;
1414
use crate::global_context::GlobalContext;
1515

@@ -25,13 +25,14 @@ pub struct ToolUpdateTextDoc;
2525
fn parse_args(args: &HashMap<String, Value>) -> Result<ToolUpdateTextDocArgs, String> {
2626
let path = match args.get("path") {
2727
Some(Value::String(s)) => {
28-
let path = canonical_path(&s.trim().to_string());
28+
let path = PathBuf::from(preprocess_path_for_normalization(s.trim().to_string()));
2929
if !path.is_absolute() {
3030
return Err(format!(
31-
"Error: The provided path '{:?}' is not absolute. Please provide a full path starting from the root directory.",
32-
path
31+
"Error: The provided path '{}' is not absolute. Please provide a full path starting from the root directory.",
32+
s.trim()
3333
));
3434
}
35+
let path = canonicalize_normalized_path(path);
3536
if !path.exists() {
3637
return Err(format!(
3738
"Error: The file '{:?}' does not exist. Please check if the path is correct and the file exists.",

refact-agent/engine/src/tools/file_edit/tool_update_textdoc_regex.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use std::path::PathBuf;
1010
use std::sync::Arc;
1111
use regex::Regex;
1212
use tokio::sync::Mutex as AMutex;
13-
use crate::files_correction::canonical_path;
13+
use crate::files_correction::{canonicalize_normalized_path, preprocess_path_for_normalization};
1414
use tokio::sync::RwLock as ARwLock;
1515
use crate::global_context::GlobalContext;
1616

@@ -26,13 +26,14 @@ pub struct ToolUpdateTextDocRegex;
2626
fn parse_args(args: &HashMap<String, Value>) -> Result<ToolUpdateTextDocRegexArgs, String> {
2727
let path = match args.get("path") {
2828
Some(Value::String(s)) => {
29-
let path = canonical_path(&s.trim().to_string());
29+
let path = PathBuf::from(preprocess_path_for_normalization(s.trim().to_string()));
3030
if !path.is_absolute() {
3131
return Err(format!(
32-
"argument 'path' should be an absolute path: {:?}",
33-
path
32+
"Error: The provided path '{}' is not absolute. Please provide a full path starting from the root directory.",
33+
s.trim()
3434
));
3535
}
36+
let path = canonicalize_normalized_path(path);
3637
if !path.exists() {
3738
return Err(format!("argument 'path' doesn't exists: {:?}", path));
3839
}

0 commit comments

Comments
 (0)