remote: Fix more quoting issues with nushell (#41547)
https://github.com/zed-industries/zed/pull/40084#issuecomment-3464159871 Closes https://github.com/zed-industries/zed/pull/41547 Release Notes: - Fixed remoting not working when the remote has nu set as its shell
This commit is contained in:
@@ -39,6 +39,7 @@ pub(crate) struct SshRemoteConnection {
|
||||
ssh_platform: RemotePlatform,
|
||||
ssh_path_style: PathStyle,
|
||||
ssh_shell: String,
|
||||
ssh_shell_kind: ShellKind,
|
||||
ssh_default_system_shell: String,
|
||||
_temp_dir: TempDir,
|
||||
}
|
||||
@@ -241,6 +242,7 @@ impl RemoteConnection for SshRemoteConnection {
|
||||
let Self {
|
||||
ssh_path_style,
|
||||
socket,
|
||||
ssh_shell_kind,
|
||||
ssh_shell,
|
||||
..
|
||||
} = self;
|
||||
@@ -254,6 +256,7 @@ impl RemoteConnection for SshRemoteConnection {
|
||||
env,
|
||||
*ssh_path_style,
|
||||
ssh_shell,
|
||||
*ssh_shell_kind,
|
||||
socket.ssh_args(),
|
||||
)
|
||||
}
|
||||
@@ -367,7 +370,7 @@ impl RemoteConnection for SshRemoteConnection {
|
||||
|
||||
let ssh_proxy_process = match self
|
||||
.socket
|
||||
.ssh_command("env", &proxy_args)
|
||||
.ssh_command(self.ssh_shell_kind, "env", &proxy_args)
|
||||
// IMPORTANT: we kill this process when we drop the task that uses it.
|
||||
.kill_on_drop(true)
|
||||
.spawn()
|
||||
@@ -490,6 +493,13 @@ impl SshRemoteConnection {
|
||||
_ => PathStyle::Posix,
|
||||
};
|
||||
let ssh_default_system_shell = String::from("/bin/sh");
|
||||
let ssh_shell_kind = ShellKind::new(
|
||||
&ssh_shell,
|
||||
match ssh_platform.os {
|
||||
"windows" => true,
|
||||
_ => false,
|
||||
},
|
||||
);
|
||||
|
||||
let mut this = Self {
|
||||
socket,
|
||||
@@ -499,6 +509,7 @@ impl SshRemoteConnection {
|
||||
ssh_path_style,
|
||||
ssh_platform,
|
||||
ssh_shell,
|
||||
ssh_shell_kind,
|
||||
ssh_default_system_shell,
|
||||
};
|
||||
|
||||
@@ -563,7 +574,11 @@ impl SshRemoteConnection {
|
||||
|
||||
if self
|
||||
.socket
|
||||
.run_command(&dst_path.display(self.path_style()), &["version"])
|
||||
.run_command(
|
||||
self.ssh_shell_kind,
|
||||
&dst_path.display(self.path_style()),
|
||||
&["version"],
|
||||
)
|
||||
.await
|
||||
.is_ok()
|
||||
{
|
||||
@@ -632,7 +647,11 @@ impl SshRemoteConnection {
|
||||
) -> Result<()> {
|
||||
if let Some(parent) = tmp_path_gz.parent() {
|
||||
self.socket
|
||||
.run_command("mkdir", &["-p", parent.display(self.path_style()).as_ref()])
|
||||
.run_command(
|
||||
self.ssh_shell_kind,
|
||||
"mkdir",
|
||||
&["-p", parent.display(self.path_style()).as_ref()],
|
||||
)
|
||||
.await?;
|
||||
}
|
||||
|
||||
@@ -641,6 +660,7 @@ impl SshRemoteConnection {
|
||||
match self
|
||||
.socket
|
||||
.run_command(
|
||||
self.ssh_shell_kind,
|
||||
"curl",
|
||||
&[
|
||||
"-f",
|
||||
@@ -660,13 +680,19 @@ impl SshRemoteConnection {
|
||||
{
|
||||
Ok(_) => {}
|
||||
Err(e) => {
|
||||
if self.socket.run_command("which", &["curl"]).await.is_ok() {
|
||||
if self
|
||||
.socket
|
||||
.run_command(self.ssh_shell_kind, "which", &["curl"])
|
||||
.await
|
||||
.is_ok()
|
||||
{
|
||||
return Err(e);
|
||||
}
|
||||
|
||||
match self
|
||||
.socket
|
||||
.run_command(
|
||||
self.ssh_shell_kind,
|
||||
"wget",
|
||||
&[
|
||||
"--header=Content-Type: application/json",
|
||||
@@ -681,7 +707,12 @@ impl SshRemoteConnection {
|
||||
{
|
||||
Ok(_) => {}
|
||||
Err(e) => {
|
||||
if self.socket.run_command("which", &["wget"]).await.is_ok() {
|
||||
if self
|
||||
.socket
|
||||
.run_command(self.ssh_shell_kind, "which", &["wget"])
|
||||
.await
|
||||
.is_ok()
|
||||
{
|
||||
return Err(e);
|
||||
} else {
|
||||
anyhow::bail!("Neither curl nor wget is available");
|
||||
@@ -703,7 +734,11 @@ impl SshRemoteConnection {
|
||||
) -> Result<()> {
|
||||
if let Some(parent) = tmp_path_gz.parent() {
|
||||
self.socket
|
||||
.run_command("mkdir", &["-p", parent.display(self.path_style()).as_ref()])
|
||||
.run_command(
|
||||
self.ssh_shell_kind,
|
||||
"mkdir",
|
||||
&["-p", parent.display(self.path_style()).as_ref()],
|
||||
)
|
||||
.await?;
|
||||
}
|
||||
|
||||
@@ -750,7 +785,7 @@ impl SshRemoteConnection {
|
||||
format!("chmod {server_mode} {orig_tmp_path} && mv {orig_tmp_path} {dst_path}",)
|
||||
};
|
||||
let args = shell_kind.args_for_shell(false, script.to_string());
|
||||
self.socket.run_command("sh", &args).await?;
|
||||
self.socket.run_command(shell_kind, "sh", &args).await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -894,11 +929,16 @@ impl SshSocket {
|
||||
// Furthermore, some setups (e.g. Coder) will change directory when SSH'ing
|
||||
// into a machine. You must use `cd` to get back to $HOME.
|
||||
// You need to do it like this: $ ssh host "cd; sh -c 'ls -l /tmp'"
|
||||
fn ssh_command(&self, program: &str, args: &[impl AsRef<str>]) -> process::Command {
|
||||
let shell_kind = ShellKind::Posix;
|
||||
fn ssh_command(
|
||||
&self,
|
||||
shell_kind: ShellKind,
|
||||
program: &str,
|
||||
args: &[impl AsRef<str>],
|
||||
) -> process::Command {
|
||||
let mut command = util::command::new_smol_command("ssh");
|
||||
let program = shell_kind.prepend_command_prefix(program);
|
||||
let mut to_run = shell_kind
|
||||
.try_quote(program)
|
||||
.try_quote_prefix_aware(&program)
|
||||
.expect("shell quoting")
|
||||
.into_owned();
|
||||
for arg in args {
|
||||
@@ -920,8 +960,13 @@ impl SshSocket {
|
||||
command
|
||||
}
|
||||
|
||||
async fn run_command(&self, program: &str, args: &[impl AsRef<str>]) -> Result<String> {
|
||||
let output = self.ssh_command(program, args).output().await?;
|
||||
async fn run_command(
|
||||
&self,
|
||||
shell_kind: ShellKind,
|
||||
program: &str,
|
||||
args: &[impl AsRef<str>],
|
||||
) -> Result<String> {
|
||||
let output = self.ssh_command(shell_kind, program, args).output().await?;
|
||||
anyhow::ensure!(
|
||||
output.status.success(),
|
||||
"failed to run command: {}",
|
||||
@@ -994,12 +1039,7 @@ impl SshSocket {
|
||||
}
|
||||
|
||||
async fn platform(&self, shell: ShellKind) -> Result<RemotePlatform> {
|
||||
let program = if shell == ShellKind::Nushell {
|
||||
"^uname"
|
||||
} else {
|
||||
"uname"
|
||||
};
|
||||
let uname = self.run_command(program, &["-sm"]).await?;
|
||||
let uname = self.run_command(shell, "uname", &["-sm"]).await?;
|
||||
let Some((os, arch)) = uname.split_once(" ") else {
|
||||
anyhow::bail!("unknown uname: {uname:?}")
|
||||
};
|
||||
@@ -1030,7 +1070,10 @@ impl SshSocket {
|
||||
}
|
||||
|
||||
async fn shell(&self) -> String {
|
||||
match self.run_command("sh", &["-c", "echo $SHELL"]).await {
|
||||
match self
|
||||
.run_command(ShellKind::Posix, "sh", &["-c", "echo $SHELL"])
|
||||
.await
|
||||
{
|
||||
Ok(shell) => shell.trim().to_owned(),
|
||||
Err(e) => {
|
||||
log::error!("Failed to get shell: {e}");
|
||||
@@ -1256,11 +1299,11 @@ fn build_command(
|
||||
ssh_env: HashMap<String, String>,
|
||||
ssh_path_style: PathStyle,
|
||||
ssh_shell: &str,
|
||||
ssh_shell_kind: ShellKind,
|
||||
ssh_args: Vec<String>,
|
||||
) -> Result<CommandTemplate> {
|
||||
use std::fmt::Write as _;
|
||||
|
||||
let shell_kind = ShellKind::new(ssh_shell, false);
|
||||
let mut exec = String::new();
|
||||
if let Some(working_dir) = working_dir {
|
||||
let working_dir = RemotePathBuf::new(working_dir, ssh_path_style).to_string();
|
||||
@@ -1270,12 +1313,24 @@ fn build_command(
|
||||
const TILDE_PREFIX: &'static str = "~/";
|
||||
if working_dir.starts_with(TILDE_PREFIX) {
|
||||
let working_dir = working_dir.trim_start_matches("~").trim_start_matches("/");
|
||||
write!(exec, "cd \"$HOME/{working_dir}\" && ",)?;
|
||||
write!(
|
||||
exec,
|
||||
"cd \"$HOME/{working_dir}\" {} ",
|
||||
ssh_shell_kind.sequential_and_commands_separator()
|
||||
)?;
|
||||
} else {
|
||||
write!(exec, "cd \"{working_dir}\" && ",)?;
|
||||
write!(
|
||||
exec,
|
||||
"cd \"{working_dir}\" {} ",
|
||||
ssh_shell_kind.sequential_and_commands_separator()
|
||||
)?;
|
||||
}
|
||||
} else {
|
||||
write!(exec, "cd && ")?;
|
||||
write!(
|
||||
exec,
|
||||
"cd {} ",
|
||||
ssh_shell_kind.sequential_and_commands_separator()
|
||||
)?;
|
||||
};
|
||||
write!(exec, "exec env ")?;
|
||||
|
||||
@@ -1284,7 +1339,7 @@ fn build_command(
|
||||
exec,
|
||||
"{}={} ",
|
||||
k,
|
||||
shell_kind.try_quote(v).context("shell quoting")?
|
||||
ssh_shell_kind.try_quote(v).context("shell quoting")?
|
||||
)?;
|
||||
}
|
||||
|
||||
@@ -1292,12 +1347,12 @@ fn build_command(
|
||||
write!(
|
||||
exec,
|
||||
"{}",
|
||||
shell_kind
|
||||
.try_quote(&input_program)
|
||||
ssh_shell_kind
|
||||
.try_quote_prefix_aware(&input_program)
|
||||
.context("shell quoting")?
|
||||
)?;
|
||||
for arg in input_args {
|
||||
let arg = shell_kind.try_quote(&arg).context("shell quoting")?;
|
||||
let arg = ssh_shell_kind.try_quote(&arg).context("shell quoting")?;
|
||||
write!(exec, " {}", &arg)?;
|
||||
}
|
||||
} else {
|
||||
@@ -1341,6 +1396,7 @@ mod tests {
|
||||
env.clone(),
|
||||
PathStyle::Posix,
|
||||
"/bin/fish",
|
||||
ShellKind::Fish,
|
||||
vec!["-p".to_string(), "2222".to_string()],
|
||||
)?;
|
||||
|
||||
@@ -1370,6 +1426,7 @@ mod tests {
|
||||
env.clone(),
|
||||
PathStyle::Posix,
|
||||
"/bin/fish",
|
||||
ShellKind::Fish,
|
||||
vec!["-p".to_string(), "2222".to_string()],
|
||||
)?;
|
||||
|
||||
|
||||
@@ -44,6 +44,7 @@ pub(crate) struct WslRemoteConnection {
|
||||
remote_binary_path: Option<Arc<RelPath>>,
|
||||
platform: RemotePlatform,
|
||||
shell: String,
|
||||
shell_kind: ShellKind,
|
||||
default_system_shell: String,
|
||||
connection_options: WslConnectionOptions,
|
||||
can_exec: bool,
|
||||
@@ -73,16 +74,17 @@ impl WslRemoteConnection {
|
||||
remote_binary_path: None,
|
||||
platform: RemotePlatform { os: "", arch: "" },
|
||||
shell: String::new(),
|
||||
shell_kind: ShellKind::Posix,
|
||||
default_system_shell: String::from("/bin/sh"),
|
||||
can_exec: true,
|
||||
};
|
||||
delegate.set_status(Some("Detecting WSL environment"), cx);
|
||||
this.shell = this.detect_shell().await?;
|
||||
let shell = ShellKind::new(&this.shell, false);
|
||||
this.can_exec = this.detect_can_exec(shell).await?;
|
||||
this.platform = this.detect_platform(shell).await?;
|
||||
this.shell_kind = ShellKind::new(&this.shell, false);
|
||||
this.can_exec = this.detect_can_exec().await?;
|
||||
this.platform = this.detect_platform().await?;
|
||||
this.remote_binary_path = Some(
|
||||
this.ensure_server_binary(&delegate, release_channel, version, commit, shell, cx)
|
||||
this.ensure_server_binary(&delegate, release_channel, version, commit, cx)
|
||||
.await?,
|
||||
);
|
||||
log::debug!("Detected WSL environment: {this:#?}");
|
||||
@@ -90,20 +92,16 @@ impl WslRemoteConnection {
|
||||
Ok(this)
|
||||
}
|
||||
|
||||
async fn detect_can_exec(&self, shell: ShellKind) -> Result<bool> {
|
||||
async fn detect_can_exec(&self) -> Result<bool> {
|
||||
let options = &self.connection_options;
|
||||
let program = if shell == ShellKind::Nushell {
|
||||
"^uname"
|
||||
} else {
|
||||
"uname"
|
||||
};
|
||||
let program = self.shell_kind.prepend_command_prefix("uname");
|
||||
let args = &["-m"];
|
||||
let output = wsl_command_impl(options, program, args, true)
|
||||
let output = wsl_command_impl(options, &program, args, true)
|
||||
.output()
|
||||
.await?;
|
||||
|
||||
if !output.status.success() {
|
||||
let output = wsl_command_impl(options, program, args, false)
|
||||
let output = wsl_command_impl(options, &program, args, false)
|
||||
.output()
|
||||
.await?;
|
||||
|
||||
@@ -120,14 +118,9 @@ impl WslRemoteConnection {
|
||||
Ok(true)
|
||||
}
|
||||
}
|
||||
async fn detect_platform(&self, shell: ShellKind) -> Result<RemotePlatform> {
|
||||
let arch_str = if shell == ShellKind::Nushell {
|
||||
// https://github.com/nushell/nushell/issues/12570
|
||||
self.run_wsl_command("sh", &["-c", "uname -m"])
|
||||
} else {
|
||||
self.run_wsl_command("uname", &["-m"])
|
||||
}
|
||||
.await?;
|
||||
async fn detect_platform(&self) -> Result<RemotePlatform> {
|
||||
let program = self.shell_kind.prepend_command_prefix("uname");
|
||||
let arch_str = self.run_wsl_command(&program, &["-m"]).await?;
|
||||
let arch_str = arch_str.trim().to_string();
|
||||
let arch = match arch_str.as_str() {
|
||||
"x86_64" => "x86_64",
|
||||
@@ -163,7 +156,6 @@ impl WslRemoteConnection {
|
||||
release_channel: ReleaseChannel,
|
||||
version: SemanticVersion,
|
||||
commit: Option<AppCommitSha>,
|
||||
shell: ShellKind,
|
||||
cx: &mut AsyncApp,
|
||||
) -> Result<Arc<RelPath>> {
|
||||
let version_str = match release_channel {
|
||||
@@ -186,12 +178,9 @@ impl WslRemoteConnection {
|
||||
|
||||
if let Some(parent) = dst_path.parent() {
|
||||
let parent = parent.display(PathStyle::Posix);
|
||||
if shell == ShellKind::Nushell {
|
||||
self.run_wsl_command("mkdir", &[&parent]).await
|
||||
} else {
|
||||
self.run_wsl_command("mkdir", &["-p", &parent]).await
|
||||
}
|
||||
.map_err(|e| anyhow!("Failed to create directory: {}", e))?;
|
||||
self.run_wsl_command("mkdir", &["-p", &parent])
|
||||
.await
|
||||
.map_err(|e| anyhow!("Failed to create directory: {}", e))?;
|
||||
}
|
||||
|
||||
#[cfg(debug_assertions)]
|
||||
@@ -206,7 +195,7 @@ impl WslRemoteConnection {
|
||||
))
|
||||
.unwrap(),
|
||||
);
|
||||
self.upload_file(&remote_server_path, &tmp_path, delegate, &shell, cx)
|
||||
self.upload_file(&remote_server_path, &tmp_path, delegate, cx)
|
||||
.await?;
|
||||
self.extract_and_install(&tmp_path, &dst_path, delegate, cx)
|
||||
.await?;
|
||||
@@ -239,8 +228,7 @@ impl WslRemoteConnection {
|
||||
);
|
||||
let tmp_path = RelPath::unix(&tmp_path).unwrap();
|
||||
|
||||
self.upload_file(&src_path, &tmp_path, delegate, &shell, cx)
|
||||
.await?;
|
||||
self.upload_file(&src_path, &tmp_path, delegate, cx).await?;
|
||||
self.extract_and_install(&tmp_path, &dst_path, delegate, cx)
|
||||
.await?;
|
||||
|
||||
@@ -252,19 +240,15 @@ impl WslRemoteConnection {
|
||||
src_path: &Path,
|
||||
dst_path: &RelPath,
|
||||
delegate: &Arc<dyn RemoteClientDelegate>,
|
||||
shell: &ShellKind,
|
||||
cx: &mut AsyncApp,
|
||||
) -> Result<()> {
|
||||
delegate.set_status(Some("Uploading remote server to WSL"), cx);
|
||||
|
||||
if let Some(parent) = dst_path.parent() {
|
||||
let parent = parent.display(PathStyle::Posix);
|
||||
if *shell == ShellKind::Nushell {
|
||||
self.run_wsl_command("mkdir", &[&parent]).await
|
||||
} else {
|
||||
self.run_wsl_command("mkdir", &["-p", &parent]).await
|
||||
}
|
||||
.map_err(|e| anyhow!("Failed to create directory when uploading file: {}", e))?;
|
||||
self.run_wsl_command("mkdir", &["-p", &parent])
|
||||
.await
|
||||
.map_err(|e| anyhow!("Failed to create directory when uploading file: {}", e))?;
|
||||
}
|
||||
|
||||
let t0 = Instant::now();
|
||||
@@ -441,7 +425,7 @@ impl RemoteConnection for WslRemoteConnection {
|
||||
bail!("WSL shares the network interface with the host system");
|
||||
}
|
||||
|
||||
let shell_kind = ShellKind::new(&self.shell, false);
|
||||
let shell_kind = self.shell_kind;
|
||||
let working_dir = working_dir
|
||||
.map(|working_dir| RemotePathBuf::new(working_dir, PathStyle::Posix).to_string())
|
||||
.unwrap_or("~".to_string());
|
||||
@@ -461,7 +445,9 @@ impl RemoteConnection for WslRemoteConnection {
|
||||
write!(
|
||||
exec,
|
||||
"{}",
|
||||
shell_kind.try_quote(&program).context("shell quoting")?
|
||||
shell_kind
|
||||
.try_quote_prefix_aware(&program)
|
||||
.context("shell quoting")?
|
||||
)?;
|
||||
for arg in args {
|
||||
let arg = shell_kind.try_quote(&arg).context("shell quoting")?;
|
||||
|
||||
Reference in New Issue
Block a user