1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
use std::collections::HashSet;

use core::{PackageId, PackageIdSpec, PackageSet, Source, SourceId, Workspace};
use core::registry::PackageRegistry;
use core::resolver::{self, Resolve, Method};
use sources::PathSource;
use ops;
use util::profile;
use util::errors::{CargoResult, CargoResultExt};

/// Resolve all dependencies for the workspace using the previous
/// lockfile as a guide if present.
///
/// This function will also write the result of resolution as a new
/// lockfile.
pub fn resolve_ws<'a>(ws: &Workspace<'a>) -> CargoResult<(PackageSet<'a>, Resolve)> {
    let mut registry = PackageRegistry::new(ws.config())?;
    let resolve = resolve_with_registry(ws, &mut registry, true)?;
    let packages = get_resolved_packages(&resolve, registry);
    Ok((packages, resolve))
}

/// Resolves dependencies for some packages of the workspace,
/// taking into account `paths` overrides and activated features.
pub fn resolve_ws_precisely<'a>(ws: &Workspace<'a>,
                                source: Option<Box<Source + 'a>>,
                                features: &[String],
                                all_features: bool,
                                no_default_features: bool,
                                specs: &[PackageIdSpec])
                                -> CargoResult<(PackageSet<'a>, Resolve)> {
    let features = features.iter()
        .flat_map(|s| s.split_whitespace())
        .flat_map(|s| s.split(','))
        .filter(|s| s.len() > 0)
        .map(|s| s.to_string())
        .collect::<Vec<String>>();

    let mut registry = PackageRegistry::new(ws.config())?;
    if let Some(source) = source {
        registry.add_preloaded(source);
    }

    let resolve = if ws.require_optional_deps() {
        // First, resolve the root_package's *listed* dependencies, as well as
        // downloading and updating all remotes and such.
        let resolve = resolve_with_registry(ws, &mut registry, false)?;

        // Second, resolve with precisely what we're doing. Filter out
        // transitive dependencies if necessary, specify features, handle
        // overrides, etc.
        let _p = profile::start("resolving w/ overrides...");

        add_overrides(&mut registry, ws)?;

        for &(ref replace_spec, ref dep) in ws.root_replace() {
            if !resolve.iter().any(|r| replace_spec.matches(r) && !dep.matches_id(r)) {
                ws.config().shell().warn(
                    format!("package replacement is not used: {}", replace_spec)
                )?
            }
        }

        Some(resolve)
    } else {
        None
    };

    let method = if all_features {
        Method::Everything
    } else {
        Method::Required {
            dev_deps: true, // TODO: remove this option?
            features: &features,
            uses_default_features: !no_default_features,
        }
    };

    let resolved_with_overrides =
    ops::resolve_with_previous(&mut registry, ws,
                               method, resolve.as_ref(), None,
                               specs, true)?;

    let packages = get_resolved_packages(&resolved_with_overrides, registry);

    Ok((packages, resolved_with_overrides))
}

fn resolve_with_registry(ws: &Workspace, registry: &mut PackageRegistry, warn: bool)
                         -> CargoResult<Resolve> {
    let prev = ops::load_pkg_lockfile(ws)?;
    let resolve = resolve_with_previous(registry, ws,
                                        Method::Everything,
                                        prev.as_ref(), None, &[], warn)?;

    if !ws.is_ephemeral() {
        ops::write_pkg_lockfile(ws, &resolve)?;
    }
    Ok(resolve)
}


