|
1 | 1 | //! Execution runner for exec mode. |
2 | 2 |
|
3 | 3 | use std::io::{self, BufRead, IsTerminal, Read, Write}; |
4 | | -use std::path::PathBuf; |
| 4 | +use std::path::{Path, PathBuf}; |
5 | 5 | use std::time::{Duration, Instant}; |
6 | 6 |
|
7 | 7 | use anyhow::{Context, Result, bail}; |
@@ -82,9 +82,15 @@ impl ExecCli { |
82 | 82 |
|
83 | 83 | // Read from file if specified |
84 | 84 | 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) |
86 | 87 | .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 | + })?; |
88 | 94 | prompt.push_str(&content); |
89 | 95 | } |
90 | 96 |
|
@@ -852,3 +858,43 @@ impl ExecCli { |
852 | 858 | Ok(()) |
853 | 859 | } |
854 | 860 | } |
| 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