#
cargo/core/
workspace.rs

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