cargo/core/
workspace.rs

1use std::cell::RefCell;
2use std::collections::hash_map::{Entry, HashMap};
3use std::collections::{BTreeMap, BTreeSet, HashSet};
4use std::path::{Path, PathBuf};
5use std::rc::Rc;
6
7use annotate_snippets::Level;
8use anyhow::{Context as _, anyhow, bail};
9use glob::glob;
10use itertools::Itertools;
11use tracing::debug;
12use url::Url;
13
14use crate::core::compiler::Unit;
15use crate::core::features::Features;
16use crate::core::registry::PackageRegistry;
17use crate::core::resolver::ResolveBehavior;
18use crate::core::resolver::features::CliFeatures;
19use crate::core::{
20    Dependency, Edition, FeatureValue, PackageId, PackageIdSpec, PackageIdSpecQuery, Patch,
21    PatchLocation,
22};
23use crate::core::{EitherManifest, Package, SourceId, VirtualManifest};
24use crate::lints::analyze_cargo_lints_table;
25use crate::lints::rules::blanket_hint_mostly_unused;
26use crate::lints::rules::check_im_a_teapot;
27use crate::lints::rules::implicit_minimum_version_req;
28use crate::ops;
29use crate::ops::lockfile::LOCKFILE_NAME;
30use crate::sources::{CRATES_IO_INDEX, CRATES_IO_REGISTRY, PathSource, SourceConfigMap};
31use crate::util::context;
32use crate::util::context::{FeatureUnification, Value};
33use crate::util::edit_distance;
34use crate::util::errors::{CargoResult, ManifestError};
35use crate::util::interning::InternedString;
36use crate::util::toml::{InheritableFields, read_manifest};
37use crate::util::{
38    Filesystem, GlobalContext, IntoUrl, context::CargoResolverConfig, context::ConfigRelativePath,
39    context::IncompatibleRustVersions,
40};
41
42use cargo_util::paths;
43use cargo_util::paths::normalize_path;
44use cargo_util_schemas::manifest;
45use cargo_util_schemas::manifest::RustVersion;
46use cargo_util_schemas::manifest::{TomlDependency, TomlProfiles};
47use pathdiff::diff_paths;
48
49/// The core abstraction in Cargo for working with a workspace of crates.
50///
51/// A workspace is often created very early on and then threaded through all
52/// other functions. It's typically through this object that the current
53/// package is loaded and/or learned about.
54#[derive(Debug)]
55pub struct Workspace<'gctx> {
56    /// Cargo configuration information. See [`GlobalContext`].
57    gctx: &'gctx GlobalContext,
58
59    /// This path is a path to where the current cargo subcommand was invoked
60    /// from. That is the `--manifest-path` argument to Cargo, and
61    /// points to the "main crate" that we're going to worry about.
62    current_manifest: PathBuf,
63
64    /// A list of packages found in this workspace. Always includes at least the
65    /// package mentioned by `current_manifest`.
66    packages: Packages<'gctx>,
67
68    /// If this workspace includes more than one crate, this points to the root
69    /// of the workspace. This is `None` in the case that `[workspace]` is
70    /// missing, `package.workspace` is missing, and no `Cargo.toml` above
71    /// `current_manifest` was found on the filesystem with `[workspace]`.
72    root_manifest: Option<PathBuf>,
73
74    /// Shared target directory for all the packages of this workspace.
75    /// `None` if the default path of `root/target` should be used.
76    target_dir: Option<Filesystem>,
77
78    /// Shared build directory for intermediate build artifacts.
79    /// This directory may be shared between multiple workspaces.
80    build_dir: Option<Filesystem>,
81
82    /// List of members in this workspace with a listing of all their manifest
83    /// paths. The packages themselves can be looked up through the `packages`
84    /// set above.
85    members: Vec<PathBuf>,
86    /// Set of ids of workspace members
87    member_ids: HashSet<PackageId>,
88
89    /// The subset of `members` that are used by the
90    /// `build`, `check`, `test`, and `bench` subcommands
91    /// when no package is selected with `--package` / `-p` and `--workspace`
92    /// is not used.
93    ///
94    /// This is set by the `default-members` config
95    /// in the `[workspace]` section.
96    /// When unset, this is the same as `members` for virtual workspaces
97    /// (`--workspace` is implied)
98    /// or only the root package for non-virtual workspaces.
99    default_members: Vec<PathBuf>,
100
101    /// `true` if this is a temporary workspace created for the purposes of the
102    /// `cargo install` or `cargo package` commands.
103    is_ephemeral: bool,
104
105    /// `true` if this workspace should enforce optional dependencies even when
106    /// not needed; false if this workspace should only enforce dependencies
107    /// needed by the current configuration (such as in cargo install). In some
108    /// cases `false` also results in the non-enforcement of dev-dependencies.
109    require_optional_deps: bool,
110
111    /// A cache of loaded packages for particular paths which is disjoint from
112    /// `packages` up above, used in the `load` method down below.
113    loaded_packages: RefCell<HashMap<PathBuf, Package>>,
114
115    /// If `true`, then the resolver will ignore any existing `Cargo.lock`
116    /// file. This is set for `cargo install` without `--locked`.
117    ignore_lock: bool,
118
119    /// Requested path of the lockfile (i.e. passed as the cli flag)
120    requested_lockfile_path: Option<PathBuf>,
121
122    /// The resolver behavior specified with the `resolver` field.
123    resolve_behavior: ResolveBehavior,
124    /// If `true`, then workspace `rust_version` would be used in `cargo resolve`
125    /// and other places that use rust version.
126    /// This is set based on the resolver version, config settings, and CLI flags.
127    resolve_honors_rust_version: bool,
128    /// The feature unification mode used when building packages.
129    resolve_feature_unification: FeatureUnification,
130    /// Latest publish time allowed for packages
131    resolve_publish_time: Option<jiff::Timestamp>,
132    /// Workspace-level custom metadata
133    custom_metadata: Option<toml::Value>,
134
135    /// Local overlay configuration. See [`crate::sources::overlay`].
136    local_overlays: HashMap<SourceId, PathBuf>,
137}
138
139// Separate structure for tracking loaded packages (to avoid loading anything
140// twice), and this is separate to help appease the borrow checker.
141#[derive(Debug)]
142struct Packages<'gctx> {
143    gctx: &'gctx GlobalContext,
144    packages: HashMap<PathBuf, MaybePackage>,
145}
146
147#[derive(Debug)]
148pub enum MaybePackage {
149    Package(Package),
150    Virtual(VirtualManifest),
151}
152
153/// Configuration of a workspace in a manifest.
154#[derive(Debug, Clone)]
155pub enum WorkspaceConfig {
156    /// Indicates that `[workspace]` was present and the members were
157    /// optionally specified as well.
158    Root(WorkspaceRootConfig),
159
160    /// Indicates that `[workspace]` was present and the `root` field is the
161    /// optional value of `package.workspace`, if present.
162    Member { root: Option<String> },
163}
164
165impl WorkspaceConfig {
166    pub fn inheritable(&self) -> Option<&InheritableFields> {
167        match self {
168            WorkspaceConfig::Root(root) => Some(&root.inheritable_fields),
169            WorkspaceConfig::Member { .. } => None,
170        }
171    }
172
173    /// Returns the path of the workspace root based on this `[workspace]` configuration.
174    ///
175    /// Returns `None` if the root is not explicitly known.
176    ///
177    /// * `self_path` is the path of the manifest this `WorkspaceConfig` is located.
178    /// * `look_from` is the path where discovery started (usually the current
179    ///   working directory), used for `workspace.exclude` checking.
180    fn get_ws_root(&self, self_path: &Path, look_from: &Path) -> Option<PathBuf> {
181        match self {
182            WorkspaceConfig::Root(ances_root_config) => {
183                debug!("find_root - found a root checking exclusion");
184                if !ances_root_config.is_excluded(look_from) {
185                    debug!("find_root - found!");
186                    Some(self_path.to_owned())
187                } else {
188                    None
189                }
190            }
191            WorkspaceConfig::Member {
192                root: Some(path_to_root),
193            } => {
194                debug!("find_root - found pointer");
195                Some(read_root_pointer(self_path, path_to_root))
196            }
197            WorkspaceConfig::Member { .. } => None,
198        }
199    }
200}
201
202/// Intermediate configuration of a workspace root in a manifest.
203///
204/// Knows the Workspace Root path, as well as `members` and `exclude` lists of path patterns, which
205/// together tell if some path is recognized as a member by this root or not.
206#[derive(Debug, Clone)]
207pub struct WorkspaceRootConfig {
208    root_dir: PathBuf,
209    members: Option<Vec<String>>,
210    default_members: Option<Vec<String>>,
211    exclude: Vec<String>,
212    inheritable_fields: InheritableFields,
213    custom_metadata: Option<toml::Value>,
214}
215
216impl<'gctx> Workspace<'gctx> {
217    /// Creates a new workspace given the target manifest pointed to by
218    /// `manifest_path`.
219    ///
220    /// This function will construct the entire workspace by determining the
221    /// root and all member packages. It will then validate the workspace
222    /// before returning it, so `Ok` is only returned for valid workspaces.
223    pub fn new(manifest_path: &Path, gctx: &'gctx GlobalContext) -> CargoResult<Workspace<'gctx>> {
224        let mut ws = Workspace::new_default(manifest_path.to_path_buf(), gctx);
225
226        if manifest_path.is_relative() {
227            bail!(
228                "manifest_path:{:?} is not an absolute path. Please provide an absolute path.",
229                manifest_path
230            )
231        } else {
232            ws.root_manifest = ws.find_root(manifest_path)?;
233        }
234
235        ws.target_dir = gctx.target_dir()?;
236        ws.build_dir = gctx.build_dir(ws.root_manifest())?;
237
238        ws.custom_metadata = ws
239            .load_workspace_config()?
240            .and_then(|cfg| cfg.custom_metadata);
241        ws.find_members()?;
242        ws.set_resolve_behavior()?;
243        ws.validate()?;
244        Ok(ws)
245    }
246
247    fn new_default(current_manifest: PathBuf, gctx: &'gctx GlobalContext) -> Workspace<'gctx> {
248        Workspace {
249            gctx,
250            current_manifest,
251            packages: Packages {
252                gctx,
253                packages: HashMap::new(),
254            },
255            root_manifest: None,
256            target_dir: None,
257            build_dir: None,
258            members: Vec::new(),
259            member_ids: HashSet::new(),
260            default_members: Vec::new(),
261            is_ephemeral: false,
262            require_optional_deps: true,
263            loaded_packages: RefCell::new(HashMap::new()),
264            ignore_lock: false,
265            requested_lockfile_path: None,
266            resolve_behavior: ResolveBehavior::V1,
267            resolve_honors_rust_version: false,
268            resolve_feature_unification: FeatureUnification::Selected,
269            resolve_publish_time: None,
270            custom_metadata: None,
271            local_overlays: HashMap::new(),
272        }
273    }
274
275    /// Creates a "temporary workspace" from one package which only contains
276    /// that package.
277    ///
278    /// This constructor will not touch the filesystem and only creates an
279    /// in-memory workspace. That is, all configuration is ignored, it's just
280    /// intended for that one package.
281    ///
282    /// This is currently only used in niche situations like `cargo install` or
283    /// `cargo package`.
284    pub fn ephemeral(
285        package: Package,
286        gctx: &'gctx GlobalContext,
287        target_dir: Option<Filesystem>,
288        require_optional_deps: bool,
289    ) -> CargoResult<Workspace<'gctx>> {
290        let mut ws = Workspace::new_default(package.manifest_path().to_path_buf(), gctx);
291        ws.is_ephemeral = true;
292        ws.require_optional_deps = require_optional_deps;
293        let id = package.package_id();
294        let package = MaybePackage::Package(package);
295        ws.packages
296            .packages
297            .insert(ws.current_manifest.clone(), package);
298        ws.target_dir = if let Some(dir) = target_dir {
299            Some(dir)
300        } else {
301            ws.gctx.target_dir()?
302        };
303        ws.build_dir = ws.target_dir.clone();
304        ws.members.push(ws.current_manifest.clone());
305        ws.member_ids.insert(id);
306        ws.default_members.push(ws.current_manifest.clone());
307        ws.set_resolve_behavior()?;
308        Ok(ws)
309    }
310
311    /// Reloads the workspace.
312    ///
313    /// This is useful if the workspace has been updated, such as with `cargo
314    /// fix` modifying the `Cargo.toml` file.
315    pub fn reload(&self, gctx: &'gctx GlobalContext) -> CargoResult<Workspace<'gctx>> {
316        let mut ws = Workspace::new(&self.current_manifest, gctx)?;
317        ws.set_resolve_honors_rust_version(Some(self.resolve_honors_rust_version));
318        ws.set_resolve_feature_unification(self.resolve_feature_unification);
319        ws.set_requested_lockfile_path(self.requested_lockfile_path.clone());
320        Ok(ws)
321    }
322
323    fn set_resolve_behavior(&mut self) -> CargoResult<()> {
324        // - If resolver is specified in the workspace definition, use that.
325        // - If the root package specifies the resolver, use that.
326        // - If the root package specifies edition 2021, use v2.
327        // - Otherwise, use the default v1.
328        self.resolve_behavior = match self.root_maybe() {
329            MaybePackage::Package(p) => p
330                .manifest()
331                .resolve_behavior()
332                .unwrap_or_else(|| p.manifest().edition().default_resolve_behavior()),
333            MaybePackage::Virtual(vm) => vm.resolve_behavior().unwrap_or(ResolveBehavior::V1),
334        };
335
336        match self.resolve_behavior() {
337            ResolveBehavior::V1 | ResolveBehavior::V2 => {}
338            ResolveBehavior::V3 => {
339                if self.resolve_behavior == ResolveBehavior::V3 {
340                    self.resolve_honors_rust_version = true;
341                }
342            }
343        }
344        let config = self.gctx().get::<CargoResolverConfig>("resolver")?;
345        if let Some(incompatible_rust_versions) = config.incompatible_rust_versions {
346            self.resolve_honors_rust_version =
347                incompatible_rust_versions == IncompatibleRustVersions::Fallback;
348        }
349        if self.gctx().cli_unstable().feature_unification {
350            self.resolve_feature_unification = config
351                .feature_unification
352                .unwrap_or(FeatureUnification::Selected);
353        } else if config.feature_unification.is_some() {
354            self.gctx()
355                .shell()
356                .warn("ignoring `resolver.feature-unification` without `-Zfeature-unification`")?;
357        };
358
359        if let Some(lockfile_path) = config.lockfile_path {
360            if self.gctx().cli_unstable().lockfile_path {
361                // Reserve the ability to add templates in the future.
362                let replacements: [(&str, &str); 0] = [];
363                let path = lockfile_path
364                    .resolve_templated_path(self.gctx(), replacements)
365                    .map_err(|e| match e {
366                        context::ResolveTemplateError::UnexpectedVariable {
367                            variable,
368                            raw_template,
369                        } => {
370                            anyhow!(
371                                "unexpected variable `{variable}` in resolver.lockfile-path `{raw_template}`"
372                            )
373                        }
374                        context::ResolveTemplateError::UnexpectedBracket { bracket_type, raw_template } => {
375                            let (btype, literal) = match bracket_type {
376                                context::BracketType::Opening => ("opening", "{"),
377                                context::BracketType::Closing => ("closing", "}"),
378                            };
379
380                            anyhow!(
381                                "unexpected {btype} bracket `{literal}` in build.build-dir path `{raw_template}`"
382                            )
383                        }
384                    })?;
385                if !path.ends_with(LOCKFILE_NAME) {
386                    bail!("the `resolver.lockfile-path` must be a path to a {LOCKFILE_NAME} file");
387                }
388                if path.is_dir() {
389                    bail!(
390                        "`resolver.lockfile-path` `{}` is a directory but expected a file",
391                        path.display()
392                    );
393                }
394                self.requested_lockfile_path = Some(path);
395            } else {
396                self.gctx().shell().warn(
397                    "ignoring `resolver.lockfile-path`, pass `-Zlockfile-path` to enable it",
398                )?;
399            }
400        }
401
402        Ok(())
403    }
404
405    /// Returns the current package of this workspace.
406    ///
407    /// Note that this can return an error if it the current manifest is
408    /// actually a "virtual Cargo.toml", in which case an error is returned
409    /// indicating that something else should be passed.
410    pub fn current(&self) -> CargoResult<&Package> {
411        let pkg = self.current_opt().ok_or_else(|| {
412            anyhow::format_err!(
413                "manifest path `{}` is a virtual manifest, but this \
414                 command requires running against an actual package in \
415                 this workspace",
416                self.current_manifest.display()
417            )
418        })?;
419        Ok(pkg)
420    }
421
422    pub fn current_mut(&mut self) -> CargoResult<&mut Package> {
423        let cm = self.current_manifest.clone();
424        let pkg = self.current_opt_mut().ok_or_else(|| {
425            anyhow::format_err!(
426                "manifest path `{}` is a virtual manifest, but this \
427                 command requires running against an actual package in \
428                 this workspace",
429                cm.display()
430            )
431        })?;
432        Ok(pkg)
433    }
434
435    pub fn current_opt(&self) -> Option<&Package> {
436        match *self.packages.get(&self.current_manifest) {
437            MaybePackage::Package(ref p) => Some(p),
438            MaybePackage::Virtual(..) => None,
439        }
440    }
441
442    pub fn current_opt_mut(&mut self) -> Option<&mut Package> {
443        match *self.packages.get_mut(&self.current_manifest) {
444            MaybePackage::Package(ref mut p) => Some(p),
445            MaybePackage::Virtual(..) => None,
446        }
447    }
448
449    pub fn is_virtual(&self) -> bool {
450        match *self.packages.get(&self.current_manifest) {
451            MaybePackage::Package(..) => false,
452            MaybePackage::Virtual(..) => true,
453        }
454    }
455
456    /// Returns the `GlobalContext` this workspace is associated with.
457    pub fn gctx(&self) -> &'gctx GlobalContext {
458        self.gctx
459    }
460
461    pub fn profiles(&self) -> Option<&TomlProfiles> {
462        self.root_maybe().profiles()
463    }
464
465    /// Returns the root path of this workspace.
466    ///
467    /// That is, this returns the path of the directory containing the
468    /// `Cargo.toml` which is the root of this workspace.
469    pub fn root(&self) -> &Path {
470        self.root_manifest().parent().unwrap()
471    }
472
473    /// Returns the path of the `Cargo.toml` which is the root of this
474    /// workspace.
475    pub fn root_manifest(&self) -> &Path {
476        self.root_manifest
477            .as_ref()
478            .unwrap_or(&self.current_manifest)
479    }
480
481    /// Returns the root Package or `VirtualManifest`.
482    pub fn root_maybe(&self) -> &MaybePackage {
483        self.packages.get(self.root_manifest())
484    }
485
486    pub fn target_dir(&self) -> Filesystem {
487        self.target_dir
488            .clone()
489            .unwrap_or_else(|| self.default_target_dir())
490    }
491
492    pub fn build_dir(&self) -> Filesystem {
493        self.build_dir
494            .clone()
495            .or_else(|| self.target_dir.clone())
496            .unwrap_or_else(|| self.default_build_dir())
497    }
498
499    fn default_target_dir(&self) -> Filesystem {
500        if self.root_maybe().is_embedded() {
501            self.build_dir().join("target")
502        } else {
503            Filesystem::new(self.root().join("target"))
504        }
505    }
506
507    fn default_build_dir(&self) -> Filesystem {
508        if self.root_maybe().is_embedded() {
509            let default = ConfigRelativePath::new(
510                "{cargo-cache-home}/build/{workspace-path-hash}"
511                    .to_owned()
512                    .into(),
513            );
514            self.gctx()
515                .custom_build_dir(&default, self.root_manifest())
516                .expect("template is correct")
517        } else {
518            self.default_target_dir()
519        }
520    }
521
522    /// Returns the root `[replace]` section of this workspace.
523    ///
524    /// This may be from a virtual crate or an actual crate.
525    pub fn root_replace(&self) -> &[(PackageIdSpec, Dependency)] {
526        match self.root_maybe() {
527            MaybePackage::Package(p) => p.manifest().replace(),
528            MaybePackage::Virtual(vm) => vm.replace(),
529        }
530    }
531
532    fn config_patch(&self) -> CargoResult<HashMap<Url, Vec<Patch>>> {
533        let config_patch: Option<
534            BTreeMap<String, BTreeMap<String, Value<TomlDependency<ConfigRelativePath>>>>,
535        > = self.gctx.get("patch")?;
536
537        let source = SourceId::for_manifest_path(self.root_manifest())?;
538
539        let mut warnings = Vec::new();
540
541        let mut patch = HashMap::new();
542        for (url, deps) in config_patch.into_iter().flatten() {
543            let url = match &url[..] {
544                CRATES_IO_REGISTRY => CRATES_IO_INDEX.parse().unwrap(),
545                url => self
546                    .gctx
547                    .get_registry_index(url)
548                    .or_else(|_| url.into_url())
549                    .with_context(|| {
550                        format!("[patch] entry `{}` should be a URL or registry name", url)
551                    })?,
552            };
553            patch.insert(
554                url,
555                deps.iter()
556                    .map(|(name, dependency_cv)| {
557                        crate::util::toml::config_patch_to_dependency(
558                            &dependency_cv.val,
559                            name,
560                            source,
561                            self.gctx,
562                            &mut warnings,
563                        )
564                        .map(|dep| Patch {
565                            dep,
566                            loc: PatchLocation::Config(dependency_cv.definition.clone()),
567                        })
568                    })
569                    .collect::<CargoResult<Vec<_>>>()?,
570            );
571        }
572
573        for message in warnings {
574            self.gctx
575                .shell()
576                .warn(format!("[patch] in cargo config: {}", message))?
577        }
578
579        Ok(patch)
580    }
581
582    /// Returns the root `[patch]` section of this workspace.
583    ///
584    /// This may be from a virtual crate or an actual crate.
585    pub fn root_patch(&self) -> CargoResult<HashMap<Url, Vec<Patch>>> {
586        let from_manifest = match self.root_maybe() {
587            MaybePackage::Package(p) => p.manifest().patch(),
588            MaybePackage::Virtual(vm) => vm.patch(),
589        };
590
591        let from_config = self.config_patch()?;
592        if from_config.is_empty() {
593            return Ok(from_manifest.clone());
594        }
595        if from_manifest.is_empty() {
596            return Ok(from_config);
597        }
598
599        // We could just chain from_manifest and from_config,
600        // but that's not quite right as it won't deal with overlaps.
601        let mut combined = from_config;
602        for (url, deps_from_manifest) in from_manifest {
603            if let Some(deps_from_config) = combined.get_mut(url) {
604                // We want from_config to take precedence for each patched name.
605                // NOTE: This is inefficient if the number of patches is large!
606                let mut from_manifest_pruned = deps_from_manifest.clone();
607                for dep_from_config in &mut *deps_from_config {
608                    if let Some(i) = from_manifest_pruned.iter().position(|dep_from_manifest| {
609                        // XXX: should this also take into account version numbers?
610                        dep_from_config.dep.name_in_toml() == dep_from_manifest.dep.name_in_toml()
611                    }) {
612                        from_manifest_pruned.swap_remove(i);
613                    }
614                }
615                // Whatever is left does not exist in manifest dependencies.
616                deps_from_config.extend(from_manifest_pruned);
617            } else {
618                combined.insert(url.clone(), deps_from_manifest.clone());
619            }
620        }
621        Ok(combined)
622    }
623
624    /// Returns an iterator over all packages in this workspace
625    pub fn members(&self) -> impl Iterator<Item = &Package> {
626        let packages = &self.packages;
627        self.members
628            .iter()
629            .filter_map(move |path| match packages.get(path) {
630                MaybePackage::Package(p) => Some(p),
631                _ => None,
632            })
633    }
634
635    /// Returns a mutable iterator over all packages in this workspace
636    pub fn members_mut(&mut self) -> impl Iterator<Item = &mut Package> {
637        let packages = &mut self.packages.packages;
638        let members: HashSet<_> = self.members.iter().map(|path| path).collect();
639
640        packages.iter_mut().filter_map(move |(path, package)| {
641            if members.contains(path) {
642                if let MaybePackage::Package(p) = package {
643                    return Some(p);
644                }
645            }
646
647            None
648        })
649    }
650
651    /// Returns an iterator over default packages in this workspace
652    pub fn default_members<'a>(&'a self) -> impl Iterator<Item = &'a Package> {
653        let packages = &self.packages;
654        self.default_members
655            .iter()
656            .filter_map(move |path| match packages.get(path) {
657                MaybePackage::Package(p) => Some(p),
658                _ => None,
659            })
660    }
661
662    /// Returns an iterator over default packages in this workspace
663    pub fn default_members_mut(&mut self) -> impl Iterator<Item = &mut Package> {
664        let packages = &mut self.packages.packages;
665        let members: HashSet<_> = self
666            .default_members
667            .iter()
668            .map(|path| path.parent().unwrap().to_owned())
669            .collect();
670
671        packages.iter_mut().filter_map(move |(path, package)| {
672            if members.contains(path) {
673                if let MaybePackage::Package(p) = package {
674                    return Some(p);
675                }
676            }
677
678            None
679        })
680    }
681
682    /// Returns true if the package is a member of the workspace.
683    pub fn is_member(&self, pkg: &Package) -> bool {
684        self.member_ids.contains(&pkg.package_id())
685    }
686
687    /// Returns true if the given package_id is a member of the workspace.
688    pub fn is_member_id(&self, package_id: PackageId) -> bool {
689        self.member_ids.contains(&package_id)
690    }
691
692    pub fn is_ephemeral(&self) -> bool {
693        self.is_ephemeral
694    }
695
696    pub fn require_optional_deps(&self) -> bool {
697        self.require_optional_deps
698    }
699
700    pub fn set_require_optional_deps(
701        &mut self,
702        require_optional_deps: bool,
703    ) -> &mut Workspace<'gctx> {
704        self.require_optional_deps = require_optional_deps;
705        self
706    }
707
708    pub fn ignore_lock(&self) -> bool {
709        self.ignore_lock
710    }
711
712    pub fn set_ignore_lock(&mut self, ignore_lock: bool) -> &mut Workspace<'gctx> {
713        self.ignore_lock = ignore_lock;
714        self
715    }
716
717    /// Returns the directory where the lockfile is in.
718    pub fn lock_root(&self) -> Filesystem {
719        if let Some(requested) = self.requested_lockfile_path.as_ref() {
720            return Filesystem::new(
721                requested
722                    .parent()
723                    .expect("Lockfile path can't be root")
724                    .to_owned(),
725            );
726        }
727        self.default_lock_root()
728    }
729
730    fn default_lock_root(&self) -> Filesystem {
731        if self.root_maybe().is_embedded() {
732            self.build_dir()
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        // FIXME: Make this more generic by using a relative path resolver between member and root.
1116        let members_msg = match current_dir.strip_prefix(root_dir) {
1117            Ok(rel) => format!(
1118                "this may be fixable by adding `{}` to the \
1119                     `workspace.members` array of the manifest \
1120                     located at: {}",
1121                rel.display(),
1122                root.display()
1123            ),
1124            Err(_) => format!(
1125                "this may be fixable by adding a member to \
1126                     the `workspace.members` array of the \
1127                     manifest located at: {}",
1128                root.display()
1129            ),
1130        };
1131        let extra = match *root_pkg {
1132            MaybePackage::Virtual(_) => members_msg,
1133            MaybePackage::Package(ref p) => {
1134                let has_members_list = match *p.manifest().workspace_config() {
1135                    WorkspaceConfig::Root(ref root_config) => root_config.has_members_list(),
1136                    WorkspaceConfig::Member { .. } => unreachable!(),
1137                };
1138                if !has_members_list {
1139                    format!(
1140                        "this may be fixable by ensuring that this \
1141                             crate is depended on by the workspace \
1142                             root: {}",
1143                        root.display()
1144                    )
1145                } else {
1146                    members_msg
1147                }
1148            }
1149        };
1150        bail!(
1151            "current package believes it's in a workspace when it's not:\n\
1152                 current:   {}\n\
1153                 workspace: {}\n\n{}\n\
1154                 Alternatively, to keep it out of the workspace, add the package \
1155                 to the `workspace.exclude` array, or add an empty `[workspace]` \
1156                 table to the package's manifest.",
1157            self.current_manifest.display(),
1158            root.display(),
1159            extra
1160        );
1161    }
1162
1163    fn validate_manifest(&mut self) -> CargoResult<()> {
1164        if let Some(ref root_manifest) = self.root_manifest {
1165            for pkg in self
1166                .members()
1167                .filter(|p| p.manifest_path() != root_manifest)
1168            {
1169                let manifest = pkg.manifest();
1170                let emit_warning = |what| -> CargoResult<()> {
1171                    let msg = format!(
1172                        "{} for the non root package will be ignored, \
1173                         specify {} at the workspace root:\n\
1174                         package:   {}\n\
1175                         workspace: {}",
1176                        what,
1177                        what,
1178                        pkg.manifest_path().display(),
1179                        root_manifest.display(),
1180                    );
1181                    self.gctx.shell().warn(&msg)
1182                };
1183                if manifest.normalized_toml().has_profiles() {
1184                    emit_warning("profiles")?;
1185                }
1186                if !manifest.replace().is_empty() {
1187                    emit_warning("replace")?;
1188                }
1189                if !manifest.patch().is_empty() {
1190                    emit_warning("patch")?;
1191                }
1192                if let Some(behavior) = manifest.resolve_behavior() {
1193                    if behavior != self.resolve_behavior {
1194                        // Only warn if they don't match.
1195                        emit_warning("resolver")?;
1196                    }
1197                }
1198            }
1199            if let MaybePackage::Virtual(vm) = self.root_maybe() {
1200                if vm.resolve_behavior().is_none() {
1201                    if let Some(edition) = self
1202                        .members()
1203                        .filter(|p| p.manifest_path() != root_manifest)
1204                        .map(|p| p.manifest().edition())
1205                        .filter(|&e| e >= Edition::Edition2021)
1206                        .max()
1207                    {
1208                        let resolver = edition.default_resolve_behavior().to_manifest();
1209                        let report = &[Level::WARNING
1210                            .primary_title(format!(
1211                                "virtual workspace defaulting to `resolver = \"1\"` despite one or more workspace members being on edition {edition} which implies `resolver = \"{resolver}\"`"
1212                            ))
1213                            .elements([
1214                                Level::NOTE.message("to keep the current resolver, specify `workspace.resolver = \"1\"` in the workspace root's manifest"),
1215                                Level::NOTE.message(
1216                                    format!("to use the edition {edition} resolver, specify `workspace.resolver = \"{resolver}\"` in the workspace root's manifest"),
1217                                ),
1218                                Level::NOTE.message("for more details see https://doc.rust-lang.org/cargo/reference/resolver.html#resolver-versions"),
1219                            ])];
1220                        self.gctx.shell().print_report(report, false)?;
1221                    }
1222                }
1223            }
1224        }
1225        Ok(())
1226    }
1227
1228    pub fn load(&self, manifest_path: &Path) -> CargoResult<Package> {
1229        match self.packages.maybe_get(manifest_path) {
1230            Some(MaybePackage::Package(p)) => return Ok(p.clone()),
1231            Some(&MaybePackage::Virtual(_)) => bail!("cannot load workspace root"),
1232            None => {}
1233        }
1234
1235        let mut loaded = self.loaded_packages.borrow_mut();
1236        if let Some(p) = loaded.get(manifest_path).cloned() {
1237            return Ok(p);
1238        }
1239        let source_id = SourceId::for_manifest_path(manifest_path)?;
1240        let package = ops::read_package(manifest_path, source_id, self.gctx)?;
1241        loaded.insert(manifest_path.to_path_buf(), package.clone());
1242        Ok(package)
1243    }
1244
1245    /// Preload the provided registry with already loaded packages.
1246    ///
1247    /// A workspace may load packages during construction/parsing/early phases
1248    /// for various operations, and this preload step avoids doubly-loading and
1249    /// parsing crates on the filesystem by inserting them all into the registry
1250    /// with their in-memory formats.
1251    pub fn preload(&self, registry: &mut PackageRegistry<'gctx>) {
1252        // These can get weird as this generally represents a workspace during
1253        // `cargo install`. Things like git repositories will actually have a
1254        // `PathSource` with multiple entries in it, so the logic below is
1255        // mostly just an optimization for normal `cargo build` in workspaces
1256        // during development.
1257        if self.is_ephemeral {
1258            return;
1259        }
1260
1261        for pkg in self.packages.packages.values() {
1262            let pkg = match *pkg {
1263                MaybePackage::Package(ref p) => p.clone(),
1264                MaybePackage::Virtual(_) => continue,
1265            };
1266            let src = PathSource::preload_with(pkg, self.gctx);
1267            registry.add_preloaded(Box::new(src));
1268        }
1269    }
1270
1271    pub fn emit_warnings(&self) -> CargoResult<()> {
1272        let mut first_emitted_error = None;
1273
1274        if let Err(e) = self.emit_ws_lints() {
1275            first_emitted_error = Some(e);
1276        }
1277
1278        for (path, maybe_pkg) in &self.packages.packages {
1279            if let MaybePackage::Package(pkg) = maybe_pkg {
1280                if let Err(e) = self.emit_pkg_lints(pkg, &path)
1281                    && first_emitted_error.is_none()
1282                {
1283                    first_emitted_error = Some(e);
1284                }
1285            }
1286            let warnings = match maybe_pkg {
1287                MaybePackage::Package(pkg) => pkg.manifest().warnings().warnings(),
1288                MaybePackage::Virtual(vm) => vm.warnings().warnings(),
1289            };
1290            for warning in warnings {
1291                if warning.is_critical {
1292                    let err = anyhow::format_err!("{}", warning.message);
1293                    let cx =
1294                        anyhow::format_err!("failed to parse manifest at `{}`", path.display());
1295                    if first_emitted_error.is_none() {
1296                        first_emitted_error = Some(err.context(cx));
1297                    }
1298                } else {
1299                    let msg = if self.root_manifest.is_none() {
1300                        warning.message.to_string()
1301                    } else {
1302                        // In a workspace, it can be confusing where a warning
1303                        // originated, so include the path.
1304                        format!("{}: {}", path.display(), warning.message)
1305                    };
1306                    self.gctx.shell().warn(msg)?
1307                }
1308            }
1309        }
1310
1311        if let Some(error) = first_emitted_error {
1312            Err(error)
1313        } else {
1314            Ok(())
1315        }
1316    }
1317
1318    pub fn emit_pkg_lints(&self, pkg: &Package, path: &Path) -> CargoResult<()> {
1319        let toml_lints = pkg
1320            .manifest()
1321            .normalized_toml()
1322            .lints
1323            .clone()
1324            .map(|lints| lints.lints)
1325            .unwrap_or(manifest::TomlLints::default());
1326        let cargo_lints = toml_lints
1327            .get("cargo")
1328            .cloned()
1329            .unwrap_or(manifest::TomlToolLints::default());
1330
1331        if self.gctx.cli_unstable().cargo_lints {
1332            let mut verify_error_count = 0;
1333
1334            analyze_cargo_lints_table(
1335                pkg.into(),
1336                &path,
1337                &cargo_lints,
1338                &mut verify_error_count,
1339                self.gctx,
1340            )?;
1341
1342            if verify_error_count > 0 {
1343                let plural = if verify_error_count == 1 { "" } else { "s" };
1344                bail!("encountered {verify_error_count} error{plural} while verifying lints")
1345            }
1346
1347            let mut run_error_count = 0;
1348
1349            check_im_a_teapot(pkg, &path, &cargo_lints, &mut run_error_count, self.gctx)?;
1350            implicit_minimum_version_req(
1351                pkg.into(),
1352                &path,
1353                &cargo_lints,
1354                &mut run_error_count,
1355                self.gctx,
1356            )?;
1357
1358            if run_error_count > 0 {
1359                let plural = if run_error_count == 1 { "" } else { "s" };
1360                bail!("encountered {run_error_count} error{plural} while running lints")
1361            }
1362        }
1363
1364        Ok(())
1365    }
1366
1367    pub fn emit_ws_lints(&self) -> CargoResult<()> {
1368        let mut run_error_count = 0;
1369
1370        let cargo_lints = match self.root_maybe() {
1371            MaybePackage::Package(pkg) => {
1372                let toml = pkg.manifest().normalized_toml();
1373                if let Some(ws) = &toml.workspace {
1374                    ws.lints.as_ref()
1375                } else {
1376                    toml.lints.as_ref().map(|l| &l.lints)
1377                }
1378            }
1379            MaybePackage::Virtual(vm) => vm
1380                .normalized_toml()
1381                .workspace
1382                .as_ref()
1383                .unwrap()
1384                .lints
1385                .as_ref(),
1386        }
1387        .and_then(|t| t.get("cargo"))
1388        .cloned()
1389        .unwrap_or(manifest::TomlToolLints::default());
1390
1391        if self.gctx.cli_unstable().cargo_lints {
1392            let mut verify_error_count = 0;
1393
1394            analyze_cargo_lints_table(
1395                self.root_maybe().into(),
1396                self.root_manifest(),
1397                &cargo_lints,
1398                &mut verify_error_count,
1399                self.gctx,
1400            )?;
1401
1402            if verify_error_count > 0 {
1403                let plural = if verify_error_count == 1 { "" } else { "s" };
1404                bail!("encountered {verify_error_count} error{plural} while verifying lints")
1405            }
1406
1407            implicit_minimum_version_req(
1408                self.root_maybe().into(),
1409                self.root_manifest(),
1410                &cargo_lints,
1411                &mut run_error_count,
1412                self.gctx,
1413            )?;
1414        }
1415
1416        // This is a short term hack to allow `blanket_hint_mostly_unused`
1417        // to run without requiring `-Zcargo-lints`, which should hopefully
1418        // improve the testing experience while we are collecting feedback
1419        if self.gctx.cli_unstable().profile_hint_mostly_unused {
1420            blanket_hint_mostly_unused(
1421                self.root_maybe(),
1422                self.root_manifest(),
1423                &cargo_lints,
1424                &mut run_error_count,
1425                self.gctx,
1426            )?;
1427        }
1428
1429        if run_error_count > 0 {
1430            let plural = if run_error_count == 1 { "" } else { "s" };
1431            bail!("encountered {run_error_count} error{plural} while running lints")
1432        } else {
1433            Ok(())
1434        }
1435    }
1436
1437    pub fn set_target_dir(&mut self, target_dir: Filesystem) {
1438        self.target_dir = Some(target_dir);
1439    }
1440
1441    /// Returns a Vec of `(&Package, CliFeatures)` tuples that
1442    /// represent the workspace members that were requested on the command-line.
1443    ///
1444    /// `specs` may be empty, which indicates it should return all workspace
1445    /// members. In this case, `requested_features.all_features` must be
1446    /// `true`. This is used for generating `Cargo.lock`, which must include
1447    /// all members with all features enabled.
1448    pub fn members_with_features(
1449        &self,
1450        specs: &[PackageIdSpec],
1451        cli_features: &CliFeatures,
1452    ) -> CargoResult<Vec<(&Package, CliFeatures)>> {
1453        assert!(
1454            !specs.is_empty() || cli_features.all_features,
1455            "no specs requires all_features"
1456        );
1457        if specs.is_empty() {
1458            // When resolving the entire workspace, resolve each member with
1459            // all features enabled.
1460            return Ok(self
1461                .members()
1462                .map(|m| (m, CliFeatures::new_all(true)))
1463                .collect());
1464        }
1465        if self.allows_new_cli_feature_behavior() {
1466            self.members_with_features_new(specs, cli_features)
1467        } else {
1468            Ok(self.members_with_features_old(specs, cli_features))
1469        }
1470    }
1471
1472    /// Returns the requested features for the given member.
1473    /// This filters out any named features that the member does not have.
1474    fn collect_matching_features(
1475        member: &Package,
1476        cli_features: &CliFeatures,
1477        found_features: &mut BTreeSet<FeatureValue>,
1478    ) -> CliFeatures {
1479        if cli_features.features.is_empty() {
1480            return cli_features.clone();
1481        }
1482
1483        // Only include features this member defines.
1484        let summary = member.summary();
1485
1486        // Features defined in the manifest
1487        let summary_features = summary.features();
1488
1489        // Dependency name -> dependency
1490        let dependencies: BTreeMap<InternedString, &Dependency> = summary
1491            .dependencies()
1492            .iter()
1493            .map(|dep| (dep.name_in_toml(), dep))
1494            .collect();
1495
1496        // Features that enable optional dependencies
1497        let optional_dependency_names: BTreeSet<_> = dependencies
1498            .iter()
1499            .filter(|(_, dep)| dep.is_optional())
1500            .map(|(name, _)| name)
1501            .copied()
1502            .collect();
1503
1504        let mut features = BTreeSet::new();
1505
1506        // Checks if a member contains the given feature.
1507        let summary_or_opt_dependency_feature = |feature: &InternedString| -> bool {
1508            summary_features.contains_key(feature) || optional_dependency_names.contains(feature)
1509        };
1510
1511        for feature in cli_features.features.iter() {
1512            match feature {
1513                FeatureValue::Feature(f) => {
1514                    if summary_or_opt_dependency_feature(f) {
1515                        // feature exists in this member.
1516                        features.insert(feature.clone());
1517                        found_features.insert(feature.clone());
1518                    }
1519                }
1520                // This should be enforced by CliFeatures.
1521                FeatureValue::Dep { .. } => panic!("unexpected dep: syntax {}", feature),
1522                FeatureValue::DepFeature {
1523                    dep_name,
1524                    dep_feature,
1525                    weak: _,
1526                } => {
1527                    if dependencies.contains_key(dep_name) {
1528                        // pkg/feat for a dependency.
1529                        // Will rely on the dependency resolver to validate `dep_feature`.
1530                        features.insert(feature.clone());
1531                        found_features.insert(feature.clone());
1532                    } else if *dep_name == member.name()
1533                        && summary_or_opt_dependency_feature(dep_feature)
1534                    {
1535                        // member/feat where "feat" is a feature in member.
1536                        //
1537                        // `weak` can be ignored here, because the member
1538                        // either is or isn't being built.
1539                        features.insert(FeatureValue::Feature(*dep_feature));
1540                        found_features.insert(feature.clone());
1541                    }
1542                }
1543            }
1544        }
1545        CliFeatures {
1546            features: Rc::new(features),
1547            all_features: cli_features.all_features,
1548            uses_default_features: cli_features.uses_default_features,
1549        }
1550    }
1551
1552    fn missing_feature_spelling_suggestions(
1553        &self,
1554        selected_members: &[&Package],
1555        cli_features: &CliFeatures,
1556        found_features: &BTreeSet<FeatureValue>,
1557    ) -> Vec<String> {
1558        // Keeps track of which features were contained in summary of `member` to suggest similar features in errors
1559        let mut summary_features: Vec<InternedString> = Default::default();
1560
1561        // Keeps track of `member` dependencies (`dep/feature`) and their features names to suggest similar features in error
1562        let mut dependencies_features: BTreeMap<InternedString, &[InternedString]> =
1563            Default::default();
1564
1565        // Keeps track of `member` optional dependencies names (which can be enabled with feature) to suggest similar features in error
1566        let mut optional_dependency_names: Vec<InternedString> = Default::default();
1567
1568        // Keeps track of which features were contained in summary of `member` to suggest similar features in errors
1569        let mut summary_features_per_member: BTreeMap<&Package, BTreeSet<InternedString>> =
1570            Default::default();
1571
1572        // Keeps track of `member` optional dependencies (which can be enabled with feature) to suggest similar features in error
1573        let mut optional_dependency_names_per_member: BTreeMap<&Package, BTreeSet<InternedString>> =
1574            Default::default();
1575
1576        for &member in selected_members {
1577            // Only include features this member defines.
1578            let summary = member.summary();
1579
1580            // Features defined in the manifest
1581            summary_features.extend(summary.features().keys());
1582            summary_features_per_member
1583                .insert(member, summary.features().keys().copied().collect());
1584
1585            // Dependency name -> dependency
1586            let dependencies: BTreeMap<InternedString, &Dependency> = summary
1587                .dependencies()
1588                .iter()
1589                .map(|dep| (dep.name_in_toml(), dep))
1590                .collect();
1591
1592            dependencies_features.extend(
1593                dependencies
1594                    .iter()
1595                    .map(|(name, dep)| (*name, dep.features())),
1596            );
1597
1598            // Features that enable optional dependencies
1599            let optional_dependency_names_raw: BTreeSet<_> = dependencies
1600                .iter()
1601                .filter(|(_, dep)| dep.is_optional())
1602                .map(|(name, _)| name)
1603                .copied()
1604                .collect();
1605
1606            optional_dependency_names.extend(optional_dependency_names_raw.iter());
1607            optional_dependency_names_per_member.insert(member, optional_dependency_names_raw);
1608        }
1609
1610        let edit_distance_test = |a: InternedString, b: InternedString| {
1611            edit_distance(a.as_str(), b.as_str(), 3).is_some()
1612        };
1613
1614        cli_features
1615            .features
1616            .difference(found_features)
1617            .map(|feature| match feature {
1618                // Simple feature, check if any of the optional dependency features or member features are close enough
1619                FeatureValue::Feature(typo) => {
1620                    // Finds member features which are similar to the requested feature.
1621                    let summary_features = summary_features
1622                        .iter()
1623                        .filter(move |feature| edit_distance_test(**feature, *typo));
1624
1625                    // Finds optional dependencies which name is similar to the feature
1626                    let optional_dependency_features = optional_dependency_names
1627                        .iter()
1628                        .filter(move |feature| edit_distance_test(**feature, *typo));
1629
1630                    summary_features
1631                        .chain(optional_dependency_features)
1632                        .map(|s| s.to_string())
1633                        .collect::<Vec<_>>()
1634                }
1635                FeatureValue::Dep { .. } => panic!("unexpected dep: syntax {}", feature),
1636                FeatureValue::DepFeature {
1637                    dep_name,
1638                    dep_feature,
1639                    weak: _,
1640                } => {
1641                    // Finds set of `pkg/feat` that are very similar to current `pkg/feat`.
1642                    let pkg_feat_similar = dependencies_features
1643                        .iter()
1644                        .filter(|(name, _)| edit_distance_test(**name, *dep_name))
1645                        .map(|(name, features)| {
1646                            (
1647                                name,
1648                                features
1649                                    .iter()
1650                                    .filter(|feature| edit_distance_test(**feature, *dep_feature))
1651                                    .collect::<Vec<_>>(),
1652                            )
1653                        })
1654                        .map(|(name, features)| {
1655                            features
1656                                .into_iter()
1657                                .map(move |feature| format!("{}/{}", name, feature))
1658                        })
1659                        .flatten();
1660
1661                    // Finds set of `member/optional_dep` features which name is similar to current `pkg/feat`.
1662                    let optional_dependency_features = optional_dependency_names_per_member
1663                        .iter()
1664                        .filter(|(package, _)| edit_distance_test(package.name(), *dep_name))
1665                        .map(|(package, optional_dependencies)| {
1666                            optional_dependencies
1667                                .into_iter()
1668                                .filter(|optional_dependency| {
1669                                    edit_distance_test(**optional_dependency, *dep_name)
1670                                })
1671                                .map(move |optional_dependency| {
1672                                    format!("{}/{}", package.name(), optional_dependency)
1673                                })
1674                        })
1675                        .flatten();
1676
1677                    // Finds set of `member/feat` features which name is similar to current `pkg/feat`.
1678                    let summary_features = summary_features_per_member
1679                        .iter()
1680                        .filter(|(package, _)| edit_distance_test(package.name(), *dep_name))
1681                        .map(|(package, summary_features)| {
1682                            summary_features
1683                                .into_iter()
1684                                .filter(|summary_feature| {
1685                                    edit_distance_test(**summary_feature, *dep_feature)
1686                                })
1687                                .map(move |summary_feature| {
1688                                    format!("{}/{}", package.name(), summary_feature)
1689                                })
1690                        })
1691                        .flatten();
1692
1693                    pkg_feat_similar
1694                        .chain(optional_dependency_features)
1695                        .chain(summary_features)
1696                        .collect::<Vec<_>>()
1697                }
1698            })
1699            .map(|v| v.into_iter())
1700            .flatten()
1701            .unique()
1702            .filter(|element| {
1703                let feature = FeatureValue::new(element.into());
1704                !cli_features.features.contains(&feature) && !found_features.contains(&feature)
1705            })
1706            .sorted()
1707            .take(5)
1708            .collect()
1709    }
1710
1711    fn report_unknown_features_error(
1712        &self,
1713        specs: &[PackageIdSpec],
1714        cli_features: &CliFeatures,
1715        found_features: &BTreeSet<FeatureValue>,
1716    ) -> CargoResult<()> {
1717        let unknown: Vec<_> = cli_features
1718            .features
1719            .difference(found_features)
1720            .map(|feature| feature.to_string())
1721            .sorted()
1722            .collect();
1723
1724        let (selected_members, unselected_members): (Vec<_>, Vec<_>) = self
1725            .members()
1726            .partition(|member| specs.iter().any(|spec| spec.matches(member.package_id())));
1727
1728        let missing_packages_with_the_features = unselected_members
1729            .into_iter()
1730            .filter(|member| {
1731                unknown
1732                    .iter()
1733                    .any(|feature| member.summary().features().contains_key(&**feature))
1734            })
1735            .map(|m| m.name())
1736            .collect_vec();
1737
1738        let these_features = if unknown.len() == 1 {
1739            "this feature"
1740        } else {
1741            "these features"
1742        };
1743        let mut msg = if let [singular] = &selected_members[..] {
1744            format!(
1745                "the package '{}' does not contain {these_features}: {}",
1746                singular.name(),
1747                unknown.join(", ")
1748            )
1749        } else {
1750            let names = selected_members.iter().map(|m| m.name()).join(", ");
1751            format!(
1752                "none of the selected packages contains {these_features}: {}\nselected packages: {names}",
1753                unknown.join(", ")
1754            )
1755        };
1756
1757        use std::fmt::Write;
1758        if !missing_packages_with_the_features.is_empty() {
1759            write!(
1760                &mut msg,
1761                "\nhelp: package{} with the missing feature{}: {}",
1762                if missing_packages_with_the_features.len() != 1 {
1763                    "s"
1764                } else {
1765                    ""
1766                },
1767                if unknown.len() != 1 { "s" } else { "" },
1768                missing_packages_with_the_features.join(", ")
1769            )?;
1770        } else {
1771            let suggestions = self.missing_feature_spelling_suggestions(
1772                &selected_members,
1773                cli_features,
1774                found_features,
1775            );
1776            if !suggestions.is_empty() {
1777                write!(
1778                    &mut msg,
1779                    "\nhelp: there {}: {}",
1780                    if suggestions.len() == 1 {
1781                        "is a similarly named feature"
1782                    } else {
1783                        "are similarly named features"
1784                    },
1785                    suggestions.join(", ")
1786                )?;
1787            }
1788        }
1789
1790        bail!("{msg}")
1791    }
1792
1793    /// New command-line feature selection behavior with resolver = "2" or the
1794    /// root of a virtual workspace. See `allows_new_cli_feature_behavior`.
1795    fn members_with_features_new(
1796        &self,
1797        specs: &[PackageIdSpec],
1798        cli_features: &CliFeatures,
1799    ) -> CargoResult<Vec<(&Package, CliFeatures)>> {
1800        // Keeps track of which features matched `member` to produce an error
1801        // if any of them did not match anywhere.
1802        let mut found_features = Default::default();
1803
1804        let members: Vec<(&Package, CliFeatures)> = self
1805            .members()
1806            .filter(|m| specs.iter().any(|spec| spec.matches(m.package_id())))
1807            .map(|m| {
1808                (
1809                    m,
1810                    Workspace::collect_matching_features(m, cli_features, &mut found_features),
1811                )
1812            })
1813            .collect();
1814
1815        if members.is_empty() {
1816            // `cargo build -p foo`, where `foo` is not a member.
1817            // Do not allow any command-line flags (defaults only).
1818            if !(cli_features.features.is_empty()
1819                && !cli_features.all_features
1820                && cli_features.uses_default_features)
1821            {
1822                bail!("cannot specify features for packages outside of workspace");
1823            }
1824            // Add all members from the workspace so we can ensure `-p nonmember`
1825            // is in the resolve graph.
1826            return Ok(self
1827                .members()
1828                .map(|m| (m, CliFeatures::new_all(false)))
1829                .collect());
1830        }
1831        if *cli_features.features != found_features {
1832            self.report_unknown_features_error(specs, cli_features, &found_features)?;
1833        }
1834        Ok(members)
1835    }
1836
1837    /// This is the "old" behavior for command-line feature selection.
1838    /// See `allows_new_cli_feature_behavior`.
1839    fn members_with_features_old(
1840        &self,
1841        specs: &[PackageIdSpec],
1842        cli_features: &CliFeatures,
1843    ) -> Vec<(&Package, CliFeatures)> {
1844        // Split off any features with the syntax `member-name/feature-name` into a map
1845        // so that those features can be applied directly to those workspace-members.
1846        let mut member_specific_features: HashMap<InternedString, BTreeSet<FeatureValue>> =
1847            HashMap::new();
1848        // Features for the member in the current directory.
1849        let mut cwd_features = BTreeSet::new();
1850        for feature in cli_features.features.iter() {
1851            match feature {
1852                FeatureValue::Feature(_) => {
1853                    cwd_features.insert(feature.clone());
1854                }
1855                // This should be enforced by CliFeatures.
1856                FeatureValue::Dep { .. } => panic!("unexpected dep: syntax {}", feature),
1857                FeatureValue::DepFeature {
1858                    dep_name,
1859                    dep_feature,
1860                    weak: _,
1861                } => {
1862                    // I think weak can be ignored here.
1863                    // * With `--features member?/feat -p member`, the ? doesn't
1864                    //   really mean anything (either the member is built or it isn't).
1865                    // * With `--features nonmember?/feat`, cwd_features will
1866                    //   handle processing it correctly.
1867                    let is_member = self.members().any(|member| {
1868                        // Check if `dep_name` is member of the workspace, but isn't associated with current package.
1869                        self.current_opt() != Some(member) && member.name() == *dep_name
1870                    });
1871                    if is_member && specs.iter().any(|spec| spec.name() == dep_name.as_str()) {
1872                        member_specific_features
1873                            .entry(*dep_name)
1874                            .or_default()
1875                            .insert(FeatureValue::Feature(*dep_feature));
1876                    } else {
1877                        cwd_features.insert(feature.clone());
1878                    }
1879                }
1880            }
1881        }
1882
1883        let ms: Vec<_> = self
1884            .members()
1885            .filter_map(|member| {
1886                let member_id = member.package_id();
1887                match self.current_opt() {
1888                    // The features passed on the command-line only apply to
1889                    // the "current" package (determined by the cwd).
1890                    Some(current) if member_id == current.package_id() => {
1891                        let feats = CliFeatures {
1892                            features: Rc::new(cwd_features.clone()),
1893                            all_features: cli_features.all_features,
1894                            uses_default_features: cli_features.uses_default_features,
1895                        };
1896                        Some((member, feats))
1897                    }
1898                    _ => {
1899                        // Ignore members that are not enabled on the command-line.
1900                        if specs.iter().any(|spec| spec.matches(member_id)) {
1901                            // -p for a workspace member that is not the "current"
1902                            // one.
1903                            //
1904                            // The odd behavior here is due to backwards
1905                            // compatibility. `--features` and
1906                            // `--no-default-features` used to only apply to the
1907                            // "current" package. As an extension, this allows
1908                            // member-name/feature-name to set member-specific
1909                            // features, which should be backwards-compatible.
1910                            let feats = CliFeatures {
1911                                features: Rc::new(
1912                                    member_specific_features
1913                                        .remove(member.name().as_str())
1914                                        .unwrap_or_default(),
1915                                ),
1916                                uses_default_features: true,
1917                                all_features: cli_features.all_features,
1918                            };
1919                            Some((member, feats))
1920                        } else {
1921                            // This member was not requested on the command-line, skip.
1922                            None
1923                        }
1924                    }
1925                }
1926            })
1927            .collect();
1928
1929        // If any member specific features were not removed while iterating over members
1930        // some features will be ignored.
1931        assert!(member_specific_features.is_empty());
1932
1933        ms
1934    }
1935
1936    /// Returns true if `unit` should depend on the output of Docscrape units.
1937    pub fn unit_needs_doc_scrape(&self, unit: &Unit) -> bool {
1938        // We do not add scraped units for Host units, as they're either build scripts
1939        // (not documented) or proc macros (have no scrape-able exports). Additionally,
1940        // naively passing a proc macro's unit_for to new_unit_dep will currently cause
1941        // Cargo to panic, see issue #10545.
1942        self.is_member(&unit.pkg) && !(unit.target.for_host() || unit.pkg.proc_macro())
1943    }
1944
1945    /// Adds a local package registry overlaying a `SourceId`.
1946    ///
1947    /// See [`crate::sources::overlay::DependencyConfusionThreatOverlaySource`] for why you shouldn't use this.
1948    pub fn add_local_overlay(&mut self, id: SourceId, registry_path: PathBuf) {
1949        self.local_overlays.insert(id, registry_path);
1950    }
1951
1952    /// Builds a package registry that reflects this workspace configuration.
1953    pub fn package_registry(&self) -> CargoResult<PackageRegistry<'gctx>> {
1954        let source_config =
1955            SourceConfigMap::new_with_overlays(self.gctx(), self.local_overlays()?)?;
1956        PackageRegistry::new_with_source_config(self.gctx(), source_config)
1957    }
1958
1959    /// Returns all the configured local overlays, including the ones from our secret environment variable.
1960    fn local_overlays(&self) -> CargoResult<impl Iterator<Item = (SourceId, SourceId)>> {
1961        let mut ret = self
1962            .local_overlays
1963            .iter()
1964            .map(|(id, path)| Ok((*id, SourceId::for_local_registry(path)?)))
1965            .collect::<CargoResult<Vec<_>>>()?;
1966
1967        if let Ok(overlay) = self
1968            .gctx
1969            .get_env("__CARGO_TEST_DEPENDENCY_CONFUSION_VULNERABILITY_DO_NOT_USE_THIS")
1970        {
1971            let (url, path) = overlay.split_once('=').ok_or(anyhow::anyhow!(
1972                "invalid overlay format. I won't tell you why; you shouldn't be using it anyway"
1973            ))?;
1974            ret.push((
1975                SourceId::from_url(url)?,
1976                SourceId::for_local_registry(path.as_ref())?,
1977            ));
1978        }
1979
1980        Ok(ret.into_iter())
1981    }
1982}
1983
1984impl<'gctx> Packages<'gctx> {
1985    fn get(&self, manifest_path: &Path) -> &MaybePackage {
1986        self.maybe_get(manifest_path).unwrap()
1987    }
1988
1989    fn get_mut(&mut self, manifest_path: &Path) -> &mut MaybePackage {
1990        self.maybe_get_mut(manifest_path).unwrap()
1991    }
1992
1993    fn maybe_get(&self, manifest_path: &Path) -> Option<&MaybePackage> {
1994        self.packages.get(manifest_path)
1995    }
1996
1997    fn maybe_get_mut(&mut self, manifest_path: &Path) -> Option<&mut MaybePackage> {
1998        self.packages.get_mut(manifest_path)
1999    }
2000
2001    fn load(&mut self, manifest_path: &Path) -> CargoResult<&MaybePackage> {
2002        match self.packages.entry(manifest_path.to_path_buf()) {
2003            Entry::Occupied(e) => Ok(e.into_mut()),
2004            Entry::Vacant(v) => {
2005                let source_id = SourceId::for_manifest_path(manifest_path)?;
2006                let manifest = read_manifest(manifest_path, source_id, self.gctx)?;
2007                Ok(v.insert(match manifest {
2008                    EitherManifest::Real(manifest) => {
2009                        MaybePackage::Package(Package::new(manifest, manifest_path))
2010                    }
2011                    EitherManifest::Virtual(vm) => MaybePackage::Virtual(vm),
2012                }))
2013            }
2014        }
2015    }
2016}
2017
2018impl MaybePackage {
2019    fn workspace_config(&self) -> &WorkspaceConfig {
2020        match *self {
2021            MaybePackage::Package(ref p) => p.manifest().workspace_config(),
2022            MaybePackage::Virtual(ref vm) => vm.workspace_config(),
2023        }
2024    }
2025
2026    /// Has an embedded manifest (single-file package)
2027    pub fn is_embedded(&self) -> bool {
2028        match self {
2029            MaybePackage::Package(p) => p.manifest().is_embedded(),
2030            MaybePackage::Virtual(_) => false,
2031        }
2032    }
2033
2034    pub fn contents(&self) -> Option<&str> {
2035        match self {
2036            MaybePackage::Package(p) => p.manifest().contents(),
2037            MaybePackage::Virtual(v) => v.contents(),
2038        }
2039    }
2040
2041    pub fn document(&self) -> Option<&toml::Spanned<toml::de::DeTable<'static>>> {
2042        match self {
2043            MaybePackage::Package(p) => p.manifest().document(),
2044            MaybePackage::Virtual(v) => v.document(),
2045        }
2046    }
2047
2048    pub fn edition(&self) -> Edition {
2049        match self {
2050            MaybePackage::Package(p) => p.manifest().edition(),
2051            MaybePackage::Virtual(_) => Edition::default(),
2052        }
2053    }
2054
2055    pub fn profiles(&self) -> Option<&TomlProfiles> {
2056        match self {
2057            MaybePackage::Package(p) => p.manifest().profiles(),
2058            MaybePackage::Virtual(v) => v.profiles(),
2059        }
2060    }
2061
2062    pub fn unstable_features(&self) -> &Features {
2063        match self {
2064            MaybePackage::Package(p) => p.manifest().unstable_features(),
2065            MaybePackage::Virtual(vm) => vm.unstable_features(),
2066        }
2067    }
2068}
2069
2070impl WorkspaceRootConfig {
2071    /// Creates a new Intermediate Workspace Root configuration.
2072    pub fn new(
2073        root_dir: &Path,
2074        members: &Option<Vec<String>>,
2075        default_members: &Option<Vec<String>>,
2076        exclude: &Option<Vec<String>>,
2077        inheritable: &Option<InheritableFields>,
2078        custom_metadata: &Option<toml::Value>,
2079    ) -> WorkspaceRootConfig {
2080        WorkspaceRootConfig {
2081            root_dir: root_dir.to_path_buf(),
2082            members: members.clone(),
2083            default_members: default_members.clone(),
2084            exclude: exclude.clone().unwrap_or_default(),
2085            inheritable_fields: inheritable.clone().unwrap_or_default(),
2086            custom_metadata: custom_metadata.clone(),
2087        }
2088    }
2089    /// Checks the path against the `excluded` list.
2090    ///
2091    /// This method does **not** consider the `members` list.
2092    fn is_excluded(&self, manifest_path: &Path) -> bool {
2093        let excluded = self
2094            .exclude
2095            .iter()
2096            .any(|ex| manifest_path.starts_with(self.root_dir.join(ex)));
2097
2098        let explicit_member = match self.members {
2099            Some(ref members) => members
2100                .iter()
2101                .any(|mem| manifest_path.starts_with(self.root_dir.join(mem))),
2102            None => false,
2103        };
2104
2105        !explicit_member && excluded
2106    }
2107
2108    /// Checks if the path is explicitly listed as a workspace member.
2109    ///
2110    /// Returns `true` ONLY if:
2111    /// - The path is the workspace root manifest itself, or
2112    /// - The path matches one of the explicit `members` patterns
2113    ///
2114    /// NOTE: This does NOT check for implicit path dependency membership.
2115    /// A `false` return does NOT mean the package is definitely not a member -
2116    /// it could still be a member via path dependencies. Callers should fallback
2117    /// to full workspace loading when this returns `false`.
2118    fn is_explicitly_listed_member(&self, manifest_path: &Path) -> bool {
2119        let root_manifest = self.root_dir.join("Cargo.toml");
2120        if manifest_path == root_manifest {
2121            return true;
2122        }
2123        match self.members {
2124            Some(ref members) => {
2125                // Use members_paths to properly expand glob patterns
2126                let Ok(expanded_members) = self.members_paths(members) else {
2127                    return false;
2128                };
2129                // Normalize the manifest path for comparison
2130                let normalized_manifest = paths::normalize_path(manifest_path);
2131                expanded_members.iter().any(|(member_path, _)| {
2132                    // Normalize the member path as glob expansion may leave ".." components
2133                    let normalized_member = paths::normalize_path(member_path);
2134                    // Compare the manifest's parent directory with the member path exactly
2135                    // instead of using starts_with to avoid matching nested directories
2136                    normalized_manifest.parent() == Some(normalized_member.as_path())
2137                })
2138            }
2139            None => false,
2140        }
2141    }
2142
2143    fn has_members_list(&self) -> bool {
2144        self.members.is_some()
2145    }
2146
2147    /// Returns true if this workspace config has default-members defined.
2148    fn has_default_members(&self) -> bool {
2149        self.default_members.is_some()
2150    }
2151
2152    /// Returns expanded paths along with the glob that they were expanded from.
2153    /// The glob is `None` if the path matched exactly.
2154    #[tracing::instrument(skip_all)]
2155    fn members_paths<'g>(
2156        &self,
2157        globs: &'g [String],
2158    ) -> CargoResult<Vec<(PathBuf, Option<&'g str>)>> {
2159        let mut expanded_list = Vec::new();
2160
2161        for glob in globs {
2162            let pathbuf = self.root_dir.join(glob);
2163            let expanded_paths = Self::expand_member_path(&pathbuf)?;
2164
2165            // If glob does not find any valid paths, then put the original
2166            // path in the expanded list to maintain backwards compatibility.
2167            if expanded_paths.is_empty() {
2168                expanded_list.push((pathbuf, None));
2169            } else {
2170                let used_glob_pattern = expanded_paths.len() > 1 || expanded_paths[0] != pathbuf;
2171                let glob = used_glob_pattern.then_some(glob.as_str());
2172
2173                // Some OS can create system support files anywhere.
2174                // (e.g. macOS creates `.DS_Store` file if you visit a directory using Finder.)
2175                // Such files can be reported as a member path unexpectedly.
2176                // Check and filter out non-directory paths to prevent pushing such accidental unwanted path
2177                // as a member.
2178                for expanded_path in expanded_paths {
2179                    if expanded_path.is_dir() {
2180                        expanded_list.push((expanded_path, glob));
2181                    }
2182                }
2183            }
2184        }
2185
2186        Ok(expanded_list)
2187    }
2188
2189    fn expand_member_path(path: &Path) -> CargoResult<Vec<PathBuf>> {
2190        let Some(path) = path.to_str() else {
2191            return Ok(Vec::new());
2192        };
2193        let res = glob(path).with_context(|| format!("could not parse pattern `{}`", &path))?;
2194        let res = res
2195            .map(|p| p.with_context(|| format!("unable to match path to pattern `{}`", &path)))
2196            .collect::<Result<Vec<_>, _>>()?;
2197        Ok(res)
2198    }
2199
2200    pub fn inheritable(&self) -> &InheritableFields {
2201        &self.inheritable_fields
2202    }
2203}
2204
2205pub fn resolve_relative_path(
2206    label: &str,
2207    old_root: &Path,
2208    new_root: &Path,
2209    rel_path: &str,
2210) -> CargoResult<String> {
2211    let joined_path = normalize_path(&old_root.join(rel_path));
2212    match diff_paths(joined_path, new_root) {
2213        None => Err(anyhow!(
2214            "`{}` was defined in {} but could not be resolved with {}",
2215            label,
2216            old_root.display(),
2217            new_root.display()
2218        )),
2219        Some(path) => Ok(path
2220            .to_str()
2221            .ok_or_else(|| {
2222                anyhow!(
2223                    "`{}` resolved to non-UTF value (`{}`)",
2224                    label,
2225                    path.display()
2226                )
2227            })?
2228            .to_owned()),
2229    }
2230}
2231
2232/// Finds the path of the root of the workspace.
2233pub fn find_workspace_root(
2234    manifest_path: &Path,
2235    gctx: &GlobalContext,
2236) -> CargoResult<Option<PathBuf>> {
2237    find_workspace_root_with_loader(manifest_path, gctx, |self_path| {
2238        let source_id = SourceId::for_manifest_path(self_path)?;
2239        let manifest = read_manifest(self_path, source_id, gctx)?;
2240        Ok(manifest
2241            .workspace_config()
2242            .get_ws_root(self_path, manifest_path))
2243    })
2244}
2245
2246/// Finds the workspace root for a manifest, with minimal verification.
2247///
2248/// This is similar to `find_workspace_root`, but additionally verifies that the
2249/// package and workspace agree on each other:
2250/// - If the package has an explicit `package.workspace` pointer, it is trusted
2251/// - Otherwise, the workspace must include the package in its `members` list
2252pub fn find_workspace_root_with_membership_check(
2253    manifest_path: &Path,
2254    gctx: &GlobalContext,
2255) -> CargoResult<Option<PathBuf>> {
2256    let source_id = SourceId::for_manifest_path(manifest_path)?;
2257    let current_manifest = read_manifest(manifest_path, source_id, gctx)?;
2258
2259    match current_manifest.workspace_config() {
2260        WorkspaceConfig::Root(root_config) => {
2261            // This manifest is a workspace root itself
2262            // If default-members are defined, fall back to full loading for proper validation
2263            if root_config.has_default_members() {
2264                Ok(None)
2265            } else {
2266                Ok(Some(manifest_path.to_path_buf()))
2267            }
2268        }
2269        WorkspaceConfig::Member {
2270            root: Some(path_to_root),
2271        } => {
2272            // Has explicit `package.workspace` pointer - verify the workspace agrees
2273            let ws_manifest_path = read_root_pointer(manifest_path, path_to_root);
2274            let ws_source_id = SourceId::for_manifest_path(&ws_manifest_path)?;
2275            let ws_manifest = read_manifest(&ws_manifest_path, ws_source_id, gctx)?;
2276
2277            // Verify the workspace includes this package in its members
2278            if let WorkspaceConfig::Root(ref root_config) = *ws_manifest.workspace_config() {
2279                if root_config.is_explicitly_listed_member(manifest_path)
2280                    && !root_config.is_excluded(manifest_path)
2281                {
2282                    return Ok(Some(ws_manifest_path));
2283                }
2284            }
2285            // Workspace doesn't agree with the pointer - not a valid workspace root
2286            Ok(None)
2287        }
2288        WorkspaceConfig::Member { root: None } => {
2289            // No explicit pointer, walk up with membership validation
2290            find_workspace_root_with_loader(manifest_path, gctx, |candidate_manifest_path| {
2291                let source_id = SourceId::for_manifest_path(candidate_manifest_path)?;
2292                let manifest = read_manifest(candidate_manifest_path, source_id, gctx)?;
2293                if let WorkspaceConfig::Root(ref root_config) = *manifest.workspace_config() {
2294                    if root_config.is_explicitly_listed_member(manifest_path)
2295                        && !root_config.is_excluded(manifest_path)
2296                    {
2297                        return Ok(Some(candidate_manifest_path.to_path_buf()));
2298                    }
2299                }
2300                Ok(None)
2301            })
2302        }
2303    }
2304}
2305
2306/// Finds the path of the root of the workspace.
2307///
2308/// This uses a callback to determine if the given path tells us what the
2309/// workspace root is.
2310fn find_workspace_root_with_loader(
2311    manifest_path: &Path,
2312    gctx: &GlobalContext,
2313    mut loader: impl FnMut(&Path) -> CargoResult<Option<PathBuf>>,
2314) -> CargoResult<Option<PathBuf>> {
2315    // Check if there are any workspace roots that have already been found that would work
2316    {
2317        let roots = gctx.ws_roots();
2318        // Iterate through the manifests parent directories until we find a workspace
2319        // root. Note we skip the first item since that is just the path itself
2320        for current in manifest_path.ancestors().skip(1) {
2321            if let Some(ws_config) = roots.get(current) {
2322                if !ws_config.is_excluded(manifest_path) {
2323                    // Add `Cargo.toml` since ws_root is the root and not the file
2324                    return Ok(Some(current.join("Cargo.toml")));
2325                }
2326            }
2327        }
2328    }
2329
2330    for ances_manifest_path in find_root_iter(manifest_path, gctx) {
2331        debug!("find_root - trying {}", ances_manifest_path.display());
2332        if let Some(ws_root_path) = loader(&ances_manifest_path)? {
2333            return Ok(Some(ws_root_path));
2334        }
2335    }
2336    Ok(None)
2337}
2338
2339fn read_root_pointer(member_manifest: &Path, root_link: &str) -> PathBuf {
2340    let path = member_manifest
2341        .parent()
2342        .unwrap()
2343        .join(root_link)
2344        .join("Cargo.toml");
2345    debug!("find_root - pointer {}", path.display());
2346    paths::normalize_path(&path)
2347}
2348
2349fn find_root_iter<'a>(
2350    manifest_path: &'a Path,
2351    gctx: &'a GlobalContext,
2352) -> impl Iterator<Item = PathBuf> + 'a {
2353    LookBehind::new(paths::ancestors(manifest_path, None).skip(2))
2354        .take_while(|path| !path.curr.ends_with("target/package"))
2355        // Don't walk across `CARGO_HOME` when we're looking for the
2356        // workspace root. Sometimes a package will be organized with
2357        // `CARGO_HOME` pointing inside of the workspace root or in the
2358        // current package, but we don't want to mistakenly try to put
2359        // crates.io crates into the workspace by accident.
2360        .take_while(|path| {
2361            if let Some(last) = path.last {
2362                gctx.home() != last
2363            } else {
2364                true
2365            }
2366        })
2367        .map(|path| path.curr.join("Cargo.toml"))
2368        .filter(|ances_manifest_path| ances_manifest_path.exists())
2369}
2370
2371struct LookBehindWindow<'a, T: ?Sized> {
2372    curr: &'a T,
2373    last: Option<&'a T>,
2374}
2375
2376struct LookBehind<'a, T: ?Sized, K: Iterator<Item = &'a T>> {
2377    iter: K,
2378    last: Option<&'a T>,
2379}
2380
2381impl<'a, T: ?Sized, K: Iterator<Item = &'a T>> LookBehind<'a, T, K> {
2382    fn new(items: K) -> Self {
2383        Self {
2384            iter: items,
2385            last: None,
2386        }
2387    }
2388}
2389
2390impl<'a, T: ?Sized, K: Iterator<Item = &'a T>> Iterator for LookBehind<'a, T, K> {
2391    type Item = LookBehindWindow<'a, T>;
2392
2393    fn next(&mut self) -> Option<Self::Item> {
2394        match self.iter.next() {
2395            None => None,
2396            Some(next) => {
2397                let last = self.last;
2398                self.last = Some(next);
2399                Some(LookBehindWindow { curr: next, last })
2400            }
2401        }
2402    }
2403}