Skip to content

armgit / zed

Public Imported from GitHub
0

Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.

remote: Fix Windows SSH commands when the default shell is Nushell (#65580)

Xin Zhao784374c6794a
diff --git a/crates/project/src/terminals.rs b/crates/project/src/terminals.rs
index c9406078c9d..c7f59903a49 100644
--- a/crates/project/src/terminals.rs
+++ b/crates/project/src/terminals.rs
@@ -587,7 +587,7 @@ impl Project {
&env,
None,
None,
- Interactive::Yes,
+ Interactive::No,
)?;
let mut command = new_std_command(command_template.program);
command.args(command_template.args);
@@ -747,6 +747,7 @@ fn quote_cmd_command_arg_for_outer_shell(arg: &str, shell_kind: ShellKind) -> Op
#[cfg(test)]
mod tests {
use super::*;
+ use gpui::TestAppContext;
use pretty_assertions::assert_eq;
fn prepared_cmd_task(command_arg: &str) -> SpawnInTerminal {
@@ -826,4 +827,103 @@ mod tests {
"&cargo test 'some test'"
);
}
+
+ async fn init_remote_client(
+ cx: &mut TestAppContext,
+ server_cx: &mut TestAppContext,
+ ) -> Entity<RemoteClient> {
+ cx.update(|cx| {
+ release_channel::init(semver::Version::new(0, 0, 0), cx);
+ });
+ server_cx.update(|cx| {
+ release_channel::init(semver::Version::new(0, 0, 0), cx);
+ });
+
+ let (opts, server_session, connect_guard) = RemoteClient::fake_server(cx, server_cx);
+ let ping_handler = server_cx.new(|_| ());
+ server_session.add_request_handler::<proto::Ping, _, _, _>(
+ ping_handler.downgrade(),
+ |_entity, _envelope, _cx| async { Ok(proto::Ack {}) },
+ );
+ drop(connect_guard);
+
+ RemoteClient::connect_mock(opts, cx).await
+ }
+
+ #[gpui::test]
+ async fn remote_exec_in_shell_uses_non_interactive_command(
+ cx: &mut TestAppContext,
+ server_cx: &mut TestAppContext,
+ ) {
+ let remote_client = init_remote_client(cx, server_cx).await;
+ let project = cx.update(|cx| {
+ let settings_store = settings::SettingsStore::test(cx);
+ cx.set_global(settings_store);
+ let client = client::Client::new(
+ Arc::new(clock::FakeSystemClock::new()),
+ http_client::FakeHttpClient::with_404_response(),
+ cx,
+ );
+ let user_store = cx.new(|cx| client::UserStore::new(client.clone(), cx));
+ Project::remote(
+ remote_client,
+ client,
+ node_runtime::NodeRuntime::unavailable(),
+ user_store,
+ Arc::new(language::LanguageRegistry::test(
+ cx.background_executor().clone(),
+ )),
+ fs::FakeFs::new(cx.background_executor().clone()),
+ false,
+ cx,
+ )
+ });
+
+ let exec_command = project
+ .update(cx, |project, cx| {
+ project.exec_in_shell("echo hello".to_string(), cx)
+ })
+ .await
+ .unwrap();
+ assert_eq!(exec_command.get_program(), "mock");
+ assert_eq!(
+ exec_command
+ .get_args()
+ .map(|arg| arg.to_string_lossy().into_owned())
+ .collect::<Vec<_>>(),
+ vec!["-T", "sh", "-c", "echo hello"],
+ );
+ }
+
+ #[gpui::test]
+ async fn remote_terminal_launch_uses_interactive_command(
+ cx: &mut TestAppContext,
+ server_cx: &mut TestAppContext,
+ ) {
+ let remote_client = init_remote_client(cx, server_cx).await;
+
+ let (expected_title, (terminal_shell, _)) = cx.update(|cx| {
+ let title = format!(
+ "{} — Terminal",
+ remote_client.read(cx).connection_options().display_name()
+ );
+ let shell = create_remote_shell(
+ None,
+ HashMap::default(),
+ Some(Arc::from(Path::new("/work"))),
+ remote_client,
+ cx,
+ )
+ .unwrap();
+ (title, shell)
+ });
+ assert_eq!(
+ terminal_shell,
+ Shell::WithArguments {
+ program: "mock".to_string(),
+ args: vec!["-t".to_string(), "sh".to_string()],
+ title_override: Some(expected_title),
+ },
+ );
+ }
}
diff --git a/crates/remote/src/transport/mock.rs b/crates/remote/src/transport/mock.rs
index 734b98bf7a0..f486295bb4b 100644
--- a/crates/remote/src/transport/mock.rs
+++ b/crates/remote/src/transport/mock.rs
@@ -212,10 +212,14 @@ impl RemoteConnection for MockRemoteConnection {
env: &HashMap<String, String>,
_working_dir: Option<String>,
_port_forward: Option<(u16, String, u16)>,
- _interactive: Interactive,
+ interactive: Interactive,
) -> Result<CommandTemplate> {
let shell_program = program.unwrap_or_else(|| "sh".to_string());
let mut shell_args = Vec::new();
+ match interactive {
+ Interactive::Yes => shell_args.push("-t".to_string()),
+ Interactive::No => shell_args.push("-T".to_string()),
+ }
shell_args.push(shell_program);
shell_args.extend(args.iter().cloned());
Ok(CommandTemplate {
diff --git a/crates/remote/src/transport/ssh.rs b/crates/remote/src/transport/ssh.rs
index efed4c99032..51c5fe71614 100644
--- a/crates/remote/src/transport/ssh.rs
+++ b/crates/remote/src/transport/ssh.rs
@@ -2288,7 +2288,13 @@ fn build_command_windows(
let byte_slice: Vec<u8> = utf16_bytes.iter().flat_map(|&u| u.to_le_bytes()).collect();
let base64_encoded = base64::engine::general_purpose::STANDARD.encode(&byte_slice);
- args.push(format!("powershell.exe -E {}", base64_encoded));
+ // Windows OpenSSH server automatically quotes the command for `ssh -T` (non-PTY),
+ // but leaves it unquoted for `ssh -t` (PTY).
+ // Related issue: https://github.com/PowerShell/Win32-OpenSSH/issues/2478
+ match interactive {
+ Interactive::Yes => args.push(format!("\"powershell.exe -E {}\"", base64_encoded)),
+ Interactive::No => args.push(format!("powershell.exe -E {}", base64_encoded)),
+ }
Ok(CommandTemplate {
program: "ssh".into(),
@@ -2890,6 +2896,114 @@ mod tests {
Ok(())
}
+ #[test]
+ fn test_build_command_windows() -> Result<()> {
+ use base64::Engine as _;
+
+ let encode_utf16_base64 = |script: &str| {
+ let utf16_bytes: Vec<u16> = script.encode_utf16().collect();
+ let byte_slice: Vec<u8> = utf16_bytes.iter().flat_map(|&u| u.to_le_bytes()).collect();
+ base64::engine::general_purpose::STANDARD.encode(&byte_slice)
+ };
+
+ let mut input_env = HashMap::default();
+ input_env.insert("INPUT_VAR".to_string(), "val".to_string());
+ let mut env = HashMap::default();
+ env.insert("SSH_VAR".to_string(), "ssh-val".to_string());
+
+ // Non-interactive command (Interactive::No should use -T and keep the command unquoted,
+ // because Windows OpenSSH already quotes the command in non-PTY mode)
+ let command = build_command_windows(
+ Some("remote_program".to_string()),
+ &["arg1".to_string(), "arg2".to_string()],
+ &input_env,
+ Some(r"C:\work".to_string()),
+ None,
+ env.clone(),
+ PathStyle::Windows,
+ "nu.exe",
+ ShellKind::Nushell,
+ vec!["-p".to_string(), "2222".to_string()],
+ vec![
+ "-l".to_string(),
+ "user".to_string(),
+ "--".to_string(),
+ "host".to_string(),
+ ],
+ Interactive::No,
+ )?;
+
+ let expected_non_interactive_exec = format!(
+ "powershell.exe -E {}",
+ encode_utf16_base64(r"Set-Location -Path C:\work ; &remote_program arg1 arg2")
+ );
+ assert_eq!(command.program, "ssh");
+ assert_eq!(
+ command.args.iter().map(String::as_str).collect::<Vec<_>>(),
+ [
+ "-p",
+ "2222",
+ "-o",
+ "LogLevel=ERROR",
+ "-T",
+ "-l",
+ "user",
+ "--",
+ "host",
+ &expected_non_interactive_exec,
+ ]
+ );
+ assert_eq!(command.env, env);
+
+ // Interactive command (Interactive::Yes should use -t and wrap the command in quotes,
+ // because Windows OpenSSH concatenates the command unquoted in PTY mode)
+ let command = build_command_windows(
+ None,
+ &[],
+ &input_env,
+ Some(r"D:\code\test_python".to_string()),
+ Some((1, "foo".to_owned(), 2)),
+ env.clone(),
+ PathStyle::Windows,
+ "nu.exe",
+ ShellKind::Nushell,
+ vec!["-p".to_string(), "2222".to_string()],
+ vec![
+ "-l".to_string(),
+ "user".to_string(),
+ "--".to_string(),
+ "host".to_string(),
+ ],
+ Interactive::Yes,
+ )?;
+
+ let expected_interactive_exec = format!(
+ "\"powershell.exe -E {}\"",
+ encode_utf16_base64(r"Set-Location -Path D:\code\test_python ; nu.exe")
+ );
+ assert_eq!(command.program, "ssh");
+ assert_eq!(
+ command.args.iter().map(String::as_str).collect::<Vec<_>>(),
+ [
+ "-p",
+ "2222",
+ "-L",
+ "1:foo:2",
+ "-o",
+ "LogLevel=ERROR",
+ "-t",
+ "-l",
+ "user",
+ "--",
+ "host",
+ &expected_interactive_exec,
+ ]
+ );
+ assert_eq!(command.env, env);
+
+ Ok(())
+ }
+
#[test]
fn test_build_command_quotes_env_assignment() -> Result<()> {
let mut input_env = HashMap::default();