From bad5a808af975a703b6b682497a539692bcd45ec Mon Sep 17 00:00:00 2001 From: OnlyYu1996 <1158673577@qq.com> Date: Sun, 17 May 2026 06:45:13 +0800 Subject: [PATCH] fix(exec): resolve prompt files against cwd --- src/cortex-cli/src/exec_cmd/runner.rs | 53 ++++++++++++++++++++++++++- 1 file changed, 51 insertions(+), 2 deletions(-) diff --git a/src/cortex-cli/src/exec_cmd/runner.rs b/src/cortex-cli/src/exec_cmd/runner.rs index a9f521ec2..cdb239830 100644 --- a/src/cortex-cli/src/exec_cmd/runner.rs +++ b/src/cortex-cli/src/exec_cmd/runner.rs @@ -82,9 +82,15 @@ impl ExecCli { // Read from file if specified if let Some(ref file_path) = self.file { - let content = tokio::fs::read_to_string(file_path) + let resolved_file_path = resolve_prompt_file_path(file_path, self.cwd.as_ref()); + let content = tokio::fs::read_to_string(&resolved_file_path) .await - .with_context(|| format!("Failed to read prompt file: {}", file_path.display()))?; + .with_context(|| { + format!( + "Failed to read prompt file: {}", + resolved_file_path.display() + ) + })?; prompt.push_str(&content); } @@ -852,3 +858,46 @@ impl ExecCli { Ok(()) } } + +fn resolve_prompt_file_path(file_path: &std::path::Path, cwd: Option<&PathBuf>) -> PathBuf { + if file_path.is_absolute() { + return file_path.to_path_buf(); + } + + cwd.map_or_else(|| file_path.to_path_buf(), |cwd| cwd.join(file_path)) +} + +#[cfg(test)] +mod tests { + use super::resolve_prompt_file_path; + use std::path::{Path, PathBuf}; + + #[test] + fn resolves_relative_prompt_file_against_cwd() { + let cwd = PathBuf::from("/tmp/cortex_test/nested"); + + assert_eq!( + resolve_prompt_file_path(Path::new("script.sh"), Some(&cwd)), + cwd.join("script.sh") + ); + } + + #[test] + fn leaves_relative_prompt_file_relative_without_cwd() { + assert_eq!( + resolve_prompt_file_path(Path::new("script.sh"), None), + PathBuf::from("script.sh") + ); + } + + #[test] + fn leaves_absolute_prompt_file_unchanged() { + let path = if cfg!(windows) { + PathBuf::from(r"C:\tmp\script.sh") + } else { + PathBuf::from("/tmp/script.sh") + }; + + assert_eq!(resolve_prompt_file_path(&path, None), path); + } +}