From 7389da924ea648906b69ee5f007b06ae5feaa1a1 Mon Sep 17 00:00:00 2001 From: Bortlesboat Date: Mon, 11 May 2026 02:28:07 -0400 Subject: [PATCH] fix: honor run cwd in command templates --- src/cortex-cli/src/run_cmd/execution.rs | 18 ++++++++++++------ src/cortex-cli/src/run_cmd/mod.rs | 1 + src/cortex-cli/tests/run_command_cwd.rs | 20 ++++++++++++++++++++ 3 files changed, 33 insertions(+), 6 deletions(-) create mode 100644 src/cortex-cli/tests/run_command_cwd.rs diff --git a/src/cortex-cli/src/run_cmd/execution.rs b/src/cortex-cli/src/run_cmd/execution.rs index 18bb58d5b..8087618a1 100644 --- a/src/cortex-cli/src/run_cmd/execution.rs +++ b/src/cortex-cli/src/run_cmd/execution.rs @@ -2,6 +2,7 @@ use anyhow::{Context, Result, bail}; use std::io::{self, IsTerminal, Read, Write}; +use std::path::Path; use std::time::Duration; use crate::styled_output::{print_success, print_warning}; @@ -17,6 +18,15 @@ use super::output::{copy_to_clipboard, send_notification}; use super::session::{SessionMode, resolve_session_id}; use super::system::check_file_descriptor_limits; +/// Return the working directory value exposed to custom command templates. +pub fn command_template_cwd(cwd: Option<&Path>) -> String { + cwd.map(Path::to_path_buf) + .or_else(|| std::env::current_dir().ok()) + .unwrap_or_default() + .to_string_lossy() + .to_string() +} + impl RunCli { /// Run the command. pub async fn run(self) -> Result<()> { @@ -428,12 +438,8 @@ impl RunCli { // Try to get the custom command registry if let Some(registry) = cortex_engine::try_custom_command_registry() { // Try to execute the custom command - let ctx = cortex_engine::TemplateContext::new(message.to_string()).with_cwd( - std::env::current_dir() - .unwrap_or_default() - .to_string_lossy() - .to_string(), - ); + let ctx = cortex_engine::TemplateContext::new(message.to_string()) + .with_cwd(command_template_cwd(self.cwd.as_deref())); // Use blocking runtime to get the command let prompt = tokio::task::block_in_place(|| { diff --git a/src/cortex-cli/src/run_cmd/mod.rs b/src/cortex-cli/src/run_cmd/mod.rs index 03ec11ea6..ff4447b7c 100644 --- a/src/cortex-cli/src/run_cmd/mod.rs +++ b/src/cortex-cli/src/run_cmd/mod.rs @@ -32,4 +32,5 @@ mod tests; // Re-export public types pub use cli::{OutputFormat, RunCli}; +pub use execution::command_template_cwd; pub use system::ModelSpec; diff --git a/src/cortex-cli/tests/run_command_cwd.rs b/src/cortex-cli/tests/run_command_cwd.rs new file mode 100644 index 000000000..eaaa3a617 --- /dev/null +++ b/src/cortex-cli/tests/run_command_cwd.rs @@ -0,0 +1,20 @@ +use std::path::PathBuf; + +use cortex_cli::run_cmd::command_template_cwd; + +#[test] +fn command_template_cwd_prefers_run_cwd_override() { + let requested_cwd = PathBuf::from("requested-project"); + + assert_eq!( + command_template_cwd(Some(requested_cwd.as_path())), + requested_cwd.to_string_lossy() + ); +} + +#[test] +fn command_template_cwd_falls_back_to_process_cwd() { + let current_dir = std::env::current_dir().expect("current dir should be available"); + + assert_eq!(command_template_cwd(None), current_dir.to_string_lossy()); +}