Skip to content
Merged
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
37 changes: 13 additions & 24 deletions tidewave-core/src/acp_proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ Because of this, we don't use the ACP SDK in the browser, but instead handle raw
JSON-RPC messages and use the ACP-SDK for types. The proxy will continue to forward
any requests to the new connection.
*/
use crate::command::create_shell_command;
use anyhow::{anyhow, Result};
use axum::{
extract::{
Expand All @@ -152,7 +153,6 @@ use serde::{Deserialize, Serialize};
use serde_json::{Map, Value};
use std::{
collections::HashMap,
path::Path,
process::Stdio,
sync::{
atomic::{AtomicBool, AtomicU64, Ordering},
Expand All @@ -161,7 +161,7 @@ use std::{
};
use tokio::{
io::{AsyncBufReadExt, AsyncWriteExt, BufReader},
process::{Child, Command},
process::Child,
sync::{mpsc, Mutex, RwLock},
};
use tracing::{debug, error, info, trace, warn};
Expand Down Expand Up @@ -239,6 +239,8 @@ pub struct TidewaveSpawnOptions {
pub command: String,
pub env: HashMap<String, String>,
pub cwd: String,
#[serde(default)]
is_wsl: bool,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
Expand Down Expand Up @@ -1201,18 +1203,6 @@ async fn forward_response_to_process(
Ok(())
}

fn get_shell_command(cmd: &str) -> (&'static str, Vec<&str>) {
#[cfg(target_os = "windows")]
{
("cmd.exe", vec!["/s", "/c", cmd])
}

#[cfg(not(target_os = "windows"))]
{
("sh", vec!["-c", cmd])
}
}

// ============================================================================
// Process Management
// ============================================================================
Expand All @@ -1221,21 +1211,19 @@ fn get_shell_command(cmd: &str) -> (&'static str, Vec<&str>) {
pub fn real_process_starter() -> ProcessStarterFn {
Arc::new(|spawn_opts: TidewaveSpawnOptions| {
Box::pin(async move {
let (cmd, args) = get_shell_command(&spawn_opts.command);
info!("Starting ACP process: {}", spawn_opts.command);

info!("Starting ACP process: {} with args: {:?}", cmd, args);
let mut cmd = create_shell_command(
&spawn_opts.command,
spawn_opts.env,
&spawn_opts.cwd,
spawn_opts.is_wsl,
);

let mut cmd = Command::new(cmd);
cmd.args(args)
.envs(spawn_opts.env)
.current_dir(Path::new(&spawn_opts.cwd))
.stdin(Stdio::piped())
cmd.stdin(Stdio::piped())
.stdout(Stdio::piped())
.stderr(Stdio::piped());

#[cfg(windows)]
cmd.creation_flags(winapi::um::winbase::CREATE_NO_WINDOW);

let mut child = cmd
.spawn()
.map_err(|e| anyhow!("Failed to spawn process: {}", e))?;
Expand Down Expand Up @@ -1903,6 +1891,7 @@ mod tests {
command: "test_cmd".to_string(),
env: HashMap::new(),
cwd: ".".to_string(),
is_wsl: false,
}
}

Expand Down
65 changes: 65 additions & 0 deletions tidewave-core/src/command.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
use std::collections::HashMap;
use std::path::Path;
use tokio::process::Command;

pub fn create_shell_command(
cmd: &str,
env: HashMap<String, String>,
cwd: &str,
#[cfg_attr(not(target_os = "windows"), allow(unused_variables))] is_wsl: bool,
) -> Command {
#[cfg(target_os = "windows")]
{
if is_wsl {
// WSL case: use --cd flag and construct env string
// Build env assignments string: VAR1=value1 VAR2=value2 ... command
let env_string: Vec<String> = env
.iter()
.map(|(k, v)| {
// Escape single quotes in the value by replacing ' with '\''
let escaped_value = v.replace("'", "'\\''");
format!("{}='{}'", k, escaped_value)
})
.collect();

let full_command = if env_string.is_empty() {
cmd.to_string()
} else {
format!("{} {}", env_string.join(" "), cmd)
};

let mut command = Command::new("wsl.exe");
command
.arg("--cd")
.arg(cwd)
.arg("sh")
.arg("-c")
.arg(full_command)
.creation_flags(winapi::um::winbase::CREATE_NO_WINDOW);
command
} else {
// Windows cmd case: use .current_dir()
let mut command = Command::new("cmd.exe");
command
.arg("/s")
.arg("/c")
.arg(cmd)
.envs(env)
.current_dir(Path::new(cwd))
.creation_flags(winapi::um::winbase::CREATE_NO_WINDOW);
command
}
}

#[cfg(not(target_os = "windows"))]
{
// Unix case: use .current_dir()
let mut command = Command::new("sh");
command
.arg("-c")
.arg(cmd)
.envs(env)
.current_dir(Path::new(cwd));
command
}
}
1 change: 1 addition & 0 deletions tidewave-core/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub mod acp_proxy;
mod command;
pub mod config;
mod mcp_remote;
pub mod server;
Expand Down
Loading