Skip to content

Preserve environment variables in resolved Git dependencies #2125

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

Merged
merged 1 commit into from
Mar 2, 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
4 changes: 2 additions & 2 deletions crates/pep508-rs/src/verbatim_url.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,9 @@ impl VerbatimUrl {

/// Set the verbatim representation of the URL.
#[must_use]
pub fn with_given(self, given: String) -> Self {
pub fn with_given(self, given: impl Into<String>) -> Self {
Self {
given: Some(given),
given: Some(given.into()),
..self
}
}
Expand Down
1 change: 1 addition & 0 deletions crates/uv-resolver/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ mod pins;
mod prerelease_mode;
mod pubgrub;
mod python_requirement;
mod redirect;
mod resolution;
mod resolution_mode;
mod resolver;
Expand Down
85 changes: 85 additions & 0 deletions crates/uv-resolver/src/redirect.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
use url::Url;

use pep508_rs::VerbatimUrl;

/// Given a [`VerbatimUrl`] and a redirect, apply the redirect to the URL while preserving as much
/// of the verbatim representation as possible.
pub(crate) fn apply_redirect(url: &VerbatimUrl, redirect: &Url) -> VerbatimUrl {
let redirect = VerbatimUrl::from_url(redirect.clone());

// The redirect should be the "same" URL, but with a specific commit hash added after the `@`.
// We take advantage of this to preserve as much of the verbatim representation as possible.
if let Some(given) = url.given() {
if let Some(precise_suffix) = redirect
.raw()
.path()
.rsplit_once('@')
.map(|(_, suffix)| suffix.to_owned())
{
// If there was an `@` in the original representation...
if let Some((.., parsed_suffix)) = url.raw().path().rsplit_once('@') {
if let Some((given_prefix, given_suffix)) = given.rsplit_once('@') {
// And the portion after the `@` is stable between the parsed and given representations...
if given_suffix == parsed_suffix {
// Preserve everything that precedes the `@` in the precise representation.
return redirect.with_given(format!("{given_prefix}@{precise_suffix}"));
}
}
} else {
// If there was no `@` in the original representation, we can just append the
// precise suffix to the given representation.
return redirect.with_given(format!("{given}@{precise_suffix}"));
}
}
}

redirect
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_apply_redirect() -> Result<(), url::ParseError> {
// If there's no `@` in the original representation, we can just append the precise suffix
// to the given representation.
let verbatim = VerbatimUrl::parse("https://github.com/flask.git")?
.with_given("git+https://github.com/flask.git");
let redirect =
Url::parse("https://github.com/flask.git@b90a4f1f4a370e92054b9cc9db0efcb864f87ebe")?;

let expected = VerbatimUrl::parse(
"https://github.com/flask.git@b90a4f1f4a370e92054b9cc9db0efcb864f87ebe",
)?
.with_given("https://github.com/flask.git@b90a4f1f4a370e92054b9cc9db0efcb864f87ebe");
assert_eq!(apply_redirect(&verbatim, &redirect), expected);

// If there's an `@` in the original representation, and it's stable between the parsed and
// given representations, we preserve everything that precedes the `@` in the precise
// representation.
let verbatim = VerbatimUrl::parse("https://github.com/flask.git@main")?
.with_given("git+https://${DOMAIN}.com/flask.git@main");
let redirect =
Url::parse("https://github.com/flask.git@b90a4f1f4a370e92054b9cc9db0efcb864f87ebe")?;

let expected = VerbatimUrl::parse(
"https://github.com/flask.git@b90a4f1f4a370e92054b9cc9db0efcb864f87ebe",
)?
.with_given("https://${DOMAIN}.com/flask.git@b90a4f1f4a370e92054b9cc9db0efcb864f87ebe");
assert_eq!(apply_redirect(&verbatim, &redirect), expected);

// If there's a conflict after the `@`, discard the original representation.
let verbatim = VerbatimUrl::parse("https://github.com/flask.git@main")?
.with_given("git+https://github.com/flask.git@${TAG}".to_string());
let redirect =
Url::parse("https://github.com/flask.git@b90a4f1f4a370e92054b9cc9db0efcb864f87ebe")?;

let expected = VerbatimUrl::parse(
"https://github.com/flask.git@b90a4f1f4a370e92054b9cc9db0efcb864f87ebe",
)?;
assert_eq!(apply_redirect(&verbatim, &redirect), expected);

Ok(())
}
}
6 changes: 3 additions & 3 deletions crates/uv-resolver/src/resolution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ use url::Url;
use distribution_types::{Dist, DistributionMetadata, LocalEditable, Name, PackageId, Verbatim};
use once_map::OnceMap;
use pep440_rs::Version;
use pep508_rs::VerbatimUrl;
use pypi_types::{Hashes, Metadata21};
use uv_normalize::{ExtraName, PackageName};

use crate::editables::Editables;
use crate::pins::FilePins;
use crate::pubgrub::{PubGrubDistribution, PubGrubPackage, PubGrubPriority};
use crate::redirect::apply_redirect;
use crate::resolver::VersionsResponse;
use crate::ResolveError;

Expand Down Expand Up @@ -106,7 +106,7 @@ impl ResolutionGraph {
} else {
let url = redirects.get(url).map_or_else(
|| url.clone(),
|url| VerbatimUrl::unknown(url.value().clone()),
|precise| apply_redirect(url, precise.value()),
);
Dist::from_url(package_name.clone(), url)?
};
Expand Down Expand Up @@ -188,7 +188,7 @@ impl ResolutionGraph {
if !metadata.provides_extras.contains(extra) {
let url = redirects.get(url).map_or_else(
|| url.clone(),
|url| VerbatimUrl::unknown(url.value().clone()),
|precise| apply_redirect(url, precise.value()),
);
let pinned_package = Dist::from_url(package_name.clone(), url)?;

Expand Down
3 changes: 2 additions & 1 deletion crates/uv-traits/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use std::path::{Path, PathBuf};
use std::str::FromStr;

use anyhow::Result;
use serde::ser::SerializeMap;

use distribution_types::{CachedDist, DistributionId, IndexLocations, Resolution, SourceDist};
use once_map::OnceMap;
Expand Down Expand Up @@ -361,6 +360,8 @@ impl ConfigSettings {
#[cfg(feature = "serde")]
impl serde::Serialize for ConfigSettings {
fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
use serde::ser::SerializeMap;

let mut map = serializer.serialize_map(Some(self.0.len()))?;
for (key, value) in &self.0 {
match value {
Expand Down