mirror of
https://github.com/zed-industries/zed.git
synced 2024-12-26 20:32:22 +03:00
Reinstall Node whenever a NodeRuntime operation has serious error
This commit is contained in:
parent
b2de28ccfc
commit
db2b3e47bc
1
Cargo.lock
generated
1
Cargo.lock
generated
@ -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",
|
||||||
|
@ -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,
|
||||||
|
@ -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
|
||||||
|
@ -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> {
|
||||||
Arc::new(NodeRuntime {
|
RUNTIME_INSTANCE
|
||||||
http,
|
.get_or_init(|| {
|
||||||
background,
|
Arc::new(NodeRuntime {
|
||||||
installation_path: Mutex::new(None),
|
http,
|
||||||
})
|
background,
|
||||||
|
installation_path: Mutex::new(None),
|
||||||
|
})
|
||||||
|
})
|
||||||
|
.clone()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn binary_path(&self) -> Result<PathBuf> {
|
pub async fn binary_path(&self) -> Result<PathBuf> {
|
||||||
@ -50,55 +57,74 @@ 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 attempt = |installation_path: PathBuf| async move {
|
||||||
|
let node_binary = installation_path.join("bin/node");
|
||||||
|
let npm_file = installation_path.join("bin/npm");
|
||||||
|
|
||||||
|
if smol::fs::metadata(&node_binary).await.is_err() {
|
||||||
|
return Err(anyhow!("missing node binary file"));
|
||||||
|
}
|
||||||
|
|
||||||
|
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 installation_path = self.install_if_needed().await?;
|
||||||
let node_binary = installation_path.join("bin/node");
|
let mut output = attempt(installation_path).await;
|
||||||
let npm_file = installation_path.join("bin/npm");
|
if output.is_err() {
|
||||||
|
let installation_path = self.reinstall().await?;
|
||||||
let output = Command::new(node_binary)
|
output = attempt(installation_path).await;
|
||||||
.arg(npm_file)
|
if output.is_err() {
|
||||||
.arg(subcommand)
|
return Err(anyhow!(
|
||||||
.args(args)
|
"failed to launch npm subcommand {subcommand} subcommand"
|
||||||
.current_dir(directory)
|
));
|
||||||
.output()
|
}
|
||||||
.await?;
|
|
||||||
|
|
||||||
if !output.status.success() {
|
|
||||||
return Err(anyhow!(
|
|
||||||
"failed to execute npm {subcommand} subcommand:\nstdout: {:?}\nstderr: {:?}",
|
|
||||||
String::from_utf8_lossy(&output.stdout),
|
|
||||||
String::from_utf8_lossy(&output.stderr)
|
|
||||||
));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
if let Ok(output) = &output {
|
||||||
|
if !output.status.success() {
|
||||||
|
return Err(anyhow!(
|
||||||
|
"failed to execute npm {subcommand} subcommand:\nstdout: {:?}\nstderr: {:?}",
|
||||||
|
String::from_utf8_lossy(&output.stdout),
|
||||||
|
String::from_utf8_lossy(&output.stderr)
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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");
|
.into_iter()
|
||||||
let npm_file = installation_path.join("bin/npm");
|
.map(|(name, version)| format!("{name}@{version}"))
|
||||||
|
.collect();
|
||||||
|
|
||||||
let output = Command::new(node_binary)
|
let mut arguments: Vec<_> = packages.iter().map(|p| p.as_str()).collect();
|
||||||
.arg(npm_file)
|
arguments.extend_from_slice(&[
|
||||||
.args(["-fetch-retry-mintimeout", "2000"])
|
"-fetch-retry-mintimeout",
|
||||||
.args(["-fetch-retry-maxtimeout", "5000"])
|
"2000",
|
||||||
.args(["-fetch-timeout", "5000"])
|
"-fetch-retry-maxtimeout",
|
||||||
.arg("install")
|
"5000",
|
||||||
.arg("--prefix")
|
"-fetch-timeout",
|
||||||
.arg(directory)
|
"5000",
|
||||||
.args(
|
]);
|
||||||
packages
|
|
||||||
.into_iter()
|
|
||||||
.map(|(name, version)| format!("{name}@{version}")),
|
|
||||||
)
|
|
||||||
.output()
|
|
||||||
.await
|
|
||||||
.context("failed to run npm install")?;
|
|
||||||
|
|
||||||
if !output.status.success() {
|
self.run_npm_subcommand(Some(directory), "install", &arguments)
|
||||||
return Err(anyhow!(
|
.await?;
|
||||||
"failed to execute npm install:\nstdout: {:?}\nstderr: {:?}",
|
|
||||||
String::from_utf8_lossy(&output.stdout),
|
|
||||||
String::from_utf8_lossy(&output.stderr)
|
|
||||||
));
|
|
||||||
}
|
|
||||||
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",
|
||||||
|
@ -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?;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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));
|
||||||
|
@ -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);
|
||||||
|
Loading…
Reference in New Issue
Block a user