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