Skip to main content

cargo/core/
workspace.rs

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