Reinstall Node whenever a NodeRuntime operation has serious error

This commit is contained in:
Julia 2023-06-28 16:43:45 -04:00
parent b2de28ccfc
commit db2b3e47bc
7 changed files with 126 additions and 87 deletions

1
Cargo.lock generated
View File

@ -4138,6 +4138,7 @@ dependencies = [
"async-tar", "async-tar",
"futures 0.3.28", "futures 0.3.28",
"gpui", "gpui",
"log",
"parking_lot 0.11.2", "parking_lot 0.11.2",
"serde", "serde",
"serde_derive", "serde_derive",

View File

@ -340,7 +340,7 @@ impl Copilot {
let http = util::http::FakeHttpClient::create(|_| async { unreachable!() }); let http = util::http::FakeHttpClient::create(|_| async { unreachable!() });
let this = cx.add_model(|cx| Self { let this = cx.add_model(|cx| Self {
http: http.clone(), http: http.clone(),
node_runtime: NodeRuntime::new(http, cx.background().clone()), node_runtime: NodeRuntime::instance(http, cx.background().clone()),
server: CopilotServer::Running(RunningCopilotServer { server: CopilotServer::Running(RunningCopilotServer {
lsp: Arc::new(server), lsp: Arc::new(server),
sign_in_status: SignInStatus::Authorized, sign_in_status: SignInStatus::Authorized,

View File

@ -20,3 +20,4 @@ serde.workspace = true
serde_derive.workspace = true serde_derive.workspace = true
serde_json.workspace = true serde_json.workspace = true
smol.workspace = true smol.workspace = true
log.workspace = true

View File

@ -1,21 +1,24 @@
use anyhow::{anyhow, bail, Context, Result}; use anyhow::{anyhow, bail, Context, Result};
use async_compression::futures::bufread::GzipDecoder; use async_compression::futures::bufread::GzipDecoder;
use async_tar::Archive; use async_tar::Archive;
use futures::lock::Mutex;
use futures::{future::Shared, FutureExt}; use futures::{future::Shared, FutureExt};
use gpui::{executor::Background, Task}; use gpui::{executor::Background, Task};
use parking_lot::Mutex;
use serde::Deserialize; use serde::Deserialize;
use smol::{fs, io::BufReader, process::Command}; use smol::{fs, io::BufReader, process::Command};
use std::process::Output;
use std::{ use std::{
env::consts, env::consts,
path::{Path, PathBuf}, path::{Path, PathBuf},
sync::Arc, sync::{Arc, OnceLock},
}; };
use util::http::HttpClient; use util::{http::HttpClient, ResultExt};
const VERSION: &str = "v18.15.0"; const VERSION: &str = "v18.15.0";
#[derive(Deserialize)] static RUNTIME_INSTANCE: OnceLock<Arc<NodeRuntime>> = OnceLock::new();
#[derive(Debug, Deserialize)]
#[serde(rename_all = "kebab-case")] #[serde(rename_all = "kebab-case")]
pub struct NpmInfo { pub struct NpmInfo {
#[serde(default)] #[serde(default)]
@ -23,7 +26,7 @@ pub struct NpmInfo {
versions: Vec<String>, versions: Vec<String>,
} }
#[derive(Deserialize, Default)] #[derive(Debug, Deserialize, Default)]
pub struct NpmInfoDistTags { pub struct NpmInfoDistTags {
latest: Option<String>, latest: Option<String>,
} }
@ -35,12 +38,16 @@ pub struct NodeRuntime {
} }
impl NodeRuntime { impl NodeRuntime {
pub fn new(http: Arc<dyn HttpClient>, background: Arc<Background>) -> Arc<NodeRuntime> { pub fn instance(http: Arc<dyn HttpClient>, background: Arc<Background>) -> Arc<NodeRuntime> {
RUNTIME_INSTANCE
.get_or_init(|| {
Arc::new(NodeRuntime { Arc::new(NodeRuntime {
http, http,
background, background,
installation_path: Mutex::new(None), installation_path: Mutex::new(None),
}) })
})
.clone()
} }
pub async fn binary_path(&self) -> Result<PathBuf> { pub async fn binary_path(&self) -> Result<PathBuf> {
@ -50,22 +57,45 @@ impl NodeRuntime {
pub async fn run_npm_subcommand( pub async fn run_npm_subcommand(
&self, &self,
directory: &Path, directory: Option<&Path>,
subcommand: &str, subcommand: &str,
args: &[&str], args: &[&str],
) -> Result<()> { ) -> Result<Output> {
let installation_path = self.install_if_needed().await?; let attempt = |installation_path: PathBuf| async move {
let node_binary = installation_path.join("bin/node"); let node_binary = installation_path.join("bin/node");
let npm_file = installation_path.join("bin/npm"); let npm_file = installation_path.join("bin/npm");
let output = Command::new(node_binary) if smol::fs::metadata(&node_binary).await.is_err() {
.arg(npm_file) return Err(anyhow!("missing node binary file"));
.arg(subcommand) }
.args(args)
.current_dir(directory)
.output()
.await?;
if smol::fs::metadata(&npm_file).await.is_err() {
return Err(anyhow!("missing npm file"));
}
let mut command = Command::new(node_binary);
command.arg(npm_file).arg(subcommand).args(args);
if let Some(directory) = directory {
command.current_dir(directory);
}
command.output().await.map_err(|e| anyhow!("{e}"))
};
let installation_path = self.install_if_needed().await?;
let mut output = attempt(installation_path).await;
if output.is_err() {
let installation_path = self.reinstall().await?;
output = attempt(installation_path).await;
if output.is_err() {
return Err(anyhow!(
"failed to launch npm subcommand {subcommand} subcommand"
));
}
}
if let Ok(output) = &output {
if !output.status.success() { if !output.status.success() {
return Err(anyhow!( return Err(anyhow!(
"failed to execute npm {subcommand} subcommand:\nstdout: {:?}\nstderr: {:?}", "failed to execute npm {subcommand} subcommand:\nstdout: {:?}\nstderr: {:?}",
@ -73,32 +103,28 @@ impl NodeRuntime {
String::from_utf8_lossy(&output.stderr) String::from_utf8_lossy(&output.stderr)
)); ));
} }
}
Ok(()) output.map_err(|e| anyhow!("{e}"))
} }
pub async fn npm_package_latest_version(&self, name: &str) -> Result<String> { pub async fn npm_package_latest_version(&self, name: &str) -> Result<String> {
let installation_path = self.install_if_needed().await?; let output = self
let node_binary = installation_path.join("bin/node"); .run_npm_subcommand(
let npm_file = installation_path.join("bin/npm"); None,
"info",
let output = Command::new(node_binary) &[
.arg(npm_file) name,
.args(["-fetch-retry-mintimeout", "2000"]) "--json",
.args(["-fetch-retry-maxtimeout", "5000"]) "-fetch-retry-mintimeout",
.args(["-fetch-timeout", "5000"]) "2000",
.args(["info", name, "--json"]) "-fetch-retry-maxtimeout",
.output() "5000",
.await "-fetch-timeout",
.context("failed to run npm info")?; "5000",
],
if !output.status.success() { )
return Err(anyhow!( .await?;
"failed to execute npm info:\nstdout: {:?}\nstderr: {:?}",
String::from_utf8_lossy(&output.stdout),
String::from_utf8_lossy(&output.stderr)
));
}
let mut info: NpmInfo = serde_json::from_slice(&output.stdout)?; let mut info: NpmInfo = serde_json::from_slice(&output.stdout)?;
info.dist_tags info.dist_tags
@ -112,41 +138,54 @@ impl NodeRuntime {
directory: &Path, directory: &Path,
packages: impl IntoIterator<Item = (&str, &str)>, packages: impl IntoIterator<Item = (&str, &str)>,
) -> Result<()> { ) -> Result<()> {
let installation_path = self.install_if_needed().await?; let packages: Vec<_> = packages
let node_binary = installation_path.join("bin/node");
let npm_file = installation_path.join("bin/npm");
let output = Command::new(node_binary)
.arg(npm_file)
.args(["-fetch-retry-mintimeout", "2000"])
.args(["-fetch-retry-maxtimeout", "5000"])
.args(["-fetch-timeout", "5000"])
.arg("install")
.arg("--prefix")
.arg(directory)
.args(
packages
.into_iter() .into_iter()
.map(|(name, version)| format!("{name}@{version}")), .map(|(name, version)| format!("{name}@{version}"))
) .collect();
.output()
.await
.context("failed to run npm install")?;
if !output.status.success() { let mut arguments: Vec<_> = packages.iter().map(|p| p.as_str()).collect();
return Err(anyhow!( arguments.extend_from_slice(&[
"failed to execute npm install:\nstdout: {:?}\nstderr: {:?}", "-fetch-retry-mintimeout",
String::from_utf8_lossy(&output.stdout), "2000",
String::from_utf8_lossy(&output.stderr) "-fetch-retry-maxtimeout",
)); "5000",
} "-fetch-timeout",
"5000",
]);
self.run_npm_subcommand(Some(directory), "install", &arguments)
.await?;
Ok(()) Ok(())
} }
async fn reinstall(&self) -> Result<PathBuf> {
log::info!("beginnning to reinstall Node runtime");
let mut installation_path = self.installation_path.lock().await;
if let Some(task) = installation_path.as_ref().cloned() {
if let Ok(installation_path) = task.await {
smol::fs::remove_dir_all(&installation_path)
.await
.context("node dir removal")
.log_err();
}
}
let http = self.http.clone();
let task = self
.background
.spawn(async move { Self::install(http).await.map_err(Arc::new) })
.shared();
*installation_path = Some(task.clone());
task.await.map_err(|e| anyhow!("{}", e))
}
async fn install_if_needed(&self) -> Result<PathBuf> { async fn install_if_needed(&self) -> Result<PathBuf> {
let task = self let task = self
.installation_path .installation_path
.lock() .lock()
.await
.get_or_insert_with(|| { .get_or_insert_with(|| {
let http = self.http.clone(); let http = self.http.clone();
self.background self.background
@ -155,13 +194,11 @@ impl NodeRuntime {
}) })
.clone(); .clone();
match task.await { task.await.map_err(|e| anyhow!("{}", e))
Ok(path) => Ok(path),
Err(error) => Err(anyhow!("{}", error)),
}
} }
async fn install(http: Arc<dyn HttpClient>) -> Result<PathBuf> { async fn install(http: Arc<dyn HttpClient>) -> Result<PathBuf> {
log::info!("installing Node runtime");
let arch = match consts::ARCH { let arch = match consts::ARCH {
"x86_64" => "x64", "x86_64" => "x64",
"aarch64" => "arm64", "aarch64" => "arm64",

View File

@ -263,11 +263,11 @@ impl LspAdapter for EsLintLspAdapter {
fs::rename(first.path(), &repo_root).await?; fs::rename(first.path(), &repo_root).await?;
self.node self.node
.run_npm_subcommand(&repo_root, "install", &[]) .run_npm_subcommand(Some(&repo_root), "install", &[])
.await?; .await?;
self.node self.node
.run_npm_subcommand(&repo_root, "run-script", &["compile"]) .run_npm_subcommand(Some(&repo_root), "run-script", &["compile"])
.await?; .await?;
} }

View File

@ -131,7 +131,7 @@ fn main() {
languages.set_executor(cx.background().clone()); languages.set_executor(cx.background().clone());
languages.set_language_server_download_dir(paths::LANGUAGES_DIR.clone()); languages.set_language_server_download_dir(paths::LANGUAGES_DIR.clone());
let languages = Arc::new(languages); let languages = Arc::new(languages);
let node_runtime = NodeRuntime::new(http.clone(), cx.background().to_owned()); let node_runtime = NodeRuntime::instance(http.clone(), cx.background().to_owned());
languages::init(languages.clone(), node_runtime.clone()); languages::init(languages.clone(), node_runtime.clone());
let user_store = cx.add_model(|cx| UserStore::new(client.clone(), http.clone(), cx)); let user_store = cx.add_model(|cx| UserStore::new(client.clone(), http.clone(), cx));

View File

@ -2177,7 +2177,7 @@ mod tests {
languages.set_executor(cx.background().clone()); languages.set_executor(cx.background().clone());
let languages = Arc::new(languages); let languages = Arc::new(languages);
let http = FakeHttpClient::with_404_response(); let http = FakeHttpClient::with_404_response();
let node_runtime = NodeRuntime::new(http, cx.background().to_owned()); let node_runtime = NodeRuntime::instance(http, cx.background().to_owned());
languages::init(languages.clone(), node_runtime); languages::init(languages.clone(), node_runtime);
for name in languages.language_names() { for name in languages.language_names() {
languages.language_for_name(&name); languages.language_for_name(&name);