SSH Remoting: Add the ability to resolve file paths on the remote host (#18250)

Release Notes:

- N/A
This commit is contained in:
Mikayla Maki 2024-09-23 15:47:25 -07:00 committed by GitHub
parent d989183f94
commit 20c06545b6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 119 additions and 33 deletions

View file

@ -3037,15 +3037,11 @@ impl Project {
buffer: &Model<Buffer>, buffer: &Model<Buffer>,
cx: &mut ModelContext<Self>, cx: &mut ModelContext<Self>,
) -> Task<Option<ResolvedPath>> { ) -> Task<Option<ResolvedPath>> {
// TODO: ssh based remoting. let path_buf = PathBuf::from(path);
if self.ssh_session.is_some() { if path_buf.is_absolute() || path.starts_with("~") {
return Task::ready(None); if self.is_local() {
} let expanded = PathBuf::from(shellexpand::tilde(&path).into_owned());
if self.is_local_or_ssh() {
let expanded = PathBuf::from(shellexpand::tilde(&path).into_owned());
if expanded.is_absolute() {
let fs = self.fs.clone(); let fs = self.fs.clone();
cx.background_executor().spawn(async move { cx.background_executor().spawn(async move {
let path = expanded.as_path(); let path = expanded.as_path();
@ -3053,16 +3049,24 @@ impl Project {
exists.then(|| ResolvedPath::AbsPath(expanded)) exists.then(|| ResolvedPath::AbsPath(expanded))
}) })
} else if let Some(ssh_session) = self.ssh_session.as_ref() {
let request = ssh_session.request(proto::CheckFileExists {
project_id: SSH_PROJECT_ID,
path: path.to_string(),
});
cx.background_executor().spawn(async move {
let response = request.await.log_err()?;
if response.exists {
Some(ResolvedPath::AbsPath(PathBuf::from(response.path)))
} else {
None
}
})
} else { } else {
self.resolve_path_in_worktrees(expanded, buffer, cx)
}
} else {
let path = PathBuf::from(path);
if path.is_absolute() || path.starts_with("~") {
return Task::ready(None); return Task::ready(None);
} }
} else {
self.resolve_path_in_worktrees(path, buffer, cx) self.resolve_path_in_worktrees(path_buf, buffer, cx)
} }
} }
@ -4016,17 +4020,7 @@ impl Project {
} }
pub fn worktree_metadata_protos(&self, cx: &AppContext) -> Vec<proto::WorktreeMetadata> { pub fn worktree_metadata_protos(&self, cx: &AppContext) -> Vec<proto::WorktreeMetadata> {
self.worktrees(cx) self.worktree_store.read(cx).worktree_metadata_protos(cx)
.map(|worktree| {
let worktree = worktree.read(cx);
proto::WorktreeMetadata {
id: worktree.id().to_proto(),
root_name: worktree.root_name().into(),
visible: worktree.is_visible(),
abs_path: worktree.abs_path().to_string_lossy().into(),
}
})
.collect()
} }
fn set_worktrees_from_proto( fn set_worktrees_from_proto(
@ -4035,10 +4029,9 @@ impl Project {
cx: &mut ModelContext<Project>, cx: &mut ModelContext<Project>,
) -> Result<()> { ) -> Result<()> {
cx.notify(); cx.notify();
let result = self.worktree_store.update(cx, |worktree_store, cx| { self.worktree_store.update(cx, |worktree_store, cx| {
worktree_store.set_worktrees_from_proto(worktrees, self.replica_id(), cx) worktree_store.set_worktrees_from_proto(worktrees, self.replica_id(), cx)
}); })
result
} }
fn set_collaborators_from_proto( fn set_collaborators_from_proto(
@ -4547,6 +4540,22 @@ pub enum ResolvedPath {
AbsPath(PathBuf), AbsPath(PathBuf),
} }
impl ResolvedPath {
pub fn abs_path(&self) -> Option<&Path> {
match self {
Self::AbsPath(path) => Some(path.as_path()),
_ => None,
}
}
pub fn project_path(&self) -> Option<&ProjectPath> {
match self {
Self::ProjectPath(path) => Some(&path),
_ => None,
}
}
}
impl Item for Buffer { impl Item for Buffer {
fn try_open( fn try_open(
project: &Model<Project>, project: &Model<Project>,

View file

@ -293,7 +293,10 @@ message Envelope {
TryExec try_exec = 252; TryExec try_exec = 252;
ReadTextFile read_text_file = 253; ReadTextFile read_text_file = 253;
ReadTextFileResponse read_text_file_response = 254; // current max ReadTextFileResponse read_text_file_response = 254;
CheckFileExists check_file_exists = 255;
CheckFileExistsResponse check_file_exists_response = 256; // current max
} }
reserved 158 to 161; reserved 158 to 161;
@ -2574,3 +2577,13 @@ message TryExec {
message TryExecResponse { message TryExecResponse {
string text = 1; string text = 1;
} }
message CheckFileExists {
uint64 project_id = 1;
string path = 2;
}
message CheckFileExistsResponse {
bool exists = 1;
string path = 2;
}

View file

@ -372,7 +372,9 @@ messages!(
(ShellEnvResponse, Foreground), (ShellEnvResponse, Foreground),
(TryExec, Foreground), (TryExec, Foreground),
(ReadTextFile, Foreground), (ReadTextFile, Foreground),
(ReadTextFileResponse, Foreground) (ReadTextFileResponse, Foreground),
(CheckFileExists, Background),
(CheckFileExistsResponse, Background)
); );
request_messages!( request_messages!(
@ -501,6 +503,7 @@ request_messages!(
(ShellEnv, ShellEnvResponse), (ShellEnv, ShellEnvResponse),
(ReadTextFile, ReadTextFileResponse), (ReadTextFile, ReadTextFileResponse),
(TryExec, Ack), (TryExec, Ack),
(CheckFileExists, CheckFileExistsResponse)
); );
entity_messages!( entity_messages!(
@ -578,7 +581,8 @@ entity_messages!(
WhichCommand, WhichCommand,
ShellEnv, ShellEnv,
TryExec, TryExec,
ReadTextFile ReadTextFile,
CheckFileExists,
); );
entity_messages!( entity_messages!(

View file

@ -108,6 +108,7 @@ impl HeadlessProject {
session.subscribe_to_entity(SSH_PROJECT_ID, &settings_observer); session.subscribe_to_entity(SSH_PROJECT_ID, &settings_observer);
client.add_request_handler(cx.weak_model(), Self::handle_list_remote_directory); client.add_request_handler(cx.weak_model(), Self::handle_list_remote_directory);
client.add_request_handler(cx.weak_model(), Self::handle_check_file_exists);
client.add_model_request_handler(Self::handle_add_worktree); client.add_model_request_handler(Self::handle_add_worktree);
client.add_model_request_handler(Self::handle_open_buffer_by_path); client.add_model_request_handler(Self::handle_open_buffer_by_path);
@ -298,4 +299,20 @@ impl HeadlessProject {
} }
Ok(proto::ListRemoteDirectoryResponse { entries }) Ok(proto::ListRemoteDirectoryResponse { entries })
} }
pub async fn handle_check_file_exists(
this: Model<Self>,
envelope: TypedEnvelope<proto::CheckFileExists>,
cx: AsyncAppContext,
) -> Result<proto::CheckFileExistsResponse> {
let fs = cx.read_model(&this, |this, _| this.fs.clone())?;
let expanded = shellexpand::tilde(&envelope.payload.path).to_string();
let exists = fs.is_file(&PathBuf::from(expanded.clone())).await;
Ok(proto::CheckFileExistsResponse {
exists,
path: expanded,
})
}
} }

View file

@ -12,7 +12,7 @@ use lsp::{CompletionContext, CompletionResponse, CompletionTriggerKind};
use node_runtime::NodeRuntime; use node_runtime::NodeRuntime;
use project::{ use project::{
search::{SearchQuery, SearchResult}, search::{SearchQuery, SearchResult},
Project, Project, ProjectPath,
}; };
use remote::SshSession; use remote::SshSession;
use serde_json::json; use serde_json::json;
@ -440,6 +440,49 @@ async fn test_remote_lsp(cx: &mut TestAppContext, server_cx: &mut TestAppContext
}) })
} }
#[gpui::test]
async fn test_remote_resolve_file_path(cx: &mut TestAppContext, server_cx: &mut TestAppContext) {
let (project, _headless, _fs) = init_test(cx, server_cx).await;
let (worktree, _) = project
.update(cx, |project, cx| {
project.find_or_create_worktree("/code/project1", true, cx)
})
.await
.unwrap();
let worktree_id = cx.update(|cx| worktree.read(cx).id());
let buffer = project
.update(cx, |project, cx| {
project.open_buffer((worktree_id, Path::new("src/lib.rs")), cx)
})
.await
.unwrap();
let path = project
.update(cx, |project, cx| {
project.resolve_existing_file_path("/code/project1/README.md", &buffer, cx)
})
.await
.unwrap();
assert_eq!(
path.abs_path().unwrap().to_string_lossy(),
"/code/project1/README.md"
);
let path = project
.update(cx, |project, cx| {
project.resolve_existing_file_path("../README.md", &buffer, cx)
})
.await
.unwrap();
assert_eq!(
path.project_path().unwrap().clone(),
ProjectPath::from((worktree_id, "README.md"))
);
}
fn init_logger() { fn init_logger() {
if std::env::var("RUST_LOG").is_ok() { if std::env::var("RUST_LOG").is_ok() {
env_logger::try_init().ok(); env_logger::try_init().ok();