From e66ff202335065e80ace742949fae5fa409f2ec3 Mon Sep 17 00:00:00 2001 From: samhanic <55490861+samhanic@users.noreply.github.com> Date: Sun, 21 Jan 2024 17:44:03 +0100 Subject: [PATCH 1/2] fix vscode extensions update using the new update-extensions cli --- src/main.rs | 2 +- src/steps/generic.rs | 42 ++++++++++++++++++++++++++---------------- src/steps/os/linux.rs | 8 +++++++- 3 files changed, 34 insertions(+), 18 deletions(-) diff --git a/src/main.rs b/src/main.rs index 8153a32e..2a66062d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -349,7 +349,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..b4184cf6 100644 --- a/src/steps/generic.rs +++ b/src/steps/generic.rs @@ -15,6 +15,7 @@ use tracing::{debug, error}; use crate::command::{CommandExt, Utf8Output}; use crate::execution_context::ExecutionContext; use crate::executor::ExecutorOutput; +use crate::os::linux::is_wsl; use crate::terminal::{print_separator, shell}; use crate::utils::{self, check_is_python_2_or_shim, require, require_option, which, PathExt, REQUIRE_SUDO}; use crate::Step; @@ -325,28 +326,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..b3b5d158 100644 --- a/src/steps/os/linux.rs +++ b/src/steps/os/linux.rs @@ -189,12 +189,18 @@ fn update_bedrock(ctx: &ExecutionContext) -> Result<()> { Ok(()) } -fn is_wsl() -> Result { +#[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) +} + fn upgrade_alpine_linux(ctx: &ExecutionContext) -> Result<()> { let apk = require("apk")?; let sudo = require_option(ctx.sudo().as_ref(), REQUIRE_SUDO.to_string())?; From 528be84ae002612dc104eb2125def2f64d2dcee3 Mon Sep 17 00:00:00 2001 From: samhanic <55490861+samhanic@users.noreply.github.com> Date: Sun, 21 Jan 2024 18:51:54 +0100 Subject: [PATCH 2/2] fix non-linux compilation --- src/steps/generic.rs | 13 ++++++++++++- src/steps/os/linux.rs | 13 +------------ 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/src/steps/generic.rs b/src/steps/generic.rs index b4184cf6..18b5a01e 100644 --- a/src/steps/generic.rs +++ b/src/steps/generic.rs @@ -15,7 +15,6 @@ use tracing::{debug, error}; use crate::command::{CommandExt, Utf8Output}; use crate::execution_context::ExecutionContext; use crate::executor::ExecutorOutput; -use crate::os::linux::is_wsl; use crate::terminal::{print_separator, shell}; use crate::utils::{self, check_is_python_2_or_shim, require, require_option, which, PathExt, REQUIRE_SUDO}; use crate::Step; @@ -25,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) diff --git a/src/steps/os/linux.rs b/src/steps/os/linux.rs index b3b5d158..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,18 +190,6 @@ fn update_bedrock(ctx: &ExecutionContext) -> Result<()> { Ok(()) } -#[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) -} - fn upgrade_alpine_linux(ctx: &ExecutionContext) -> Result<()> { let apk = require("apk")?; let sudo = require_option(ctx.sudo().as_ref(), REQUIRE_SUDO.to_string())?;