Skip to content
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
14 changes: 14 additions & 0 deletions src/backend/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -671,6 +671,20 @@ pub trait Backend: Debug + Send + Sync {
async fn latest_stable_version(&self, _config: &Arc<Config>) -> eyre::Result<Option<String>> {
Ok(None)
}

/// Backend opt-in for installing an unresolved `latest` request.
///
/// Most backends must resolve `latest` to a concrete version before install.
/// Override this only when the backend can pass an unresolved selector through
/// to its installer, and only for requests where the selector is meaningful.
///
/// `ToolVersion::resolve_version` uses this as a last resort after normal
/// latest resolution fails, and only when the backend's unfiltered remote
/// version list is empty. If remote versions exist but are all filtered out by
/// `minimum_release_age` / `--before`, this hook is not used.
fn unresolved_latest_version(&self) -> Option<String> {
None
}
fn list_installed_versions(&self) -> Vec<String> {
install_state::list_versions(&self.ba().short)
}
Expand Down
73 changes: 59 additions & 14 deletions src/backend/pipx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::cmd::CmdLineRunner;
use crate::config::{Config, Settings};
use crate::env;
use crate::file;
use crate::github;
use crate::github::{self, GithubRelease};
use crate::http::HTTP_FETCH;
use crate::install_context::InstallContext;
use crate::timeout;
Expand Down Expand Up @@ -122,20 +122,9 @@ impl Backend for PIPXBackend {
PipxRequest::Git(url) if url.starts_with("https://github.com/") => {
let repo = url.strip_prefix("https://github.com/").unwrap();
let data = github::list_releases(repo).await?;
Ok(data
.into_iter()
.rev()
.map(|r| VersionInfo {
version: r.tag_name,
created_at: Some(r.created_at),
..Default::default()
})
.collect())
Ok(Self::versions_from_github_releases(data))
}
PipxRequest::Git { .. } => Ok(vec![VersionInfo {
version: "latest".to_string(),
..Default::default()
}]),
PipxRequest::Git { .. } => Ok(vec![]),
}
}

Expand Down Expand Up @@ -195,6 +184,13 @@ impl Backend for PIPXBackend {
.cloned()
}

fn unresolved_latest_version(&self) -> Option<String> {
match self.tool_name().parse() {
Ok(PipxRequest::Git(_)) => Some("latest".to_string()),
_ => None,
}
}

async fn install_version_(&self, ctx: &InstallContext, tv: ToolVersion) -> Result<ToolVersion> {
// Check if pipx is available (unless uvx is being used)
let use_uvx = self.uv_is_installed(&ctx.config).await
Expand Down Expand Up @@ -292,6 +288,18 @@ pub fn install_time_option_keys() -> Vec<String> {
}

impl PIPXBackend {
fn versions_from_github_releases(releases: Vec<GithubRelease>) -> Vec<VersionInfo> {
releases
.into_iter()
.rev()
.map(|r| VersionInfo {
version: r.tag_name,
created_at: Some(r.created_at),
..Default::default()
})
.collect()
}

fn uv_exclude_newer_args(before_date: Option<Timestamp>) -> Vec<OsString> {
match before_date {
Some(before_date) => vec!["--exclude-newer".into(), before_date.to_string().into()],
Expand Down Expand Up @@ -666,9 +674,36 @@ fn fix_venv_python_symlink(_install_path: &Path, _pkg_name: &str) -> Result<()>
#[cfg(test)]
mod tests {
use super::PIPXBackend;
use crate::github::GithubRelease;
use pretty_assertions::assert_eq;
use std::ffi::OsString;

#[test]
fn test_versions_from_empty_github_releases_stays_empty() {
let versions = PIPXBackend::versions_from_github_releases(vec![]);

assert!(versions.is_empty());
}

#[test]
fn test_versions_from_github_releases_preserves_tags() {
let versions = PIPXBackend::versions_from_github_releases(vec![
github_release("2.0.0", "2024-02-01T00:00:00Z"),
github_release("1.0.0", "2024-01-01T00:00:00Z"),
]);

assert_eq!(
versions
.iter()
.map(|v| (v.version.as_str(), v.created_at.as_deref()))
.collect::<Vec<_>>(),
vec![
("1.0.0", Some("2024-01-01T00:00:00Z")),
("2.0.0", Some("2024-02-01T00:00:00Z")),
]
);
}

#[test]
fn test_uv_exclude_newer_args_with_cutoff() {
let before_date = "2024-01-02T03:04:05Z".parse().unwrap();
Expand Down Expand Up @@ -712,4 +747,14 @@ mod tests {
Vec::<OsString>::new()
);
}

fn github_release(tag_name: &str, created_at: &str) -> GithubRelease {
GithubRelease {
tag_name: tag_name.to_string(),
draft: false,
prerelease: false,
created_at: created_at.to_string(),
assets: vec![],
}
}
}
8 changes: 8 additions & 0 deletions src/toolset/tool_version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,14 @@ impl ToolVersion {
{
return build(v);
}
if !is_offline {
let versions = backend.list_remote_versions(config).await?;
if versions.is_empty()
&& let Some(v) = backend.unresolved_latest_version()
{
return build(v);
}
}
Comment thread
cursor[bot] marked this conversation as resolved.
return Err(Self::no_versions_found(&backend, opts.before_date));
}
if !opts.latest_versions {
Expand Down
Loading