Skip to content
Open
Show file tree
Hide file tree
Changes from 6 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
124 changes: 86 additions & 38 deletions crates/pixi_core/src/lock_file/resolve/pypi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ use indicatif::ProgressBar;
use itertools::{Either, Itertools};
use miette::{Context, IntoDiagnostic};
use pixi_consts::consts;
use pixi_git::git::GitReference;
use pixi_manifest::{
EnvironmentName, SolveStrategy, SystemRequirements, pypi::pypi_options::PypiOptions,
};
Expand Down Expand Up @@ -51,7 +50,6 @@ use uv_distribution_types::{
IndexCapabilities, IndexUrl, Name, RequirementSource, RequiresPython, Resolution, ResolvedDist,
SourceDist, ToUrlError,
};
use uv_git_types::GitUrl;
use uv_pep508::VerbatimUrl;
use uv_pypi_types::{Conflicts, HashAlgorithm, HashDigests};
use uv_redacted::DisplaySafeUrl;
Expand Down Expand Up @@ -121,6 +119,51 @@ fn parse_hashes_from_hash_vec(hashes: &HashDigests) -> Result<Option<PackageHash
}
}

/// Error type for creating a constraint source from a locked git URL.
#[derive(Debug, thiserror::Error)]
pub enum LockedGitSourceError {
#[error("failed to parse locked git URL: {0}")]
Parse(String),
#[error("failed to parse git OID: {0}")]
GitOid(#[from] uv_git_types::OidParseError),
#[error("failed to parse git URL: {0}")]
GitUrlParse(#[from] uv_git_types::GitUrlParseError),
}

/// Creates a [`RequirementSource::Git`] from a previously locked git URL.
///
/// This creates the GitUrl with DefaultBranch reference (via try_from) and then
/// adds the precise commit. The cache must be pre-populated with the
/// (url, reference) -> commit mapping before using this, otherwise uv's redirect
/// mechanism will panic in debug mode.
pub(crate) fn locked_git_url_to_requirement_source(
location: &Url,
) -> Result<RequirementSource, LockedGitSourceError> {
let git_locked_url = LockedGitUrl::from(location.clone());
let pinned_git_spec = git_locked_url
.to_pinned_git_spec()
.map_err(|e| LockedGitSourceError::Parse(e.to_string()))?;

let verbatim_url = VerbatimUrl::from(location.clone());

let display_safe = DisplaySafeUrl::from(pinned_git_spec.git.clone());

let git_oid = uv_git_types::GitOid::from_str(&pinned_git_spec.source.commit.to_string())?;

// Use try_from + with_precise. The cache should already have the
// (url, DefaultBranch) -> commit mapping from pre-population.
let git_url = uv_git_types::GitUrl::try_from(display_safe)?.with_precise(git_oid);

Ok(RequirementSource::Git {
git: git_url,
subdirectory: pinned_git_spec
.source
.subdirectory
.map(|s| PathBuf::from(s).into_boxed_path()),
url: verbatim_url,
})
}

#[derive(Debug, thiserror::Error)]
enum ProcessPathUrlError {
#[error("expected given path for {0} but none found")]
Expand Down Expand Up @@ -579,9 +622,36 @@ pub async fn resolve_pypi(

#[error(transparent)]
GitUrlParse(#[from] uv_git_types::GitUrlParseError),
// #[error("{0}")]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove?

// Other(miette::ErrReport),
}

#[error("{0}")]
Other(miette::ErrReport),
// Pre-populate the GitResolver cache with locked git commits.
// This is necessary because when we enrich requirements with locked precise commits,
// uv's redirect mechanism looks up the commit in the GitResolver cache.
// Without pre-population, the lookup fails and causes a panic in debug mode.
for (package_data, _) in locked_pypi_packages.iter() {
if let Some(location) = package_data.location.as_url() {
if LockedGitUrl::is_locked_git_url(location) {
if let Ok(pinned_git_spec) =
LockedGitUrl::from(location.clone()).to_pinned_git_spec()
{
// Create the repository reference that matches what the manifest will use
let display_safe =
uv_redacted::DisplaySafeUrl::from(pinned_git_spec.git.clone());
if let Ok(git_url) = uv_git_types::GitUrl::try_from(display_safe) {
// Parse the commit SHA
if let Ok(git_oid) = uv_git_types::GitOid::from_str(
&pinned_git_spec.source.commit.to_string(),
) {
// Insert the (url, reference) -> commit mapping into the cache
let repo_ref = uv_git::RepositoryReference::from(&git_url);
context.shared_state.git().insert(repo_ref, git_oid);
}
}
}
}
}
}

// Create preferences from the locked pypi packages
Expand Down Expand Up @@ -611,51 +681,29 @@ pub async fn resolve_pypi(
// This will help the resolver to pick the commit that we already have locked
// instead of updating to a newer commit that also matches the requirement.
if let Some(location) = package_data.location.as_url() {
// now check if it's a git url
// Check if it's a git URL that we can enrich with locked commit
if LockedGitUrl::is_locked_git_url(location) {
// we need to parse back `LockedGitUrl` in order to get the `PinnedGitSpec`
// then we will precise commit to set for the `GitUrl`
// that will be used in the `RequirementSource::Git` below
let git_locked_url = LockedGitUrl::from(location.clone());
let pinned_git_spec = git_locked_url
.to_pinned_git_spec()
.map_err(PixiPreferencesError::Other)?;
// we need to create VerbatimUrl from the original location
let verbatim_url = VerbatimUrl::from(location.clone());

// but the display safe url should come from the `PinnedGitSpec` url
// which don't have anymore git+ prefix
let display_safe = DisplaySafeUrl::from(pinned_git_spec.git.clone());

let git_oid =
uv_git_types::GitOid::from_str(&pinned_git_spec.source.commit.to_string())?;

let git_url = GitUrl::try_from(display_safe)?.with_precise(git_oid);

let constraint_source = RequirementSource::Git {
git: git_url,
subdirectory: None,
url: verbatim_url,
};

// find this requirements in dependencies and skip adding it as preference
// Find this requirement in the requirements list
let req_from_dep = requirements.iter_mut().find(|r| r.name == requirement.name);
if let Some(req) = req_from_dep {
// we need to update the requirement source in the requirements list
// to use the precise git commit
// only if the requirements do not already have a source set with something specific
// Only enrich git requirements that don't already have a precise commit
if let RequirementSource::Git { git, .. } = &req.source {
// only update if the git url does not already have a precise commit
if git.precise().is_none() && !GitReference::looks_like_commit_hash(git.reference().as_rev()) {
if git.precise().is_none() {
// Create the constraint source with precise commit
// The GitResolver cache was pre-populated above, so this won't panic
if let Ok(constraint_source) =
locked_git_url_to_requirement_source(location)
{
tracing::debug!(
"updating requirement source to precise git commit for requirement: {:?}",
&req
);
req.source = constraint_source.clone();
req.source = constraint_source;
}

}
}
}
return Ok(None);
}
Ok(None)
} else {
Expand Down
26 changes: 23 additions & 3 deletions crates/pixi_core/src/lock_file/satisfiability/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -970,10 +970,30 @@ pub(crate) fn pypi_satifisfies_requirement(
}
.into());
}
// If the spec does not specify a revision than any will do
// E.g `git.com/user/repo` is the same as `git.com/user/repo@adbdd`
// If the spec uses DefaultBranch, we need to check what the lock has:
// - If lock has Branch("main") or Tag("v1.0") → NOT satisfiable
// (user removed explicit branch/tag, should re-resolve)
// - If lock has DefaultBranch or Rev (specific commit) → satisfiable
// (specific commit is still valid even without explicit ref)
if *reference == GitReference::DefaultBranch {
return Ok(());
match &pinned_git_spec.source.reference {
// These are explicit named references - not satisfiable
// when manifest has DefaultBranch (user removed the explicit ref)
pixi_spec::GitReference::Branch(_)
| pixi_spec::GitReference::Tag(_) => {
return Err(PlatformUnsat::LockedPyPIGitRefMismatch {
name: spec.name.clone().to_string(),
expected_ref: reference.to_string(),
found_ref: pinned_git_spec.source.reference.to_string(),
}
.into());
}
// These are satisfiable - either DefaultBranch or specific commits
pixi_spec::GitReference::DefaultBranch
| pixi_spec::GitReference::Rev(_) => {
return Ok(());
}
}
}

if pinned_git_spec.source.subdirectory
Expand Down
Loading