From 96f6b89508367d564e3a1ef3698e54fff1aae441 Mon Sep 17 00:00:00 2001 From: Kirill Bulatov Date: Tue, 28 Nov 2023 23:12:46 +0200 Subject: [PATCH] Clear failed installation task when error threshold gets exceeded --- crates/prettier/src/prettier.rs | 2 +- crates/project/src/prettier_support.rs | 27 +++++++++++++++++++------- 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/crates/prettier/src/prettier.rs b/crates/prettier/src/prettier.rs index 61a6656ea4..0886d68747 100644 --- a/crates/prettier/src/prettier.rs +++ b/crates/prettier/src/prettier.rs @@ -34,7 +34,7 @@ pub struct TestPrettier { default: bool, } -pub const LAUNCH_THRESHOLD: usize = 5; +pub const FAIL_THRESHOLD: usize = 4; pub const PRETTIER_SERVER_FILE: &str = "prettier_server.js"; pub const PRETTIER_SERVER_JS: &str = include_str!("./prettier_server.js"); const PRETTIER_PACKAGE_NAME: &str = "prettier"; diff --git a/crates/project/src/prettier_support.rs b/crates/project/src/prettier_support.rs index 35ba1d0f8b..a61589b8b4 100644 --- a/crates/project/src/prettier_support.rs +++ b/crates/project/src/prettier_support.rs @@ -147,7 +147,7 @@ impl PrettierInstance { worktree_id: Option, cx: &mut ModelContext<'_, Project>, ) -> Option>> { - if self.attempt > prettier::LAUNCH_THRESHOLD { + if self.attempt > prettier::FAIL_THRESHOLD { match prettier_dir { Some(prettier_dir) => log::warn!( "Prettier from path {prettier_dir:?} exceeded launch threshold, not starting" @@ -643,13 +643,20 @@ impl Project { new_plugins .retain(|plugin| !self.default_prettier.installed_plugins.contains(plugin)); let mut installation_attempt = 0; - let previous_installation_task = match &self.default_prettier.prettier { + let previous_installation_task = match &mut self.default_prettier.prettier { PrettierInstallation::NotInstalled { installation_task, attempts, not_installed_plugins } => { installation_attempt = *attempts; + if installation_attempt > prettier::FAIL_THRESHOLD { + *installation_task = None; + log::warn!( + "Default prettier installation had failed {installation_attempt} times, not attempting again", + ); + return; + } new_plugins.extend(not_installed_plugins.iter()); installation_task.clone() } @@ -660,6 +667,7 @@ impl Project { None } }; + let plugins_to_install = new_plugins.clone(); let fs = Arc::clone(&self.fs); let new_installation_task = cx @@ -677,20 +685,25 @@ impl Project { let mut needs_install = false; if let Some(previous_installation_task) = previous_installation_task { if let Err(e) = previous_installation_task.await { - log::error!("Failed to install default prettier (attempt {installation_attempt}): {e:#}"); + log::error!("Failed to install default prettier: {e:#}"); project.update(&mut cx, |project, _| { if let PrettierInstallation::NotInstalled { attempts, not_installed_plugins, .. } = &mut project.default_prettier.prettier { *attempts += 1; new_plugins.extend(not_installed_plugins.iter()); installation_attempt = *attempts; needs_install = true; - } - }) + }; + }); } }; - if installation_attempt > prettier::LAUNCH_THRESHOLD { + if installation_attempt > prettier::FAIL_THRESHOLD { + project.update(&mut cx, |project, _| { + if let PrettierInstallation::NotInstalled { installation_task, .. } = &mut project.default_prettier.prettier { + *installation_task = None; + }; + }); log::warn!( - "Default prettier installation has failed {installation_attempt} times, not attempting again", + "Default prettier installation had failed {installation_attempt} times, not attempting again", ); return Ok(()); }