From 02d47bf64fcece0edc5b849d25e3a60a67d01c83 Mon Sep 17 00:00:00 2001 From: Finn Evers Date: Wed, 28 Oct 2026 21:52:54 +0000 Subject: [PATCH] http_client: Unify asset downloading logic (#66048) Supersedes https://github.com/zed-industries/zed/pull/62653 ## Summary This PR unifies the download handling we have in the http_client crate, because we were alternating between the ways we were downloading a lot, in one case we changed the way of downloading entirely if a hex digest was present or not. With this in, we now handle this one way for all kinds of downloads, removing a lot of duplicated code and unifying the behavior here across download types. ## Testing Tested this manually. ## Self-review - [X] I've reviewed my diff for quality, security, reliability, and performance. - [ ] UI changes follow the [checklist](https://zed.dev/docs/development/ui-checklist). - [ ] Tests cover the new or changed behavior. Release Notes: - Improved reliability of language server and agent downloads by verifying checksums consistently, rejecting failed HTTP responses, and improving cleanup of partial downloads. --- Cargo.lock | 30 +- Cargo.toml | 2 + crates/dap/Cargo.toml | 1 + crates/dap/src/adapters.rs | 3 +- crates/extension_host/Cargo.toml | 1 + .../src/wasm_host/wit/since_v0_8_0.rs | 12 +- crates/github_releases/Cargo.toml | 31 + crates/github_releases/LICENSE-APACHE | 1 + crates/github_releases/src/github_releases.rs | 589 ++++++++++++++++++ .../src/releases.rs} | 16 +- crates/http_client/Cargo.toml | 18 +- crates/http_client/src/github_download.rs | 532 ---------------- crates/http_client/src/http_client.rs | 4 - crates/language/src/language.rs | 3 +- crates/languages/Cargo.toml | 2 +- crates/languages/src/c.rs | 9 +- crates/languages/src/eslint.rs | 8 +- crates/languages/src/go.rs | 2 +- crates/languages/src/json.rs | 2 +- crates/languages/src/python.rs | 15 +- crates/languages/src/rust.rs | 10 +- crates/project/Cargo.toml | 3 +- crates/project/src/agent_server_store.rs | 16 +- crates/project/src/lsp_store.rs | 10 +- 24 files changed, 713 insertions(+), 607 deletions(-) create mode 100644 crates/github_releases/Cargo.toml create mode 120000 crates/github_releases/LICENSE-APACHE create mode 100644 crates/github_releases/src/github_releases.rs rename crates/{http_client/src/github.rs => github_releases/src/releases.rs} (95%) delete mode 100644 crates/http_client/src/github_download.rs diff --git a/Cargo.lock b/Cargo.lock index f6169ea6e48..361ee9b884f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4422,6 +4422,7 @@ dependencies = [ "dap-types", "fs", "futures 0.3.34", + "github_releases", "gpui", "http_client", "language", @@ -6064,6 +6065,7 @@ dependencies = [ "extension", "fs", "futures 0.3.34", + "github_releases", "gpui", "gpui_tokio", "http_client", @@ -7163,6 +7165,25 @@ dependencies = [ "ztracing", ] +[[package]] +name = "github_releases" +version = "0.1.0" +dependencies = [ + "anyhow", + "async-compression", + "async-fs", + "async-tar", + "futures 0.3.34", + "gpui", + "http_client", + "log", + "serde", + "serde_json", + "sha2", + "tempfile", + "util", +] + [[package]] name = "gl_generator" version = "0.14.0" @@ -8214,9 +8235,6 @@ name = "http_client" version = "0.1.0" dependencies = [ "anyhow", - "async-compression", - "async-fs", - "async-tar", "bytes 1.11.1", "futures 0.3.34", "http 1.3.1", @@ -8226,10 +8244,7 @@ dependencies = [ "serde", "serde_json", "serde_urlencoded", - "sha2", - "tempfile", "url", - "util", ] [[package]] @@ -9735,10 +9750,10 @@ dependencies = [ "collections", "fs", "futures 0.3.34", + "github_releases", "globset", "gpui", "grammars", - "http_client", "itertools 0.14.0", "json_schema_store", "language", @@ -13862,6 +13877,7 @@ dependencies = [ "fuzzy_nucleo", "git", "git_hosting_providers", + "github_releases", "globset", "gpui", "http_client", diff --git a/Cargo.toml b/Cargo.toml index 84ed9442090..2807ee18140 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -91,6 +91,7 @@ members = [ "crates/git_hosting_providers", "crates/git_ui", "crates/git_ui_core", + "crates/github_releases", "crates/go_to_line", "crates/google_ai", "crates/gpui", @@ -364,6 +365,7 @@ git = { path = "crates/git" } git_hosting_providers = { path = "crates/git_hosting_providers" } git_ui = { path = "crates/git_ui" } git_ui_core = { path = "crates/git_ui_core" } +github_releases = { path = "crates/github_releases" } go_to_line = { path = "crates/go_to_line" } google_ai = { path = "crates/google_ai" } gpui = { path = "crates/gpui", default-features = false } diff --git a/crates/dap/Cargo.toml b/crates/dap/Cargo.toml index 146de96a9ae..d04d3b05fdf 100644 --- a/crates/dap/Cargo.toml +++ b/crates/dap/Cargo.toml @@ -32,6 +32,7 @@ collections.workspace = true dap-types.workspace = true fs.workspace = true futures.workspace = true +github_releases.workspace = true gpui.workspace = true http_client.workspace = true language.workspace = true diff --git a/crates/dap/src/adapters.rs b/crates/dap/src/adapters.rs index 70cf30878ae..5027e341f18 100644 --- a/crates/dap/src/adapters.rs +++ b/crates/dap/src/adapters.rs @@ -6,8 +6,9 @@ use collections::HashMap; pub use dap_types::{StartDebuggingRequestArguments, StartDebuggingRequestArgumentsRequest}; use fs::Fs; use futures::io::BufReader; +pub use github_releases::latest_github_release; use gpui::{AsyncApp, SharedString}; -pub use http_client::{HttpClient, github::latest_github_release}; +pub use http_client::HttpClient; use language::{LanguageName, LanguageToolchainStore}; use node_runtime::NodeRuntime; use schemars::JsonSchema; diff --git a/crates/extension_host/Cargo.toml b/crates/extension_host/Cargo.toml index d4209c0f98c..aa58c7ac3b2 100644 --- a/crates/extension_host/Cargo.toml +++ b/crates/extension_host/Cargo.toml @@ -27,6 +27,7 @@ dap.workspace = true extension.workspace = true fs.workspace = true futures.workspace = true +github_releases.workspace = true gpui.workspace = true gpui_tokio.workspace = true http_client.workspace = true diff --git a/crates/extension_host/src/wasm_host/wit/since_v0_8_0.rs b/crates/extension_host/src/wasm_host/wit/since_v0_8_0.rs index 57d5e808f13..e406544d24e 100644 --- a/crates/extension_host/src/wasm_host/wit/since_v0_8_0.rs +++ b/crates/extension_host/src/wasm_host/wit/since_v0_8_0.rs @@ -809,8 +809,8 @@ impl nodejs::Host for WasmState { #[async_trait] impl lsp::Host for WasmState {} -impl From<::http_client::github::GithubRelease> for github::GithubRelease { - fn from(value: ::http_client::github::GithubRelease) -> Self { +impl From<::github_releases::GithubRelease> for github::GithubRelease { + fn from(value: ::github_releases::GithubRelease) -> Self { Self { version: value.tag_name, assets: value.assets.into_iter().map(Into::into).collect(), @@ -818,8 +818,8 @@ impl From<::http_client::github::GithubRelease> for github::GithubRelease { } } -impl From<::http_client::github::GithubReleaseAsset> for github::GithubReleaseAsset { - fn from(value: ::http_client::github::GithubReleaseAsset) -> Self { +impl From<::github_releases::GithubReleaseAsset> for github::GithubReleaseAsset { + fn from(value: ::github_releases::GithubReleaseAsset) -> Self { Self { name: value.name, download_url: value.browser_download_url, @@ -835,7 +835,7 @@ impl github::Host for WasmState { options: github::GithubReleaseOptions, ) -> wasmtime::Result> { maybe!(async { - let release = ::http_client::github::latest_github_release( + let release = ::github_releases::latest_github_release( &repo, options.require_assets, options.pre_release, @@ -854,7 +854,7 @@ impl github::Host for WasmState { tag: String, ) -> wasmtime::Result> { maybe!(async { - let release = ::http_client::github::get_release_by_tag_name( + let release = ::github_releases::get_release_by_tag_name( &repo, &tag, self.host.http_client.clone(), diff --git a/crates/github_releases/Cargo.toml b/crates/github_releases/Cargo.toml new file mode 100644 index 00000000000..426b80301d3 --- /dev/null +++ b/crates/github_releases/Cargo.toml @@ -0,0 +1,31 @@ +[package] +name = "github_releases" +version = "0.1.0" +edition.workspace = true +publish.workspace = true +license = "Apache-2.0" + +[lints] +workspace = true + +[lib] +path = "src/github_releases.rs" +doctest = false + +[dependencies] +anyhow.workspace = true +async-compression.workspace = true +async-fs.workspace = true +async-tar.workspace = true +futures.workspace = true +gpui.workspace = true +http_client.workspace = true +log.workspace = true +serde.workspace = true +serde_json.workspace = true +sha2.workspace = true +tempfile.workspace = true +util.workspace = true + +[dev-dependencies] +gpui = { workspace = true, features = ["test-support"] } diff --git a/crates/github_releases/LICENSE-APACHE b/crates/github_releases/LICENSE-APACHE new file mode 120000 index 00000000000..1cd601d0a3a --- /dev/null +++ b/crates/github_releases/LICENSE-APACHE @@ -0,0 +1 @@ +../../LICENSE-APACHE \ No newline at end of file diff --git a/crates/github_releases/src/github_releases.rs b/crates/github_releases/src/github_releases.rs new file mode 100644 index 00000000000..5e8859a5f03 --- /dev/null +++ b/crates/github_releases/src/github_releases.rs @@ -0,0 +1,589 @@ +mod releases; + +pub use releases::{ + AssetKind, GitHubLspBinaryVersion, GithubRelease, GithubReleaseAsset, build_asset_url, + get_release_by_tag_name, latest_github_release, +}; + +use std::{path::Path, pin::Pin, sync::Arc, task::Poll}; + +use anyhow::{Context, Result}; +use async_compression::futures::bufread::{BzDecoder, GzipDecoder}; +use futures::{AsyncRead, AsyncSeekExt, AsyncWrite, io::BufReader}; +use gpui::{BackgroundExecutor, Task}; +use http_client::HttpClient; +use sha2::{Digest, Sha256}; + +fn sha256_matches(actual: &str, expected: &str) -> bool { + actual.eq_ignore_ascii_case(expected) +} + +#[derive(serde::Deserialize, serde::Serialize, Debug)] +pub struct GithubBinaryMetadata { + pub metadata_version: u64, + pub digest: Option, +} + +impl GithubBinaryMetadata { + pub async fn read_from_file(metadata_path: &Path) -> Result { + let metadata_content = async_fs::read_to_string(metadata_path) + .await + .with_context(|| format!("reading metadata file at {metadata_path:?}"))?; + serde_json::from_str(&metadata_content) + .with_context(|| format!("parsing metadata file at {metadata_path:?}")) + } + + pub async fn write_to_file(&self, metadata_path: &Path) -> Result<()> { + let metadata_content = serde_json::to_string(self) + .with_context(|| format!("serializing metadata for {metadata_path:?}"))?; + async_fs::write(metadata_path, metadata_content.as_bytes()) + .await + .with_context(|| format!("writing metadata file at {metadata_path:?}"))?; + Ok(()) + } +} + +pub fn download_server_binary( + executor: &BackgroundExecutor, + http_client: Arc, + url: &str, + digest: Option<&str>, + destination_path: &Path, + asset_kind: AssetKind, +) -> Task> { + spawn_download( + executor, + http_client, + url, + digest, + destination_path, + DownloadedAsset::Archive(asset_kind), + ) +} + +pub fn download_server_raw_binary( + executor: &BackgroundExecutor, + http_client: Arc, + url: &str, + digest: Option<&str>, + destination_path: &Path, + binary_file_name: &str, +) -> Task> { + spawn_download( + executor, + http_client, + url, + digest, + destination_path, + DownloadedAsset::RawBinary(binary_file_name.to_owned()), + ) +} + +enum DownloadedAsset { + Archive(AssetKind), + RawBinary(String), +} + +fn spawn_download( + executor: &BackgroundExecutor, + http_client: Arc, + url: &str, + digest: Option<&str>, + destination_path: &Path, + downloaded_asset: DownloadedAsset, +) -> Task> { + let url = url.to_owned(); + let digest = digest.map(ToOwned::to_owned); + let destination_path = destination_path.to_owned(); + executor.spawn(async move { + download_server_asset( + &*http_client, + &url, + digest.as_deref(), + &destination_path, + downloaded_asset, + ) + .await + }) +} + +async fn download_server_asset( + http_client: &dyn HttpClient, + url: &str, + digest: Option<&str>, + destination_path: &Path, + downloaded_asset: DownloadedAsset, +) -> Result<()> { + log::info!("downloading {url} into {destination_path:?}"); + let mut response = http_client + .get(url, Default::default(), true) + .await + .with_context(|| format!("downloading release from {url}"))?; + anyhow::ensure!( + response.status().is_success(), + "downloading {url} failed with status {status}", + status = response.status() + ); + + let destination_parent = destination_path + .parent() + .with_context(|| format!("destination path has no parent: {destination_path:?}"))?; + let staging_directory = tempfile::Builder::new() + .prefix(".tmp-github-download-") + .tempdir_in(destination_parent) + .with_context(|| format!("creating staging directory in {destination_parent:?}"))?; + let staged_path = staging_directory.path().join("download"); + + match downloaded_asset { + DownloadedAsset::Archive(asset_kind) => { + download_and_extract( + response.body_mut(), + digest, + url, + staging_directory.path(), + &staged_path, + asset_kind, + ) + .await? + } + DownloadedAsset::RawBinary(binary_file_name) => { + async_fs::create_dir(&staged_path) + .await + .with_context(|| format!("creating directory {staged_path:?}"))?; + let binary_path = staged_path.join(binary_file_name); + let mut binary_file = async_fs::File::create(&binary_path) + .await + .with_context(|| format!("creating a file {binary_path:?} for {url}"))?; + download_to_writer(response.body_mut(), &mut binary_file, digest, url).await?; + util::fs::make_file_executable(&binary_path) + .await + .with_context(|| format!("marking {binary_path:?} as executable"))?; + } + } + + // `remove_dir_all` removes a symlink itself without following it, including dangling ones. + if async_fs::symlink_metadata(destination_path) + .await + .is_ok_and(|metadata| metadata.is_dir() || metadata.is_symlink()) + { + async_fs::remove_dir_all(destination_path) + .await + .with_context(|| format!("removing existing {destination_path:?}"))?; + } + async_fs::rename(&staged_path, destination_path) + .await + .with_context(|| format!("renaming {staged_path:?} to {destination_path:?}")) +} + +async fn download_and_extract( + body: impl AsyncRead + Unpin, + digest: Option<&str>, + url: &str, + staging_directory: &Path, + staged_path: &Path, + asset_kind: AssetKind, +) -> Result<()> { + // Buffer on the destination's volume rather than a possibly RAM-backed system temp dir. + let mut asset_file = + async_fs::File::from(tempfile::tempfile_in(staging_directory).with_context(|| { + format!("creating a temporary file for {url} in {staging_directory:?}") + })?); + download_to_writer(body, &mut asset_file, digest, url).await?; + asset_file + .seek(std::io::SeekFrom::Start(0)) + .await + .with_context(|| format!("seeking to the start of the temporary file for {url}"))?; + + if asset_kind != AssetKind::Gz { + async_fs::create_dir(staged_path) + .await + .with_context(|| format!("creating directory {staged_path:?}"))?; + } + stream_file_archive(asset_file, url, staged_path, asset_kind) + .await + .with_context(|| format!("extracting {url} into {staged_path:?}")) +} + +async fn download_to_writer( + body: impl AsyncRead + Unpin, + writer: &mut (impl AsyncWrite + Unpin), + digest: Option<&str>, + url: &str, +) -> Result<()> { + let mut writer = HashingWriter { + writer, + hasher: Sha256::new(), + }; + futures::io::copy(&mut BufReader::new(body), &mut writer) + .await + .with_context(|| format!("saving downloaded contents from {url}"))?; + + if let Some(expected_sha_256) = digest { + let asset_sha_256 = format!("{:x}", writer.hasher.finalize()); + anyhow::ensure!( + sha256_matches(&asset_sha_256, expected_sha_256), + "{url} asset got SHA-256 mismatch. Expected: {expected_sha_256}, Got: {asset_sha_256}", + ); + } + + Ok(()) +} + +async fn stream_file_archive( + file_archive: async_fs::File, + url: &str, + destination_path: &Path, + asset_kind: AssetKind, +) -> Result<()> { + match asset_kind { + AssetKind::TarGz => extract_tar_gz(destination_path, url, file_archive).await?, + AssetKind::TarBz2 => extract_tar_bz2(destination_path, url, file_archive).await?, + AssetKind::Gz => extract_gz(destination_path, url, file_archive).await?, + #[cfg(not(windows))] + AssetKind::Zip => { + util::archive::extract_seekable_zip(destination_path, file_archive).await?; + } + #[cfg(windows)] + AssetKind::Zip => { + util::archive::extract_zip(destination_path, file_archive).await?; + } + }; + Ok(()) +} + +async fn extract_tar_gz( + destination_path: &Path, + url: &str, + from: impl AsyncRead + Unpin, +) -> Result<(), anyhow::Error> { + let decompressed_bytes = GzipDecoder::new(BufReader::new(from)); + unpack_tar_archive(destination_path, url, decompressed_bytes).await?; + Ok(()) +} + +async fn extract_tar_bz2( + destination_path: &Path, + url: &str, + from: impl AsyncRead + Unpin, +) -> Result<(), anyhow::Error> { + let decompressed_bytes = BzDecoder::new(BufReader::new(from)); + unpack_tar_archive(destination_path, url, decompressed_bytes).await?; + Ok(()) +} + +async fn unpack_tar_archive( + destination_path: &Path, + url: &str, + archive_bytes: impl AsyncRead + Unpin, +) -> Result<(), anyhow::Error> { + // We don't need to set the modified time. It's irrelevant to downloaded + // archive verification, and some filesystems return errors when asked to + // apply it after extraction. + let archive = async_tar::ArchiveBuilder::new(archive_bytes) + .set_preserve_mtime(false) + .build(); + archive + .unpack(&destination_path) + .await + .with_context(|| format!("extracting {url} to {destination_path:?}"))?; + Ok(()) +} + +async fn extract_gz( + destination_path: &Path, + url: &str, + from: impl AsyncRead + Unpin, +) -> Result<(), anyhow::Error> { + let mut decompressed_bytes = GzipDecoder::new(BufReader::new(from)); + let mut file = async_fs::File::create(&destination_path) + .await + .with_context(|| { + format!("creating a file {destination_path:?} for a download from {url}") + })?; + futures::io::copy(&mut decompressed_bytes, &mut file) + .await + .with_context(|| format!("extracting {url} to {destination_path:?}"))?; + Ok(()) +} + +struct HashingWriter { + writer: W, + hasher: Sha256, +} + +impl AsyncWrite for HashingWriter { + fn poll_write( + mut self: Pin<&mut Self>, + cx: &mut std::task::Context<'_>, + buf: &[u8], + ) -> Poll> { + match Pin::new(&mut self.writer).poll_write(cx, buf) { + Poll::Ready(Ok(n)) => { + self.hasher.update(&buf[..n]); + Poll::Ready(Ok(n)) + } + other => other, + } + } + + fn poll_flush( + mut self: Pin<&mut Self>, + cx: &mut std::task::Context<'_>, + ) -> Poll> { + Pin::new(&mut self.writer).poll_flush(cx) + } + + fn poll_close( + mut self: Pin<&mut Self>, + cx: &mut std::task::Context<'_>, + ) -> Poll> { + Pin::new(&mut self.writer).poll_close(cx) + } +} + +#[cfg(test)] +mod tests { + use super::*; + use futures::future::BoxFuture; + use http_client::{AsyncBody, Response, Url, http::HeaderValue}; + + struct StaticResponseClient { + status: u16, + body: Vec, + } + + impl HttpClient for StaticResponseClient { + fn send( + &self, + _req: http_client::Request, + ) -> BoxFuture<'static, anyhow::Result>> { + let status = self.status; + let body = self.body.clone(); + Box::pin(async move { + Ok(Response::builder() + .status(status) + .body(AsyncBody::from(body)) + .unwrap()) + }) + } + + fn user_agent(&self) -> Option<&HeaderValue> { + None + } + + fn proxy(&self) -> Option<&Url> { + None + } + } + + #[gpui::test] + async fn downloads_raw_binary_with_uppercase_digest_into_destination_dir( + executor: BackgroundExecutor, + ) { + executor.allow_parking(); + let temp_dir = tempfile::tempdir().unwrap(); + let destination_path = temp_dir.path().join("v_1"); + let contents = b"#!/bin/sh\necho hello\n".to_vec(); + let expected_sha_256 = format!("{:X}", Sha256::digest(&contents)); + let client = StaticResponseClient { + status: 200, + body: contents, + }; + + download_server_raw_binary( + &executor, + Arc::new(client), + "https://example.com/agent-binary", + Some(&expected_sha_256), + &destination_path, + "agent-binary", + ) + .await + .unwrap(); + + let binary_path = destination_path.join("agent-binary"); + assert_eq!( + std::fs::read(&binary_path).unwrap(), + b"#!/bin/sh\necho hello\n" + ); + #[cfg(unix)] + { + use std::os::unix::fs::PermissionsExt; + let mode = std::fs::metadata(&binary_path) + .unwrap() + .permissions() + .mode(); + assert_eq!(mode & 0o111, 0o111, "binary should be executable"); + } + } + + #[gpui::test] + async fn raw_binary_without_digest_rejects_failed_response(executor: BackgroundExecutor) { + executor.allow_parking(); + let temp_dir = tempfile::tempdir().unwrap(); + let destination_path = temp_dir.path().join("v_1"); + let client = StaticResponseClient { + status: 403, + body: b"rate limit exceeded".to_vec(), + }; + + let error = download_server_raw_binary( + &executor, + Arc::new(client), + "https://example.com/agent-binary", + None, + &destination_path, + "agent-binary", + ) + .await + .unwrap_err(); + + assert!(error.to_string().contains("403")); + assert!(!destination_path.exists()); + let leftover_entries = std::fs::read_dir(temp_dir.path()).unwrap().count(); + assert_eq!( + leftover_entries, 0, + "no staging directory should be created" + ); + } + + #[gpui::test] + async fn raw_binary_digest_mismatch_cleans_up_staging(executor: BackgroundExecutor) { + executor.allow_parking(); + let temp_dir = tempfile::tempdir().unwrap(); + let destination_path = temp_dir.path().join("v_1"); + let client = StaticResponseClient { + status: 200, + body: b"some binary".to_vec(), + }; + + let error = download_server_raw_binary( + &executor, + Arc::new(client), + "https://example.com/agent-binary", + Some("0000000000000000000000000000000000000000000000000000000000000000"), + &destination_path, + "agent-binary", + ) + .await + .unwrap_err(); + + assert!(error.to_string().contains("SHA-256 mismatch")); + assert!(!destination_path.exists()); + let leftover_entries = std::fs::read_dir(temp_dir.path()).unwrap().count(); + assert_eq!(leftover_entries, 0, "staging directory should be removed"); + } + + #[gpui::test] + async fn downloads_archive_with_uppercase_digest_and_extracts_contents( + executor: BackgroundExecutor, + ) { + executor.allow_parking(); + let archive = vec![ + 0x50, 0x4b, 0x03, 0x04, 0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x21, 0x00, + 0x86, 0xa6, 0x10, 0x36, 0x05, 0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, 0x05, 0x00, + 0x00, 0x00, 0x61, 0x67, 0x65, 0x6e, 0x74, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x50, 0x4b, + 0x01, 0x02, 0x14, 0x03, 0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x21, 0x00, + 0x86, 0xa6, 0x10, 0x36, 0x05, 0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, 0x05, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x01, 0x00, 0x00, + 0x00, 0x00, 0x61, 0x67, 0x65, 0x6e, 0x74, 0x50, 0x4b, 0x05, 0x06, 0x00, 0x00, 0x00, + 0x00, 0x01, 0x00, 0x01, 0x00, 0x33, 0x00, 0x00, 0x00, 0x28, 0x00, 0x00, 0x00, 0x00, + 0x00, + ]; + let expected_sha_256 = format!("{:X}", Sha256::digest(&archive)); + let client = StaticResponseClient { + status: 200, + body: archive, + }; + let temp_dir = tempfile::tempdir().unwrap(); + let destination_path = temp_dir.path().join("v_1"); + + download_server_binary( + &executor, + Arc::new(client), + "https://example.com/agent.zip", + Some(&expected_sha_256), + &destination_path, + AssetKind::Zip, + ) + .await + .unwrap(); + + assert_eq!( + std::fs::read(destination_path.join("agent")).unwrap(), + b"hello" + ); + } + + #[cfg(unix)] + #[gpui::test] + async fn replaces_symlinked_destination_without_touching_its_target( + executor: BackgroundExecutor, + ) { + executor.allow_parking(); + let temp_dir = tempfile::tempdir().unwrap(); + let symlink_target = temp_dir.path().join("target"); + std::fs::create_dir(&symlink_target).unwrap(); + std::fs::write(symlink_target.join("existing"), b"keep").unwrap(); + let destination_path = temp_dir.path().join("v_1"); + std::os::unix::fs::symlink(&symlink_target, &destination_path).unwrap(); + let client = StaticResponseClient { + status: 200, + body: b"binary".to_vec(), + }; + + download_server_raw_binary( + &executor, + Arc::new(client), + "https://example.com/agent-binary", + None, + &destination_path, + "agent-binary", + ) + .await + .unwrap(); + + assert!( + !std::fs::symlink_metadata(&destination_path) + .unwrap() + .is_symlink() + ); + assert_eq!( + std::fs::read(destination_path.join("agent-binary")).unwrap(), + b"binary" + ); + assert_eq!( + std::fs::read(symlink_target.join("existing")).unwrap(), + b"keep" + ); + } + + #[gpui::test] + async fn archive_digest_mismatch_prevents_extraction_and_cleans_up_staging( + executor: BackgroundExecutor, + ) { + executor.allow_parking(); + let temp_dir = tempfile::tempdir().unwrap(); + let destination_path = temp_dir.path().join("v_1"); + let client = StaticResponseClient { + status: 200, + body: b"not an archive".to_vec(), + }; + + let error = download_server_binary( + &executor, + Arc::new(client), + "https://example.com/agent.zip", + Some("0000000000000000000000000000000000000000000000000000000000000000"), + &destination_path, + AssetKind::Zip, + ) + .await + .unwrap_err(); + + assert!(error.to_string().contains("SHA-256 mismatch")); + assert!(!destination_path.exists()); + let leftover_entries = std::fs::read_dir(temp_dir.path()).unwrap().count(); + assert_eq!(leftover_entries, 0, "staging directory should be removed"); + } +} diff --git a/crates/http_client/src/github.rs b/crates/github_releases/src/releases.rs similarity index 95% rename from crates/http_client/src/github.rs rename to crates/github_releases/src/releases.rs index 93f840e7426..1074d97c875 100644 --- a/crates/http_client/src/github.rs +++ b/crates/github_releases/src/releases.rs @@ -1,10 +1,8 @@ -use crate::{AsyncBody, HttpClient, HttpRequestExt}; use anyhow::{Context as _, Result, anyhow, bail}; use futures::AsyncReadExt; -use http::Request; +use http_client::{AsyncBody, HttpClient, HttpRequestExt, Request, Url}; use serde::{Deserialize, Deserializer}; use std::{sync::Arc, time::Duration}; -use url::Url; const GITHUB_API_URL: &str = "https://api.github.com"; const GITHUB_RELEASE_REQUEST_TIMEOUT: Duration = Duration::from_secs(10); @@ -142,7 +140,7 @@ pub async fn get_release_by_tag_name( fn github_api_request(url: &str) -> Result> { Request::get(url) - .follow_redirects(crate::RedirectPolicy::FollowAll) + .follow_redirects(http_client::RedirectPolicy::FollowAll) .timeout(GITHUB_RELEASE_REQUEST_TIMEOUT) .when_some(std::env::var("GITHUB_TOKEN").ok(), |builder, token| { builder.header("Authorization", format!("Bearer {}", token)) @@ -182,13 +180,11 @@ pub fn build_asset_url(repo_name_with_owner: &str, tag: &str, kind: AssetKind) - #[cfg(test)] mod tests { - use crate::{ - RequestTimeout, - github::{ - AssetKind, GITHUB_RELEASE_REQUEST_TIMEOUT, GithubReleaseAsset, build_asset_url, - github_api_request, - }, + use super::{ + AssetKind, GITHUB_RELEASE_REQUEST_TIMEOUT, GithubReleaseAsset, build_asset_url, + github_api_request, }; + use http_client::RequestTimeout; #[test] fn test_asset_digest_deserialization() { diff --git a/crates/http_client/Cargo.toml b/crates/http_client/Cargo.toml index 6c1a58aba6c..7e3ad20b247 100644 --- a/crates/http_client/Cargo.toml +++ b/crates/http_client/Cargo.toml @@ -11,17 +11,10 @@ workspace = true [features] test-support = ["dep:log"] -github-download = [ - "dep:async-compression", - "dep:async-fs", - "dep:async-tar", - "dep:sha2", - "dep:tempfile", - "dep:util", -] [lib] path = "src/http_client.rs" +test = false doctest = false [dependencies] @@ -36,12 +29,3 @@ serde.workspace = true serde_json.workspace = true serde_urlencoded.workspace = true url.workspace = true - -[target.'cfg(not(target_family = "wasm"))'.dependencies] -log.workspace = true -async-compression = { workspace = true, optional = true } -util = { workspace = true, optional = true } -async-fs = { workspace = true, optional = true } -async-tar = { workspace = true, optional = true } -sha2 = { workspace = true, optional = true } -tempfile = { workspace = true, optional = true } diff --git a/crates/http_client/src/github_download.rs b/crates/http_client/src/github_download.rs deleted file mode 100644 index bbb85c52491..00000000000 --- a/crates/http_client/src/github_download.rs +++ /dev/null @@ -1,532 +0,0 @@ -use std::{ - path::{Path, PathBuf}, - pin::Pin, - task::Poll, -}; - -use anyhow::{Context, Result}; -use async_compression::futures::bufread::{BzDecoder, GzipDecoder}; -use futures::{AsyncRead, AsyncSeek, AsyncSeekExt, AsyncWrite, AsyncWriteExt, io::BufReader}; -use sha2::{Digest, Sha256}; - -use crate::{HttpClient, github::AssetKind}; - -fn sha256_matches(actual: &str, expected: &str) -> bool { - actual.eq_ignore_ascii_case(expected) -} - -#[derive(serde::Deserialize, serde::Serialize, Debug)] -pub struct GithubBinaryMetadata { - pub metadata_version: u64, - pub digest: Option, -} - -impl GithubBinaryMetadata { - pub async fn read_from_file(metadata_path: &Path) -> Result { - let metadata_content = async_fs::read_to_string(metadata_path) - .await - .with_context(|| format!("reading metadata file at {metadata_path:?}"))?; - serde_json::from_str(&metadata_content) - .with_context(|| format!("parsing metadata file at {metadata_path:?}")) - } - - pub async fn write_to_file(&self, metadata_path: &Path) -> Result<()> { - let metadata_content = serde_json::to_string(self) - .with_context(|| format!("serializing metadata for {metadata_path:?}"))?; - async_fs::write(metadata_path, metadata_content.as_bytes()) - .await - .with_context(|| format!("writing metadata file at {metadata_path:?}"))?; - Ok(()) - } -} - -pub async fn download_server_binary( - http_client: &dyn HttpClient, - url: &str, - digest: Option<&str>, - destination_path: &Path, - asset_kind: AssetKind, -) -> Result<(), anyhow::Error> { - log::info!("downloading github artifact from {url}"); - let Some(destination_parent) = destination_path.parent() else { - anyhow::bail!("destination path has no parent: {destination_path:?}"); - }; - - let staging_path = staging_path(destination_parent, asset_kind)?; - let mut response = http_client - .get(url, Default::default(), true) - .await - .with_context(|| format!("downloading release from {url}"))?; - let body = response.body_mut(); - - if let Err(err) = extract_to_staging(body, digest, url, &staging_path, asset_kind).await { - cleanup_staging_path(&staging_path, asset_kind).await; - return Err(err); - } - - if let Err(err) = finalize_download(&staging_path, destination_path).await { - cleanup_staging_path(&staging_path, asset_kind).await; - return Err(err); - } - - Ok(()) -} - -pub async fn download_server_raw_binary( - http_client: &dyn HttpClient, - url: &str, - digest: Option<&str>, - destination_path: &Path, - binary_file_name: &str, -) -> Result<(), anyhow::Error> { - log::info!("downloading raw binary from {url}"); - let Some(destination_parent) = destination_path.parent() else { - anyhow::bail!("destination path has no parent: {destination_path:?}"); - }; - - let staging_path = staging_dir_path(destination_parent)?; - let result = async { - let mut response = http_client - .get(url, Default::default(), true) - .await - .with_context(|| format!("downloading release from {url}"))?; - - let binary_path = staging_path.join(binary_file_name); - let mut writer = HashingWriter { - writer: async_fs::File::create(&binary_path) - .await - .with_context(|| format!("creating a file {binary_path:?} for {url}"))?, - hasher: Sha256::new(), - }; - futures::io::copy(&mut BufReader::new(response.body_mut()), &mut writer) - .await - .with_context(|| format!("saving binary contents from {url}"))?; - let asset_sha_256 = writer - .finish() - .await - .with_context(|| format!("flushing binary contents for {url}"))?; - - if let Some(expected_sha_256) = digest { - anyhow::ensure!( - sha256_matches(&asset_sha_256, expected_sha_256), - "{url} asset got SHA-256 mismatch. Expected: {expected_sha_256}, Got: {asset_sha_256}", - ); - } - - util::fs::make_file_executable(&binary_path) - .await - .with_context(|| format!("marking {binary_path:?} as executable"))?; - finalize_download(&staging_path, destination_path).await - } - .await; - - if let Err(err) = result { - if let Err(err) = async_fs::remove_dir_all(&staging_path).await { - log::warn!("failed to remove staging directory {staging_path:?}: {err:?}"); - } - return Err(err); - } - - Ok(()) -} - -async fn extract_to_staging( - body: impl AsyncRead + Unpin, - digest: Option<&str>, - url: &str, - staging_path: &Path, - asset_kind: AssetKind, -) -> Result<()> { - match digest { - Some(expected_sha_256) => { - let temp_asset_file = tempfile::NamedTempFile::new() - .with_context(|| format!("creating a temporary file for {url}"))?; - let (temp_asset_file, _temp_guard) = temp_asset_file.into_parts(); - let mut writer = HashingWriter { - writer: async_fs::File::from(temp_asset_file), - hasher: Sha256::new(), - }; - futures::io::copy(&mut BufReader::new(body), &mut writer) - .await - .with_context(|| { - format!("saving archive contents into the temporary file for {url}") - })?; - let asset_sha_256 = format!("{:x}", writer.hasher.finalize()); - - anyhow::ensure!( - sha256_matches(&asset_sha_256, expected_sha_256), - "{url} asset got SHA-256 mismatch. Expected: {expected_sha_256}, Got: {asset_sha_256}", - ); - writer - .writer - .seek(std::io::SeekFrom::Start(0)) - .await - .with_context(|| format!("seeking temporary file for {url}"))?; - stream_file_archive(&mut writer.writer, url, staging_path, asset_kind) - .await - .with_context(|| { - format!("extracting downloaded asset for {url} into {staging_path:?}") - })?; - } - None => { - stream_response_archive(body, url, staging_path, asset_kind) - .await - .with_context(|| { - format!("extracting response for asset {url} into {staging_path:?}") - })?; - } - } - Ok(()) -} - -fn staging_dir_path(parent: &Path) -> Result { - let dir = tempfile::Builder::new() - .prefix(".tmp-github-download-") - .tempdir_in(parent) - .with_context(|| format!("creating staging directory in {parent:?}"))?; - Ok(dir.keep()) -} - -fn staging_path(parent: &Path, asset_kind: AssetKind) -> Result { - match asset_kind { - AssetKind::TarGz | AssetKind::TarBz2 | AssetKind::Zip => staging_dir_path(parent), - AssetKind::Gz => { - let path = tempfile::Builder::new() - .prefix(".tmp-github-download-") - .tempfile_in(parent) - .with_context(|| format!("creating staging file in {parent:?}"))? - .into_temp_path() - .keep() - .with_context(|| format!("persisting staging file in {parent:?}"))?; - Ok(path) - } - } -} - -async fn cleanup_staging_path(staging_path: &Path, asset_kind: AssetKind) { - match asset_kind { - AssetKind::TarGz | AssetKind::TarBz2 | AssetKind::Zip => { - if let Err(err) = async_fs::remove_dir_all(staging_path).await { - log::warn!("failed to remove staging directory {staging_path:?}: {err:?}"); - } - } - AssetKind::Gz => { - if let Err(err) = async_fs::remove_file(staging_path).await { - log::warn!("failed to remove staging file {staging_path:?}: {err:?}"); - } - } - } -} - -async fn finalize_download(staging_path: &Path, destination_path: &Path) -> Result<()> { - _ = async_fs::remove_dir_all(destination_path).await; - async_fs::rename(staging_path, destination_path) - .await - .with_context(|| format!("renaming {staging_path:?} to {destination_path:?}"))?; - Ok(()) -} - -async fn stream_response_archive( - response: impl AsyncRead + Unpin, - url: &str, - destination_path: &Path, - asset_kind: AssetKind, -) -> Result<()> { - match asset_kind { - AssetKind::TarGz => extract_tar_gz(destination_path, url, response).await?, - AssetKind::TarBz2 => extract_tar_bz2(destination_path, url, response).await?, - AssetKind::Gz => extract_gz(destination_path, url, response).await?, - AssetKind::Zip => { - util::archive::extract_zip(destination_path, response).await?; - } - }; - Ok(()) -} - -async fn stream_file_archive( - file_archive: impl AsyncRead + AsyncSeek + Unpin, - url: &str, - destination_path: &Path, - asset_kind: AssetKind, -) -> Result<()> { - match asset_kind { - AssetKind::TarGz => extract_tar_gz(destination_path, url, file_archive).await?, - AssetKind::TarBz2 => extract_tar_bz2(destination_path, url, file_archive).await?, - AssetKind::Gz => extract_gz(destination_path, url, file_archive).await?, - #[cfg(not(windows))] - AssetKind::Zip => { - util::archive::extract_seekable_zip(destination_path, file_archive).await?; - } - #[cfg(windows)] - AssetKind::Zip => { - util::archive::extract_zip(destination_path, file_archive).await?; - } - }; - Ok(()) -} - -async fn extract_tar_gz( - destination_path: &Path, - url: &str, - from: impl AsyncRead + Unpin, -) -> Result<(), anyhow::Error> { - let decompressed_bytes = GzipDecoder::new(BufReader::new(from)); - unpack_tar_archive(destination_path, url, decompressed_bytes).await?; - Ok(()) -} - -async fn extract_tar_bz2( - destination_path: &Path, - url: &str, - from: impl AsyncRead + Unpin, -) -> Result<(), anyhow::Error> { - let decompressed_bytes = BzDecoder::new(BufReader::new(from)); - unpack_tar_archive(destination_path, url, decompressed_bytes).await?; - Ok(()) -} - -async fn unpack_tar_archive( - destination_path: &Path, - url: &str, - archive_bytes: impl AsyncRead + Unpin, -) -> Result<(), anyhow::Error> { - // We don't need to set the modified time. It's irrelevant to downloaded - // archive verification, and some filesystems return errors when asked to - // apply it after extraction. - let archive = async_tar::ArchiveBuilder::new(archive_bytes) - .set_preserve_mtime(false) - .build(); - archive - .unpack(&destination_path) - .await - .with_context(|| format!("extracting {url} to {destination_path:?}"))?; - Ok(()) -} - -async fn extract_gz( - destination_path: &Path, - url: &str, - from: impl AsyncRead + Unpin, -) -> Result<(), anyhow::Error> { - let mut decompressed_bytes = GzipDecoder::new(BufReader::new(from)); - let mut file = async_fs::File::create(&destination_path) - .await - .with_context(|| { - format!("creating a file {destination_path:?} for a download from {url}") - })?; - futures::io::copy(&mut decompressed_bytes, &mut file) - .await - .with_context(|| format!("extracting {url} to {destination_path:?}"))?; - Ok(()) -} - -struct HashingWriter { - writer: W, - hasher: Sha256, -} - -impl HashingWriter { - /// Closes and drops the inner writer, returning the hex SHA-256 digest of - /// everything written. - /// - /// Taking `self` by value guarantees the writer is dropped before this - /// returns. For file writers this releases the OS handle, which Windows - /// requires before an ancestor directory can be renamed or deleted; note - /// that closing alone is not enough, as `async_fs::File` holds its handle - /// until dropped. - async fn finish(mut self) -> std::io::Result { - self.writer.close().await?; - drop(self.writer); - Ok(format!("{:x}", self.hasher.finalize())) - } -} - -impl AsyncWrite for HashingWriter { - fn poll_write( - mut self: Pin<&mut Self>, - cx: &mut std::task::Context<'_>, - buf: &[u8], - ) -> Poll> { - match Pin::new(&mut self.writer).poll_write(cx, buf) { - Poll::Ready(Ok(n)) => { - self.hasher.update(&buf[..n]); - Poll::Ready(Ok(n)) - } - other => other, - } - } - - fn poll_flush( - mut self: Pin<&mut Self>, - cx: &mut std::task::Context<'_>, - ) -> Poll> { - Pin::new(&mut self.writer).poll_flush(cx) - } - - fn poll_close( - mut self: Pin<&mut Self>, - cx: &mut std::task::Context<'_>, - ) -> Poll> { - Pin::new(&mut self.writer).poll_close(cx) - } -} - -#[cfg(test)] -mod tests { - use super::*; - use crate::{AsyncBody, Response}; - use futures::future::BoxFuture; - use http::HeaderValue; - use url::Url; - - struct StaticResponseClient { - body: Vec, - } - - impl HttpClient for StaticResponseClient { - fn send( - &self, - _req: http::Request, - ) -> BoxFuture<'static, anyhow::Result>> { - let body = self.body.clone(); - Box::pin(async move { - Ok(Response::builder() - .status(200) - .body(AsyncBody::from(body)) - .unwrap()) - }) - } - - fn user_agent(&self) -> Option<&HeaderValue> { - None - } - - fn proxy(&self) -> Option<&Url> { - None - } - } - - #[test] - fn downloads_raw_binary_with_uppercase_digest_into_destination_dir() { - futures::executor::block_on(async { - let temp_dir = tempfile::tempdir().unwrap(); - let destination_path = temp_dir.path().join("v_1"); - let contents = b"#!/bin/sh\necho hello\n".to_vec(); - let expected_sha_256 = format!("{:X}", Sha256::digest(&contents)); - let client = StaticResponseClient { body: contents }; - - download_server_raw_binary( - &client, - "https://example.com/agent-binary", - Some(&expected_sha_256), - &destination_path, - "agent-binary", - ) - .await - .unwrap(); - - let binary_path = destination_path.join("agent-binary"); - assert_eq!( - std::fs::read(&binary_path).unwrap(), - b"#!/bin/sh\necho hello\n" - ); - #[cfg(unix)] - { - use std::os::unix::fs::PermissionsExt; - let mode = std::fs::metadata(&binary_path) - .unwrap() - .permissions() - .mode(); - assert_eq!(mode & 0o111, 0o111, "binary should be executable"); - } - }); - } - - #[test] - fn raw_binary_digest_mismatch_cleans_up_staging() { - futures::executor::block_on(async { - let temp_dir = tempfile::tempdir().unwrap(); - let destination_path = temp_dir.path().join("v_1"); - let client = StaticResponseClient { - body: b"some binary".to_vec(), - }; - - let error = download_server_raw_binary( - &client, - "https://example.com/agent-binary", - Some("0000000000000000000000000000000000000000000000000000000000000000"), - &destination_path, - "agent-binary", - ) - .await - .unwrap_err(); - - assert!(error.to_string().contains("SHA-256 mismatch")); - assert!(!destination_path.exists()); - let leftover_entries = std::fs::read_dir(temp_dir.path()).unwrap().count(); - assert_eq!(leftover_entries, 0, "staging directory should be removed"); - }); - } - - #[test] - fn downloads_archive_with_uppercase_digest_and_extracts_contents() { - futures::executor::block_on(async { - let archive = vec![ - 0x50, 0x4b, 0x03, 0x04, 0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x21, 0x00, - 0x86, 0xa6, 0x10, 0x36, 0x05, 0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, 0x05, 0x00, - 0x00, 0x00, 0x61, 0x67, 0x65, 0x6e, 0x74, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x50, 0x4b, - 0x01, 0x02, 0x14, 0x03, 0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x21, 0x00, - 0x86, 0xa6, 0x10, 0x36, 0x05, 0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, 0x05, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x01, 0x00, 0x00, - 0x00, 0x00, 0x61, 0x67, 0x65, 0x6e, 0x74, 0x50, 0x4b, 0x05, 0x06, 0x00, 0x00, 0x00, - 0x00, 0x01, 0x00, 0x01, 0x00, 0x33, 0x00, 0x00, 0x00, 0x28, 0x00, 0x00, 0x00, 0x00, - 0x00, - ]; - let expected_sha_256 = format!("{:X}", Sha256::digest(&archive)); - let client = StaticResponseClient { body: archive }; - let temp_dir = tempfile::tempdir().unwrap(); - let destination_path = temp_dir.path().join("v_1"); - - download_server_binary( - &client, - "https://example.com/agent.zip", - Some(&expected_sha_256), - &destination_path, - AssetKind::Zip, - ) - .await - .unwrap(); - - assert_eq!( - std::fs::read(destination_path.join("agent")).unwrap(), - b"hello" - ); - }); - } - - #[test] - fn archive_digest_mismatch_prevents_extraction_and_cleans_up_staging() { - futures::executor::block_on(async { - let temp_dir = tempfile::tempdir().unwrap(); - let destination_path = temp_dir.path().join("v_1"); - let client = StaticResponseClient { - body: b"not an archive".to_vec(), - }; - - let error = download_server_binary( - &client, - "https://example.com/agent.zip", - Some("0000000000000000000000000000000000000000000000000000000000000000"), - &destination_path, - AssetKind::Zip, - ) - .await - .unwrap_err(); - - assert!(error.to_string().contains("SHA-256 mismatch")); - assert!(!destination_path.exists()); - let leftover_entries = std::fs::read_dir(temp_dir.path()).unwrap().count(); - assert_eq!(leftover_entries, 0, "staging directory should be removed"); - }); - } -} diff --git a/crates/http_client/src/http_client.rs b/crates/http_client/src/http_client.rs index 380ff535d97..41d210258b3 100644 --- a/crates/http_client/src/http_client.rs +++ b/crates/http_client/src/http_client.rs @@ -1,8 +1,4 @@ mod async_body; -#[cfg(not(target_family = "wasm"))] -pub mod github; -#[cfg(all(not(target_family = "wasm"), feature = "github-download"))] -pub mod github_download; pub use anyhow::{Result, anyhow}; pub use async_body::{AsyncBody, Inner, Json}; diff --git a/crates/language/src/language.rs b/crates/language/src/language.rs index 82a218564db..cf2dfcbd523 100644 --- a/crates/language/src/language.rs +++ b/crates/language/src/language.rs @@ -38,7 +38,7 @@ use collections::{HashMap, HashSet}; use futures::Future; use futures::future::LocalBoxFuture; use futures::lock::OwnedMutexGuard; -use gpui::{App, AsyncApp, Entity, EntityId, SharedString}; +use gpui::{App, AsyncApp, BackgroundExecutor, Entity, EntityId, SharedString}; use http_client::HttpClient; pub use language_core::{ @@ -500,6 +500,7 @@ impl CachedLspAdapter { pub trait LspAdapterDelegate: Send + Sync { fn show_notification(&self, message: &str, cx: &mut App); fn http_client(&self) -> Arc; + fn background_executor(&self) -> &BackgroundExecutor; fn worktree_id(&self) -> WorktreeId; fn worktree_root_path(&self) -> &Path; fn resolve_relative_path(&self, path: PathBuf) -> PathBuf; diff --git a/crates/languages/Cargo.toml b/crates/languages/Cargo.toml index 5e0d8003eab..f300452a062 100644 --- a/crates/languages/Cargo.toml +++ b/crates/languages/Cargo.toml @@ -30,10 +30,10 @@ async-trait.workspace = true chrono.workspace = true collections.workspace = true futures.workspace = true +github_releases.workspace = true globset.workspace = true gpui.workspace = true grammars.workspace = true -http_client = { workspace = true, features = ["github-download"] } itertools.workspace = true json_schema_store.workspace = true language.workspace = true diff --git a/crates/languages/src/c.rs b/crates/languages/src/c.rs index 8ab9da4e98e..fe3b19309d4 100644 --- a/crates/languages/src/c.rs +++ b/crates/languages/src/c.rs @@ -1,9 +1,11 @@ use anyhow::{Context as _, Result, bail}; use async_trait::async_trait; use futures::StreamExt; +use github_releases::{ + AssetKind, GitHubLspBinaryVersion, GithubBinaryMetadata, download_server_binary, + latest_github_release, +}; use gpui::{App, AsyncApp}; -use http_client::github::{AssetKind, GitHubLspBinaryVersion, latest_github_release}; -use http_client::github_download::{GithubBinaryMetadata, download_server_binary}; pub use language::*; use lsp::{InitializeParams, LanguageServerBinary, LanguageServerName}; use project::lsp_store::clangd_ext; @@ -129,7 +131,8 @@ impl LspInstaller for CLspAdapter { } } download_server_binary( - &*delegate.http_client(), + &delegate.background_executor(), + delegate.http_client(), &url, expected_digest.as_deref(), &container_dir, diff --git a/crates/languages/src/eslint.rs b/crates/languages/src/eslint.rs index a876f392b9f..c55d7135850 100644 --- a/crates/languages/src/eslint.rs +++ b/crates/languages/src/eslint.rs @@ -1,10 +1,7 @@ use anyhow::{Context as _, Result}; use async_trait::async_trait; +use github_releases::{AssetKind, GitHubLspBinaryVersion, build_asset_url, download_server_binary}; use gpui::AsyncApp; -use http_client::{ - github::{AssetKind, GitHubLspBinaryVersion, build_asset_url}, - github_download::download_server_binary, -}; use language::{LspAdapter, LspAdapterDelegate, LspInstaller, Toolchain}; use lsp::{CodeActionKind, LanguageServerBinary, LanguageServerName, Uri}; use node_runtime::{NodeRuntime, read_package_installed_version}; @@ -117,7 +114,8 @@ impl LspInstaller for EsLintLspAdapter { remove_matching(&container_dir, |_| true).await; download_server_binary( - &*delegate.http_client(), + &delegate.background_executor(), + delegate.http_client(), &version.url, None, &destination_path, diff --git a/crates/languages/src/go.rs b/crates/languages/src/go.rs index 2d3fd4f6ccc..0c096a398ab 100644 --- a/crates/languages/src/go.rs +++ b/crates/languages/src/go.rs @@ -2,8 +2,8 @@ use anyhow::{Context as _, Result}; use async_trait::async_trait; use collections::HashMap; use futures::StreamExt; +use github_releases::latest_github_release; use gpui::{App, AsyncApp, Entity, Task}; -use http_client::github::latest_github_release; pub use language::*; use language::{ LanguageName, LanguageToolchainStore, LspAdapterDelegate, LspInstaller, diff --git a/crates/languages/src/json.rs b/crates/languages/src/json.rs index 2a2c4d2026c..28ece19a0f4 100644 --- a/crates/languages/src/json.rs +++ b/crates/languages/src/json.rs @@ -4,8 +4,8 @@ use async_tar::Archive; use async_trait::async_trait; use collections::HashMap; use futures::StreamExt; +use github_releases::{GitHubLspBinaryVersion, latest_github_release}; use gpui::{App, AsyncApp, Entity, Task}; -use http_client::github::{GitHubLspBinaryVersion, latest_github_release}; use language::{ Buffer, ContextProvider, LanguageName, LanguageRegistry, LocalFile as _, LspAdapter, LspAdapterDelegate, LspInstaller, Toolchain, diff --git a/crates/languages/src/python.rs b/crates/languages/src/python.rs index dedcf1f32ef..d596ccab6cf 100644 --- a/crates/languages/src/python.rs +++ b/crates/languages/src/python.rs @@ -5,8 +5,11 @@ use collections::HashMap; use futures::future::BoxFuture; use futures::lock::OwnedMutexGuard; use futures::{AsyncBufReadExt, StreamExt as _}; +use github_releases::{ + AssetKind, GitHubLspBinaryVersion, GithubBinaryMetadata, download_server_binary, + latest_github_release, +}; use gpui::{App, AsyncApp, Entity, SharedString, Task}; -use http_client::github::{AssetKind, GitHubLspBinaryVersion, latest_github_release}; use language::language_settings::LanguageSettings; use language::{ Buffer, ContextLocation, DynLspInstaller, LanguageToolchainStore, LspInstaller, Symbol, @@ -39,7 +42,6 @@ use util::fs::{make_file_executable, remove_matching}; use util::paths::PathStyle; use util::rel_path::RelPath; -use http_client::github_download::{GithubBinaryMetadata, download_server_binary}; use parking_lot::Mutex; use std::str::FromStr; use std::{ @@ -573,7 +575,8 @@ impl LspInstaller for TyLspAdapter { } download_server_binary( - &*delegate.http_client(), + delegate.background_executor(), + delegate.http_client(), &url, expected_digest.as_deref(), &destination_path, @@ -2663,7 +2666,8 @@ impl LspInstaller for RuffLspAdapter { } download_server_binary( - &*delegate.http_client(), + delegate.background_executor(), + delegate.http_client(), &url, expected_digest.as_deref(), &destination_path, @@ -2965,7 +2969,8 @@ impl LspInstaller for PyreflyLspAdapter { } download_server_binary( - &*delegate.http_client(), + delegate.background_executor(), + delegate.http_client(), &url, expected_digest.as_deref(), &destination_path, diff --git a/crates/languages/src/rust.rs b/crates/languages/src/rust.rs index 7d3b42e760d..dfe8075bce7 100644 --- a/crates/languages/src/rust.rs +++ b/crates/languages/src/rust.rs @@ -3,10 +3,11 @@ use async_trait::async_trait; use collections::HashMap; use futures::StreamExt; use futures::lock::OwnedMutexGuard; +use github_releases::{ + AssetKind, GitHubLspBinaryVersion, GithubBinaryMetadata, download_server_binary, + latest_github_release, +}; use gpui::{App, AppContext, AsyncApp, Entity, SharedString, Task}; -use http_client::github::AssetKind; -use http_client::github::{GitHubLspBinaryVersion, latest_github_release}; -use http_client::github_download::{GithubBinaryMetadata, download_server_binary}; pub use language::*; use lsp::{InitializeParams, LanguageServerBinary, LanguageServerBinaryOptions}; use project::lsp_store::lsp_ext_command; @@ -897,7 +898,8 @@ impl LspInstaller for RustLspAdapter { } download_server_binary( - &*delegate.http_client(), + delegate.background_executor(), + delegate.http_client(), &url, expected_digest.as_deref(), &destination_path, diff --git a/crates/project/Cargo.toml b/crates/project/Cargo.toml index de0762bb8e2..de922bee0b2 100644 --- a/crates/project/Cargo.toml +++ b/crates/project/Cargo.toml @@ -57,9 +57,10 @@ fuzzy.workspace = true fuzzy_nucleo.workspace = true git.workspace = true git_hosting_providers.workspace = true +github_releases.workspace = true globset.workspace = true gpui.workspace = true -http_client = { workspace = true, features = ["github-download"] } +http_client.workspace = true image.workspace = true indexmap.workspace = true itertools.workspace = true diff --git a/crates/project/src/agent_server_store.rs b/crates/project/src/agent_server_store.rs index a4c45070b4c..8f0aff6b591 100644 --- a/crates/project/src/agent_server_store.rs +++ b/crates/project/src/agent_server_store.rs @@ -9,11 +9,12 @@ use anyhow::{Context as _, Result, bail}; use collections::HashMap; use fs::{Fs, RemoveOptions}; use futures::StreamExt; +use github_releases::AssetKind; use gpui::{ AppContext as _, AsyncApp, Context, Entity, EventEmitter, SharedString, Subscription, Task, TaskExt, }; -use http_client::{HttpClient, github::AssetKind}; +use http_client::HttpClient; use node_runtime::NodeRuntime; use percent_encoding::percent_decode_str; use remote::RemoteClient; @@ -1228,7 +1229,7 @@ impl ExternalAgentServer for LocalRegistryArchiveAgent { let sha256 = if let Some(provided_sha) = &target_config.sha256 { Some(provided_sha.clone()) } else if let Some(github_archive) = github_release_archive_from_url(archive_url) { - if let Ok(release) = ::http_client::github::get_release_by_tag_name( + if let Ok(release) = github_releases::get_release_by_tag_name( &github_archive.repo_name_with_owner, &github_archive.tag, http_client.clone(), @@ -1255,10 +1256,12 @@ impl ExternalAgentServer for LocalRegistryArchiveAgent { None }; + let executor = cx.background_executor(); match registry_archive_kind_for_url(archive_url)? { RegistryArchiveKind::Archive(asset_kind) => { - ::http_client::github_download::download_server_binary( - &*http_client, + github_releases::download_server_binary( + executor, + http_client.clone(), archive_url, sha256.as_deref(), &version_dir, @@ -1267,8 +1270,9 @@ impl ExternalAgentServer for LocalRegistryArchiveAgent { .await?; } RegistryArchiveKind::RawBinary { file_name } => { - ::http_client::github_download::download_server_raw_binary( - &*http_client, + github_releases::download_server_raw_binary( + executor, + http_client.clone(), archive_url, sha256.as_deref(), &version_dir, diff --git a/crates/project/src/lsp_store.rs b/crates/project/src/lsp_store.rs index 9f364e79108..7580df2345b 100644 --- a/crates/project/src/lsp_store.rs +++ b/crates/project/src/lsp_store.rs @@ -70,8 +70,8 @@ use futures::{ }; use globset::{Glob, GlobBuilder, GlobMatcher, GlobSet, GlobSetBuilder}; use gpui::{ - App, AppContext, AsyncApp, Context, Entity, EventEmitter, PromptLevel, SharedString, - Subscription, Task, TaskExt, WeakEntity, + App, AppContext, AsyncApp, BackgroundExecutor, Context, Entity, EventEmitter, PromptLevel, + SharedString, Subscription, Task, TaskExt, WeakEntity, }; use http_client::HttpClient; use itertools::Itertools as _; @@ -17004,6 +17004,7 @@ pub struct LocalLspAdapterDelegate { worktree: worktree::Snapshot, fs: Arc, http_client: Arc, + background_executor: BackgroundExecutor, language_registry: Arc, load_shell_env_task: Shared>>>, } @@ -17026,6 +17027,7 @@ impl LocalLspAdapterDelegate { worktree: worktree.read(cx).snapshot(), fs, http_client, + background_executor: cx.background_executor().clone(), language_registry, load_shell_env_task, }) @@ -17062,6 +17064,10 @@ impl LspAdapterDelegate for LocalLspAdapterDelegate { self.http_client.clone() } + fn background_executor(&self) -> &BackgroundExecutor { + &self.background_executor + } + fn worktree_id(&self) -> WorktreeId { self.worktree.id() }