Skip to content

Commit 7389da9

Browse files
committed
fix: honor run cwd in command templates
1 parent 7954d02 commit 7389da9

3 files changed

Lines changed: 33 additions & 6 deletions

File tree

src/cortex-cli/src/run_cmd/execution.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
33
use anyhow::{Context, Result, bail};
44
use std::io::{self, IsTerminal, Read, Write};
5+
use std::path::Path;
56
use std::time::Duration;
67

78
use crate::styled_output::{print_success, print_warning};
@@ -17,6 +18,15 @@ use super::output::{copy_to_clipboard, send_notification};
1718
use super::session::{SessionMode, resolve_session_id};
1819
use super::system::check_file_descriptor_limits;
1920

21+
/// Return the working directory value exposed to custom command templates.
22+
pub fn command_template_cwd(cwd: Option<&Path>) -> String {
23+
cwd.map(Path::to_path_buf)
24+
.or_else(|| std::env::current_dir().ok())
25+
.unwrap_or_default()
26+
.to_string_lossy()
27+
.to_string()
28+
}
29+
2030
impl RunCli {
2131
/// Run the command.
2232
pub async fn run(self) -> Result<()> {
@@ -428,12 +438,8 @@ impl RunCli {
428438
// Try to get the custom command registry
429439
if let Some(registry) = cortex_engine::try_custom_command_registry() {
430440
// Try to execute the custom command
431-
let ctx = cortex_engine::TemplateContext::new(message.to_string()).with_cwd(
432-
std::env::current_dir()
433-
.unwrap_or_default()
434-
.to_string_lossy()
435-
.to_string(),
436-
);
441+
let ctx = cortex_engine::TemplateContext::new(message.to_string())
442+
.with_cwd(command_template_cwd(self.cwd.as_deref()));
437443

438444
// Use blocking runtime to get the command
439445
let prompt = tokio::task::block_in_place(|| {

src/cortex-cli/src/run_cmd/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,5 @@ mod tests;
3232

3333
// Re-export public types
3434
pub use cli::{OutputFormat, RunCli};
35+
pub use execution::command_template_cwd;
3536
pub use system::ModelSpec;
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
use std::path::PathBuf;
2+
3+
use cortex_cli::run_cmd::command_template_cwd;
4+
5+
#[test]
6+
fn command_template_cwd_prefers_run_cwd_override() {
7+
let requested_cwd = PathBuf::from("requested-project");
8+
9+
assert_eq!(
10+
command_template_cwd(Some(requested_cwd.as_path())),
11+
requested_cwd.to_string_lossy()
12+
);
13+
}
14+
15+
#[test]
16+
fn command_template_cwd_falls_back_to_process_cwd() {
17+
let current_dir = std::env::current_dir().expect("current dir should be available");
18+
19+
assert_eq!(command_template_cwd(None), current_dir.to_string_lossy());
20+
}

0 commit comments

Comments
 (0)