Add zip extract support for Windows (#11156)

Release Notes:

- [x] Fixed install Node.js runtime and NPM lsp installation on Windows.
- [x] Update Node runtime command to execute on Windows with no window
popup.

Ref #9619, #9424

---------

Co-authored-by: Marshall Bowers <elliott.codes@gmail.com>
This commit is contained in:
Jason Lee 2024-05-09 21:23:21 +08:00 committed by GitHub
parent 3bd53d0441
commit 5e06ce4df3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 228 additions and 20 deletions

View file

@ -1,10 +1,13 @@
mod archive;
use anyhow::{anyhow, bail, Context, Result};
use async_compression::futures::bufread::GzipDecoder;
use async_tar::Archive;
use futures::AsyncReadExt;
use semver::Version;
use serde::Deserialize;
use smol::{fs, io::BufReader, lock::Mutex, process::Command};
use smol::io::BufReader;
use smol::{fs, lock::Mutex, process::Command};
use std::io;
use std::process::{Output, Stdio};
use std::{
@ -15,8 +18,26 @@ use std::{
use util::http::HttpClient;
use util::ResultExt;
#[cfg(windows)]
use smol::process::windows::CommandExt;
const VERSION: &str = "v18.15.0";
#[cfg(not(windows))]
const NODE_PATH: &str = "bin/node";
#[cfg(windows)]
const NODE_PATH: &str = "node.exe";
#[cfg(not(windows))]
const NPM_PATH: &str = "bin/npm";
#[cfg(windows)]
const NPM_PATH: &str = "node_modules/npm/bin/npm-cli.js";
enum ArchiveType {
TarGz,
Zip,
}
#[derive(Debug, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub struct NpmInfo {
@ -119,10 +140,12 @@ impl RealNodeRuntime {
let folder_name = format!("node-{VERSION}-{os}-{arch}");
let node_containing_dir = util::paths::SUPPORT_DIR.join("node");
let node_dir = node_containing_dir.join(folder_name);
let node_binary = node_dir.join("bin/node");
let npm_file = node_dir.join("bin/npm");
let node_binary = node_dir.join(NODE_PATH);
let npm_file = node_dir.join(NPM_PATH);
let result = Command::new(&node_binary)
let mut command = Command::new(&node_binary);
command
.env_clear()
.arg(npm_file)
.arg("--version")
@ -131,9 +154,12 @@ impl RealNodeRuntime {
.stderr(Stdio::null())
.args(["--cache".into(), node_dir.join("cache")])
.args(["--userconfig".into(), node_dir.join("blank_user_npmrc")])
.args(["--globalconfig".into(), node_dir.join("blank_global_npmrc")])
.status()
.await;
.args(["--globalconfig".into(), node_dir.join("blank_global_npmrc")]);
#[cfg(windows)]
command.creation_flags(windows::Win32::System::Threading::CREATE_NO_WINDOW.0);
let result = command.status().await;
let valid = matches!(result, Ok(status) if status.success());
if !valid {
@ -142,7 +168,19 @@ impl RealNodeRuntime {
.await
.context("error creating node containing dir")?;
let file_name = format!("node-{VERSION}-{os}-{arch}.tar.gz");
let archive_type = match consts::OS {
"macos" | "linux" => ArchiveType::TarGz,
"windows" => ArchiveType::Zip,
other => bail!("Running on unsupported os: {other}"),
};
let file_name = format!(
"node-{VERSION}-{os}-{arch}.{extension}",
extension = match archive_type {
ArchiveType::TarGz => "tar.gz",
ArchiveType::Zip => "zip",
}
);
let url = format!("https://nodejs.org/dist/{VERSION}/{file_name}");
let mut response = self
.http
@ -150,9 +188,15 @@ impl RealNodeRuntime {
.await
.context("error downloading Node binary tarball")?;
let decompressed_bytes = GzipDecoder::new(BufReader::new(response.body_mut()));
let archive = Archive::new(decompressed_bytes);
archive.unpack(&node_containing_dir).await?;
let body = response.body_mut();
match archive_type {
ArchiveType::TarGz => {
let decompressed_bytes = GzipDecoder::new(BufReader::new(response.body_mut()));
let archive = Archive::new(decompressed_bytes);
archive.unpack(&node_containing_dir).await?;
}
ArchiveType::Zip => archive::extract_zip(&node_containing_dir, body).await?,
}
}
// Note: Not in the `if !valid {}` so we can populate these for existing installations
@ -168,7 +212,7 @@ impl RealNodeRuntime {
impl NodeRuntime for RealNodeRuntime {
async fn binary_path(&self) -> Result<PathBuf> {
let installation_path = self.install_if_needed().await?;
Ok(installation_path.join("bin/node"))
Ok(installation_path.join(NODE_PATH))
}
async fn run_npm_subcommand(
@ -180,7 +224,13 @@ impl NodeRuntime for RealNodeRuntime {
let attempt = || async move {
let installation_path = self.install_if_needed().await?;
let mut env_path = installation_path.join("bin").into_os_string();
let node_binary = installation_path.join(NODE_PATH);
let npm_file = installation_path.join(NPM_PATH);
let mut env_path = node_binary
.parent()
.expect("invalid node binary path")
.to_path_buf();
if let Some(existing_path) = std::env::var_os("PATH") {
if !existing_path.is_empty() {
env_path.push(":");
@ -188,9 +238,6 @@ impl NodeRuntime for RealNodeRuntime {
}
}
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"));
}
@ -219,6 +266,9 @@ impl NodeRuntime for RealNodeRuntime {
command.args(["--prefix".into(), directory.to_path_buf()]);
}
#[cfg(windows)]
command.creation_flags(windows::Win32::System::Threading::CREATE_NO_WINDOW.0);
command.output().await.map_err(|e| anyhow!("{e}"))
};
@ -227,7 +277,8 @@ impl NodeRuntime for RealNodeRuntime {
output = attempt().await;
if output.is_err() {
return Err(anyhow!(
"failed to launch npm subcommand {subcommand} subcommand"
"failed to launch npm subcommand {subcommand} subcommand\nerr: {:?}",
output.err()
));
}
}