Skip to content

Draft: allow patching Git source dependencies with patch files #9001

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

Closed
wants to merge 3 commits into from
Closed
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
15 changes: 15 additions & 0 deletions src/cargo/core/dependency.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ struct Inner {
specified_req: bool,
kind: DepKind,
only_match_name: bool,
patch_files: Vec<String>,
explicit_name_in_toml: Option<InternedString>,

optional: bool,
Expand Down Expand Up @@ -217,6 +218,7 @@ impl Dependency {
specified_req: false,
platform: None,
explicit_name_in_toml: None,
patch_files: vec![],
}),
}
}
Expand Down Expand Up @@ -249,6 +251,10 @@ impl Dependency {
self.explicit_name_in_toml().unwrap_or(self.inner.name)
}

pub fn patch_files(&self) -> &Vec<String> {
&self.inner.patch_files
}

/// The name of the package that this `Dependency` depends on.
///
/// Usually this is what's written on the left hand side of a dependencies
Expand Down Expand Up @@ -329,6 +335,15 @@ impl Dependency {
self
}

/// Sets the list of patch files
pub fn set_patch_files(
&mut self,
patch_files: impl IntoIterator<Item = impl Into<String>>,
) -> &mut Dependency {
Rc::make_mut(&mut self.inner).patch_files = patch_files.into_iter().map(|s| s.into()).collect();
self
}

