Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: reinstall pipx packages when upgrading python #3172

Merged
merged 1 commit into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions e2e/backend/test_pipx_deep_dependencies
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,6 @@ mise install
# Assert that mkdocs 1.6.0 has been installed with pipx and uses python 3.12
# (mkdocs conveniently returns its installation path in with --version)
assert_contains "mise x -- mkdocs --version" "/mise/installs/pipx-mkdocs/1.6.0/venvs/mkdocs/lib/python3.12/"

assert "mise up --bump python"
assert_contains "mise x -- mkdocs --version" "mkdocs, version 1.6.0"
1 change: 0 additions & 1 deletion src/backend/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,6 @@ pub trait Backend: Debug + Send + Sync {
}

fn list_remote_versions(&self) -> eyre::Result<Vec<String>> {
self.warn_if_dependencies_missing()?;
self.get_remote_version_cache()
.get_or_try_init(|| {
trace!("Listing remote versions for {}", self.ba().to_string());
Expand Down
72 changes: 60 additions & 12 deletions src/backend/pipx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ use crate::config::{Config, SETTINGS};
use crate::github;
use crate::http::HTTP_FETCH;
use crate::install_context::InstallContext;
use crate::toolset::{ToolVersion, ToolVersionOptions};
use crate::toolset::{ToolVersion, ToolVersionOptions, Toolset, ToolsetBuilder};
use crate::ui::multi_progress_report::MultiProgressReport;
use crate::ui::progress_report::SingleReport;
use eyre::Result;
use indexmap::IndexMap;
use itertools::Itertools;
use std::fmt::Debug;
Expand Down Expand Up @@ -109,17 +112,14 @@ impl Backend for PIPXBackend {
}
cmd.execute()?;
} else {
let mut cmd = CmdLineRunner::new("pipx")
.arg("install")
.arg(pipx_request)
.with_pr(ctx.pr.as_ref())
.env("PIPX_HOME", tv.install_path())
.env("PIPX_BIN_DIR", tv.install_path().join("bin"))
.envs(ctx.ts.env_with_path(&config)?)
.prepend_path(ctx.ts.list_paths())?
// Prepend install path so pipx doesn't issue a warning about missing path
.prepend_path(vec![tv.install_path().join("bin")])?
.prepend_path(self.dependency_toolset()?.list_paths())?;
let mut cmd = Self::pipx_cmd(
&config,
&["install", &pipx_request],
self,
&tv,
ctx.ts,
&*ctx.pr,
)?;
if let Some(args) = tv.request.options().get("pipx_args") {
cmd = cmd.args(shell_words::split(args)?);
}
Expand All @@ -145,6 +145,54 @@ impl PIPXBackend {
ba,
}
}

pub fn reinstall_all() -> Result<()> {
if SETTINGS.pipx.uvx {
debug!("skipping pipx reinstall because uvx is enabled");
return Ok(());
}
let config = Config::load()?;
let ts = ToolsetBuilder::new().build(&config)?;
let pipx_tools = ts
.list_installed_versions()?
.into_iter()
.filter(|(b, _tv)| b.ba().backend_type() == BackendType::Pipx)
.collect_vec();
let pr = MultiProgressReport::get().add("reinstalling pipx tools");
for (b, tv) in pipx_tools {
Self::pipx_cmd(
&config,
&["reinstall", &tv.ba().tool_name],
&*b,
&tv,
&ts,
&*pr,
)?
.execute()?;
}
Ok(())
}

fn pipx_cmd<'a>(
config: &Config,
args: &[&str],
b: &dyn Backend,
tv: &ToolVersion,
ts: &Toolset,
pr: &'a dyn SingleReport,
) -> Result<CmdLineRunner<'a>> {
let mut cmd = CmdLineRunner::new("pipx");
for arg in args {
cmd = cmd.arg(arg);
}
cmd.with_pr(pr)
.env("PIPX_HOME", tv.install_path())
.env("PIPX_BIN_DIR", tv.install_path().join("bin"))
.envs(ts.env_with_path(config)?)
.prepend_path(ts.list_paths())?
.prepend_path(vec![tv.install_path().join("bin")])?
.prepend_path(b.dependency_toolset()?.list_paths())
}
}

enum PipxRequest {
Expand Down
7 changes: 7 additions & 0 deletions src/cli/upgrade.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::backend::pipx::PIPXBackend;
use crate::cli::args::ToolArg;
use crate::config::{config_file, Config};
use crate::file::display_path;
Expand Down Expand Up @@ -158,6 +159,12 @@ impl Upgrade {
}

config::rebuild_shims_and_runtime_symlinks(&versions)?;

if versions.iter().any(|v| v.short() == "python") {
PIPXBackend::reinstall_all().unwrap_or_else(|err| {
warn!("failed to reinstall pipx tools: {err}");
})
}
Ok(())
}

Expand Down
14 changes: 12 additions & 2 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use crate::file::display_path;
use crate::shorthands::{get_shorthands, Shorthands};
use crate::task::Task;
use crate::toolset::{
install_state, ToolRequestSet, ToolRequestSetBuilder, ToolVersion, ToolsetBuilder,
install_state, ToolRequestSet, ToolRequestSetBuilder, ToolVersion, Toolset, ToolsetBuilder,
};
use crate::ui::style;
use crate::{backend, dirs, env, file, lockfile, registry, runtime_symlinks, shims};
Expand Down Expand Up @@ -53,6 +53,7 @@ pub struct Config {
shorthands: OnceLock<Shorthands>,
tasks: OnceCell<BTreeMap<String, Task>>,
tool_request_set: OnceCell<ToolRequestSet>,
toolset: OnceCell<Toolset>,
}

#[derive(Debug, Clone, Default)]
Expand Down Expand Up @@ -190,6 +191,14 @@ impl Config {
.get_or_try_init(|| ToolRequestSetBuilder::new().build())
}

pub fn get_toolset(&self) -> eyre::Result<&Toolset> {
self.toolset.get_or_try_init(|| {
let mut ts = Toolset::from(self.get_tool_request_set()?.clone());
ts.resolve()?;
Ok(ts)
})
}

pub fn get_repo_url(&self, plugin_name: &str) -> Option<String> {
let plugin_name = self
.all_aliases
Expand Down Expand Up @@ -903,16 +912,17 @@ fn default_task_includes() -> Vec<PathBuf> {
}

pub fn rebuild_shims_and_runtime_symlinks(new_versions: &[ToolVersion]) -> Result<()> {
install_state::reset();
let config = Config::load()?;
let ts = ToolsetBuilder::new().build(&config)?;
install_state::reset();
trace!("rebuilding shims");
shims::reshim(&ts, false).wrap_err("failed to rebuild shims")?;
trace!("rebuilding runtime symlinks");
runtime_symlinks::rebuild(&config).wrap_err("failed to rebuild runtime symlinks")?;
trace!("updating lockfiles");
lockfile::update_lockfiles(&config, &ts, new_versions)
.wrap_err("failed to update lockfiles")?;

Ok(())
}

Expand Down
4 changes: 4 additions & 0 deletions src/toolset/tool_version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ impl ToolVersion {
self.ba().backend()
}

pub fn short(&self) -> &str {
&self.ba().short
}

pub fn install_path(&self) -> PathBuf {
let pathname = match &self.request {
ToolRequest::Path { path: p, .. } => p.to_string_lossy().to_string(),
Expand Down
Loading