Skip to main content

cargo/core/
workspace.rs

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