From 10e1e170b7a4f47ae85e8f853ec1a97eec5188db Mon Sep 17 00:00:00 2001 From: samhanic <55490861+samhanic@users.noreply.github.com> Date: Wed, 24 Jan 2024 03:32:00 +0100 Subject: [PATCH] fix vscode extensions update step (#650) * fix vscode extensions update using the new update-extensions cli * fix non-linux compilation --- src/main.rs | 2 +- src/steps/generic.rs | 53 ++++++++++++++++++++++++++++++------------- src/steps/os/linux.rs | 7 +----- 3 files changed, 39 insertions(+), 23 deletions(-) diff --git a/src/main.rs b/src/main.rs index de478ad1..ba9506de 100644 --- a/src/main.rs +++ b/src/main.rs @@ -353,7 +353,7 @@ fn run() -> Result<()> { runner.execute(Step::Vcpkg, "vcpkg", || generic::run_vcpkg_update(&ctx))?; runner.execute(Step::Pipx, "pipx", || generic::run_pipx_update(&ctx))?; runner.execute(Step::Vscode, "Visual Studio Code extensions", || { - generic::run_vscode_extensions_upgrade(&ctx) + generic::run_vscode_extensions_update(&ctx) })?; runner.execute(Step::Conda, "conda", || generic::run_conda_update(&ctx))?; runner.execute(Step::Mamba, "mamba", || generic::run_mamba_update(&ctx))?; diff --git a/src/steps/generic.rs b/src/steps/generic.rs index 7f27ef8f..18b5a01e 100644 --- a/src/steps/generic.rs +++ b/src/steps/generic.rs @@ -24,6 +24,18 @@ use crate::{ terminal::print_warning, }; +#[cfg(target_os = "linux")] +pub fn is_wsl() -> Result { + let output = Command::new("uname").arg("-r").output_checked_utf8()?.stdout; + debug!("Uname output: {}", output); + Ok(output.contains("microsoft")) +} + +#[cfg(not(target_os = "linux"))] +pub fn is_wsl() -> Result { + Ok(false) +} + pub fn run_cargo_update(ctx: &ExecutionContext) -> Result<()> { let cargo_dir = env::var_os("CARGO_HOME") .map(PathBuf::from) @@ -325,28 +337,37 @@ pub fn run_vcpkg_update(ctx: &ExecutionContext) -> Result<()> { command.args(["upgrade", "--no-dry-run"]).status_checked() } -pub fn run_vscode_extensions_upgrade(ctx: &ExecutionContext) -> Result<()> { +pub fn run_vscode_extensions_update(ctx: &ExecutionContext) -> Result<()> { + // Calling vscode in WSL may install a server instead of updating extensions (https://github.com/topgrade-rs/topgrade/issues/594#issuecomment-1782157367) + if is_wsl()? { + return Err(SkipStep(String::from("Should not run in WSL")).into()); + } + let vscode = require("code")?; - print_separator("Visual Studio Code extensions"); - // Vscode does not have CLI command to upgrade all extensions (see https://github.com/microsoft/vscode/issues/56578) - // Instead we get the list of installed extensions with `code --list-extensions` command (obtain a line-return separated list of installed extensions) - let extensions = Command::new(&vscode) - .arg("--list-extensions") + // Vscode has update command only since 1.86 version ("january 2024" update), disable the update for prior versions + // Use command `code --version` which returns 3 lines: version, git commit, instruction set. We parse only the first one + let version: Result = match Command::new("code") + .arg("--version") .output_checked_utf8()? - .stdout; - - // Then we construct the upgrade command: `code --force --install-extension [ext0] --install-extension [ext1] ... --install-extension [extN]` - if !extensions.is_empty() { - let mut command_args = vec!["--force"]; - for extension in extensions.split_whitespace() { - command_args.extend(["--install-extension", extension]); - } + .stdout + .lines() + .next() + { + Some(item) => Version::parse(item).map_err(|err| err.into()), + _ => return Err(SkipStep(String::from("Cannot find vscode version")).into()), + }; - ctx.run_type().execute(&vscode).args(command_args).status_checked()?; + if !matches!(version, Ok(version) if version >= Version::new(1, 86, 0)) { + return Err(SkipStep(String::from("Too old vscode version to have update extensions command")).into()); } - Ok(()) + print_separator("Visual Studio Code extensions"); + + ctx.run_type() + .execute(vscode) + .arg("--update-extensions") + .status_checked() } pub fn run_pipx_update(ctx: &ExecutionContext) -> Result<()> { diff --git a/src/steps/os/linux.rs b/src/steps/os/linux.rs index 1dbf7c2d..f6142db4 100644 --- a/src/steps/os/linux.rs +++ b/src/steps/os/linux.rs @@ -8,6 +8,7 @@ use tracing::{debug, warn}; use crate::command::CommandExt; use crate::error::{SkipStep, TopgradeError}; use crate::execution_context::ExecutionContext; +use crate::steps::generic::is_wsl; use crate::steps::os::archlinux; use crate::terminal::print_separator; use crate::utils::{require, require_option, which, PathExt, REQUIRE_SUDO}; @@ -189,12 +190,6 @@ fn update_bedrock(ctx: &ExecutionContext) -> Result<()> { Ok(()) } -fn is_wsl() -> Result { - let output = Command::new("uname").arg("-r").output_checked_utf8()?.stdout; - debug!("Uname output: {}", output); - Ok(output.contains("microsoft")) -} - fn upgrade_alpine_linux(ctx: &ExecutionContext) -> Result<()> { let apk = require("apk")?; let sudo = require_option(ctx.sudo().as_ref(), REQUIRE_SUDO.to_string())?;