Skip to content

Commit 6ccb5be

Browse files
committed
fix(exec): 让 file 相对路径跟随 cwd
1 parent 8b723ac commit 6ccb5be

1 file changed

Lines changed: 49 additions & 3 deletions

File tree

src/cortex-cli/src/exec_cmd/runner.rs

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Execution runner for exec mode.
22
33
use std::io::{self, BufRead, IsTerminal, Read, Write};
4-
use std::path::PathBuf;
4+
use std::path::{Path, PathBuf};
55
use std::time::{Duration, Instant};
66

77
use anyhow::{Context, Result, bail};
@@ -82,9 +82,15 @@ impl ExecCli {
8282

8383
// Read from file if specified
8484
if let Some(ref file_path) = self.file {
85-
let content = tokio::fs::read_to_string(file_path)
85+
let resolved_file_path = resolve_prompt_file_path(file_path, self.cwd.as_deref());
86+
let content = tokio::fs::read_to_string(&resolved_file_path)
8687
.await
87-
.with_context(|| format!("Failed to read prompt file: {}", file_path.display()))?;
88+
.with_context(|| {
89+
format!(
90+
"Failed to read prompt file: {}",
91+
resolved_file_path.display()
92+
)
93+
})?;
8894
prompt.push_str(&content);
8995
}
9096

@@ -852,3 +858,43 @@ impl ExecCli {
852858
Ok(())
853859
}
854860
}
861+
862+
fn resolve_prompt_file_path(file_path: &Path, cwd: Option<&Path>) -> PathBuf {
863+
if file_path.is_absolute() {
864+
file_path.to_path_buf()
865+
} else if let Some(cwd) = cwd {
866+
cwd.join(file_path)
867+
} else {
868+
file_path.to_path_buf()
869+
}
870+
}
871+
872+
#[cfg(test)]
873+
mod tests {
874+
use super::resolve_prompt_file_path;
875+
use std::path::{Path, PathBuf};
876+
877+
#[test]
878+
fn resolve_prompt_file_path_uses_cwd_for_relative_paths() {
879+
let cwd = PathBuf::from("workspace").join("nested");
880+
let resolved = resolve_prompt_file_path(Path::new("script.sh"), Some(&cwd));
881+
882+
assert_eq!(resolved, cwd.join("script.sh"));
883+
}
884+
885+
#[test]
886+
fn resolve_prompt_file_path_keeps_absolute_paths() {
887+
let absolute_file_path = std::env::temp_dir().join("script.sh");
888+
let resolved =
889+
resolve_prompt_file_path(&absolute_file_path, Some(Path::new("workspace/nested")));
890+
891+
assert_eq!(resolved, absolute_file_path);
892+
}
893+
894+
#[test]
895+
fn resolve_prompt_file_path_keeps_relative_path_without_cwd() {
896+
let resolved = resolve_prompt_file_path(Path::new("script.sh"), None);
897+
898+
assert_eq!(resolved, Path::new("script.sh"));
899+
}
900+
}

0 commit comments

Comments
 (0)