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