cargo/core/
workspace.rs

1use std::cell::RefCell;
2use std::collections::hash_map::{Entry, HashMap};
3use std::collections::{BTreeMap, BTreeSet, HashSet};
4use std::path::{Path, PathBuf};
5use std::rc::Rc;
6
7use anyhow::{anyhow, bail, Context as _};
8use glob::glob;
9use itertools::Itertools;
10use tracing::debug;
11use url::Url;
12
13use crate::core::compiler::Unit;
14use crate::core::features::Features;
15use crate::core::registry::PackageRegistry;
16use crate::core::resolver::features::CliFeatures;
17use crate::core::resolver::ResolveBehavior;
18use crate::core::{
19    Dependency, Edition, FeatureValue, PackageId, PackageIdSpec, PackageIdSpecQuery,
20};
21use crate::core::{EitherManifest, Package, SourceId, VirtualManifest};
22use crate::ops;
23use crate::sources::{PathSource, SourceConfigMap, CRATES_IO_INDEX, CRATES_IO_REGISTRY};
24use crate::util::context::FeatureUnification;
25use crate::util::edit_distance;
26use crate::util::errors::{CargoResult, ManifestError};
27use crate::util::interning::InternedString;
28use crate::util::lints::{analyze_cargo_lints_table, check_im_a_teapot};
29use crate::util::toml::{read_manifest, InheritableFields};
30use crate::util::{
31    context::CargoResolverConfig, context::ConfigRelativePath, context::IncompatibleRustVersions,
32    Filesystem, GlobalContext, IntoUrl,
33};
34use cargo_util::paths;
35use cargo_util::paths::normalize_path;
36use cargo_util_schemas::manifest;
37use cargo_util_schemas::manifest::RustVersion;
38use cargo_util_schemas::manifest::{TomlDependency, TomlProfiles};
39use pathdiff::diff_paths;
40
41/// The core abstraction in Cargo for working with a workspace of crates.
42///
43/// A workspace is often created very early on and then threaded through all
44/// other functions. It's typically through this object that the current
45/// package is loaded and/or learned about.
46#[derive(Debug)]
47pub struct Workspace<'gctx> {
48    /// Cargo configuration information. See [`GlobalContext`].
49    gctx: &'gctx GlobalContext,
50
51    /// This path is a path to where the current cargo subcommand was invoked
52    /// from. That is the `--manifest-path` argument to Cargo, and
53    /// points to the "main crate" that we're going to worry about.
54    current_manifest: PathBuf,
55
56    /// A list of packages found in this workspace. Always includes at least the
57    /// package mentioned by `current_manifest`.
58    packages: Packages<'gctx>,
59
60    /// If this workspace includes more than one crate, this points to the root
61    /// of the workspace. This is `None` in the case that `[workspace]` is
62    /// missing, `package.workspace` is missing, and no `Cargo.toml` above
63    /// `current_manifest` was found on the filesystem with `[workspace]`.
64    root_manifest: Option<PathBuf>,
65
66    /// Shared target directory for all the packages of this workspace.
67    /// `None` if the default path of `root/target` should be used.
68    target_dir: Option<Filesystem>,
69
70    /// Shared build directory for intermediate build artifacts.
71    /// This directory may be shared between multiple workspaces.
72    build_dir: Option<Filesystem>,
73
74    /// List of members in this workspace with a listing of all their manifest
75    /// paths. The packages themselves can be looked up through the `packages`
76    /// set above.
77    members: Vec<PathBuf>,
78    /// Set of ids of workspace members
79    member_ids: HashSet<PackageId>,
80
81    /// The subset of `members` that are used by the
82    /// `build`, `check`, `test`, and `bench` subcommands
83    /// when no package is selected with `--package` / `-p` and `--workspace`
84    /// is not used.
85    ///
86    /// This is set by the `default-members` config
87    /// in the `[workspace]` section.
88    /// When unset, this is the same as `members` for virtual workspaces
89    /// (`--workspace` is implied)
90    /// or only the root package for non-virtual workspaces.
91    default_members: Vec<PathBuf>,
92
93    /// `true` if this is a temporary workspace created for the purposes of the
94    /// `cargo install` or `cargo package` commands.
95    is_ephemeral: bool,
96
97    /// `true` if this workspace should enforce optional dependencies even when
98    /// not needed; false if this workspace should only enforce dependencies
99    /// needed by the current configuration (such as in cargo install). In some
100    /// cases `false` also results in the non-enforcement of dev-dependencies.
101    require_optional_deps: bool,
102
103    /// A cache of loaded packages for particular paths which is disjoint from
104    /// `packages` up above, used in the `load` method down below.
105    loaded_packages: RefCell<HashMap<PathBuf, Package>>,
106
107    /// If `true`, then the resolver will ignore any existing `Cargo.lock`
108    /// file. This is set for `cargo install` without `--locked`.
109    ignore_lock: bool,
110
111    /// Requested path of the lockfile (i.e. passed as the cli flag)
112    requested_lockfile_path: Option<PathBuf>,
113
114    /// The resolver behavior specified with the `resolver` field.
115    resolve_behavior: ResolveBehavior,
116    /// If `true`, then workspace `rust_version` would be used in `cargo resolve`
117    /// and other places that use rust version.
118    /// This is set based on the resolver version, config settings, and CLI flags.
119    resolve_honors_rust_version: bool,
120    /// The feature unification mode used when building packages.
121    resolve_feature_unification: FeatureUnification,
122    /// Workspace-level custom metadata
123    custom_metadata: Option<toml::Value>,
124
125    /// Local overlay configuration. See [`crate::sources::overlay`].
126    local_overlays: HashMap<SourceId, PathBuf>,
127}
128
129// Separate structure for tracking loaded packages (to avoid loading anything
130// twice), and this is separate to help appease the borrow checker.
131#[derive(Debug)]
132struct Packages<'gctx> {
133    gctx: &'gctx GlobalContext,
134    packages: HashMap<PathBuf, MaybePackage>,
135}
136
137#[derive(Debug)]
138pub enum MaybePackage {
139    Package(Package),
140    Virtual(VirtualManifest),
141}
142
143/// Configuration of a workspace in a manifest.
144#[derive(Debug, Clone)]
145pub enum WorkspaceConfig {
146    /// Indicates that `[workspace]` was present and the members were
147    /// optionally specified as well.
148    Root(WorkspaceRootConfig),
149
150    /// Indicates that `[workspace]` was present and the `root` field is the
151    /// optional value of `package.workspace`, if present.
152    Member { root: Option<String> },
153}
154
155impl WorkspaceConfig {
156    pub fn inheritable(&self) -> Option<&InheritableFields> {
157        match self {
158            WorkspaceConfig::Root(root) => Some(&root.inheritable_fields),
159            WorkspaceConfig::Member { .. } => None,
160        }
161    }
162
163    /// Returns the path of the workspace root based on this `[workspace]` configuration.
164    ///
165    /// Returns `None` if the root is not explicitly known.
166    ///
167    /// * `self_path` is the path of the manifest this `WorkspaceConfig` is located.
168    /// * `look_from` is the path where discovery started (usually the current
169    ///   working directory), used for `workspace.exclude` checking.
170    fn get_ws_root(&self, self_path: &Path, look_from: &Path) -> Option<PathBuf> {
171        match self {
172            WorkspaceConfig::Root(ances_root_config) => {
173                debug!("find_root - found a root checking exclusion");
174                if !ances_root_config.is_excluded(look_from) {
175                    debug!("find_root - found!");
176                    Some(self_path.to_owned())
177                } else {
178                    None
179                }
180            }
181            WorkspaceConfig::Member {
182                root: Some(path_to_root),
183            } => {
184                debug!("find_root - found pointer");
185                Some(read_root_pointer(self_path, path_to_root))
186            }
187            WorkspaceConfig::Member { .. } => None,
188        }
189    }
190}
191
192/// Intermediate configuration of a workspace root in a manifest.
193///
194/// Knows the Workspace Root path, as well as `members` and `exclude` lists of path patterns, which
195/// together tell if some path is recognized as a member by this root or not.
196#[derive(Debug, Clone)]
197pub struct WorkspaceRootConfig {
198    root_dir: PathBuf,
199    members: Option<Vec<String>>,
200    default_members: Option<Vec<String>>,
201    exclude: Vec<String>,
202    inheritable_fields: InheritableFields,
203    custom_metadata: Option<toml::Value>,
204}
205
206impl<'gctx> Workspace<'gctx> {
207    /// Creates a new workspace given the target manifest pointed to by
208    /// `manifest_path`.
209    ///
210    /// This function will construct the entire workspace by determining the
211    /// root and all member packages. It will then validate the workspace
212    /// before returning it, so `Ok` is only returned for valid workspaces.
213    pub fn new(manifest_path: &Path, gctx: &'gctx GlobalContext) -> CargoResult<Workspace<'gctx>> {
214        let mut ws = Workspace::new_default(manifest_path.to_path_buf(), gctx);
215        ws.target_dir = gctx.target_dir()?;
216
217        if manifest_path.is_relative() {
218            bail!(
219                "manifest_path:{:?} is not an absolute path. Please provide an absolute path.",
220                manifest_path
221            )
222        } else {
223            ws.root_manifest = ws.find_root(manifest_path)?;
224        }
225
226        ws.build_dir = gctx.build_dir(
227            ws.root_manifest
228                .as_ref()
229                .unwrap_or(&manifest_path.to_path_buf()),
230        )?;
231
232        ws.custom_metadata = ws
233            .load_workspace_config()?
234            .and_then(|cfg| cfg.custom_metadata);
235        ws.find_members()?;
236        ws.set_resolve_behavior()?;
237        ws.validate()?;
238        Ok(ws)
239    }
240
241    fn new_default(current_manifest: PathBuf, gctx: &'gctx GlobalContext) -> Workspace<'gctx> {
242        Workspace {
243            gctx,
244            current_manifest,
245            packages: Packages {
246                gctx,
247                packages: HashMap::new(),
248            },
249            root_manifest: None,
250            target_dir: None,
251            build_dir: None,
252            members: Vec::new(),
253            member_ids: HashSet::new(),
254            default_members: Vec::new(),
255            is_ephemeral: false,
256            require_optional_deps: true,
257            loaded_packages: RefCell::new(HashMap::new()),
258            ignore_lock: false,
259            requested_lockfile_path: None,
260            resolve_behavior: ResolveBehavior::V1,
261            resolve_honors_rust_version: false,
262            resolve_feature_unification: FeatureUnification::Selected,
263            custom_metadata: None,
264            local_overlays: HashMap::new(),
265        }
266    }
267
268    /// Creates a "temporary workspace" from one package which only contains
269    /// that package.
270    ///
271    /// This constructor will not touch the filesystem and only creates an
272    /// in-memory workspace. That is, all configuration is ignored, it's just
273    /// intended for that one package.
274    ///
275    /// This is currently only used in niche situations like `cargo install` or
276    /// `cargo package`.
277    pub fn ephemeral(
278        package: Package,
279        gctx: &'gctx GlobalContext,
280        target_dir: Option<Filesystem>,
281        require_optional_deps: bool,
282    ) -> CargoResult<Workspace<'gctx>> {
283        let mut ws = Workspace::new_default(package.manifest_path().to_path_buf(), gctx);
284        ws.is_ephemeral = true;
285        ws.require_optional_deps = require_optional_deps;
286        let id = package.package_id();
287        let package = MaybePackage::Package(package);
288        ws.packages
289            .packages
290            .insert(ws.current_manifest.clone(), package);
291        ws.target_dir = if let Some(dir) = target_dir {
292            Some(dir)
293        } else {
294            ws.gctx.target_dir()?
295        };
296        ws.build_dir = ws.target_dir.clone();
297        ws.members.push(ws.current_manifest.clone());
298        ws.member_ids.insert(id);
299        ws.default_members.push(ws.current_manifest.clone());
300        ws.set_resolve_behavior()?;
301        Ok(ws)
302    }
303
304    fn set_resolve_behavior(&mut self) -> CargoResult<()> {
305        // - If resolver is specified in the workspace definition, use that.
306        // - If the root package specifies the resolver, use that.
307        // - If the root package specifies edition 2021, use v2.
308        // - Otherwise, use the default v1.
309        self.resolve_behavior = match self.root_maybe() {
310            MaybePackage::Package(p) => p
311                .manifest()
312                .resolve_behavior()
313                .unwrap_or_else(|| p.manifest().edition().default_resolve_behavior()),
314            MaybePackage::Virtual(vm) => vm.resolve_behavior().unwrap_or(ResolveBehavior::V1),
315        };
316
317        match self.resolve_behavior() {
318            ResolveBehavior::V1 | ResolveBehavior::V2 => {}
319            ResolveBehavior::V3 => {
320                if self.resolve_behavior == ResolveBehavior::V3 {
321                    self.resolve_honors_rust_version = true;
322                }
323            }
324        }
325        let config = self.gctx().get::<CargoResolverConfig>("resolver")?;
326        if let Some(incompatible_rust_versions) = config.incompatible_rust_versions {
327            self.resolve_honors_rust_version =
328                incompatible_rust_versions == IncompatibleRustVersions::Fallback;
329        }
330        if self.gctx().cli_unstable().feature_unification {
331            self.resolve_feature_unification = config
332                .feature_unification
333                .unwrap_or(FeatureUnification::Selected);
334        } else if config.feature_unification.is_some() {
335            self.gctx()
336                .shell()
337                .warn("ignoring `resolver.feature-unification` without `-Zfeature-unification`")?;
338        };
339
340        Ok(())
341    }
342
343    /// Returns the current package of this workspace.
344    ///
345    /// Note that this can return an error if it the current manifest is
346    /// actually a "virtual Cargo.toml", in which case an error is returned
347    /// indicating that something else should be passed.
348    pub fn current(&self) -> CargoResult<&Package> {
349        let pkg = self.current_opt().ok_or_else(|| {
350            anyhow::format_err!(
351                "manifest path `{}` is a virtual manifest, but this \
352                 command requires running against an actual package in \
353                 this workspace",
354                self.current_manifest.display()
355            )
356        })?;
357        Ok(pkg)
358    }
359
360    pub fn current_mut(&mut self) -> CargoResult<&mut Package> {
361        let cm = self.current_manifest.clone();
362        let pkg = self.current_opt_mut().ok_or_else(|| {
363            anyhow::format_err!(
364                "manifest path `{}` is a virtual manifest, but this \
365                 command requires running against an actual package in \
366                 this workspace",
367                cm.display()
368            )
369        })?;
370        Ok(pkg)
371    }
372
373    pub fn current_opt(&self) -> Option<&Package> {
374        match *self.packages.get(&self.current_manifest) {
375            MaybePackage::Package(ref p) => Some(p),
376            MaybePackage::Virtual(..) => None,
377        }
378    }
379
380    pub fn current_opt_mut(&mut self) -> Option<&mut Package> {
381        match *self.packages.get_mut(&self.current_manifest) {
382            MaybePackage::Package(ref mut p) => Some(p),
383            MaybePackage::Virtual(..) => None,
384        }
385    }
386
387    pub fn is_virtual(&self) -> bool {
388        match *self.packages.get(&self.current_manifest) {
389            MaybePackage::Package(..) => false,
390            MaybePackage::Virtual(..) => true,
391        }
392    }
393
394    /// Returns the `GlobalContext` this workspace is associated with.
395    pub fn gctx(&self) -> &'gctx GlobalContext {
396        self.gctx
397    }
398
399    pub fn profiles(&self) -> Option<&TomlProfiles> {
400        match self.root_maybe() {
401            MaybePackage::Package(p) => p.manifest().profiles(),
402            MaybePackage::Virtual(vm) => vm.profiles(),
403        }
404    }
405
406    /// Returns the root path of this workspace.
407    ///
408    /// That is, this returns the path of the directory containing the
409    /// `Cargo.toml` which is the root of this workspace.
410    pub fn root(&self) -> &Path {
411        self.root_manifest().parent().unwrap()
412    }
413
414    /// Returns the path of the `Cargo.toml` which is the root of this
415    /// workspace.
416    pub fn root_manifest(&self) -> &Path {
417        self.root_manifest
418            .as_ref()
419            .unwrap_or(&self.current_manifest)
420    }
421
422    /// Returns the root Package or `VirtualManifest`.
423    pub fn root_maybe(&self) -> &MaybePackage {
424        self.packages.get(self.root_manifest())
425    }
426
427    pub fn target_dir(&self) -> Filesystem {
428        self.target_dir
429            .clone()
430            .unwrap_or_else(|| self.default_target_dir())
431    }
432
433    pub fn build_dir(&self) -> Filesystem {
434        if !self.gctx().cli_unstable().build_dir {
435            return self.target_dir();
436        }
437        self.build_dir.clone().unwrap_or_else(|| self.target_dir())
438    }
439
440    fn default_target_dir(&self) -> Filesystem {
441        if self.root_maybe().is_embedded() {
442            let hash = crate::util::hex::short_hash(&self.root_manifest().to_string_lossy());
443            let mut rel_path = PathBuf::new();
444            rel_path.push("target");
445            rel_path.push(&hash[0..2]);
446            rel_path.push(&hash[2..]);
447
448            self.gctx().home().join(rel_path)
449        } else {
450            Filesystem::new(self.root().join("target"))
451        }
452    }
453
454    /// Returns the root `[replace]` section of this workspace.
455    ///
456    /// This may be from a virtual crate or an actual crate.
457    pub fn root_replace(&self) -> &[(PackageIdSpec, Dependency)] {
458        match self.root_maybe() {
459            MaybePackage::Package(p) => p.manifest().replace(),
460            MaybePackage::Virtual(vm) => vm.replace(),
461        }
462    }
463
464    fn config_patch(&self) -> CargoResult<HashMap<Url, Vec<Dependency>>> {
465        let config_patch: Option<
466            BTreeMap<String, BTreeMap<String, TomlDependency<ConfigRelativePath>>>,
467        > = self.gctx.get("patch")?;
468
469        let source = SourceId::for_manifest_path(self.root_manifest())?;
470
471        let mut warnings = Vec::new();
472
473        let mut patch = HashMap::new();
474        for (url, deps) in config_patch.into_iter().flatten() {
475            let url = match &url[..] {
476                CRATES_IO_REGISTRY => CRATES_IO_INDEX.parse().unwrap(),
477                url => self
478                    .gctx
479                    .get_registry_index(url)
480                    .or_else(|_| url.into_url())
481                    .with_context(|| {
482                        format!("[patch] entry `{}` should be a URL or registry name", url)
483                    })?,
484            };
485            patch.insert(
486                url,
487                deps.iter()
488                    .map(|(name, dep)| {
489                        crate::util::toml::to_dependency(
490                            dep,
491                            name,
492                            source,
493                            self.gctx,
494                            &mut warnings,
495                            /* platform */ None,
496                            // NOTE: Since we use ConfigRelativePath, this root isn't used as
497                            // any relative paths are resolved before they'd be joined with root.
498                            Path::new("unused-relative-path"),
499                            /* kind */ None,
500                        )
501                    })
502                    .collect::<CargoResult<Vec<_>>>()?,
503            );
504        }
505
506        for message in warnings {
507            self.gctx
508                .shell()
509                .warn(format!("[patch] in cargo config: {}", message))?
510        }
511
512        Ok(patch)
513    }
514
515    /// Returns the root `[patch]` section of this workspace.
516    ///
517    /// This may be from a virtual crate or an actual crate.
518    pub fn root_patch(&self) -> CargoResult<HashMap<Url, Vec<Dependency>>> {
519        let from_manifest = match self.root_maybe() {
520            MaybePackage::Package(p) => p.manifest().patch(),
521            MaybePackage::Virtual(vm) => vm.patch(),
522        };
523
524        let from_config = self.config_patch()?;
525        if from_config.is_empty() {
526            return Ok(from_manifest.clone());
527        }
528        if from_manifest.is_empty() {
529            return Ok(from_config);
530        }
531
532        // We could just chain from_manifest and from_config,
533        // but that's not quite right as it won't deal with overlaps.
534        let mut combined = from_config;
535        for (url, deps_from_manifest) in from_manifest {
536            if let Some(deps_from_config) = combined.get_mut(url) {
537                // We want from_config to take precedence for each patched name.
538                // NOTE: This is inefficient if the number of patches is large!
539                let mut from_manifest_pruned = deps_from_manifest.clone();
540                for dep_from_config in &mut *deps_from_config {
541                    if let Some(i) = from_manifest_pruned.iter().position(|dep_from_manifest| {
542                        // XXX: should this also take into account version numbers?
543                        dep_from_config.name_in_toml() == dep_from_manifest.name_in_toml()
544                    }) {
545                        from_manifest_pruned.swap_remove(i);
546                    }
547                }
548                // Whatever is left does not exist in manifest dependencies.
549                deps_from_config.extend(from_manifest_pruned);
550            } else {
551                combined.insert(url.clone(), deps_from_manifest.clone());
552            }
553        }
554        Ok(combined)
555    }
556
557    /// Returns an iterator over all packages in this workspace
558    pub fn members(&self) -> impl Iterator<Item = &Package> {
559        let packages = &self.packages;
560        self.members
561            .iter()
562            .filter_map(move |path| match packages.get(path) {
563                MaybePackage::Package(p) => Some(p),
564                _ => None,
565            })
566    }
567
568    /// Returns a mutable iterator over all packages in this workspace
569    pub fn members_mut(&mut self) -> impl Iterator<Item = &mut Package> {
570        let packages = &mut self.packages.packages;
571        let members: HashSet<_> = self.members.iter().map(|path| path).collect();
572
573        packages.iter_mut().filter_map(move |(path, package)| {
574            if members.contains(path) {
575                if let MaybePackage::Package(ref mut p) = package {
576                    return Some(p);
577                }
578            }
579
580            None
581        })
582    }
583
584    /// Returns an iterator over default packages in this workspace
585    pub fn default_members<'a>(&'a self) -> impl Iterator<Item = &'a Package> {
586        let packages = &self.packages;
587        self.default_members
588            .iter()
589            .filter_map(move |path| match packages.get(path) {
590                MaybePackage::Package(p) => Some(p),
591                _ => None,
592            })
593    }
594
595    /// Returns an iterator over default packages in this workspace
596    pub fn default_members_mut(&mut self) -> impl Iterator<Item = &mut Package> {
597        let packages = &mut self.packages.packages;
598        let members: HashSet<_> = self
599            .default_members
600            .iter()
601            .map(|path| path.parent().unwrap().to_owned())
602            .collect();
603
604        packages.iter_mut().filter_map(move |(path, package)| {
605            if members.contains(path) {
606                if let MaybePackage::Package(ref mut p) = package {
607                    return Some(p);
608                }
609            }
610
611            None
612        })
613    }
614
615    /// Returns true if the package is a member of the workspace.
616    pub fn is_member(&self, pkg: &Package) -> bool {
617        self.member_ids.contains(&pkg.package_id())
618    }
619
620    /// Returns true if the given package_id is a member of the workspace.
621    pub fn is_member_id(&self, package_id: PackageId) -> bool {
622        self.member_ids.contains(&package_id)
623    }
624
625    pub fn is_ephemeral(&self) -> bool {
626        self.is_ephemeral
627    }
628
629    pub fn require_optional_deps(&self) -> bool {
630        self.require_optional_deps
631    }
632
633    pub fn set_require_optional_deps(
634        &mut self,
635        require_optional_deps: bool,
636    ) -> &mut Workspace<'gctx> {
637        self.require_optional_deps = require_optional_deps;
638        self
639    }
640
641    pub fn ignore_lock(&self) -> bool {
642        self.ignore_lock
643    }
644
645    pub fn set_ignore_lock(&mut self, ignore_lock: bool) -> &mut Workspace<'gctx> {
646        self.ignore_lock = ignore_lock;
647        self
648    }
649
650    /// Returns the directory where the lockfile is in.
651    pub fn lock_root(&self) -> Filesystem {
652        if let Some(requested) = self.requested_lockfile_path.as_ref() {
653            return Filesystem::new(
654                requested
655                    .parent()
656                    .expect("Lockfile path can't be root")
657                    .to_owned(),
658            );
659        }
660        self.default_lock_root()
661    }
662
663    fn default_lock_root(&self) -> Filesystem {
664        if self.root_maybe().is_embedded() {
665            self.target_dir()
666        } else {
667            Filesystem::new(self.root().to_owned())
668        }
669    }
670
671    pub fn set_requested_lockfile_path(&mut self, path: Option<PathBuf>) {
672        self.requested_lockfile_path = path;
673    }
674
675    pub fn requested_lockfile_path(&self) -> Option<&Path> {
676        self.requested_lockfile_path.as_deref()
677    }
678
679    /// Get the lowest-common denominator `package.rust-version` within the workspace, if specified
680    /// anywhere
681    pub fn lowest_rust_version(&self) -> Option<&RustVersion> {
682        self.members().filter_map(|pkg| pkg.rust_version()).min()
683    }
684
685    pub fn set_resolve_honors_rust_version(&mut self, honor_rust_version: Option<bool>) {
686        if let Some(honor_rust_version) = honor_rust_version {
687            self.resolve_honors_rust_version = honor_rust_version;
688        }
689    }
690
691    pub fn resolve_honors_rust_version(&self) -> bool {
692        self.resolve_honors_rust_version
693    }
694
695    pub fn set_resolve_feature_unification(&mut self, feature_unification: FeatureUnification) {
696        self.resolve_feature_unification = feature_unification;
697    }
698
699    pub fn resolve_feature_unification(&self) -> FeatureUnification {
700        self.resolve_feature_unification
701    }
702
703    pub fn custom_metadata(&self) -> Option<&toml::Value> {
704        self.custom_metadata.as_ref()
705    }
706
707    pub fn load_workspace_config(&mut self) -> CargoResult<Option<WorkspaceRootConfig>> {
708        // If we didn't find a root, it must mean there is no [workspace] section, and thus no
709        // metadata.
710        if let Some(root_path) = &self.root_manifest {
711            let root_package = self.packages.load(root_path)?;
712            match root_package.workspace_config() {
713                WorkspaceConfig::Root(ref root_config) => {
714                    return Ok(Some(root_config.clone()));
715                }
716
717                _ => bail!(
718                    "root of a workspace inferred but wasn't a root: {}",
719                    root_path.display()
720                ),
721            }
722        }
723
724        Ok(None)
725    }
726
727    /// Finds the root of a workspace for the crate whose manifest is located
728    /// at `manifest_path`.
729    ///
730    /// This will parse the `Cargo.toml` at `manifest_path` and then interpret
731    /// the workspace configuration, optionally walking up the filesystem
732    /// looking for other workspace roots.
733    ///
734    /// Returns an error if `manifest_path` isn't actually a valid manifest or
735    /// if some other transient error happens.
736    fn find_root(&mut self, manifest_path: &Path) -> CargoResult<Option<PathBuf>> {
737        let current = self.packages.load(manifest_path)?;
738        match current
739            .workspace_config()
740            .get_ws_root(manifest_path, manifest_path)
741        {
742            Some(root_path) => {
743                debug!("find_root - is root {}", manifest_path.display());
744                Ok(Some(root_path))
745            }
746            None => find_workspace_root_with_loader(manifest_path, self.gctx, |self_path| {
747                Ok(self
748                    .packages
749                    .load(self_path)?
750                    .workspace_config()
751                    .get_ws_root(self_path, manifest_path))
752            }),
753        }
754    }
755
756    /// After the root of a workspace has been located, probes for all members
757    /// of a workspace.
758    ///
759    /// If the `workspace.members` configuration is present, then this just
760    /// verifies that those are all valid packages to point to. Otherwise, this
761    /// will transitively follow all `path` dependencies looking for members of
762    /// the workspace.
763    #[tracing::instrument(skip_all)]
764    fn find_members(&mut self) -> CargoResult<()> {
765        let Some(workspace_config) = self.load_workspace_config()? else {
766            debug!("find_members - only me as a member");
767            self.members.push(self.current_manifest.clone());
768            self.default_members.push(self.current_manifest.clone());
769            if let Ok(pkg) = self.current() {
770                let id = pkg.package_id();
771                self.member_ids.insert(id);
772            }
773            return Ok(());
774        };
775
776        // self.root_manifest must be Some to have retrieved workspace_config
777        let root_manifest_path = self.root_manifest.clone().unwrap();
778
779        let members_paths = workspace_config
780            .members_paths(workspace_config.members.as_deref().unwrap_or_default())?;
781        let default_members_paths = if root_manifest_path == self.current_manifest {
782            if let Some(ref default) = workspace_config.default_members {
783                Some(workspace_config.members_paths(default)?)
784            } else {
785                None
786            }
787        } else {
788            None
789        };
790
791        for (path, glob) in &members_paths {
792            self.find_path_deps(&path.join("Cargo.toml"), &root_manifest_path, false)
793                .with_context(|| {
794                    format!(
795                        "failed to load manifest for workspace member `{}`\n\
796                        referenced{} by workspace at `{}`",
797                        path.display(),
798                        glob.map(|g| format!(" via `{g}`")).unwrap_or_default(),
799                        root_manifest_path.display(),
800                    )
801                })?;
802        }
803
804        self.find_path_deps(&root_manifest_path, &root_manifest_path, false)?;
805
806        if let Some(default) = default_members_paths {
807            for (path, default_member_glob) in default {
808                let normalized_path = paths::normalize_path(&path);
809                let manifest_path = normalized_path.join("Cargo.toml");
810                if !self.members.contains(&manifest_path) {
811                    // default-members are allowed to be excluded, but they
812                    // still must be referred to by the original (unfiltered)
813                    // members list. Note that we aren't testing against the
814                    // manifest path, both because `members_paths` doesn't
815                    // include `/Cargo.toml`, and because excluded paths may not
816                    // be crates.
817                    let exclude = members_paths.iter().any(|(m, _)| *m == normalized_path)
818                        && workspace_config.is_excluded(&normalized_path);
819                    if exclude {
820                        continue;
821                    }
822                    bail!(
823                        "package `{}` is listed in default-members{} but is not a member\n\
824                        for workspace at `{}`.",
825                        path.display(),
826                        default_member_glob
827                            .map(|g| format!(" via `{g}`"))
828                            .unwrap_or_default(),
829                        root_manifest_path.display(),
830                    )
831                }
832                self.default_members.push(manifest_path)
833            }
834        } else if self.is_virtual() {
835            self.default_members = self.members.clone()
836        } else {
837            self.default_members.push(self.current_manifest.clone())
838        }
839
840        Ok(())
841    }
842
843    fn find_path_deps(
844        &mut self,
845        manifest_path: &Path,
846        root_manifest: &Path,
847        is_path_dep: bool,
848    ) -> CargoResult<()> {
849        let manifest_path = paths::normalize_path(manifest_path);
850        if self.members.contains(&manifest_path) {
851            return Ok(());
852        }
853        if is_path_dep && self.root_maybe().is_embedded() {
854            // Embedded manifests cannot have workspace members
855            return Ok(());
856        }
857        if is_path_dep
858            && !manifest_path.parent().unwrap().starts_with(self.root())
859            && self.find_root(&manifest_path)? != self.root_manifest
860        {
861            // If `manifest_path` is a path dependency outside of the workspace,
862            // don't add it, or any of its dependencies, as a members.
863            return Ok(());
864        }
865
866        if let WorkspaceConfig::Root(ref root_config) =
867            *self.packages.load(root_manifest)?.workspace_config()
868        {
869            if root_config.is_excluded(&manifest_path) {
870                return Ok(());
871            }
872        }
873
874        debug!("find_path_deps - {}", manifest_path.display());
875        self.members.push(manifest_path.clone());
876
877        let candidates = {
878            let pkg = match *self.packages.load(&manifest_path)? {
879                MaybePackage::Package(ref p) => p,
880                MaybePackage::Virtual(_) => return Ok(()),
881            };
882            self.member_ids.insert(pkg.package_id());
883            pkg.dependencies()
884                .iter()
885                .map(|d| (d.source_id(), d.package_name()))
886                .filter(|(s, _)| s.is_path())
887                .filter_map(|(s, n)| s.url().to_file_path().ok().map(|p| (p, n)))
888                .map(|(p, n)| (p.join("Cargo.toml"), n))
889                .collect::<Vec<_>>()
890        };
891        for (path, name) in candidates {
892            self.find_path_deps(&path, root_manifest, true)
893                .with_context(|| format!("failed to load manifest for dependency `{}`", name))
894                .map_err(|err| ManifestError::new(err, manifest_path.clone()))?;
895        }
896        Ok(())
897    }
898
899    /// Returns the unstable nightly-only features enabled via `cargo-features` in the manifest.
900    pub fn unstable_features(&self) -> &Features {
901        match self.root_maybe() {
902            MaybePackage::Package(p) => p.manifest().unstable_features(),
903            MaybePackage::Virtual(vm) => vm.unstable_features(),
904        }
905    }
906
907    pub fn resolve_behavior(&self) -> ResolveBehavior {
908        self.resolve_behavior
909    }
910
911    /// Returns `true` if this workspace uses the new CLI features behavior.
912    ///
913    /// The old behavior only allowed choosing the features from the package
914    /// in the current directory, regardless of which packages were chosen
915    /// with the -p flags. The new behavior allows selecting features from the
916    /// packages chosen on the command line (with -p or --workspace flags),
917    /// ignoring whatever is in the current directory.
918    pub fn allows_new_cli_feature_behavior(&self) -> bool {
919        self.is_virtual()
920            || match self.resolve_behavior() {
921                ResolveBehavior::V1 => false,
922                ResolveBehavior::V2 | ResolveBehavior::V3 => true,
923            }
924    }
925
926    /// Validates a workspace, ensuring that a number of invariants are upheld:
927    ///
928    /// 1. A workspace only has one root.
929    /// 2. All workspace members agree on this one root as the root.
930    /// 3. The current crate is a member of this workspace.
931    #[tracing::instrument(skip_all)]
932    fn validate(&mut self) -> CargoResult<()> {
933        // The rest of the checks require a VirtualManifest or multiple members.
934        if self.root_manifest.is_none() {
935            return Ok(());
936        }
937
938        self.validate_unique_names()?;
939        self.validate_workspace_roots()?;
940        self.validate_members()?;
941        self.error_if_manifest_not_in_members()?;
942        self.validate_manifest()
943    }
944
945    fn validate_unique_names(&self) -> CargoResult<()> {
946        let mut names = BTreeMap::new();
947        for member in self.members.iter() {
948            let package = self.packages.get(member);
949            let name = match *package {
950                MaybePackage::Package(ref p) => p.name(),
951                MaybePackage::Virtual(_) => continue,
952            };
953            if let Some(prev) = names.insert(name, member) {
954                bail!(
955                    "two packages named `{}` in this workspace:\n\
956                         - {}\n\
957                         - {}",
958                    name,
959                    prev.display(),
960                    member.display()
961                );
962            }
963        }
964        Ok(())
965    }
966
967    fn validate_workspace_roots(&self) -> CargoResult<()> {
968        let roots: Vec<PathBuf> = self
969            .members
970            .iter()
971            .filter(|&member| {
972                let config = self.packages.get(member).workspace_config();
973                matches!(config, WorkspaceConfig::Root(_))
974            })
975            .map(|member| member.parent().unwrap().to_path_buf())
976            .collect();
977        match roots.len() {
978            1 => Ok(()),
979            0 => bail!(
980                "`package.workspace` configuration points to a crate \
981                 which is not configured with [workspace]: \n\
982                 configuration at: {}\n\
983                 points to: {}",
984                self.current_manifest.display(),
985                self.root_manifest.as_ref().unwrap().display()
986            ),
987            _ => {
988                bail!(
989                    "multiple workspace roots found in the same workspace:\n{}",
990                    roots
991                        .iter()
992                        .map(|r| format!("  {}", r.display()))
993                        .collect::<Vec<_>>()
994                        .join("\n")
995                );
996            }
997        }
998    }
999
1000    #[tracing::instrument(skip_all)]
1001    fn validate_members(&mut self) -> CargoResult<()> {
1002        for member in self.members.clone() {
1003            let root = self.find_root(&member)?;
1004            if root == self.root_manifest {
1005                continue;
1006            }
1007
1008            match root {
1009                Some(root) => {
1010                    bail!(
1011                        "package `{}` is a member of the wrong workspace\n\
1012                         expected: {}\n\
1013                         actual:   {}",
1014                        member.display(),
1015                        self.root_manifest.as_ref().unwrap().display(),
1016                        root.display()
1017                    );
1018                }
1019                None => {
1020                    bail!(
1021                        "workspace member `{}` is not hierarchically below \
1022                         the workspace root `{}`",
1023                        member.display(),
1024                        self.root_manifest.as_ref().unwrap().display()
1025                    );
1026                }
1027            }
1028        }
1029        Ok(())
1030    }
1031
1032    fn error_if_manifest_not_in_members(&mut self) -> CargoResult<()> {
1033        if self.members.contains(&self.current_manifest) {
1034            return Ok(());
1035        }
1036
1037        let root = self.root_manifest.as_ref().unwrap();
1038        let root_dir = root.parent().unwrap();
1039        let current_dir = self.current_manifest.parent().unwrap();
1040        let root_pkg = self.packages.get(root);
1041
1042        // FIXME: Make this more generic by using a relative path resolver between member and root.
1043        let members_msg = match current_dir.strip_prefix(root_dir) {
1044            Ok(rel) => format!(
1045                "this may be fixable by adding `{}` to the \
1046                     `workspace.members` array of the manifest \
1047                     located at: {}",
1048                rel.display(),
1049                root.display()
1050            ),
1051            Err(_) => format!(
1052                "this may be fixable by adding a member to \
1053                     the `workspace.members` array of the \
1054                     manifest located at: {}",
1055                root.display()
1056            ),
1057        };
1058        let extra = match *root_pkg {
1059            MaybePackage::Virtual(_) => members_msg,
1060            MaybePackage::Package(ref p) => {
1061                let has_members_list = match *p.manifest().workspace_config() {
1062                    WorkspaceConfig::Root(ref root_config) => root_config.has_members_list(),
1063                    WorkspaceConfig::Member { .. } => unreachable!(),
1064                };
1065                if !has_members_list {
1066                    format!(
1067                        "this may be fixable by ensuring that this \
1068                             crate is depended on by the workspace \
1069                             root: {}",
1070                        root.display()
1071                    )
1072                } else {
1073                    members_msg
1074                }
1075            }
1076        };
1077        bail!(
1078            "current package believes it's in a workspace when it's not:\n\
1079                 current:   {}\n\
1080                 workspace: {}\n\n{}\n\
1081                 Alternatively, to keep it out of the workspace, add the package \
1082                 to the `workspace.exclude` array, or add an empty `[workspace]` \
1083                 table to the package's manifest.",
1084            self.current_manifest.display(),
1085            root.display(),
1086            extra
1087        );
1088    }
1089
1090    fn validate_manifest(&mut self) -> CargoResult<()> {
1091        if let Some(ref root_manifest) = self.root_manifest {
1092            for pkg in self
1093                .members()
1094                .filter(|p| p.manifest_path() != root_manifest)
1095            {
1096                let manifest = pkg.manifest();
1097                let emit_warning = |what| -> CargoResult<()> {
1098                    let msg = format!(
1099                        "{} for the non root package will be ignored, \
1100                         specify {} at the workspace root:\n\
1101                         package:   {}\n\
1102                         workspace: {}",
1103                        what,
1104                        what,
1105                        pkg.manifest_path().display(),
1106                        root_manifest.display(),
1107                    );
1108                    self.gctx.shell().warn(&msg)
1109                };
1110                if manifest.normalized_toml().has_profiles() {
1111                    emit_warning("profiles")?;
1112                }
1113                if !manifest.replace().is_empty() {
1114                    emit_warning("replace")?;
1115                }
1116                if !manifest.patch().is_empty() {
1117                    emit_warning("patch")?;
1118                }
1119                if let Some(behavior) = manifest.resolve_behavior() {
1120                    if behavior != self.resolve_behavior {
1121                        // Only warn if they don't match.
1122                        emit_warning("resolver")?;
1123                    }
1124                }
1125            }
1126            if let MaybePackage::Virtual(vm) = self.root_maybe() {
1127                if vm.resolve_behavior().is_none() {
1128                    if let Some(edition) = self
1129                        .members()
1130                        .filter(|p| p.manifest_path() != root_manifest)
1131                        .map(|p| p.manifest().edition())
1132                        .filter(|&e| e >= Edition::Edition2021)
1133                        .max()
1134                    {
1135                        let resolver = edition.default_resolve_behavior().to_manifest();
1136                        self.gctx.shell().warn(format_args!(
1137                            "virtual workspace defaulting to `resolver = \"1\"` despite one or more workspace members being on edition {edition} which implies `resolver = \"{resolver}\"`"
1138                        ))?;
1139                        self.gctx.shell().note(
1140                            "to keep the current resolver, specify `workspace.resolver = \"1\"` in the workspace root's manifest",
1141                        )?;
1142                        self.gctx.shell().note(format_args!(
1143                            "to use the edition {edition} resolver, specify `workspace.resolver = \"{resolver}\"` in the workspace root's manifest"
1144                        ))?;
1145                        self.gctx.shell().note(
1146                            "for more details see https://doc.rust-lang.org/cargo/reference/resolver.html#resolver-versions",
1147                        )?;
1148                    }
1149                }
1150            }
1151        }
1152        Ok(())
1153    }
1154
1155    pub fn load(&self, manifest_path: &Path) -> CargoResult<Package> {
1156        match self.packages.maybe_get(manifest_path) {
1157            Some(MaybePackage::Package(p)) => return Ok(p.clone()),
1158            Some(&MaybePackage::Virtual(_)) => bail!("cannot load workspace root"),
1159            None => {}
1160        }
1161
1162        let mut loaded = self.loaded_packages.borrow_mut();
1163        if let Some(p) = loaded.get(manifest_path).cloned() {
1164            return Ok(p);
1165        }
1166        let source_id = SourceId::for_manifest_path(manifest_path)?;
1167        let package = ops::read_package(manifest_path, source_id, self.gctx)?;
1168        loaded.insert(manifest_path.to_path_buf(), package.clone());
1169        Ok(package)
1170    }
1171
1172    /// Preload the provided registry with already loaded packages.
1173    ///
1174    /// A workspace may load packages during construction/parsing/early phases
1175    /// for various operations, and this preload step avoids doubly-loading and
1176    /// parsing crates on the filesystem by inserting them all into the registry
1177    /// with their in-memory formats.
1178    pub fn preload(&self, registry: &mut PackageRegistry<'gctx>) {
1179        // These can get weird as this generally represents a workspace during
1180        // `cargo install`. Things like git repositories will actually have a
1181        // `PathSource` with multiple entries in it, so the logic below is
1182        // mostly just an optimization for normal `cargo build` in workspaces
1183        // during development.
1184        if self.is_ephemeral {
1185            return;
1186        }
1187
1188        for pkg in self.packages.packages.values() {
1189            let pkg = match *pkg {
1190                MaybePackage::Package(ref p) => p.clone(),
1191                MaybePackage::Virtual(_) => continue,
1192            };
1193            let src = PathSource::preload_with(pkg, self.gctx);
1194            registry.add_preloaded(Box::new(src));
1195        }
1196    }
1197
1198    pub fn emit_warnings(&self) -> CargoResult<()> {
1199        for (path, maybe_pkg) in &self.packages.packages {
1200            if let MaybePackage::Package(pkg) = maybe_pkg {
1201                if self.gctx.cli_unstable().cargo_lints {
1202                    self.emit_lints(pkg, &path)?
1203                }
1204            }
1205            let warnings = match maybe_pkg {
1206                MaybePackage::Package(pkg) => pkg.manifest().warnings().warnings(),
1207                MaybePackage::Virtual(vm) => vm.warnings().warnings(),
1208            };
1209            for warning in warnings {
1210                if warning.is_critical {
1211                    let err = anyhow::format_err!("{}", warning.message);
1212                    let cx =
1213                        anyhow::format_err!("failed to parse manifest at `{}`", path.display());
1214                    return Err(err.context(cx));
1215                } else {
1216                    let msg = if self.root_manifest.is_none() {
1217                        warning.message.to_string()
1218                    } else {
1219                        // In a workspace, it can be confusing where a warning
1220                        // originated, so include the path.
1221                        format!("{}: {}", path.display(), warning.message)
1222                    };
1223                    self.gctx.shell().warn(msg)?
1224                }
1225            }
1226        }
1227        Ok(())
1228    }
1229
1230    pub fn emit_lints(&self, pkg: &Package, path: &Path) -> CargoResult<()> {
1231        let mut error_count = 0;
1232        let toml_lints = pkg
1233            .manifest()
1234            .normalized_toml()
1235            .lints
1236            .clone()
1237            .map(|lints| lints.lints)
1238            .unwrap_or(manifest::TomlLints::default());
1239        let cargo_lints = toml_lints
1240            .get("cargo")
1241            .cloned()
1242            .unwrap_or(manifest::TomlToolLints::default());
1243
1244        let ws_contents = match self.root_maybe() {
1245            MaybePackage::Package(pkg) => pkg.manifest().contents(),
1246            MaybePackage::Virtual(v) => v.contents(),
1247        };
1248
1249        let ws_document = match self.root_maybe() {
1250            MaybePackage::Package(pkg) => pkg.manifest().document(),
1251            MaybePackage::Virtual(v) => v.document(),
1252        };
1253
1254        analyze_cargo_lints_table(
1255            pkg,
1256            &path,
1257            &cargo_lints,
1258            ws_contents,
1259            ws_document,
1260            self.root_manifest(),
1261            self.gctx,
1262        )?;
1263        check_im_a_teapot(pkg, &path, &cargo_lints, &mut error_count, self.gctx)?;
1264        if error_count > 0 {
1265            Err(crate::util::errors::AlreadyPrintedError::new(anyhow!(
1266                "encountered {error_count} errors(s) while running lints"
1267            ))
1268            .into())
1269        } else {
1270            Ok(())
1271        }
1272    }
1273
1274    pub fn set_target_dir(&mut self, target_dir: Filesystem) {
1275        self.target_dir = Some(target_dir);
1276    }
1277
1278    /// Returns a Vec of `(&Package, RequestedFeatures)` tuples that
1279    /// represent the workspace members that were requested on the command-line.
1280    ///
1281    /// `specs` may be empty, which indicates it should return all workspace
1282    /// members. In this case, `requested_features.all_features` must be
1283    /// `true`. This is used for generating `Cargo.lock`, which must include
1284    /// all members with all features enabled.
1285    pub fn members_with_features(
1286        &self,
1287        specs: &[PackageIdSpec],
1288        cli_features: &CliFeatures,
1289    ) -> CargoResult<Vec<(&Package, CliFeatures)>> {
1290        assert!(
1291            !specs.is_empty() || cli_features.all_features,
1292            "no specs requires all_features"
1293        );
1294        if specs.is_empty() {
1295            // When resolving the entire workspace, resolve each member with
1296            // all features enabled.
1297            return Ok(self
1298                .members()
1299                .map(|m| (m, CliFeatures::new_all(true)))
1300                .collect());
1301        }
1302        if self.allows_new_cli_feature_behavior() {
1303            self.members_with_features_new(specs, cli_features)
1304        } else {
1305            Ok(self.members_with_features_old(specs, cli_features))
1306        }
1307    }
1308
1309    /// Returns the requested features for the given member.
1310    /// This filters out any named features that the member does not have.
1311    fn collect_matching_features(
1312        member: &Package,
1313        cli_features: &CliFeatures,
1314        found_features: &mut BTreeSet<FeatureValue>,
1315    ) -> CliFeatures {
1316        if cli_features.features.is_empty() {
1317            return cli_features.clone();
1318        }
1319
1320        // Only include features this member defines.
1321        let summary = member.summary();
1322
1323        // Features defined in the manifest
1324        let summary_features = summary.features();
1325
1326        // Dependency name -> dependency
1327        let dependencies: BTreeMap<InternedString, &Dependency> = summary
1328            .dependencies()
1329            .iter()
1330            .map(|dep| (dep.name_in_toml(), dep))
1331            .collect();
1332
1333        // Features that enable optional dependencies
1334        let optional_dependency_names: BTreeSet<_> = dependencies
1335            .iter()
1336            .filter(|(_, dep)| dep.is_optional())
1337            .map(|(name, _)| name)
1338            .copied()
1339            .collect();
1340
1341        let mut features = BTreeSet::new();
1342
1343        // Checks if a member contains the given feature.
1344        let summary_or_opt_dependency_feature = |feature: &InternedString| -> bool {
1345            summary_features.contains_key(feature) || optional_dependency_names.contains(feature)
1346        };
1347
1348        for feature in cli_features.features.iter() {
1349            match feature {
1350                FeatureValue::Feature(f) => {
1351                    if summary_or_opt_dependency_feature(f) {
1352                        // feature exists in this member.
1353                        features.insert(feature.clone());
1354                        found_features.insert(feature.clone());
1355                    }
1356                }
1357                // This should be enforced by CliFeatures.
1358                FeatureValue::Dep { .. } => panic!("unexpected dep: syntax {}", feature),
1359                FeatureValue::DepFeature {
1360                    dep_name,
1361                    dep_feature,
1362                    weak: _,
1363                } => {
1364                    if dependencies.contains_key(dep_name) {
1365                        // pkg/feat for a dependency.
1366                        // Will rely on the dependency resolver to validate `dep_feature`.
1367                        features.insert(feature.clone());
1368                        found_features.insert(feature.clone());
1369                    } else if *dep_name == member.name()
1370                        && summary_or_opt_dependency_feature(dep_feature)
1371                    {
1372                        // member/feat where "feat" is a feature in member.
1373                        //
1374                        // `weak` can be ignored here, because the member
1375                        // either is or isn't being built.
1376                        features.insert(FeatureValue::Feature(*dep_feature));
1377                        found_features.insert(feature.clone());
1378                    }
1379                }
1380            }
1381        }
1382        CliFeatures {
1383            features: Rc::new(features),
1384            all_features: cli_features.all_features,
1385            uses_default_features: cli_features.uses_default_features,
1386        }
1387    }
1388
1389    fn missing_feature_spelling_suggestions(
1390        &self,
1391        selected_members: &[&Package],
1392        cli_features: &CliFeatures,
1393        found_features: &BTreeSet<FeatureValue>,
1394    ) -> Vec<String> {
1395        // Keeps track of which features were contained in summary of `member` to suggest similar features in errors
1396        let mut summary_features: Vec<InternedString> = Default::default();
1397
1398        // Keeps track of `member` dependencies (`dep/feature`) and their features names to suggest similar features in error
1399        let mut dependencies_features: BTreeMap<InternedString, &[InternedString]> =
1400            Default::default();
1401
1402        // Keeps track of `member` optional dependencies names (which can be enabled with feature) to suggest similar features in error
1403        let mut optional_dependency_names: Vec<InternedString> = Default::default();
1404
1405        // Keeps track of which features were contained in summary of `member` to suggest similar features in errors
1406        let mut summary_features_per_member: BTreeMap<&Package, BTreeSet<InternedString>> =
1407            Default::default();
1408
1409        // Keeps track of `member` optional dependencies (which can be enabled with feature) to suggest similar features in error
1410        let mut optional_dependency_names_per_member: BTreeMap<&Package, BTreeSet<InternedString>> =
1411            Default::default();
1412
1413        for &member in selected_members {
1414            // Only include features this member defines.
1415            let summary = member.summary();
1416
1417            // Features defined in the manifest
1418            summary_features.extend(summary.features().keys());
1419            summary_features_per_member
1420                .insert(member, summary.features().keys().copied().collect());
1421
1422            // Dependency name -> dependency
1423            let dependencies: BTreeMap<InternedString, &Dependency> = summary
1424                .dependencies()
1425                .iter()
1426                .map(|dep| (dep.name_in_toml(), dep))
1427                .collect();
1428
1429            dependencies_features.extend(
1430                dependencies
1431                    .iter()
1432                    .map(|(name, dep)| (*name, dep.features())),
1433            );
1434
1435            // Features that enable optional dependencies
1436            let optional_dependency_names_raw: BTreeSet<_> = dependencies
1437                .iter()
1438                .filter(|(_, dep)| dep.is_optional())
1439                .map(|(name, _)| name)
1440                .copied()
1441                .collect();
1442
1443            optional_dependency_names.extend(optional_dependency_names_raw.iter());
1444            optional_dependency_names_per_member.insert(member, optional_dependency_names_raw);
1445        }
1446
1447        let edit_distance_test = |a: InternedString, b: InternedString| {
1448            edit_distance(a.as_str(), b.as_str(), 3).is_some()
1449        };
1450
1451        cli_features
1452            .features
1453            .difference(found_features)
1454            .map(|feature| match feature {
1455                // Simple feature, check if any of the optional dependency features or member features are close enough
1456                FeatureValue::Feature(typo) => {
1457                    // Finds member features which are similar to the requested feature.
1458                    let summary_features = summary_features
1459                        .iter()
1460                        .filter(move |feature| edit_distance_test(**feature, *typo));
1461
1462                    // Finds optional dependencies which name is similar to the feature
1463                    let optional_dependency_features = optional_dependency_names
1464                        .iter()
1465                        .filter(move |feature| edit_distance_test(**feature, *typo));
1466
1467                    summary_features
1468                        .chain(optional_dependency_features)
1469                        .map(|s| s.to_string())
1470                        .collect::<Vec<_>>()
1471                }
1472                FeatureValue::Dep { .. } => panic!("unexpected dep: syntax {}", feature),
1473                FeatureValue::DepFeature {
1474                    dep_name,
1475                    dep_feature,
1476                    weak: _,
1477                } => {
1478                    // Finds set of `pkg/feat` that are very similar to current `pkg/feat`.
1479                    let pkg_feat_similar = dependencies_features
1480                        .iter()
1481                        .filter(|(name, _)| edit_distance_test(**name, *dep_name))
1482                        .map(|(name, features)| {
1483                            (
1484                                name,
1485                                features
1486                                    .iter()
1487                                    .filter(|feature| edit_distance_test(**feature, *dep_feature))
1488                                    .collect::<Vec<_>>(),
1489                            )
1490                        })
1491                        .map(|(name, features)| {
1492                            features
1493                                .into_iter()
1494                                .map(move |feature| format!("{}/{}", name, feature))
1495                        })
1496                        .flatten();
1497
1498                    // Finds set of `member/optional_dep` features which name is similar to current `pkg/feat`.
1499                    let optional_dependency_features = optional_dependency_names_per_member
1500                        .iter()
1501                        .filter(|(package, _)| edit_distance_test(package.name(), *dep_name))
1502                        .map(|(package, optional_dependencies)| {
1503                            optional_dependencies
1504                                .into_iter()
1505                                .filter(|optional_dependency| {
1506                                    edit_distance_test(**optional_dependency, *dep_name)
1507                                })
1508                                .map(move |optional_dependency| {
1509                                    format!("{}/{}", package.name(), optional_dependency)
1510                                })
1511                        })
1512                        .flatten();
1513
1514                    // Finds set of `member/feat` features which name is similar to current `pkg/feat`.
1515                    let summary_features = summary_features_per_member
1516                        .iter()
1517                        .filter(|(package, _)| edit_distance_test(package.name(), *dep_name))
1518                        .map(|(package, summary_features)| {
1519                            summary_features
1520                                .into_iter()
1521                                .filter(|summary_feature| {
1522                                    edit_distance_test(**summary_feature, *dep_feature)
1523                                })
1524                                .map(move |summary_feature| {
1525                                    format!("{}/{}", package.name(), summary_feature)
1526                                })
1527                        })
1528                        .flatten();
1529
1530                    pkg_feat_similar
1531                        .chain(optional_dependency_features)
1532                        .chain(summary_features)
1533                        .collect::<Vec<_>>()
1534                }
1535            })
1536            .map(|v| v.into_iter())
1537            .flatten()
1538            .unique()
1539            .filter(|element| {
1540                let feature = FeatureValue::new(InternedString::new(element));
1541                !cli_features.features.contains(&feature) && !found_features.contains(&feature)
1542            })
1543            .sorted()
1544            .take(5)
1545            .collect()
1546    }
1547
1548    fn report_unknown_features_error(
1549        &self,
1550        specs: &[PackageIdSpec],
1551        cli_features: &CliFeatures,
1552        found_features: &BTreeSet<FeatureValue>,
1553    ) -> CargoResult<()> {
1554        let unknown: Vec<_> = cli_features
1555            .features
1556            .difference(found_features)
1557            .map(|feature| feature.to_string())
1558            .sorted()
1559            .collect();
1560
1561        let (selected_members, unselected_members): (Vec<_>, Vec<_>) = self
1562            .members()
1563            .partition(|member| specs.iter().any(|spec| spec.matches(member.package_id())));
1564
1565        let missing_packages_with_the_features = unselected_members
1566            .into_iter()
1567            .filter(|member| {
1568                unknown
1569                    .iter()
1570                    .any(|feature| member.summary().features().contains_key(&**feature))
1571            })
1572            .map(|m| m.name())
1573            .collect_vec();
1574
1575        let these_features = if unknown.len() == 1 {
1576            "this feature"
1577        } else {
1578            "these features"
1579        };
1580        let mut msg = if let [singular] = &selected_members[..] {
1581            format!(
1582                "the package '{}' does not contain {these_features}: {}",
1583                singular.name(),
1584                unknown.join(", ")
1585            )
1586        } else {
1587            let names = selected_members.iter().map(|m| m.name()).join(", ");
1588            format!("none of the selected packages contains {these_features}: {}\nselected packages: {names}", unknown.join(", "))
1589        };
1590
1591        use std::fmt::Write;
1592        if !missing_packages_with_the_features.is_empty() {
1593            write!(
1594                &mut msg,
1595                "\nhelp: package{} with the missing feature{}: {}",
1596                if missing_packages_with_the_features.len() != 1 {
1597                    "s"
1598                } else {
1599                    ""
1600                },
1601                if unknown.len() != 1 { "s" } else { "" },
1602                missing_packages_with_the_features.join(", ")
1603            )?;
1604        } else {
1605            let suggestions = self.missing_feature_spelling_suggestions(
1606                &selected_members,
1607                cli_features,
1608                found_features,
1609            );
1610            if !suggestions.is_empty() {
1611                write!(
1612                    &mut msg,
1613                    "\nhelp: there {}: {}",
1614                    if suggestions.len() == 1 {
1615                        "is a similarly named feature"
1616                    } else {
1617                        "are similarly named features"
1618                    },
1619                    suggestions.join(", ")
1620                )?;
1621            }
1622        }
1623
1624        bail!("{msg}")
1625    }
1626
1627    /// New command-line feature selection behavior with resolver = "2" or the
1628    /// root of a virtual workspace. See `allows_new_cli_feature_behavior`.
1629    fn members_with_features_new(
1630        &self,
1631        specs: &[PackageIdSpec],
1632        cli_features: &CliFeatures,
1633    ) -> CargoResult<Vec<(&Package, CliFeatures)>> {
1634        // Keeps track of which features matched `member` to produce an error
1635        // if any of them did not match anywhere.
1636        let mut found_features = Default::default();
1637
1638        let members: Vec<(&Package, CliFeatures)> = self
1639            .members()
1640            .filter(|m| specs.iter().any(|spec| spec.matches(m.package_id())))
1641            .map(|m| {
1642                (
1643                    m,
1644                    Workspace::collect_matching_features(m, cli_features, &mut found_features),
1645                )
1646            })
1647            .collect();
1648
1649        if members.is_empty() {
1650            // `cargo build -p foo`, where `foo` is not a member.
1651            // Do not allow any command-line flags (defaults only).
1652            if !(cli_features.features.is_empty()
1653                && !cli_features.all_features
1654                && cli_features.uses_default_features)
1655            {
1656                bail!("cannot specify features for packages outside of workspace");
1657            }
1658            // Add all members from the workspace so we can ensure `-p nonmember`
1659            // is in the resolve graph.
1660            return Ok(self
1661                .members()
1662                .map(|m| (m, CliFeatures::new_all(false)))
1663                .collect());
1664        }
1665        if *cli_features.features != found_features {
1666            self.report_unknown_features_error(specs, cli_features, &found_features)?;
1667        }
1668        Ok(members)
1669    }
1670
1671    /// This is the "old" behavior for command-line feature selection.
1672    /// See `allows_new_cli_feature_behavior`.
1673    fn members_with_features_old(
1674        &self,
1675        specs: &[PackageIdSpec],
1676        cli_features: &CliFeatures,
1677    ) -> Vec<(&Package, CliFeatures)> {
1678        // Split off any features with the syntax `member-name/feature-name` into a map
1679        // so that those features can be applied directly to those workspace-members.
1680        let mut member_specific_features: HashMap<InternedString, BTreeSet<FeatureValue>> =
1681            HashMap::new();
1682        // Features for the member in the current directory.
1683        let mut cwd_features = BTreeSet::new();
1684        for feature in cli_features.features.iter() {
1685            match feature {
1686                FeatureValue::Feature(_) => {
1687                    cwd_features.insert(feature.clone());
1688                }
1689                // This should be enforced by CliFeatures.
1690                FeatureValue::Dep { .. } => panic!("unexpected dep: syntax {}", feature),
1691                FeatureValue::DepFeature {
1692                    dep_name,
1693                    dep_feature,
1694                    weak: _,
1695                } => {
1696                    // I think weak can be ignored here.
1697                    // * With `--features member?/feat -p member`, the ? doesn't
1698                    //   really mean anything (either the member is built or it isn't).
1699                    // * With `--features nonmember?/feat`, cwd_features will
1700                    //   handle processing it correctly.
1701                    let is_member = self.members().any(|member| {
1702                        // Check if `dep_name` is member of the workspace, but isn't associated with current package.
1703                        self.current_opt() != Some(member) && member.name() == *dep_name
1704                    });
1705                    if is_member && specs.iter().any(|spec| spec.name() == dep_name.as_str()) {
1706                        member_specific_features
1707                            .entry(*dep_name)
1708                            .or_default()
1709                            .insert(FeatureValue::Feature(*dep_feature));
1710                    } else {
1711                        cwd_features.insert(feature.clone());
1712                    }
1713                }
1714            }
1715        }
1716
1717        let ms: Vec<_> = self
1718            .members()
1719            .filter_map(|member| {
1720                let member_id = member.package_id();
1721                match self.current_opt() {
1722                    // The features passed on the command-line only apply to
1723                    // the "current" package (determined by the cwd).
1724                    Some(current) if member_id == current.package_id() => {
1725                        let feats = CliFeatures {
1726                            features: Rc::new(cwd_features.clone()),
1727                            all_features: cli_features.all_features,
1728                            uses_default_features: cli_features.uses_default_features,
1729                        };
1730                        Some((member, feats))
1731                    }
1732                    _ => {
1733                        // Ignore members that are not enabled on the command-line.
1734                        if specs.iter().any(|spec| spec.matches(member_id)) {
1735                            // -p for a workspace member that is not the "current"
1736                            // one.
1737                            //
1738                            // The odd behavior here is due to backwards
1739                            // compatibility. `--features` and
1740                            // `--no-default-features` used to only apply to the
1741                            // "current" package. As an extension, this allows
1742                            // member-name/feature-name to set member-specific
1743                            // features, which should be backwards-compatible.
1744                            let feats = CliFeatures {
1745                                features: Rc::new(
1746                                    member_specific_features
1747                                        .remove(member.name().as_str())
1748                                        .unwrap_or_default(),
1749                                ),
1750                                uses_default_features: true,
1751                                all_features: cli_features.all_features,
1752                            };
1753                            Some((member, feats))
1754                        } else {
1755                            // This member was not requested on the command-line, skip.
1756                            None
1757                        }
1758                    }
1759                }
1760            })
1761            .collect();
1762
1763        // If any member specific features were not removed while iterating over members
1764        // some features will be ignored.
1765        assert!(member_specific_features.is_empty());
1766
1767        ms
1768    }
1769
1770    /// Returns true if `unit` should depend on the output of Docscrape units.
1771    pub fn unit_needs_doc_scrape(&self, unit: &Unit) -> bool {
1772        // We do not add scraped units for Host units, as they're either build scripts
1773        // (not documented) or proc macros (have no scrape-able exports). Additionally,
1774        // naively passing a proc macro's unit_for to new_unit_dep will currently cause
1775        // Cargo to panic, see issue #10545.
1776        self.is_member(&unit.pkg) && !(unit.target.for_host() || unit.pkg.proc_macro())
1777    }
1778
1779    /// Adds a local package registry overlaying a `SourceId`.
1780    ///
1781    /// See [`crate::sources::overlay::DependencyConfusionThreatOverlaySource`] for why you shouldn't use this.
1782    pub fn add_local_overlay(&mut self, id: SourceId, registry_path: PathBuf) {
1783        self.local_overlays.insert(id, registry_path);
1784    }
1785
1786    /// Builds a package registry that reflects this workspace configuration.
1787    pub fn package_registry(&self) -> CargoResult<PackageRegistry<'gctx>> {
1788        let source_config =
1789            SourceConfigMap::new_with_overlays(self.gctx(), self.local_overlays()?)?;
1790        PackageRegistry::new_with_source_config(self.gctx(), source_config)
1791    }
1792
1793    /// Returns all the configured local overlays, including the ones from our secret environment variable.
1794    fn local_overlays(&self) -> CargoResult<impl Iterator<Item = (SourceId, SourceId)>> {
1795        let mut ret = self
1796            .local_overlays
1797            .iter()
1798            .map(|(id, path)| Ok((*id, SourceId::for_local_registry(path)?)))
1799            .collect::<CargoResult<Vec<_>>>()?;
1800
1801        if let Ok(overlay) = self
1802            .gctx
1803            .get_env("__CARGO_TEST_DEPENDENCY_CONFUSION_VULNERABILITY_DO_NOT_USE_THIS")
1804        {
1805            let (url, path) = overlay.split_once('=').ok_or(anyhow::anyhow!(
1806                "invalid overlay format. I won't tell you why; you shouldn't be using it anyway"
1807            ))?;
1808            ret.push((
1809                SourceId::from_url(url)?,
1810                SourceId::for_local_registry(path.as_ref())?,
1811            ));
1812        }
1813
1814        Ok(ret.into_iter())
1815    }
1816}
1817
1818impl<'gctx> Packages<'gctx> {
1819    fn get(&self, manifest_path: &Path) -> &MaybePackage {
1820        self.maybe_get(manifest_path).unwrap()
1821    }
1822
1823    fn get_mut(&mut self, manifest_path: &Path) -> &mut MaybePackage {
1824        self.maybe_get_mut(manifest_path).unwrap()
1825    }
1826
1827    fn maybe_get(&self, manifest_path: &Path) -> Option<&MaybePackage> {
1828        self.packages.get(manifest_path)
1829    }
1830
1831    fn maybe_get_mut(&mut self, manifest_path: &Path) -> Option<&mut MaybePackage> {
1832        self.packages.get_mut(manifest_path)
1833    }
1834
1835    fn load(&mut self, manifest_path: &Path) -> CargoResult<&MaybePackage> {
1836        match self.packages.entry(manifest_path.to_path_buf()) {
1837            Entry::Occupied(e) => Ok(e.into_mut()),
1838            Entry::Vacant(v) => {
1839                let source_id = SourceId::for_manifest_path(manifest_path)?;
1840                let manifest = read_manifest(manifest_path, source_id, self.gctx)?;
1841                Ok(v.insert(match manifest {
1842                    EitherManifest::Real(manifest) => {
1843                        MaybePackage::Package(Package::new(manifest, manifest_path))
1844                    }
1845                    EitherManifest::Virtual(vm) => MaybePackage::Virtual(vm),
1846                }))
1847            }
1848        }
1849    }
1850}
1851
1852impl MaybePackage {
1853    fn workspace_config(&self) -> &WorkspaceConfig {
1854        match *self {
1855            MaybePackage::Package(ref p) => p.manifest().workspace_config(),
1856            MaybePackage::Virtual(ref vm) => vm.workspace_config(),
1857        }
1858    }
1859
1860    /// Has an embedded manifest (single-file package)
1861    pub fn is_embedded(&self) -> bool {
1862        match self {
1863            MaybePackage::Package(p) => p.manifest().is_embedded(),
1864            MaybePackage::Virtual(_) => false,
1865        }
1866    }
1867}
1868
1869impl WorkspaceRootConfig {
1870    /// Creates a new Intermediate Workspace Root configuration.
1871    pub fn new(
1872        root_dir: &Path,
1873        members: &Option<Vec<String>>,
1874        default_members: &Option<Vec<String>>,
1875        exclude: &Option<Vec<String>>,
1876        inheritable: &Option<InheritableFields>,
1877        custom_metadata: &Option<toml::Value>,
1878    ) -> WorkspaceRootConfig {
1879        WorkspaceRootConfig {
1880            root_dir: root_dir.to_path_buf(),
1881            members: members.clone(),
1882            default_members: default_members.clone(),
1883            exclude: exclude.clone().unwrap_or_default(),
1884            inheritable_fields: inheritable.clone().unwrap_or_default(),
1885            custom_metadata: custom_metadata.clone(),
1886        }
1887    }
1888    /// Checks the path against the `excluded` list.
1889    ///
1890    /// This method does **not** consider the `members` list.
1891    fn is_excluded(&self, manifest_path: &Path) -> bool {
1892        let excluded = self
1893            .exclude
1894            .iter()
1895            .any(|ex| manifest_path.starts_with(self.root_dir.join(ex)));
1896
1897        let explicit_member = match self.members {
1898            Some(ref members) => members
1899                .iter()
1900                .any(|mem| manifest_path.starts_with(self.root_dir.join(mem))),
1901            None => false,
1902        };
1903
1904        !explicit_member && excluded
1905    }
1906
1907    fn has_members_list(&self) -> bool {
1908        self.members.is_some()
1909    }
1910
1911    /// Returns expanded paths along with the glob that they were expanded from.
1912    /// The glob is `None` if the path matched exactly.
1913    #[tracing::instrument(skip_all)]
1914    fn members_paths<'g>(
1915        &self,
1916        globs: &'g [String],
1917    ) -> CargoResult<Vec<(PathBuf, Option<&'g str>)>> {
1918        let mut expanded_list = Vec::new();
1919
1920        for glob in globs {
1921            let pathbuf = self.root_dir.join(glob);
1922            let expanded_paths = Self::expand_member_path(&pathbuf)?;
1923
1924            // If glob does not find any valid paths, then put the original
1925            // path in the expanded list to maintain backwards compatibility.
1926            if expanded_paths.is_empty() {
1927                expanded_list.push((pathbuf, None));
1928            } else {
1929                let used_glob_pattern = expanded_paths.len() > 1 || expanded_paths[0] != pathbuf;
1930                let glob = used_glob_pattern.then_some(glob.as_str());
1931
1932                // Some OS can create system support files anywhere.
1933                // (e.g. macOS creates `.DS_Store` file if you visit a directory using Finder.)
1934                // Such files can be reported as a member path unexpectedly.
1935                // Check and filter out non-directory paths to prevent pushing such accidental unwanted path
1936                // as a member.
1937                for expanded_path in expanded_paths {
1938                    if expanded_path.is_dir() {
1939                        expanded_list.push((expanded_path, glob));
1940                    }
1941                }
1942            }
1943        }
1944
1945        Ok(expanded_list)
1946    }
1947
1948    fn expand_member_path(path: &Path) -> CargoResult<Vec<PathBuf>> {
1949        let Some(path) = path.to_str() else {
1950            return Ok(Vec::new());
1951        };
1952        let res = glob(path).with_context(|| format!("could not parse pattern `{}`", &path))?;
1953        let res = res
1954            .map(|p| p.with_context(|| format!("unable to match path to pattern `{}`", &path)))
1955            .collect::<Result<Vec<_>, _>>()?;
1956        Ok(res)
1957    }
1958
1959    pub fn inheritable(&self) -> &InheritableFields {
1960        &self.inheritable_fields
1961    }
1962}
1963
1964pub fn resolve_relative_path(
1965    label: &str,
1966    old_root: &Path,
1967    new_root: &Path,
1968    rel_path: &str,
1969) -> CargoResult<String> {
1970    let joined_path = normalize_path(&old_root.join(rel_path));
1971    match diff_paths(joined_path, new_root) {
1972        None => Err(anyhow!(
1973            "`{}` was defined in {} but could not be resolved with {}",
1974            label,
1975            old_root.display(),
1976            new_root.display()
1977        )),
1978        Some(path) => Ok(path
1979            .to_str()
1980            .ok_or_else(|| {
1981                anyhow!(
1982                    "`{}` resolved to non-UTF value (`{}`)",
1983                    label,
1984                    path.display()
1985                )
1986            })?
1987            .to_owned()),
1988    }
1989}
1990
1991/// Finds the path of the root of the workspace.
1992pub fn find_workspace_root(
1993    manifest_path: &Path,
1994    gctx: &GlobalContext,
1995) -> CargoResult<Option<PathBuf>> {
1996    find_workspace_root_with_loader(manifest_path, gctx, |self_path| {
1997        let source_id = SourceId::for_manifest_path(self_path)?;
1998        let manifest = read_manifest(self_path, source_id, gctx)?;
1999        Ok(manifest
2000            .workspace_config()
2001            .get_ws_root(self_path, manifest_path))
2002    })
2003}
2004
2005/// Finds the path of the root of the workspace.
2006///
2007/// This uses a callback to determine if the given path tells us what the
2008/// workspace root is.
2009fn find_workspace_root_with_loader(
2010    manifest_path: &Path,
2011    gctx: &GlobalContext,
2012    mut loader: impl FnMut(&Path) -> CargoResult<Option<PathBuf>>,
2013) -> CargoResult<Option<PathBuf>> {
2014    // Check if there are any workspace roots that have already been found that would work
2015    {
2016        let roots = gctx.ws_roots.borrow();
2017        // Iterate through the manifests parent directories until we find a workspace
2018        // root. Note we skip the first item since that is just the path itself
2019        for current in manifest_path.ancestors().skip(1) {
2020            if let Some(ws_config) = roots.get(current) {
2021                if !ws_config.is_excluded(manifest_path) {
2022                    // Add `Cargo.toml` since ws_root is the root and not the file
2023                    return Ok(Some(current.join("Cargo.toml")));
2024                }
2025            }
2026        }
2027    }
2028
2029    for ances_manifest_path in find_root_iter(manifest_path, gctx) {
2030        debug!("find_root - trying {}", ances_manifest_path.display());
2031        if let Some(ws_root_path) = loader(&ances_manifest_path)? {
2032            return Ok(Some(ws_root_path));
2033        }
2034    }
2035    Ok(None)
2036}
2037
2038fn read_root_pointer(member_manifest: &Path, root_link: &str) -> PathBuf {
2039    let path = member_manifest
2040        .parent()
2041        .unwrap()
2042        .join(root_link)
2043        .join("Cargo.toml");
2044    debug!("find_root - pointer {}", path.display());
2045    paths::normalize_path(&path)
2046}
2047
2048fn find_root_iter<'a>(
2049    manifest_path: &'a Path,
2050    gctx: &'a GlobalContext,
2051) -> impl Iterator<Item = PathBuf> + 'a {
2052    LookBehind::new(paths::ancestors(manifest_path, None).skip(2))
2053        .take_while(|path| !path.curr.ends_with("target/package"))
2054        // Don't walk across `CARGO_HOME` when we're looking for the
2055        // workspace root. Sometimes a package will be organized with
2056        // `CARGO_HOME` pointing inside of the workspace root or in the
2057        // current package, but we don't want to mistakenly try to put
2058        // crates.io crates into the workspace by accident.
2059        .take_while(|path| {
2060            if let Some(last) = path.last {
2061                gctx.home() != last
2062            } else {
2063                true
2064            }
2065        })
2066        .map(|path| path.curr.join("Cargo.toml"))
2067        .filter(|ances_manifest_path| ances_manifest_path.exists())
2068}
2069
2070struct LookBehindWindow<'a, T: ?Sized> {
2071    curr: &'a T,
2072    last: Option<&'a T>,
2073}
2074
2075struct LookBehind<'a, T: ?Sized, K: Iterator<Item = &'a T>> {
2076    iter: K,
2077    last: Option<&'a T>,
2078}
2079
2080impl<'a, T: ?Sized, K: Iterator<Item = &'a T>> LookBehind<'a, T, K> {
2081    fn new(items: K) -> Self {
2082        Self {
2083            iter: items,
2084            last: None,
2085        }
2086    }
2087}
2088
2089impl<'a, T: ?Sized, K: Iterator<Item = &'a T>> Iterator for LookBehind<'a, T, K> {
2090    type Item = LookBehindWindow<'a, T>;
2091
2092    fn next(&mut self) -> Option<Self::Item> {
2093        match self.iter.next() {
2094            None => None,
2095            Some(next) => {
2096                let last = self.last;
2097                self.last = Some(next);
2098                Some(LookBehindWindow { curr: next, last })
2099            }
2100        }
2101    }
2102}