/// Sets the list of features requested for the package.
pub fn set_features(
&mut self,
Expand Down
29 changes: 16 additions & 13 deletions src/cargo/core/registry.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::collections::{HashMap, HashSet};

use crate::core::PackageSet;
use crate::core::{PackageSet, Workspace};
use crate::core::{Dependency, PackageId, Source, SourceId, SourceMap, Summary};
use crate::sources::config::SourceConfigMap;
use crate::util::errors::{CargoResult, CargoResultExt};
Expand All @@ -14,18 +14,19 @@ use url::Url;
/// Source of information about a group of packages.
///
/// See also `core::Source`.
pub trait Registry {
pub trait Registry<'cfg> {
/// Attempt to find the packages that match a dependency request.
fn query(
&mut self,
ws: &Workspace<'cfg>,
dep: &Dependency,
f: &mut dyn FnMut(Summary),
fuzzy: bool,
) -> CargoResult<()>;

fn query_vec(&mut self, dep: &Dependency, fuzzy: bool) -> CargoResult<Vec<Summary>> {
fn query_vec(&mut self, ws: &Workspace<'cfg>, dep: &Dependency, fuzzy: bool) -> CargoResult<Vec<Summary>> {
let mut ret = Vec::new();
self.query(dep, &mut |s| ret.push(s), fuzzy)?;
self.query(ws, dep, &mut |s| ret.push(s), fuzzy)?;
Ok(ret)
}

Expand Down Expand Up @@ -129,7 +130,7 @@ impl<'cfg> PackageRegistry<'cfg> {
PackageSet::new(package_ids, self.sources, self.config)
}

fn ensure_loaded(&mut self, namespace: SourceId, kind: Kind) -> CargoResult<()> {
fn ensure_loaded(&mut self, ws: &Workspace<'cfg>, patch_files: &Vec<String>, namespace: SourceId, kind: Kind) -> CargoResult<()> {
match self.source_ids.get(&namespace) {
// We've previously loaded this source, and we've already locked it,
// so we're not allowed to change it even if `namespace` has a
Expand Down Expand Up @@ -161,13 +162,13 @@ impl<'cfg> PackageRegistry<'cfg> {
}
}

self.load(namespace, kind)?;
self.load(ws, patch_files, namespace, kind)?;
Ok(())
}

pub fn add_sources(&mut self, ids: impl IntoIterator<Item = SourceId>) -> CargoResult<()> {
pub fn add_sources(&mut self, ws: &Workspace<'cfg>, ids: impl IntoIterator<Item = SourceId>) -> CargoResult<()> {
for id in ids {
self.ensure_loaded(id, Kind::Locked)?;
self.ensure_loaded(ws, &vec![], id, Kind::Locked)?;
}
Ok(())
}
Expand Down Expand Up @@ -240,6 +241,7 @@ impl<'cfg> PackageRegistry<'cfg> {
pub fn patch(
&mut self,
url: &Url,
ws: &Workspace<'cfg>,
deps: &[(&Dependency, Option<(Dependency, PackageId)>)],
) -> CargoResult<Vec<(Dependency, PackageId)>> {
// NOTE: None of this code is aware of required features. If a patch
Expand Down Expand Up @@ -280,7 +282,7 @@ impl<'cfg> PackageRegistry<'cfg> {
// Go straight to the source for resolving `dep`. Load it as we
// normally would and then ask it directly for the list of summaries
// corresponding to this `dep`.
self.ensure_loaded(dep.source_id(), Kind::Normal)
self.ensure_loaded(ws, dep.patch_files(), dep.source_id(), Kind::Normal)
.chain_err(|| {
anyhow::format_err!(
"failed to load source for dependency `{}`",
Expand Down Expand Up @@ -373,7 +375,7 @@ impl<'cfg> PackageRegistry<'cfg> {
.collect()
}

fn load(&mut self, source_id: SourceId, kind: Kind) -> CargoResult<()> {
fn load(&mut self, ws: &Workspace<'cfg>, patch_files: &Vec<String>, source_id: SourceId, kind: Kind) -> CargoResult<()> {
(|| {
debug!("loading source {}", source_id);
let source = self.source_config.load(source_id, &self.yanked_whitelist)?;
Expand All @@ -386,7 +388,7 @@ impl<'cfg> PackageRegistry<'cfg> {

// Ensure the source has fetched all necessary remote data.
let _p = profile::start(format!("updating: {}", source_id));
self.sources.get_mut(source_id).unwrap().update()
self.sources.get_mut(source_id).unwrap().update_ws(Some(ws), patch_files)
})()
.chain_err(|| anyhow::format_err!("Unable to update {}", source_id))?;
Ok(())
Expand Down Expand Up @@ -481,9 +483,10 @@ https://doc.rust-lang.org/cargo/reference/overriding-dependencies.html
}
}

impl<'cfg> Registry for PackageRegistry<'cfg> {
impl<'cfg> Registry<'cfg> for PackageRegistry<'cfg> {
fn query(
&mut self,
ws: &Workspace<'cfg>,
dep: &Dependency,
f: &mut dyn FnMut(Summary),
fuzzy: bool,
Expand Down Expand Up @@ -538,7 +541,7 @@ impl<'cfg> Registry for PackageRegistry<'cfg> {
}

// Ensure the requested source_id is loaded
self.ensure_loaded(dep.source_id(), Kind::Normal)
self.ensure_loaded(ws, dep.patch_files(), dep.source_id(), Kind::Normal)
.chain_err(|| {
anyhow::format_err!(
"failed to load source for dependency `{}`",
Expand Down
6 changes: 3 additions & 3 deletions src/cargo/core/resolver/conflict_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,9 +169,9 @@ impl ConflictCache {
/// which are activated in `cx` and contain `PackageId` specified.
/// If more than one are activated, then it will return
/// one that will allow for the most jump-back.
pub fn find_conflicting(
pub fn find_conflicting<'a, 'cfg>(
&self,
cx: &Context,
cx: &Context<'a, 'cfg>,
dep: &Dependency,
must_contain: Option<PackageId>,
) -> Option<&ConflictMap> {
Expand All @@ -186,7 +186,7 @@ impl ConflictCache {
}
out
}
pub fn conflicting(&self, cx: &Context, dep: &Dependency) -> Option<&ConflictMap> {
pub fn conflicting<'a, 'cfg>(&self, cx: &Context<'a, 'cfg>, dep: &Dependency) -> Option<&ConflictMap> {
self.find_conflicting(cx, dep, None)
}

Expand Down
12 changes: 7 additions & 5 deletions src/cargo/core/resolver/context.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use super::dep_cache::RegistryQueryer;
use super::errors::ActivateResult;
use super::types::{ConflictMap, ConflictReason, FeaturesSet, ResolveOpts};
use crate::core::{Dependency, PackageId, SourceId, Summary};
use crate::core::{Dependency, PackageId, SourceId, Summary, Workspace};
use crate::util::interning::InternedString;
use crate::util::Graph;
use anyhow::format_err;
Expand All @@ -18,7 +18,8 @@ pub use super::resolve::Resolve;
// risk of being cloned *a lot* so we want to make this as cheap to clone as
// possible.
#[derive(Clone)]
pub struct Context {
pub struct Context<'a, 'cfg> {
pub ws: &'a Workspace<'cfg>,
pub age: ContextAge,
pub activations: Activations,
/// list the features that are activated for each package
Expand Down Expand Up @@ -76,9 +77,10 @@ impl PackageId {
}
}

impl Context {
pub fn new(check_public_visible_dependencies: bool) -> Context {
impl<'a, 'cfg> Context<'a, 'cfg> {
pub fn new(ws: &'a Workspace<'cfg>, check_public_visible_dependencies: bool) -> Context<'a, 'cfg> {
Context {
ws,
age: 0,
resolve_features: im_rc::HashMap::new(),
links: im_rc::HashMap::new(),
Expand Down Expand Up @@ -243,7 +245,7 @@ impl Context {

pub fn resolve_replacements(
&self,
registry: &RegistryQueryer<'_>,
registry: &RegistryQueryer<'_, '_>,
) -> HashMap<PackageId, PackageId> {
self.activations
.values()
Expand Down
19 changes: 10 additions & 9 deletions src/cargo/core/resolver/dep_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use crate::core::resolver::errors::describe_path;
use crate::core::resolver::types::{ConflictReason, DepInfo, FeaturesSet};
use crate::core::resolver::{ActivateError, ActivateResult, ResolveOpts};
use crate::core::{Dependency, FeatureValue, PackageId, PackageIdSpec, Registry, Summary};
use crate::core::{GitReference, SourceId};
use crate::core::{GitReference, SourceId, Workspace};
use crate::util::errors::{CargoResult, CargoResultExt};
use crate::util::interning::InternedString;
use crate::util::Config;
Expand All @@ -23,8 +23,8 @@ use std::cmp::Ordering;
use std::collections::{BTreeSet, HashMap, HashSet};
use std::rc::Rc;

pub struct RegistryQueryer<'a> {
pub registry: &'a mut (dyn Registry + 'a),
pub struct RegistryQueryer<'a, 'cfg> {
pub registry: &'a mut (dyn Registry<'cfg> + 'a),
replacements: &'a [(PackageIdSpec, Dependency)],
try_to_use: &'a HashSet<PackageId>,
/// If set the list of dependency candidates will be sorted by minimal
Expand All @@ -46,9 +46,9 @@ pub struct RegistryQueryer<'a> {
warned_git_collisions: HashSet<SourceId>,
}

impl<'a> RegistryQueryer<'a> {
impl<'a, 'cfg> RegistryQueryer<'a, 'cfg> {
pub fn new(
registry: &'a mut dyn Registry,
registry: &'a mut dyn Registry<'cfg>,
replacements: &'a [(PackageIdSpec, Dependency)],
try_to_use: &'a HashSet<PackageId>,
minimal_versions: bool,
Expand Down Expand Up @@ -119,14 +119,15 @@ impl<'a> RegistryQueryer<'a> {
/// any candidates are returned which match an override then the override is
/// applied by performing a second query for what the override should
/// return.
pub fn query(&mut self, dep: &Dependency) -> CargoResult<Rc<Vec<Summary>>> {
pub fn query(&mut self, ws: &Workspace<'cfg>, dep: &Dependency) -> CargoResult<Rc<Vec<Summary>>> {
self.warn_colliding_git_sources(dep.source_id())?;
if let Some(out) = self.registry_cache.get(dep).cloned() {
return Ok(out);
}

let mut ret = Vec::new();
self.registry.query(
ws,
dep,
&mut |s| {
ret.push(s);
Expand All @@ -149,7 +150,7 @@ impl<'a> RegistryQueryer<'a> {
dep.version_req()
);

let mut summaries = self.registry.query_vec(dep, false)?.into_iter();
let mut summaries = self.registry.query_vec(ws, dep, false)?.into_iter();
let s = summaries.next().ok_or_else(|| {
anyhow::format_err!(
"no matching package for override `{}` found\n\
Expand Down Expand Up @@ -244,7 +245,7 @@ impl<'a> RegistryQueryer<'a> {
/// next obvious question.
pub fn build_deps(
&mut self,
cx: &Context,
cx: &Context<'a, 'cfg>,
parent: Option<PackageId>,
candidate: &Summary,
opts: &ResolveOpts,
Expand All @@ -268,7 +269,7 @@ impl<'a> RegistryQueryer<'a> {
let mut deps = deps
.into_iter()
.map(|(dep, features)| {
let candidates = self.query(&dep).chain_err(|| {
let candidates = self.query(cx.ws, &dep).chain_err(|| {
anyhow::format_err!(
"failed to get `{}` as a dependency of {}",
dep.package_name(),
Expand Down
10 changes: 5 additions & 5 deletions src/cargo/core/resolver/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ impl From<(PackageId, ConflictReason)> for ActivateError {
}
}

pub(super) fn activation_error(
cx: &Context,
registry: &mut dyn Registry,
pub(super) fn activation_error<'a, 'cfg>(
cx: &Context<'a, 'cfg>,
registry: &mut dyn Registry<'cfg>,
parent: &Summary,
dep: &Dependency,
conflicting_activations: &ConflictMap,
Expand Down Expand Up @@ -214,7 +214,7 @@ pub(super) fn activation_error(
let all_req = semver::VersionReq::parse("*").unwrap();
let mut new_dep = dep.clone();
new_dep.set_version_req(all_req);
let mut candidates = match registry.query_vec(&new_dep, false) {
let mut candidates = match registry.query_vec(cx.ws, &new_dep, false) {
Ok(candidates) => candidates,
Err(e) => return to_resolve_err(e),
};
Expand Down Expand Up @@ -269,7 +269,7 @@ pub(super) fn activation_error(
// Maybe the user mistyped the name? Like `dep-thing` when `Dep_Thing`
// was meant. So we try asking the registry for a `fuzzy` search for suggestions.
let mut candidates = Vec::new();
if let Err(e) = registry.query(&new_dep, &mut |s| candidates.push(s), true) {
if let Err(e) = registry.query(cx.ws, &new_dep, &mut |s| candidates.push(s), true) {
return to_resolve_err(e);
};
candidates.sort_unstable_by_key(|a| a.name());
Expand Down
Loading