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