summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNayeem Rahman <nayeemrmn99@gmail.com>2023-11-14 22:10:51 +0000
committerGitHub <noreply@github.com>2023-11-14 22:10:51 +0000
commit4913274a6508a5e5ad6c8babf2e90a4a84bf98ec (patch)
tree687e2b6846a8288b4bc9c2896a947b68007731f3
parentab0c637425c3219c2a5f59c25ae8ea593018568b (diff)
feat(lsp): upgrade check on init and notification (#21105)
-rw-r--r--cli/lsp/client.rs31
-rw-r--r--cli/lsp/language_server.rs21
-rw-r--r--cli/lsp/logging.rs12
-rw-r--r--cli/lsp/lsp_custom.rs21
-rw-r--r--cli/tools/upgrade.rs61
-rw-r--r--test_util/src/lsp.rs1
6 files changed, 131 insertions, 16 deletions
diff --git a/cli/lsp/client.rs b/cli/lsp/client.rs
index 84953c4d4..915af6831 100644
--- a/cli/lsp/client.rs
+++ b/cli/lsp/client.rs
@@ -96,6 +96,17 @@ impl Client {
});
}
+ pub fn send_did_upgrade_check_notification(
+ &self,
+ params: lsp_custom::DidUpgradeCheckNotificationParams,
+ ) {
+ // do on a task in case the caller currently is in the lsp lock
+ let client = self.0.clone();
+ spawn(async move {
+ client.send_did_upgrade_check_notification(params).await;
+ });
+ }
+
pub fn show_message(
&self,
message_type: lsp::MessageType,
@@ -165,6 +176,10 @@ trait ClientTrait: Send + Sync {
&self,
params: lsp_custom::DidChangeDenoConfigurationNotificationParams,
);
+ async fn send_did_upgrade_check_notification(
+ &self,
+ params: lsp_custom::DidUpgradeCheckNotificationParams,
+ );
async fn workspace_configuration(
&self,
scopes: Vec<Option<lsp::Url>>,
@@ -249,6 +264,16 @@ impl ClientTrait for TowerClient {
.await
}
+ async fn send_did_upgrade_check_notification(
+ &self,
+ params: lsp_custom::DidUpgradeCheckNotificationParams,
+ ) {
+ self
+ .0
+ .send_notification::<lsp_custom::DidUpgradeCheckNotification>(params)
+ .await
+ }
+
async fn workspace_configuration(
&self,
scopes: Vec<Option<lsp::Url>>,
@@ -350,6 +375,12 @@ impl ClientTrait for ReplClient {
) {
}
+ async fn send_did_upgrade_check_notification(
+ &self,
+ _params: lsp_custom::DidUpgradeCheckNotificationParams,
+ ) {
+ }
+
async fn workspace_configuration(
&self,
scopes: Vec<Option<lsp::Url>>,
diff --git a/cli/lsp/language_server.rs b/cli/lsp/language_server.rs
index 667df74bb..6f0c96746 100644
--- a/cli/lsp/language_server.rs
+++ b/cli/lsp/language_server.rs
@@ -112,6 +112,8 @@ use crate::npm::CliNpmResolverManagedPackageJsonInstallerOption;
use crate::npm::CliNpmResolverManagedSnapshotOption;
use crate::tools::fmt::format_file;
use crate::tools::fmt::format_parsed_source;
+use crate::tools::upgrade::check_for_upgrades_for_lsp;
+use crate::tools::upgrade::upgrade_check_enabled;
use crate::util::fs::remove_dir_all_if_exists;
use crate::util::path::is_importable_ext;
use crate::util::path::specifier_to_file_path;
@@ -3189,6 +3191,25 @@ impl tower_lsp::LanguageServer for LanguageServer {
}
lsp_log!("Server ready.");
+
+ if upgrade_check_enabled() {
+ let http_client = self.0.read().await.http_client.clone();
+ match check_for_upgrades_for_lsp(http_client).await {
+ Ok(version_info) => {
+ client.send_did_upgrade_check_notification(
+ lsp_custom::DidUpgradeCheckNotificationParams {
+ upgrade_available: version_info.map(
+ |(latest_version, is_canary)| lsp_custom::UpgradeAvailable {
+ latest_version,
+ is_canary,
+ },
+ ),
+ },
+ );
+ }
+ Err(err) => lsp_warn!("Failed to check for upgrades: {err}"),
+ }
+ }
}
async fn shutdown(&self) -> LspResult<()> {
diff --git a/cli/lsp/logging.rs b/cli/lsp/logging.rs
index 8b703f2a5..b8c8c99e6 100644
--- a/cli/lsp/logging.rs
+++ b/cli/lsp/logging.rs
@@ -62,11 +62,13 @@ macro_rules! lsp_log {
/// for downgrading these logs to another log level in the REPL.
macro_rules! lsp_warn {
($($arg:tt)+) => (
- let lsp_log_level = $crate::lsp::logging::lsp_warn_level();
- if lsp_log_level == log::Level::Debug {
- $crate::lsp::logging::lsp_debug!($($arg)+)
- } else {
- log::log!(lsp_log_level, $($arg)+)
+ {
+ let lsp_log_level = $crate::lsp::logging::lsp_warn_level();
+ if lsp_log_level == log::Level::Debug {
+ $crate::lsp::logging::lsp_debug!($($arg)+)
+ } else {
+ log::log!(lsp_log_level, $($arg)+)
+ }
}
)
}
diff --git a/cli/lsp/lsp_custom.rs b/cli/lsp/lsp_custom.rs
index 685da6b14..96f7be790 100644
--- a/cli/lsp/lsp_custom.rs
+++ b/cli/lsp/lsp_custom.rs
@@ -91,6 +91,27 @@ impl lsp::notification::Notification
const METHOD: &'static str = "deno/didChangeDenoConfiguration";
}
+pub enum DidUpgradeCheckNotification {}
+
+impl lsp::notification::Notification for DidUpgradeCheckNotification {
+ type Params = DidUpgradeCheckNotificationParams;
+
+ const METHOD: &'static str = "deno/didUpgradeCheck";
+}
+
+#[derive(Debug, Deserialize, Serialize)]
+#[serde(rename_all = "camelCase")]
+pub struct UpgradeAvailable {
+ pub latest_version: String,
+ pub is_canary: bool,
+}
+
+#[derive(Debug, Deserialize, Serialize)]
+#[serde(rename_all = "camelCase")]
+pub struct DidUpgradeCheckNotificationParams {
+ pub upgrade_available: Option<UpgradeAvailable>,
+}
+
/// This notification is only sent for testing purposes
/// in order to know what the latest diagnostics are.
pub enum DiagnosticBatchNotification {}
diff --git a/cli/tools/upgrade.rs b/cli/tools/upgrade.rs
index d2cb58d28..dccde44e2 100644
--- a/cli/tools/upgrade.rs
+++ b/cli/tools/upgrade.rs
@@ -74,9 +74,9 @@ impl UpdateCheckerEnvironment for RealUpdateCheckerEnvironment {
let http_client = self.http_client.clone();
async move {
if version::is_canary() {
- get_latest_canary_version(&http_client).await
+ get_latest_canary_version(&http_client, false).await
} else {
- get_latest_release_version(&http_client).await
+ get_latest_release_version(&http_client, false).await
}
}
.boxed()
@@ -186,11 +186,18 @@ fn print_release_notes(current_version: &str, new_version: &str) {
}
}
+pub fn upgrade_check_enabled() -> bool {
+ matches!(
+ env::var("DENO_NO_UPDATE_CHECK"),
+ Err(env::VarError::NotPresent)
+ )
+}
+
pub fn check_for_upgrades(
http_client: Arc<HttpClient>,
cache_file_path: PathBuf,
) {
- if env::var("DENO_NO_UPDATE_CHECK").is_ok() {
+ if !upgrade_check_enabled() {
return;
}
@@ -239,6 +246,32 @@ pub fn check_for_upgrades(
}
}
+pub async fn check_for_upgrades_for_lsp(
+ http_client: Arc<HttpClient>,
+) -> Result<Option<(String, bool)>, AnyError> {
+ if !upgrade_check_enabled() {
+ return Ok(None);
+ }
+ let is_canary = version::is_canary();
+ let latest_version;
+ let mut is_upgrade;
+ if is_canary {
+ latest_version = get_latest_canary_version(&http_client, true).await?;
+ is_upgrade = latest_version != version::GIT_COMMIT_HASH;
+ } else {
+ latest_version = get_latest_release_version(&http_client, true).await?;
+ is_upgrade = true;
+ if let Ok(current) = Version::parse_standard(version::deno()) {
+ if let Ok(latest) = Version::parse_standard(&latest_version) {
+ if current >= latest {
+ is_upgrade = false;
+ }
+ }
+ }
+ };
+ Ok(is_upgrade.then_some((latest_version, is_canary)))
+}
+
async fn fetch_and_store_latest_version<
TEnvironment: UpdateCheckerEnvironment,
>(
@@ -334,10 +367,10 @@ pub async fn upgrade(
None => {
let latest_version = if upgrade_flags.canary {
log::info!("Looking up latest canary version");
- get_latest_canary_version(client).await?
+ get_latest_canary_version(client, false).await?
} else {
log::info!("Looking up latest version");
- get_latest_release_version(client).await?
+ get_latest_release_version(client, false).await?
};
let current_is_most_recent = if upgrade_flags.canary {
@@ -448,20 +481,26 @@ pub async fn upgrade(
async fn get_latest_release_version(
client: &HttpClient,
+ for_lsp: bool,
) -> Result<String, AnyError> {
- let text = client
- .download_text("https://dl.deno.land/release-latest.txt")
- .await?;
+ let mut url = "https://dl.deno.land/release-latest.txt".to_string();
+ if for_lsp {
+ url.push_str("?lsp");
+ }
+ let text = client.download_text(url).await?;
let version = text.trim().to_string();
Ok(version.replace('v', ""))
}
async fn get_latest_canary_version(
client: &HttpClient,
+ for_lsp: bool,
) -> Result<String, AnyError> {
- let text = client
- .download_text("https://dl.deno.land/canary-latest.txt")
- .await?;
+ let mut url = "https://dl.deno.land/canary-latest.txt".to_string();
+ if for_lsp {
+ url.push_str("?lsp");
+ }
+ let text = client.download_text(url).await?;
let version = text.trim().to_string();
Ok(version)
}
diff --git a/test_util/src/lsp.rs b/test_util/src/lsp.rs
index 352f5d365..2fb227709 100644
--- a/test_util/src/lsp.rs
+++ b/test_util/src/lsp.rs
@@ -529,6 +529,7 @@ impl LspClientBuilder {
"DENO_DONT_USE_INTERNAL_LSP_DIAGNOSTIC_SYNC_FLAG",
if self.use_diagnostic_sync { "1" } else { "" },
)
+ .env("DENO_NO_UPDATE_CHECK", "1")
.arg("lsp")
.stdin(Stdio::piped())
.stdout(Stdio::piped());