SSH remoting: terminal & tasks (#15321)

This also rolls back the `TerminalWorkDir` abstraction I added for the
original remoting, and tidies up the terminal creation code to be clear
about whether we're creating a task *or* a terminal. The previous logic
was a little muddy because it assumed we could be doing both at the same
time (which was not true).

Release Notes:

- remoting alpha: Removed the ability to specify `gh cs ssh` or `gcloud
compute ssh` etc. See https://zed.dev/docs/remote-development for
alternatives.
- remoting alpha: Added support for terminal and tasks to new
experimental ssh remoting
This commit is contained in:
Conrad Irwin 2024-07-28 22:45:00 -06:00 committed by GitHub
parent 26d0a33e79
commit 583b6235fb
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
10 changed files with 404 additions and 454 deletions

View file

@ -1,21 +1,18 @@
use crate::Project;
use anyhow::Context as _;
use collections::HashMap;
use gpui::{
AnyWindowHandle, AppContext, Context, Entity, Model, ModelContext, SharedString, WeakModel,
};
use gpui::{AnyWindowHandle, AppContext, Context, Entity, Model, ModelContext, WeakModel};
use itertools::Itertools;
use settings::{Settings, SettingsLocation};
use smol::channel::bounded;
use std::{
env,
fs::File,
io::Write,
env::{self},
iter,
path::{Path, PathBuf},
};
use task::{Shell, SpawnInTerminal, TerminalWorkDir};
use task::{Shell, SpawnInTerminal};
use terminal::{
terminal_settings::{self, TerminalSettings, VenvSettingsContent},
terminal_settings::{self, TerminalSettings},
TaskState, TaskStatus, Terminal, TerminalBuilder,
};
use util::ResultExt;
@ -27,21 +24,53 @@ pub struct Terminals {
pub(crate) local_handles: Vec<WeakModel<terminal::Terminal>>,
}
#[derive(Debug, Clone)]
pub struct ConnectRemoteTerminal {
pub ssh_connection_string: SharedString,
pub project_path: SharedString,
/// Terminals are opened either for the users shell, or to run a task.
#[allow(clippy::large_enum_variant)]
#[derive(Debug)]
pub enum TerminalKind {
/// Run a shell at the given path (or $HOME if None)
Shell(Option<PathBuf>),
/// Run a task.
Task(SpawnInTerminal),
}
/// SshCommand describes how to connect to a remote server
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum SshCommand {
/// DevServers give a string from the user
DevServer(String),
/// Direct ssh has a list of arguments to pass to ssh
Direct(Vec<String>),
}
impl Project {
pub fn terminal_work_dir_for(
&self,
pathbuf: Option<&Path>,
cx: &AppContext,
) -> Option<TerminalWorkDir> {
if self.is_local() {
return Some(TerminalWorkDir::Local(pathbuf?.to_owned()));
pub fn active_project_directory(&self, cx: &AppContext) -> Option<PathBuf> {
let worktree = self
.active_entry()
.and_then(|entry_id| self.worktree_for_entry(entry_id, cx))
.or_else(|| self.worktrees(cx).next())?;
let worktree = worktree.read(cx);
if !worktree.root_entry()?.is_dir() {
return None;
}
Some(worktree.abs_path().to_path_buf())
}
pub fn first_project_directory(&self, cx: &AppContext) -> Option<PathBuf> {
let worktree = self.worktrees(cx).next()?;
let worktree = worktree.read(cx);
if worktree.root_entry()?.is_dir() {
return Some(worktree.abs_path().to_path_buf());
} else {
None
}
}
fn ssh_command(&self, cx: &AppContext) -> Option<SshCommand> {
if let Some(ssh_session) = self.ssh_session.as_ref() {
return Some(SshCommand::Direct(ssh_session.ssh_args()));
}
let dev_server_project_id = self.dev_server_project_id()?;
let projects_store = dev_server_projects::Store::global(cx).read(cx);
let ssh_command = projects_store
@ -49,133 +78,132 @@ impl Project {
.ssh_connection_string
.as_ref()?
.to_string();
let path = if let Some(pathbuf) = pathbuf {
pathbuf.to_string_lossy().to_string()
} else {
projects_store
.dev_server_project(dev_server_project_id)?
.paths
.get(0)
.unwrap()
.to_string()
};
Some(TerminalWorkDir::Ssh {
ssh_command,
path: Some(path),
})
Some(SshCommand::DevServer(ssh_command))
}
pub fn create_terminal(
&mut self,
working_directory: Option<TerminalWorkDir>,
spawn_task: Option<SpawnInTerminal>,
kind: TerminalKind,
window: AnyWindowHandle,
cx: &mut ModelContext<Self>,
) -> anyhow::Result<Model<Terminal>> {
// used only for TerminalSettings::get
let worktree = {
let terminal_cwd = working_directory.as_ref().and_then(|cwd| cwd.local_path());
let task_cwd = spawn_task
.as_ref()
.and_then(|spawn_task| spawn_task.cwd.as_ref())
.and_then(|cwd| cwd.local_path());
terminal_cwd
.and_then(|terminal_cwd| self.find_worktree(&terminal_cwd, cx))
.or_else(|| task_cwd.and_then(|spawn_cwd| self.find_worktree(&spawn_cwd, cx)))
let path = match &kind {
TerminalKind::Shell(path) => path.as_ref().map(|path| path.to_path_buf()),
TerminalKind::Task(spawn_task) => {
if let Some(cwd) = &spawn_task.cwd {
Some(cwd.clone())
} else {
self.active_project_directory(cx)
}
}
};
let ssh_command = self.ssh_command(cx);
let settings_location = worktree.as_ref().map(|(worktree, path)| SettingsLocation {
worktree_id: worktree.read(cx).id().to_usize(),
path,
});
let is_terminal = spawn_task.is_none()
&& working_directory
.as_ref()
.map_or(true, |work_dir| work_dir.is_local());
let mut settings_location = None;
if let Some(path) = path.as_ref() {
if let Some((worktree, _)) = self.find_worktree(path, cx) {
settings_location = Some(SettingsLocation {
worktree_id: worktree.read(cx).id().to_usize(),
path,
});
}
}
let settings = TerminalSettings::get(settings_location, cx);
let python_settings = settings.detect_venv.clone();
let (completion_tx, completion_rx) = bounded(1);
let mut env = settings.env.clone();
// Alacritty uses parent project's working directory when no working directory is provided
// https://github.com/alacritty/alacritty/blob/fd1a3cc79192d1d03839f0fd8c72e1f8d0fce42e/extra/man/alacritty.5.scd?plain=1#L47-L52
let mut retained_script = None;
let venv_base_directory = working_directory
let local_path = if ssh_command.is_none() {
path.clone()
} else {
None
};
let python_venv_directory = path
.as_ref()
.and_then(|cwd| cwd.local_path())
.unwrap_or_else(|| Path::new(""));
.and_then(|path| self.python_venv_directory(path, settings, cx));
let mut python_venv_activate_command = None;
let (spawn_task, shell) = match working_directory.as_ref() {
Some(TerminalWorkDir::Ssh { ssh_command, path }) => {
log::debug!("Connecting to a remote server: {ssh_command:?}");
let tmp_dir = tempfile::tempdir()?;
let ssh_shell_result = prepare_ssh_shell(
&mut env,
tmp_dir.path(),
spawn_task.as_ref(),
ssh_command,
path.as_deref(),
);
retained_script = Some(tmp_dir);
let ssh_shell = ssh_shell_result?;
let (spawn_task, shell) = match kind {
TerminalKind::Shell(_) => {
if let Some(python_venv_directory) = python_venv_directory {
python_venv_activate_command =
self.python_activate_command(&python_venv_directory, settings);
}
(
spawn_task.map(|spawn_task| TaskState {
id: spawn_task.id,
full_label: spawn_task.full_label,
label: spawn_task.label,
command_label: spawn_task.command_label,
hide: spawn_task.hide,
status: TaskStatus::Running,
completion_rx,
}),
ssh_shell,
)
}
_ => {
if let Some(spawn_task) = spawn_task {
log::debug!("Spawning task: {spawn_task:?}");
env.extend(spawn_task.env);
// Activate minimal Python virtual environment
if let Some(python_settings) = &python_settings.as_option() {
self.set_python_venv_path_for_tasks(
python_settings,
&venv_base_directory,
&mut env,
);
match &ssh_command {
Some(ssh_command) => {
log::debug!("Connecting to a remote server: {ssh_command:?}");
// Alacritty sets its terminfo to `alacritty`, this requiring hosts to have it installed
// to properly display colors.
// We do not have the luxury of assuming the host has it installed,
// so we set it to a default that does not break the highlighting via ssh.
env.entry("TERM".to_string())
.or_insert_with(|| "xterm-256color".to_string());
let (program, args) =
wrap_for_ssh(ssh_command, None, path.as_deref(), env, None);
env = HashMap::default();
(None, Shell::WithArguments { program, args })
}
None => (None, settings.shell.clone()),
}
}
TerminalKind::Task(spawn_task) => {
let task_state = Some(TaskState {
id: spawn_task.id,
full_label: spawn_task.full_label,
label: spawn_task.label,
command_label: spawn_task.command_label,
hide: spawn_task.hide,
status: TaskStatus::Running,
completion_rx,
});
env.extend(spawn_task.env);
if let Some(venv_path) = &python_venv_directory {
env.insert(
"VIRTUAL_ENV".to_string(),
venv_path.to_string_lossy().to_string(),
);
}
match &ssh_command {
Some(ssh_command) => {
log::debug!("Connecting to a remote server: {ssh_command:?}");
env.entry("TERM".to_string())
.or_insert_with(|| "xterm-256color".to_string());
let (program, args) = wrap_for_ssh(
ssh_command,
Some((&spawn_task.command, &spawn_task.args)),
path.as_deref(),
env,
python_venv_directory,
);
env = HashMap::default();
(task_state, Shell::WithArguments { program, args })
}
None => {
if let Some(venv_path) = &python_venv_directory {
add_environment_path(&mut env, &venv_path.join("bin")).log_err();
}
(
task_state,
Shell::WithArguments {
program: spawn_task.command,
args: spawn_task.args,
},
)
}
(
Some(TaskState {
id: spawn_task.id,
full_label: spawn_task.full_label,
label: spawn_task.label,
command_label: spawn_task.command_label,
hide: spawn_task.hide,
status: TaskStatus::Running,
completion_rx,
}),
Shell::WithArguments {
program: spawn_task.command,
args: spawn_task.args,
},
)
} else {
(None, settings.shell.clone())
}
}
};
let terminal = TerminalBuilder::new(
working_directory
.as_ref()
.and_then(|cwd| cwd.local_path())
.map(ToOwned::to_owned),
local_path,
spawn_task,
shell,
env,
@ -195,7 +223,6 @@ impl Project {
let id = terminal_handle.entity_id();
cx.observe_release(&terminal_handle, move |project, _terminal, cx| {
drop(retained_script);
let handles = &mut project.terminals.local_handles;
if let Some(index) = handles
@ -208,20 +235,8 @@ impl Project {
})
.detach();
// if the terminal is not a task, activate full Python virtual environment
if is_terminal {
if let Some(python_settings) = &python_settings.as_option() {
if let Some(activate_script_path) =
self.find_activate_script_path(python_settings, &venv_base_directory)
{
self.activate_python_virtual_environment(
Project::get_activate_command(python_settings),
activate_script_path,
&terminal_handle,
cx,
);
}
}
if let Some(activate_command) = python_venv_activate_command {
self.activate_python_virtual_environment(activate_command, &terminal_handle, cx);
}
terminal_handle
});
@ -229,80 +244,58 @@ impl Project {
terminal
}
pub fn find_activate_script_path(
&mut self,
settings: &VenvSettingsContent,
venv_base_directory: &Path,
pub fn python_venv_directory(
&self,
abs_path: &Path,
settings: &TerminalSettings,
cx: &AppContext,
) -> Option<PathBuf> {
let activate_script_name = match settings.activate_script {
let venv_settings = settings.detect_venv.as_option()?;
venv_settings
.directories
.into_iter()
.map(|virtual_environment_name| abs_path.join(virtual_environment_name))
.find(|venv_path| {
self.find_worktree(&venv_path, cx)
.and_then(|(worktree, relative_path)| {
worktree.read(cx).entry_for_path(&relative_path)
})
.is_some()
})
}
fn python_activate_command(
&self,
venv_base_directory: &Path,
settings: &TerminalSettings,
) -> Option<String> {
let venv_settings = settings.detect_venv.as_option()?;
let activate_script_name = match venv_settings.activate_script {
terminal_settings::ActivateScript::Default => "activate",
terminal_settings::ActivateScript::Csh => "activate.csh",
terminal_settings::ActivateScript::Fish => "activate.fish",
terminal_settings::ActivateScript::Nushell => "activate.nu",
};
let path = venv_base_directory
.join("bin")
.join(activate_script_name)
.to_string_lossy()
.to_string();
let quoted = shlex::try_quote(&path).ok()?;
settings
.directories
.into_iter()
.find_map(|virtual_environment_name| {
let path = venv_base_directory
.join(virtual_environment_name)
.join("bin")
.join(activate_script_name);
path.exists().then_some(path)
})
}
pub fn set_python_venv_path_for_tasks(
&mut self,
settings: &VenvSettingsContent,
venv_base_directory: &Path,
env: &mut HashMap<String, String>,
) {
let activate_path = settings
.directories
.into_iter()
.find_map(|virtual_environment_name| {
let path = venv_base_directory.join(virtual_environment_name);
path.exists().then_some(path)
});
if let Some(path) = activate_path {
// Some tools use VIRTUAL_ENV to detect the virtual environment
env.insert(
"VIRTUAL_ENV".to_string(),
path.to_string_lossy().to_string(),
);
// We need to set the PATH to include the virtual environment's bin directory
add_environment_path(env, &path.join("bin")).log_err();
}
}
fn get_activate_command(settings: &VenvSettingsContent) -> &'static str {
match settings.activate_script {
terminal_settings::ActivateScript::Nushell => "overlay use",
_ => "source",
}
Some(match venv_settings.activate_script {
terminal_settings::ActivateScript::Nushell => format!("overlay use {}\n", quoted),
_ => format!("source {}\n", quoted),
})
}
fn activate_python_virtual_environment(
&mut self,
activate_command: &'static str,
activate_script: PathBuf,
&self,
command: String,
terminal_handle: &Model<Terminal>,
cx: &mut ModelContext<Project>,
) {
// Paths are not strings so we need to jump through some hoops to format the command without `format!`
let mut command = Vec::from(activate_command.as_bytes());
command.push(b' ');
// Wrapping path in double quotes to catch spaces in folder name
command.extend_from_slice(b"\"");
command.extend_from_slice(activate_script.as_os_str().as_encoded_bytes());
command.extend_from_slice(b"\"");
command.push(b'\n');
terminal_handle.update(cx, |this, _| this.input_bytes(command));
terminal_handle.update(cx, |this, _| this.input_bytes(command.into_bytes()));
}
pub fn local_terminal_handles(&self) -> &Vec<WeakModel<terminal::Terminal>> {
@ -310,65 +303,55 @@ impl Project {
}
}
fn prepare_ssh_shell(
env: &mut HashMap<String, String>,
tmp_dir: &Path,
spawn_task: Option<&SpawnInTerminal>,
ssh_command: &str,
path: Option<&str>,
) -> anyhow::Result<Shell> {
// Alacritty sets its terminfo to `alacritty`, this requiring hosts to have it installed
// to properly display colors.
// We do not have the luxury of assuming the host has it installed,
// so we set it to a default that does not break the highlighting via ssh.
env.entry("TERM".to_string())
.or_insert_with(|| "xterm-256color".to_string());
let real_ssh = which::which("ssh")?;
let ssh_path = tmp_dir.join("ssh");
let mut ssh_file = File::create(&ssh_path)?;
let to_run = if let Some(spawn_task) = spawn_task {
Some(shlex::try_quote(&spawn_task.command)?)
.into_iter()
.chain(
spawn_task
.args
.iter()
.filter_map(|arg| shlex::try_quote(arg).ok()),
)
pub fn wrap_for_ssh(
ssh_command: &SshCommand,
command: Option<(&String, &Vec<String>)>,
path: Option<&Path>,
env: HashMap<String, String>,
venv_directory: Option<PathBuf>,
) -> (String, Vec<String>) {
let to_run = if let Some((command, args)) = command {
iter::once(command)
.chain(args)
.filter_map(|arg| shlex::try_quote(arg).ok())
.join(" ")
} else {
"exec $SHELL -l".to_string()
"exec ${SHELL:-sh} -l".to_string()
};
let mut env_changes = String::new();
for (k, v) in env.iter() {
if let Some((k, v)) = shlex::try_quote(k).ok().zip(shlex::try_quote(v).ok()) {
env_changes.push_str(&format!("{}={} ", k, v));
}
}
if let Some(venv_directory) = venv_directory {
if let Some(str) = shlex::try_quote(venv_directory.to_string_lossy().as_ref()).ok() {
env_changes.push_str(&format!("PATH={}:$PATH ", str));
}
}
let commands = if let Some(path) = path {
format!("cd {path}; {to_run}")
format!("cd {:?}; {} {}", path, env_changes, to_run)
} else {
format!("cd; {to_run}")
format!("cd; {env_changes} {to_run}")
};
let shell_invocation = &format!("sh -c {}", shlex::try_quote(&commands)?);
let shell_invocation = format!("sh -c {}", shlex::try_quote(&commands).unwrap());
// To support things like `gh cs ssh`/`coder ssh`, we run whatever command
// you have configured, but place our custom script on the path so that it will
// be run instead.
write!(
&mut ssh_file,
"#!/bin/sh\nexec {} \"$@\" {} {}",
real_ssh.to_string_lossy(),
if spawn_task.is_none() { "-t" } else { "" },
shlex::try_quote(shell_invocation)?,
)?;
let (program, mut args) = match ssh_command {
SshCommand::DevServer(ssh_command) => {
let mut args = shlex::split(&ssh_command).unwrap_or_default();
let program = args.drain(0..1).next().unwrap_or("ssh".to_string());
(program, args)
}
SshCommand::Direct(ssh_args) => ("ssh".to_string(), ssh_args.clone()),
};
// todo(windows)
#[cfg(not(target_os = "windows"))]
std::fs::set_permissions(ssh_path, smol::fs::unix::PermissionsExt::from_mode(0o755))?;
add_environment_path(env, tmp_dir)?;
let mut args = shlex::split(&ssh_command).unwrap_or_default();
let program = args.drain(0..1).next().unwrap_or("ssh".to_string());
Ok(Shell::WithArguments { program, args })
if command.is_none() {
args.push("-t".to_string())
}
args.push(shell_invocation);
(program, args)
}
fn add_environment_path(env: &mut HashMap<String, String>, new_path: &Path) -> anyhow::Result<()> {