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        for (path, maybe_pkg) in &self.packages.packages {
1209            if let MaybePackage::Package(pkg) = maybe_pkg {
1210                if self.gctx.cli_unstable().cargo_lints {
1211                    self.emit_lints(pkg, &path)?
1212                }
1213            }
1214            let warnings = match maybe_pkg {
1215                MaybePackage::Package(pkg) => pkg.manifest().warnings().warnings(),
1216                MaybePackage::Virtual(vm) => vm.warnings().warnings(),
1217            };
1218            for warning in warnings {
1219                if warning.is_critical {
1220                    let err = anyhow::format_err!("{}", warning.message);
1221                    let cx =
1222                        anyhow::format_err!("failed to parse manifest at `{}`", path.display());
1223                    return Err(err.context(cx));
1224                } else {
1225                    let msg = if self.root_manifest.is_none() {
1226                        warning.message.to_string()
1227                    } else {
1228                        // In a workspace, it can be confusing where a warning
1229                        // originated, so include the path.
1230                        format!("{}: {}", path.display(), warning.message)
1231                    };
1232                    self.gctx.shell().warn(msg)?
1233                }
1234            }
1235        }
1236        Ok(())
1237    }
1238
1239    pub fn emit_lints(&self, pkg: &Package, path: &Path) -> CargoResult<()> {
1240        let mut error_count = 0;
1241        let toml_lints = pkg
1242            .manifest()
1243            .normalized_toml()
1244            .lints
1245            .clone()
1246            .map(|lints| lints.lints)
1247            .unwrap_or(manifest::TomlLints::default());
1248        let cargo_lints = toml_lints
1249            .get("cargo")
1250            .cloned()
1251            .unwrap_or(manifest::TomlToolLints::default());
1252
1253        let ws_contents = match self.root_maybe() {
1254            MaybePackage::Package(pkg) => pkg.manifest().contents(),
1255            MaybePackage::Virtual(v) => v.contents(),
1256        };
1257
1258        let ws_document = match self.root_maybe() {
1259            MaybePackage::Package(pkg) => pkg.manifest().document(),
1260            MaybePackage::Virtual(v) => v.document(),
1261        };
1262
1263        analyze_cargo_lints_table(
1264            pkg,
1265            &path,
1266            &cargo_lints,
1267            ws_contents,
1268            ws_document,
1269            self.root_manifest(),
1270            self.gctx,
1271        )?;
1272        check_im_a_teapot(pkg, &path, &cargo_lints, &mut error_count, self.gctx)?;
1273        if error_count > 0 {
1274            Err(crate::util::errors::AlreadyPrintedError::new(anyhow!(
1275                "encountered {error_count} errors(s) while running lints"
1276            ))
1277            .into())
1278        } else {
1279            Ok(())
1280        }
1281    }
1282
1283    pub fn set_target_dir(&mut self, target_dir: Filesystem) {
1284        self.target_dir = Some(target_dir);
1285    }
1286
1287    /// Returns a Vec of `(&Package, CliFeatures)` tuples that
1288    /// represent the workspace members that were requested on the command-line.
1289    ///
1290    /// `specs` may be empty, which indicates it should return all workspace
1291    /// members. In this case, `requested_features.all_features` must be
1292    /// `true`. This is used for generating `Cargo.lock`, which must include
1293    /// all members with all features enabled.
1294    pub fn members_with_features(
1295        &self,
1296        specs: &[PackageIdSpec],
1297        cli_features: &CliFeatures,
1298    ) -> CargoResult<Vec<(&Package, CliFeatures)>> {
1299        assert!(
1300            !specs.is_empty() || cli_features.all_features,
1301            "no specs requires all_features"
1302        );
1303        if specs.is_empty() {
1304            // When resolving the entire workspace, resolve each member with
1305            // all features enabled.
1306            return Ok(self
1307                .members()
1308                .map(|m| (m, CliFeatures::new_all(true)))
1309                .collect());
1310        }
1311        if self.allows_new_cli_feature_behavior() {
1312            self.members_with_features_new(specs, cli_features)
1313        } else {
1314            Ok(self.members_with_features_old(specs, cli_features))
1315        }
1316    }
1317
1318    /// Returns the requested features for the given member.
1319    /// This filters out any named features that the member does not have.
1320    fn collect_matching_features(
1321        member: &Package,
1322        cli_features: &CliFeatures,
1323        found_features: &mut BTreeSet<FeatureValue>,
1324    ) -> CliFeatures {
1325        if cli_features.features.is_empty() {
1326            return cli_features.clone();
1327        }
1328
1329        // Only include features this member defines.
1330        let summary = member.summary();
1331
1332        // Features defined in the manifest
1333        let summary_features = summary.features();
1334
1335        // Dependency name -> dependency
1336        let dependencies: BTreeMap<InternedString, &Dependency> = summary
1337            .dependencies()
1338            .iter()
1339            .map(|dep| (dep.name_in_toml(), dep))
1340            .collect();
1341
1342        // Features that enable optional dependencies
1343        let optional_dependency_names: BTreeSet<_> = dependencies
1344            .iter()
1345            .filter(|(_, dep)| dep.is_optional())
1346            .map(|(name, _)| name)
1347            .copied()
1348            .collect();
1349
1350        let mut features = BTreeSet::new();
1351
1352        // Checks if a member contains the given feature.
1353        let summary_or_opt_dependency_feature = |feature: &InternedString| -> bool {
1354            summary_features.contains_key(feature) || optional_dependency_names.contains(feature)
1355        };
1356
1357        for feature in cli_features.features.iter() {
1358            match feature {
1359                FeatureValue::Feature(f) => {
1360                    if summary_or_opt_dependency_feature(f) {
1361                        // feature exists in this member.
1362                        features.insert(feature.clone());
1363                        found_features.insert(feature.clone());
1364                    }
1365                }
1366                // This should be enforced by CliFeatures.
1367                FeatureValue::Dep { .. } => panic!("unexpected dep: syntax {}", feature),
1368                FeatureValue::DepFeature {
1369                    dep_name,
1370                    dep_feature,
1371                    weak: _,
1372                } => {
1373                    if dependencies.contains_key(dep_name) {
1374                        // pkg/feat for a dependency.
1375                        // Will rely on the dependency resolver to validate `dep_feature`.
1376                        features.insert(feature.clone());
1377                        found_features.insert(feature.clone());
1378                    } else if *dep_name == member.name()
1379                        && summary_or_opt_dependency_feature(dep_feature)
1380                    {
1381                        // member/feat where "feat" is a feature in member.
1382                        //
1383                        // `weak` can be ignored here, because the member
1384                        // either is or isn't being built.
1385                        features.insert(FeatureValue::Feature(*dep_feature));
1386                        found_features.insert(feature.clone());
1387                    }
1388                }
1389            }
1390        }
1391        CliFeatures {
1392            features: Rc::new(features),
1393            all_features: cli_features.all_features,
1394            uses_default_features: cli_features.uses_default_features,
1395        }
1396    }
1397
1398    fn missing_feature_spelling_suggestions(
1399        &self,
1400        selected_members: &[&Package],
1401        cli_features: &CliFeatures,
1402        found_features: &BTreeSet<FeatureValue>,
1403    ) -> Vec<String> {
1404        // Keeps track of which features were contained in summary of `member` to suggest similar features in errors
1405        let mut summary_features: Vec<InternedString> = Default::default();
1406
1407        // Keeps track of `member` dependencies (`dep/feature`) and their features names to suggest similar features in error
1408        let mut dependencies_features: BTreeMap<InternedString, &[InternedString]> =
1409            Default::default();
1410
1411        // Keeps track of `member` optional dependencies names (which can be enabled with feature) to suggest similar features in error
1412        let mut optional_dependency_names: Vec<InternedString> = Default::default();
1413
1414        // Keeps track of which features were contained in summary of `member` to suggest similar features in errors
1415        let mut summary_features_per_member: BTreeMap<&Package, BTreeSet<InternedString>> =
1416            Default::default();
1417
1418        // Keeps track of `member` optional dependencies (which can be enabled with feature) to suggest similar features in error
1419        let mut optional_dependency_names_per_member: BTreeMap<&Package, BTreeSet<InternedString>> =
1420            Default::default();
1421
1422        for &member in selected_members {
1423            // Only include features this member defines.
1424            let summary = member.summary();
1425
1426            // Features defined in the manifest
1427            summary_features.extend(summary.features().keys());
1428            summary_features_per_member
1429                .insert(member, summary.features().keys().copied().collect());
1430
1431            // Dependency name -> dependency
1432            let dependencies: BTreeMap<InternedString, &Dependency> = summary
1433                .dependencies()
1434                .iter()
1435                .map(|dep| (dep.name_in_toml(), dep))
1436                .collect();
1437
1438            dependencies_features.extend(
1439                dependencies
1440                    .iter()
1441                    .map(|(name, dep)| (*name, dep.features())),
1442            );
1443
1444            // Features that enable optional dependencies
1445            let optional_dependency_names_raw: BTreeSet<_> = dependencies
1446                .iter()
1447                .filter(|(_, dep)| dep.is_optional())
1448                .map(|(name, _)| name)
1449                .copied()
1450                .collect();
1451
1452            optional_dependency_names.extend(optional_dependency_names_raw.iter());
1453            optional_dependency_names_per_member.insert(member, optional_dependency_names_raw);
1454        }
1455
1456        let edit_distance_test = |a: InternedString, b: InternedString| {
1457            edit_distance(a.as_str(), b.as_str(), 3).is_some()
1458        };
1459
1460        cli_features
1461            .features
1462            .difference(found_features)
1463            .map(|feature| match feature {
1464                // Simple feature, check if any of the optional dependency features or member features are close enough
1465                FeatureValue::Feature(typo) => {
1466                    // Finds member features which are similar to the requested feature.
1467                    let summary_features = summary_features
1468                        .iter()
1469                        .filter(move |feature| edit_distance_test(**feature, *typo));
1470
1471                    // Finds optional dependencies which name is similar to the feature
1472                    let optional_dependency_features = optional_dependency_names
1473                        .iter()
1474                        .filter(move |feature| edit_distance_test(**feature, *typo));
1475
1476                    summary_features
1477                        .chain(optional_dependency_features)
1478                        .map(|s| s.to_string())
1479                        .collect::<Vec<_>>()
1480                }
1481                FeatureValue::Dep { .. } => panic!("unexpected dep: syntax {}", feature),
1482                FeatureValue::DepFeature {
1483                    dep_name,
1484                    dep_feature,
1485                    weak: _,
1486                } => {
1487                    // Finds set of `pkg/feat` that are very similar to current `pkg/feat`.
1488                    let pkg_feat_similar = dependencies_features
1489                        .iter()
1490                        .filter(|(name, _)| edit_distance_test(**name, *dep_name))
1491                        .map(|(name, features)| {
1492                            (
1493                                name,
1494                                features
1495                                    .iter()
1496                                    .filter(|feature| edit_distance_test(**feature, *dep_feature))
1497                                    .collect::<Vec<_>>(),
1498                            )
1499                        })
1500                        .map(|(name, features)| {
1501                            features
1502                                .into_iter()
1503                                .map(move |feature| format!("{}/{}", name, feature))
1504                        })
1505                        .flatten();
1506
1507                    // Finds set of `member/optional_dep` features which name is similar to current `pkg/feat`.
1508                    let optional_dependency_features = optional_dependency_names_per_member
1509                        .iter()
1510                        .filter(|(package, _)| edit_distance_test(package.name(), *dep_name))
1511                        .map(|(package, optional_dependencies)| {
1512                            optional_dependencies
1513                                .into_iter()
1514                                .filter(|optional_dependency| {
1515                                    edit_distance_test(**optional_dependency, *dep_name)
1516                                })
1517                                .map(move |optional_dependency| {
1518                                    format!("{}/{}", package.name(), optional_dependency)
1519                                })
1520                        })
1521                        .flatten();
1522
1523                    // Finds set of `member/feat` features which name is similar to current `pkg/feat`.
1524                    let summary_features = summary_features_per_member
1525                        .iter()
1526                        .filter(|(package, _)| edit_distance_test(package.name(), *dep_name))
1527                        .map(|(package, summary_features)| {
1528                            summary_features
1529                                .into_iter()
1530                                .filter(|summary_feature| {
1531                                    edit_distance_test(**summary_feature, *dep_feature)
1532                                })
1533                                .map(move |summary_feature| {
1534                                    format!("{}/{}", package.name(), summary_feature)
1535                                })
1536                        })
1537                        .flatten();
1538
1539                    pkg_feat_similar
1540                        .chain(optional_dependency_features)
1541                        .chain(summary_features)
1542                        .collect::<Vec<_>>()
1543                }
1544            })
1545            .map(|v| v.into_iter())
1546            .flatten()
1547            .unique()
1548            .filter(|element| {
1549                let feature = FeatureValue::new(element.into());
1550                !cli_features.features.contains(&feature) && !found_features.contains(&feature)
1551            })
1552            .sorted()
1553            .take(5)
1554            .collect()
1555    }
1556
1557    fn report_unknown_features_error(
1558        &self,
1559        specs: &[PackageIdSpec],
1560        cli_features: &CliFeatures,
1561        found_features: &BTreeSet<FeatureValue>,
1562    ) -> CargoResult<()> {
1563        let unknown: Vec<_> = cli_features
1564            .features
1565            .difference(found_features)
1566            .map(|feature| feature.to_string())
1567            .sorted()
1568            .collect();
1569
1570        let (selected_members, unselected_members): (Vec<_>, Vec<_>) = self
1571            .members()
1572            .partition(|member| specs.iter().any(|spec| spec.matches(member.package_id())));
1573
1574        let missing_packages_with_the_features = unselected_members
1575            .into_iter()
1576            .filter(|member| {
1577                unknown
1578                    .iter()
1579                    .any(|feature| member.summary().features().contains_key(&**feature))
1580            })
1581            .map(|m| m.name())
1582            .collect_vec();
1583
1584        let these_features = if unknown.len() == 1 {
1585            "this feature"
1586        } else {
1587            "these features"
1588        };
1589        let mut msg = if let [singular] = &selected_members[..] {
1590            format!(
1591                "the package '{}' does not contain {these_features}: {}",
1592                singular.name(),
1593                unknown.join(", ")
1594            )
1595        } else {
1596            let names = selected_members.iter().map(|m| m.name()).join(", ");
1597            format!(
1598                "none of the selected packages contains {these_features}: {}\nselected packages: {names}",
1599                unknown.join(", ")
1600            )
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}