From f3bfa111488b69e73afa9ac1bab988ad793a43d5 Mon Sep 17 00:00:00 2001 From: Kirill Bulatov Date: Fri, 9 Feb 2024 16:44:13 +0200 Subject: [PATCH] Show better errors when failing to start golps (#7614) Part of https://github.com/zed-industries/zed/issues/4471#issuecomment-1936008584 Improves gopls error logging to actually see what is wrong with the output we failed to match against the version regex. Release Notes: - N/A --- crates/zed/src/languages/go.rs | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/crates/zed/src/languages/go.rs b/crates/zed/src/languages/go.rs index 05bea740c5..0c2843c03f 100644 --- a/crates/zed/src/languages/go.rs +++ b/crates/zed/src/languages/go.rs @@ -1,4 +1,4 @@ -use anyhow::{anyhow, Result}; +use anyhow::{anyhow, Context, Result}; use async_trait::async_trait; use futures::StreamExt; use gpui::{AsyncAppContext, Task}; @@ -124,21 +124,22 @@ impl super::LspAdapter for GoLspAdapter { .args(["install", "golang.org/x/tools/gopls@latest"]) .output() .await?; - if !install_output.status.success() { - Err(anyhow!("failed to install gopls. Is go installed?"))?; - } + anyhow::ensure!( + install_output.status.success(), + "failed to install gopls. Is `go` installed and in the PATH?" + ); let installed_binary_path = gobin_dir.join("gopls"); let version_output = process::Command::new(&installed_binary_path) .arg("version") .output() .await - .map_err(|e| anyhow!("failed to run installed gopls binary {:?}", e))?; + .context("failed to run installed gopls binary")?; let version_stdout = str::from_utf8(&version_output.stdout) - .map_err(|_| anyhow!("gopls version produced invalid utf8"))?; + .context("gopls version produced invalid utf8 output")?; let version = GOPLS_VERSION_REGEX .find(version_stdout) - .ok_or_else(|| anyhow!("failed to parse gopls version output"))? + .with_context(|| format!("failed to parse golps version output '{version_stdout}'"))? .as_str(); let binary_path = container_dir.join(&format!("gopls_{version}")); fs::rename(&installed_binary_path, &binary_path).await?;