Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions src/cortex-cli/src/run_cmd/execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -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<()> {
Expand Down Expand Up @@ -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(|| {
Expand Down
1 change: 1 addition & 0 deletions src/cortex-cli/src/run_cmd/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
20 changes: 20 additions & 0 deletions src/cortex-cli/tests/run_command_cwd.rs
Original file line number Diff line number Diff line change
@@ -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());
}