Add the ability to edit remote directories over SSH (#14530)

This is a first step towards allowing you to edit remote projects
directly over SSH. We'll start with a pretty bare-bones feature set, and
incrementally add further features.

### Todo

Distribution
* [x] Build nightly releases of `zed-remote-server` binaries
    * [x] linux (arm + x86)
    * [x] mac (arm + x86)
* [x] Build stable + preview releases of `zed-remote-server`
* [x] download and cache remote server binaries as needed when opening
ssh project
* [x] ensure server has the latest version of the binary


Auth
* [x] allow specifying password at the command line
* [x] auth via ssh keys
* [x] UI password prompt

Features
* [x] upload remote server binary to server automatically
* [x] opening directories
* [x] tracking file system updates
* [x] opening, editing, saving buffers
* [ ] file operations (rename, delete, create)
* [ ] git diffs
* [ ] project search

Release Notes:

- N/A

---------

Co-authored-by: Piotr Osiewicz <24362066+osiewicz@users.noreply.github.com>
This commit is contained in:
Max Brunsfeld 2024-07-19 10:27:26 -07:00 committed by GitHub
parent 7733bf686b
commit b9a53ffa0b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
50 changed files with 2194 additions and 250 deletions

View file

@ -46,7 +46,7 @@ use welcome::{show_welcome_view, BaseKeymap, FIRST_OPEN};
use workspace::{AppState, WorkspaceSettings, WorkspaceStore};
use zed::{
app_menus, build_window_options, handle_cli_connection, handle_keymap_file_changes,
initialize_workspace, open_paths_with_positions, OpenListener, OpenRequest,
initialize_workspace, open_paths_with_positions, open_ssh_paths, OpenListener, OpenRequest,
};
use crate::zed::inline_completion_registry;
@ -520,6 +520,21 @@ fn handle_open_request(request: OpenRequest, app_state: Arc<AppState>, cx: &mut
return;
};
if let Some(connection_info) = request.ssh_connection {
cx.spawn(|mut cx| async move {
open_ssh_paths(
connection_info,
request.open_paths,
app_state,
workspace::OpenOptions::default(),
&mut cx,
)
.await
})
.detach_and_log_err(cx);
return;
}
let mut task = None;
if !request.open_paths.is_empty() {
let app_state = app_state.clone();
@ -890,7 +905,10 @@ fn parse_url_arg(arg: &str, cx: &AppContext) -> Result<String> {
match std::fs::canonicalize(Path::new(&arg)) {
Ok(path) => Ok(format!("file://{}", path.to_string_lossy())),
Err(error) => {
if arg.starts_with("file://") || arg.starts_with("zed-cli://") {
if arg.starts_with("file://")
|| arg.starts_with("zed-cli://")
|| arg.starts_with("ssh://")
{
Ok(arg.into())
} else if let Some(_) = parse_zed_link(&arg, cx) {
Ok(arg.into())

View file

@ -5,6 +5,7 @@ pub(crate) mod linux_prompts;
#[cfg(not(target_os = "linux"))]
pub(crate) mod only_instance;
mod open_listener;
mod password_prompt;
pub use app_menus::*;
use breadcrumbs::Breadcrumbs;

View file

@ -1,4 +1,6 @@
use crate::{handle_open_request, init_headless, init_ui, zed::password_prompt::PasswordPrompt};
use anyhow::{anyhow, Context, Result};
use auto_update::AutoUpdater;
use cli::{ipc, IpcHandshake};
use cli::{ipc::IpcSender, CliRequest, CliResponse};
use client::parse_zed_link;
@ -9,8 +11,12 @@ use editor::Editor;
use futures::channel::mpsc::{UnboundedReceiver, UnboundedSender};
use futures::channel::{mpsc, oneshot};
use futures::{FutureExt, SinkExt, StreamExt};
use gpui::{AppContext, AsyncAppContext, Global, WindowHandle};
use gpui::{
AppContext, AsyncAppContext, Global, SemanticVersion, VisualContext as _, WindowHandle,
};
use language::{Bias, Point};
use release_channel::{AppVersion, ReleaseChannel};
use remote::SshPlatform;
use std::path::Path;
use std::path::PathBuf;
use std::sync::Arc;
@ -22,14 +28,21 @@ use welcome::{show_welcome_view, FIRST_OPEN};
use workspace::item::ItemHandle;
use workspace::{AppState, Workspace};
use crate::{handle_open_request, init_headless, init_ui};
#[derive(Default, Debug)]
pub struct OpenRequest {
pub cli_connection: Option<(mpsc::Receiver<CliRequest>, IpcSender<CliResponse>)>,
pub open_paths: Vec<PathLikeWithPosition<PathBuf>>,
pub open_channel_notes: Vec<(u64, Option<String>)>,
pub join_channel: Option<u64>,
pub ssh_connection: Option<SshConnectionInfo>,
}
#[derive(Debug, PartialEq, Eq)]
pub struct SshConnectionInfo {
pub username: String,
pub password: Option<String>,
pub host: String,
pub port: u16,
}
impl OpenRequest {
@ -42,6 +55,8 @@ impl OpenRequest {
this.parse_file_path(file)
} else if let Some(file) = url.strip_prefix("zed://file") {
this.parse_file_path(file)
} else if url.starts_with("ssh://") {
this.parse_ssh_file_path(&url)?
} else if let Some(request_path) = parse_zed_link(&url, cx) {
this.parse_request_path(request_path).log_err();
} else {
@ -62,6 +77,37 @@ impl OpenRequest {
}
}
fn parse_ssh_file_path(&mut self, file: &str) -> Result<()> {
let url = url::Url::parse(file)?;
let host = url
.host()
.ok_or_else(|| anyhow!("missing host in ssh url: {}", file))?
.to_string();
let username = url.username().to_string();
if username.is_empty() {
return Err(anyhow!("missing username in ssh url: {}", file));
}
let password = url.password().map(|s| s.to_string());
let port = url.port().unwrap_or(22);
if !self.open_paths.is_empty() {
return Err(anyhow!("cannot open both local and ssh paths"));
}
let connection = SshConnectionInfo {
username,
password,
host,
port,
};
if let Some(ssh_connection) = &self.ssh_connection {
if *ssh_connection != connection {
return Err(anyhow!("cannot open multiple ssh connections"));
}
}
self.ssh_connection = Some(connection);
self.parse_file_path(url.path());
Ok(())
}
fn parse_request_path(&mut self, request_path: &str) -> Result<()> {
let mut parts = request_path.split('/');
if parts.next() == Some("channel") {
@ -109,6 +155,95 @@ impl OpenListener {
}
}
struct SshClientDelegate {
window: WindowHandle<Workspace>,
known_password: Option<String>,
}
impl remote::SshClientDelegate for SshClientDelegate {
fn ask_password(
&self,
prompt: String,
cx: &mut AsyncAppContext,
) -> oneshot::Receiver<Result<String>> {
let (tx, rx) = oneshot::channel();
let mut known_password = self.known_password.clone();
self.window
.update(cx, |workspace, cx| {
cx.activate_window();
if let Some(password) = known_password.take() {
tx.send(Ok(password)).ok();
} else {
workspace.toggle_modal(cx, |cx| PasswordPrompt::new(prompt, tx, cx));
}
})
.unwrap();
rx
}
fn get_server_binary(
&self,
platform: SshPlatform,
cx: &mut AsyncAppContext,
) -> oneshot::Receiver<Result<(PathBuf, SemanticVersion)>> {
let (tx, rx) = oneshot::channel();
cx.spawn(|mut cx| async move {
tx.send(get_server_binary(platform, &mut cx).await).ok();
})
.detach();
rx
}
fn remote_server_binary_path(&self, cx: &mut AsyncAppContext) -> Result<PathBuf> {
let release_channel = cx.update(|cx| ReleaseChannel::global(cx))?;
Ok(format!(".local/zed-remote-server-{}", release_channel.dev_name()).into())
}
}
async fn get_server_binary(
platform: SshPlatform,
cx: &mut AsyncAppContext,
) -> Result<(PathBuf, SemanticVersion)> {
let (version, release_channel) =
cx.update(|cx| (AppVersion::global(cx), ReleaseChannel::global(cx)))?;
// In dev mode, build the remote server binary from source
#[cfg(debug_assertions)]
if crate::stdout_is_a_pty()
&& release_channel == ReleaseChannel::Dev
&& platform.arch == std::env::consts::ARCH
&& platform.os == std::env::consts::OS
{
use smol::process::{Command, Stdio};
log::info!("building remote server binary from source");
run_cmd(Command::new("cargo").args(["build", "--package", "remote_server"])).await?;
run_cmd(Command::new("strip").args(["target/debug/remote_server"])).await?;
run_cmd(Command::new("gzip").args(["-9", "-f", "target/debug/remote_server"])).await?;
let path = std::env::current_dir()?.join("target/debug/remote_server.gz");
return Ok((path, version));
async fn run_cmd(command: &mut Command) -> Result<()> {
let output = command.stderr(Stdio::inherit()).output().await?;
if !output.status.success() {
Err(anyhow!("failed to run command: {:?}", command))?;
}
Ok(())
}
}
let binary_path = AutoUpdater::get_latest_remote_server_release(
platform.os,
platform.arch,
release_channel,
cx,
)
.await?;
Ok((binary_path, version))
}
#[cfg(target_os = "linux")]
pub fn listen_for_cli_connections(opener: OpenListener) -> Result<()> {
use release_channel::RELEASE_CHANNEL_NAME;
@ -160,6 +295,72 @@ fn connect_to_cli(
Ok((async_request_rx, response_tx))
}
pub async fn open_ssh_paths(
connection_info: SshConnectionInfo,
paths: Vec<PathLikeWithPosition<PathBuf>>,
app_state: Arc<AppState>,
_open_options: workspace::OpenOptions,
cx: &mut AsyncAppContext,
) -> Result<()> {
let options = cx.update(|cx| (app_state.build_window_options)(None, cx))?;
let window = cx.open_window(options, |cx| {
let project = project::Project::local(
app_state.client.clone(),
app_state.node_runtime.clone(),
app_state.user_store.clone(),
app_state.languages.clone(),
app_state.fs.clone(),
cx,
);
cx.new_view(|cx| Workspace::new(None, project, app_state.clone(), cx))
})?;
let session = remote::SshSession::client(
connection_info.username,
connection_info.host,
connection_info.port,
Arc::new(SshClientDelegate {
window,
known_password: connection_info.password,
}),
cx,
)
.await;
if session.is_err() {
window.update(cx, |_, cx| cx.remove_window()).ok();
}
let session = session?;
let project = cx.update(|cx| {
project::Project::ssh(
session,
app_state.client.clone(),
app_state.node_runtime.clone(),
app_state.user_store.clone(),
app_state.languages.clone(),
app_state.fs.clone(),
cx,
)
})?;
for path in paths {
project
.update(cx, |project, cx| {
project.find_or_create_worktree(&path.path_like, true, cx)
})?
.await?;
}
window.update(cx, |_, cx| {
cx.replace_root_view(|cx| Workspace::new(None, project, app_state, cx))
})?;
window.update(cx, |_, cx| cx.activate_window())?;
Ok(())
}
pub async fn open_paths_with_positions(
path_likes: &Vec<PathLikeWithPosition<PathBuf>>,
app_state: Arc<AppState>,

View file

@ -0,0 +1,69 @@
use anyhow::Result;
use editor::Editor;
use futures::channel::oneshot;
use gpui::{
px, DismissEvent, EventEmitter, FocusableView, ParentElement as _, Render, SharedString, View,
};
use ui::{v_flex, InteractiveElement, Label, Styled, StyledExt as _, ViewContext, VisualContext};
use workspace::ModalView;
pub struct PasswordPrompt {
prompt: SharedString,
tx: Option<oneshot::Sender<Result<String>>>,
editor: View<Editor>,
}
impl PasswordPrompt {
pub fn new(
prompt: String,
tx: oneshot::Sender<Result<String>>,
cx: &mut ViewContext<Self>,
) -> Self {
Self {
prompt: SharedString::from(prompt),
tx: Some(tx),
editor: cx.new_view(|cx| {
let mut editor = Editor::single_line(cx);
editor.set_redact_all(true, cx);
editor
}),
}
}
fn confirm(&mut self, _: &menu::Confirm, cx: &mut ViewContext<Self>) {
let text = self.editor.read(cx).text(cx);
if let Some(tx) = self.tx.take() {
tx.send(Ok(text)).ok();
};
cx.emit(DismissEvent)
}
fn dismiss(&mut self, _: &menu::Cancel, cx: &mut ViewContext<Self>) {
cx.emit(DismissEvent)
}
}
impl Render for PasswordPrompt {
fn render(&mut self, cx: &mut ui::ViewContext<Self>) -> impl ui::IntoElement {
v_flex()
.key_context("PasswordPrompt")
.elevation_3(cx)
.p_4()
.gap_2()
.on_action(cx.listener(Self::dismiss))
.on_action(cx.listener(Self::confirm))
.w(px(400.))
.child(Label::new(self.prompt.clone()))
.child(self.editor.clone())
}
}
impl FocusableView for PasswordPrompt {
fn focus_handle(&self, cx: &gpui::AppContext) -> gpui::FocusHandle {
self.editor.focus_handle(cx)
}
}
impl EventEmitter<DismissEvent> for PasswordPrompt {}
impl ModalView for PasswordPrompt {}