Initial running of servers on downloaded Node

This commit is contained in:
Julia 2023-03-23 17:32:13 -04:00
parent b579211861
commit edd6c85af7
8 changed files with 97 additions and 26 deletions

View file

@ -188,7 +188,7 @@ impl TestServer {
let app_state = Arc::new(workspace::AppState {
client: client.clone(),
user_store: user_store.clone(),
languages: Arc::new(LanguageRegistry::new(Task::ready(()))),
languages: Arc::new(LanguageRegistry::test()),
themes: ThemeRegistry::new((), cx.font_cache()),
fs: fs.clone(),
build_window_options: |_, _, _| Default::default(),

View file

@ -492,6 +492,7 @@ pub struct LanguageRegistry {
lsp_binary_statuses_tx: async_broadcast::Sender<(Arc<Language>, LanguageServerBinaryStatus)>,
lsp_binary_statuses_rx: async_broadcast::Receiver<(Arc<Language>, LanguageServerBinaryStatus)>,
login_shell_env_loaded: Shared<Task<()>>,
node_path: Shared<Task<Option<PathBuf>>>,
#[allow(clippy::type_complexity)]
lsp_binary_paths: Mutex<
HashMap<
@ -513,7 +514,7 @@ struct LanguageRegistryState {
}
impl LanguageRegistry {
pub fn new(login_shell_env_loaded: Task<()>) -> Self {
pub fn new(login_shell_env_loaded: Task<()>, node_path: Task<Option<PathBuf>>) -> Self {
let (lsp_binary_statuses_tx, lsp_binary_statuses_rx) = async_broadcast::broadcast(16);
Self {
state: RwLock::new(LanguageRegistryState {
@ -529,6 +530,7 @@ impl LanguageRegistry {
lsp_binary_statuses_tx,
lsp_binary_statuses_rx,
login_shell_env_loaded: login_shell_env_loaded.shared(),
node_path: node_path.shared(),
lsp_binary_paths: Default::default(),
executor: None,
}
@ -536,7 +538,7 @@ impl LanguageRegistry {
#[cfg(any(test, feature = "test-support"))]
pub fn test() -> Self {
Self::new(Task::ready(()))
Self::new(Task::ready(()), Task::Ready(None))
}
pub fn set_executor(&mut self, executor: Arc<Background>) {
@ -802,8 +804,12 @@ impl LanguageRegistry {
let adapter = language.adapter.clone()?;
let lsp_binary_statuses = self.lsp_binary_statuses_tx.clone();
let login_shell_env_loaded = self.login_shell_env_loaded.clone();
let node_path = self.node_path.clone();
Some(cx.spawn(|cx| async move {
login_shell_env_loaded.await;
let node_path = node_path.await;
let server_binary_path = this
.lsp_binary_paths
.lock()
@ -824,14 +830,27 @@ impl LanguageRegistry {
.map_err(|e| anyhow!(e));
let server_binary_path = server_binary_path.await?;
let server_args = &adapter.server_args;
let server = lsp::LanguageServer::new(
server_id,
&server_binary_path,
server_args,
&root_path,
cx,
)?;
let server_name = server_binary_path
.file_name()
.map(|name| name.to_string_lossy().to_string());
let mut command = match adapter.adapter.server_execution_kind().await {
ServerExecutionKind::Node => {
let node_path = node_path
.ok_or(anyhow!("Missing Node path for Node based language server"))?;
let node_binary = node_path.join("bin/node");
dbg!(&node_binary);
let mut command = smol::process::Command::new(node_binary);
command.arg(dbg!(server_binary_path));
command
}
ServerExecutionKind::Launch => smol::process::Command::new(server_binary_path),
};
command.args(&adapter.server_args);
let server = lsp::LanguageServer::new(server_id, server_name, command, &root_path, cx)?;
Ok(server)
}))
}
@ -1528,7 +1547,7 @@ mod tests {
#[gpui::test(iterations = 10)]
async fn test_language_loading(cx: &mut TestAppContext) {
let mut languages = LanguageRegistry::new(Task::ready(()));
let mut languages = LanguageRegistry::test();
languages.set_executor(cx.background());
let languages = Arc::new(languages);
languages.register(

View file

@ -105,10 +105,10 @@ struct Error {
}
impl LanguageServer {
pub fn new<T: AsRef<std::ffi::OsStr>>(
pub fn new(
server_id: usize,
binary_path: &Path,
args: &[T],
server_name: Option<String>,
mut command: process::Command,
root_path: &Path,
cx: AsyncAppContext,
) -> Result<Self> {
@ -117,18 +117,17 @@ impl LanguageServer {
} else {
root_path.parent().unwrap_or_else(|| Path::new("/"))
};
let mut server = process::Command::new(binary_path)
let mut server = dbg!(command
.current_dir(working_dir)
.args(args)
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.stderr(Stdio::inherit())
.kill_on_drop(true)
.spawn()?;
.kill_on_drop(true))
.spawn()?;
let stdin = server.stdin.take().unwrap();
let stout = server.stdout.take().unwrap();
let mut server = Self::new_internal(
server_id,
stdin,
@ -147,8 +146,9 @@ impl LanguageServer {
);
},
);
if let Some(name) = binary_path.file_name() {
server.name = name.to_string_lossy().to_string();
if let Some(name) = server_name {
server.name = name;
}
Ok(server)
}

View file

@ -4,6 +4,7 @@ lazy_static::lazy_static! {
pub static ref HOME: PathBuf = dirs::home_dir().expect("failed to determine home directory");
pub static ref CONFIG_DIR: PathBuf = HOME.join(".config").join("zed");
pub static ref LOGS_DIR: PathBuf = HOME.join("Library/Logs/Zed");
pub static ref SUPPORT_DIR: PathBuf = HOME.join("Library/Application Support/Zed");
pub static ref LANGUAGES_DIR: PathBuf = HOME.join("Library/Application Support/Zed/languages");
pub static ref DB_DIR: PathBuf = HOME.join("Library/Application Support/Zed/db");
pub static ref SETTINGS: PathBuf = CONFIG_DIR.join("settings.json");

View file

@ -18,6 +18,8 @@ mod rust;
mod typescript;
mod yaml;
pub use installation::ensure_node_installation_dir;
// 1. Add tree-sitter-{language} parser to zed crate
// 2. Create a language directory in zed/crates/zed/src/languages and add the language to init function below
// 3. Add config.toml to the newly created language directory using existing languages as a template

View file

@ -1,9 +1,15 @@
use anyhow::{anyhow, Context, Result};
use async_compression::futures::bufread::GzipDecoder;
use async_tar::Archive;
use client::http::HttpClient;
use futures::{io::BufReader, StreamExt};
use serde::Deserialize;
use smol::fs::{self, File};
use smol::io::AsyncReadExt;
use std::{path::Path, sync::Arc};
use std::{
path::{Path, PathBuf},
sync::Arc,
};
pub struct GitHubLspBinaryVersion {
pub name: String,
@ -35,6 +41,40 @@ pub(crate) struct GithubReleaseAsset {
pub browser_download_url: String,
}
pub async fn ensure_node_installation_dir(http: Arc<dyn HttpClient>) -> Result<PathBuf> {
eprintln!("ensure_node_installation_dir");
let version = "v18.15.0";
let arch = "arm64";
let folder_name = format!("node-{version}-darwin-{arch}");
let node_containing_dir = dbg!(util::paths::SUPPORT_DIR.join("node"));
let node_dir = dbg!(node_containing_dir.join(folder_name));
let node_binary = node_dir.join("bin/node");
if fs::metadata(&node_binary).await.is_err() {
_ = fs::remove_dir_all(&node_containing_dir).await;
fs::create_dir(&node_containing_dir)
.await
.context("error creating node containing dir")?;
let url = format!("https://nodejs.org/dist/{version}/node-{version}-darwin-{arch}.tar.gz");
dbg!(&url);
let mut response = http
.get(&url, Default::default(), true)
.await
.context("error downloading Node binary tarball")?;
let decompressed_bytes = GzipDecoder::new(BufReader::new(response.body_mut()));
let archive = Archive::new(decompressed_bytes);
archive.unpack(&node_containing_dir).await?;
eprintln!("unpacked");
}
eprintln!("returning");
Ok(dbg!(node_dir))
}
pub async fn npm_package_latest_version(name: &str) -> Result<String> {
let output = smol::process::Command::new("npm")
.args(["-fetch-retry-mintimeout", "2000"])

View file

@ -81,6 +81,15 @@ fn main() {
})
};
let node_path = {
let http = http.clone();
app.background().spawn(async move {
languages::ensure_node_installation_dir(http.clone())
.await
.log_err()
})
};
let (cli_connections_tx, mut cli_connections_rx) = mpsc::unbounded();
let (open_paths_tx, mut open_paths_rx) = mpsc::unbounded();
app.on_open_urls(move |urls, _| {
@ -135,7 +144,7 @@ fn main() {
}
let client = client::Client::new(http.clone(), cx);
let mut languages = LanguageRegistry::new(login_shell_env_loaded);
let mut languages = LanguageRegistry::new(login_shell_env_loaded, node_path);
languages.set_executor(cx.background().clone());
languages.set_language_server_download_dir(paths::LANGUAGES_DIR.clone());
let languages = Arc::new(languages);

View file

@ -1846,7 +1846,7 @@ mod tests {
#[gpui::test]
fn test_bundled_languages(cx: &mut MutableAppContext) {
let mut languages = LanguageRegistry::new(Task::ready(()));
let mut languages = LanguageRegistry::test();
languages.set_executor(cx.background().clone());
let languages = Arc::new(languages);
let themes = ThemeRegistry::new((), cx.font_cache().clone());