/// Resolve all dependencies for a package using an optional previous instance
/// of resolve to guide the resolution process.
///
/// This also takes an optional hash set, `to_avoid`, which is a list of package
/// ids that should be avoided when consulting the previous instance of resolve
/// (often used in pairings with updates).
///
/// The previous resolve normally comes from a lockfile. This function does not
/// read or write lockfiles from the filesystem.
pub fn resolve_with_previous<'a>(registry: &mut PackageRegistry,
                                 ws: &Workspace,
                                 method: Method,
                                 previous: Option<&'a Resolve>,
                                 to_avoid: Option<&HashSet<&'a PackageId>>,
                                 specs: &[PackageIdSpec],
                                 warn: bool)
                                 -> CargoResult<Resolve> {
    // Here we place an artificial limitation that all non-registry sources
    // cannot be locked at more than one revision. This means that if a git
    // repository provides more than one package, they must all be updated in
    // step when any of them are updated.
    //
    // TODO: This seems like a hokey reason to single out the registry as being
    //       different
    let mut to_avoid_sources = HashSet::new();
    if let Some(to_avoid) = to_avoid {
        to_avoid_sources.extend(to_avoid.iter()
                                        .map(|p| p.source_id())
                                        .filter(|s| !s.is_registry()));
    }

    let ref keep = |p: &&'a PackageId| {
        !to_avoid_sources.contains(&p.source_id()) && match to_avoid {
            Some(set) => !set.contains(p),
            None => true,
        }
    };

    // In the case where a previous instance of resolve is available, we
    // want to lock as many packages as possible to the previous version
    // without disturbing the graph structure. To this end we perform
    // two actions here:
    //
    // 1. We inform the package registry of all locked packages. This
    //    involves informing it of both the locked package's id as well
    //    as the versions of all locked dependencies. The registry will
    //    then takes this information into account when it is queried.
    //
    // 2. The specified package's summary will have its dependencies
    //    modified to their precise variants. This will instruct the
    //    first step of the resolution process to not query for ranges
    //    but rather for precise dependency versions.
    //
    //    This process must handle altered dependencies, however, as
    //    it's possible for a manifest to change over time to have
    //    dependencies added, removed, or modified to different version
    //    ranges. To deal with this, we only actually lock a dependency
    //    to the previously resolved version if the dependency listed
    //    still matches the locked version.
    if let Some(r) = previous {
        trace!("previous: {:?}", r);
        for node in r.iter().filter(keep) {
            let deps = r.deps_not_replaced(node)
                        .filter(keep)
                        .cloned().collect();
            registry.register_lock(node.clone(), deps);
        }
    }

    for (url, patches) in ws.root_patch() {
        let previous = match previous {
            Some(r) => r,
            None => {
                registry.patch(url, patches)?;
                continue
            }
        };
        let patches = patches.iter().map(|dep| {
            let unused = previous.unused_patches();
            let candidates = previous.iter().chain(unused);
            match candidates.filter(keep).find(|id| dep.matches_id(id)) {
                Some(id) => {
                    let mut dep = dep.clone();
                    dep.lock_to(id);
                    dep
                }
                None => dep.clone(),
            }
        }).collect::<Vec<_>>();
        registry.patch(url, &patches)?;
    }

    let mut summaries = Vec::new();
    for member in ws.members() {
        registry.add_sources(&[member.package_id().source_id().clone()])?;
        let method_to_resolve = match method {
            // When everything for a workspace we want to be sure to resolve all
            // members in the workspace, so propagate the `Method::Everything`.
            Method::Everything => Method::Everything,

            // If we're not resolving everything though then we're constructing the
            // exact crate graph we're going to build. Here we don't necessarily
            // want to keep around all workspace crates as they may not all be
            // built/tested.
            //
            // Additionally, the `method` specified represents command line
            // flags, which really only matters for the current package
            // (determined by the cwd). If other packages are specified (via
            // `-p`) then the command line flags like features don't apply to
            // them.
            //
            // As a result, if this `member` is the current member of the
            // workspace, then we use `method` specified. Otherwise we use a
            // base method with no features specified but using default features
            // for any other packages specified with `-p`.
            Method::Required { dev_deps, .. } => {
                let base = Method::Required {
                    dev_deps: dev_deps,
                    features: &[],
                    uses_default_features: true,
                };
                let member_id = member.package_id();
                match ws.current_opt() {
                    Some(current) if member_id == current.package_id() => method,
                    _ => {
                        if specs.iter().any(|spec| spec.matches(member_id)) {
                            base
                        } else {
                            continue
                        }
                    }
                }
            }
        };

        let summary = registry.lock(member.summary().clone());
        summaries.push((summary, method_to_resolve));
    }

    let root_replace = ws.root_replace();

    let replace = match previous {
        Some(r) => {
            root_replace.iter().map(|&(ref spec, ref dep)| {
                for (key, val) in r.replacements().iter() {
                    if spec.matches(key) && dep.matches_id(val) && keep(&val) {
                        let mut dep = dep.clone();
                        dep.lock_to(val);
                        return (spec.clone(), dep)
                    }
                }
                (spec.clone(), dep.clone())
            }).collect::<Vec<_>>()
        }
        None => root_replace.to_vec(),
    };

    let config = if warn {
        Some(ws.config())
    } else {
        None
    };
    let mut resolved = resolver::resolve(&summaries,
                                         &replace,
                                         registry,
                                         config)?;
    resolved.register_used_patches(registry.patches());
    if let Some(previous) = previous {
        resolved.merge_from(previous)?;
    }
    return Ok(resolved);
}

/// Read the `paths` configuration variable to discover all path overrides that
/// have been configured.
fn add_overrides<'a>(registry: &mut PackageRegistry<'a>,
                     ws: &Workspace<'a>) -> CargoResult<()> {
    let paths = match ws.config().get_list("paths")? {
        Some(list) => list,
        None => return Ok(())
    };

    let paths = paths.val.iter().map(|&(ref s, ref p)| {
        // The path listed next to the string is the config file in which the
        // key was located, so we want to pop off the `.cargo/config` component
        // to get the directory containing the `.cargo` folder.
        (p.parent().unwrap().parent().unwrap().join(s), p)
    });

    for (path, definition) in paths {
        let id = SourceId::for_path(&path)?;
        let mut source = PathSource::new_recursive(&path, &id, ws.config());
        source.update().chain_err(|| {
            format!("failed to update path override `{}` \
                           (defined in `{}`)", path.display(),
                          definition.display())
        })?;
        registry.add_override(Box::new(source));
    }
    Ok(())
}

fn get_resolved_packages<'a>(resolve: &Resolve,
                             registry: PackageRegistry<'a>)
                             -> PackageSet<'a> {
    let ids: Vec<PackageId> = resolve.iter().cloned().collect();
    registry.get(&ids)
}