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::unused_build_dependencies_no_build_rs;
38use crate::lints::rules::unused_workspace_dependencies;
39use crate::lints::rules::unused_workspace_package_fields;
40use crate::ops;
41use crate::ops::lockfile::LOCKFILE_NAME;
42use crate::sources::{CRATES_IO_INDEX, CRATES_IO_REGISTRY, PathSource, SourceConfigMap};
43use crate::util::context;
44use crate::util::context::{FeatureUnification, Value};
45use crate::util::edit_distance;
46use crate::util::errors::{CargoResult, ManifestError};
47use crate::util::interning::InternedString;
48use crate::util::toml::{InheritableFields, read_manifest};
49use crate::util::{
50 Filesystem, GlobalContext, IntoUrl, context::CargoResolverConfig, context::ConfigRelativePath,
51 context::IncompatibleRustVersions,
52};
53
54use cargo_util::paths;
55use cargo_util::paths::normalize_path;
56use cargo_util_schemas::manifest;
57use cargo_util_schemas::manifest::RustVersion;
58use cargo_util_schemas::manifest::{TomlDependency, TomlManifest, TomlProfiles};
59use pathdiff::diff_paths;
60
61#[derive(Debug)]
67pub struct Workspace<'gctx> {
68 gctx: &'gctx GlobalContext,
70
71 current_manifest: PathBuf,
75
76 packages: Packages<'gctx>,
79
80 root_manifest: Option<PathBuf>,
85
86 target_dir: Option<Filesystem>,
89
90 build_dir: Option<Filesystem>,
93
94 members: Vec<PathBuf>,
98 member_ids: HashSet<PackageId>,
100
101 default_members: Vec<PathBuf>,
112
113 is_ephemeral: bool,
116
117 require_optional_deps: bool,
122
123 loaded_packages: RefCell<HashMap<PathBuf, Package>>,
126
127 ignore_lock: bool,
130
131 requested_lockfile_path: Option<PathBuf>,
133
134 resolve_behavior: ResolveBehavior,
136 resolve_honors_rust_version: bool,
140 resolve_feature_unification: FeatureUnification,
142 resolve_publish_time: Option<jiff::Timestamp>,
144 custom_metadata: Option<toml::Value>,
146
147 local_overlays: HashMap<SourceId, PathBuf>,
149}
150
151#[derive(Debug)]
154struct Packages<'gctx> {
155 gctx: &'gctx GlobalContext,
156 packages: HashMap<PathBuf, MaybePackage>,
157}
158
159#[derive(Debug)]
160pub enum MaybePackage {
161 Package(Package),
162 Virtual(VirtualManifest),
163}
164
165#[derive(Debug, Clone)]
167pub enum WorkspaceConfig {
168 Root(WorkspaceRootConfig),
171
172 Member { root: Option<String> },
175}
176
177impl WorkspaceConfig {
178 pub fn inheritable(&self) -> Option<&InheritableFields> {
179 match self {
180 WorkspaceConfig::Root(root) => Some(&root.inheritable_fields),
181 WorkspaceConfig::Member { .. } => None,
182 }
183 }
184
185 fn get_ws_root(&self, self_path: &Path, look_from: &Path) -> Option<PathBuf> {
193 match self {
194 WorkspaceConfig::Root(ances_root_config) => {
195 debug!("find_root - found a root checking exclusion");
196 if !ances_root_config.is_excluded(look_from) {
197 debug!("find_root - found!");
198 Some(self_path.to_owned())
199 } else {
200 None
201 }
202 }
203 WorkspaceConfig::Member {
204 root: Some(path_to_root),
205 } => {
206 debug!("find_root - found pointer");
207 Some(read_root_pointer(self_path, path_to_root))
208 }
209 WorkspaceConfig::Member { .. } => None,
210 }
211 }
212}
213
214#[derive(Debug, Clone)]
219pub struct WorkspaceRootConfig {
220 root_dir: PathBuf,
221 members: Option<Vec<String>>,
222 default_members: Option<Vec<String>>,
223 exclude: Vec<String>,
224 inheritable_fields: InheritableFields,
225 custom_metadata: Option<toml::Value>,
226}
227
228impl<'gctx> Workspace<'gctx> {
229 pub fn new(manifest_path: &Path, gctx: &'gctx GlobalContext) -> CargoResult<Workspace<'gctx>> {
236 let mut ws = Workspace::new_default(manifest_path.to_path_buf(), gctx);
237
238 if manifest_path.is_relative() {
239 bail!(
240 "manifest_path:{:?} is not an absolute path. Please provide an absolute path.",
241 manifest_path
242 )
243 } else {
244 ws.root_manifest = ws.find_root(manifest_path)?;
245 }
246
247 ws.target_dir = gctx.target_dir()?;
248 ws.build_dir = gctx.build_dir(ws.root_manifest())?;
249
250 ws.custom_metadata = ws
251 .load_workspace_config()?
252 .and_then(|cfg| cfg.custom_metadata);
253 ws.find_members()?;
254 ws.set_resolve_behavior()?;
255 ws.validate()?;
256 Ok(ws)
257 }
258
259 fn new_default(current_manifest: PathBuf, gctx: &'gctx GlobalContext) -> Workspace<'gctx> {
260 Workspace {
261 gctx,
262 current_manifest,
263 packages: Packages {
264 gctx,
265 packages: HashMap::new(),
266 },
267 root_manifest: None,
268 target_dir: None,
269 build_dir: None,
270 members: Vec::new(),
271 member_ids: HashSet::new(),
272 default_members: Vec::new(),
273 is_ephemeral: false,
274 require_optional_deps: true,
275 loaded_packages: RefCell::new(HashMap::new()),
276 ignore_lock: false,
277 requested_lockfile_path: None,
278 resolve_behavior: ResolveBehavior::V1,
279 resolve_honors_rust_version: false,
280 resolve_feature_unification: FeatureUnification::Selected,
281 resolve_publish_time: None,
282 custom_metadata: None,
283 local_overlays: HashMap::new(),
284 }
285 }
286
287 pub fn ephemeral(
297 package: Package,
298 gctx: &'gctx GlobalContext,
299 target_dir: Option<Filesystem>,
300 require_optional_deps: bool,
301 ) -> CargoResult<Workspace<'gctx>> {
302 let mut ws = Workspace::new_default(package.manifest_path().to_path_buf(), gctx);
303 ws.is_ephemeral = true;
304 ws.require_optional_deps = require_optional_deps;
305 let id = package.package_id();
306 let package = MaybePackage::Package(package);
307 ws.packages
308 .packages
309 .insert(ws.current_manifest.clone(), package);
310 ws.target_dir = if let Some(dir) = target_dir {
311 Some(dir)
312 } else {
313 ws.gctx.target_dir()?
314 };
315 ws.build_dir = ws.target_dir.clone();
316 ws.members.push(ws.current_manifest.clone());
317 ws.member_ids.insert(id);
318 ws.default_members.push(ws.current_manifest.clone());
319 ws.set_resolve_behavior()?;
320 Ok(ws)
321 }
322
323 pub fn reload(&self, gctx: &'gctx GlobalContext) -> CargoResult<Workspace<'gctx>> {
328 let mut ws = Workspace::new(&self.current_manifest, gctx)?;
329 ws.set_resolve_honors_rust_version(Some(self.resolve_honors_rust_version));
330 ws.set_resolve_feature_unification(self.resolve_feature_unification);
331 ws.set_requested_lockfile_path(self.requested_lockfile_path.clone());
332 Ok(ws)
333 }
334
335 fn set_resolve_behavior(&mut self) -> CargoResult<()> {
336 self.resolve_behavior = match self.root_maybe() {
341 MaybePackage::Package(p) => p
342 .manifest()
343 .resolve_behavior()
344 .unwrap_or_else(|| p.manifest().edition().default_resolve_behavior()),
345 MaybePackage::Virtual(vm) => vm.resolve_behavior().unwrap_or(ResolveBehavior::V1),
346 };
347
348 match self.resolve_behavior() {
349 ResolveBehavior::V1 | ResolveBehavior::V2 => {}
350 ResolveBehavior::V3 => {
351 if self.resolve_behavior == ResolveBehavior::V3 {
352 self.resolve_honors_rust_version = true;
353 }
354 }
355 }
356 let config = self.gctx().get::<CargoResolverConfig>("resolver")?;
357 if let Some(incompatible_rust_versions) = config.incompatible_rust_versions {
358 self.resolve_honors_rust_version =
359 incompatible_rust_versions == IncompatibleRustVersions::Fallback;
360 }
361 if self.gctx().cli_unstable().feature_unification {
362 self.resolve_feature_unification = config
363 .feature_unification
364 .unwrap_or(FeatureUnification::Selected);
365 } else if config.feature_unification.is_some() {
366 self.gctx()
367 .shell()
368 .warn("ignoring `resolver.feature-unification` without `-Zfeature-unification`")?;
369 };
370
371 if let Some(lockfile_path) = config.lockfile_path {
372 if self.gctx().cli_unstable().lockfile_path {
373 let replacements: [(&str, &str); 0] = [];
375 let path = lockfile_path
376 .resolve_templated_path(self.gctx(), replacements)
377 .map_err(|e| match e {
378 context::ResolveTemplateError::UnexpectedVariable {
379 variable,
380 raw_template,
381 } => {
382 anyhow!(
383 "unexpected variable `{variable}` in resolver.lockfile-path `{raw_template}`"
384 )
385 }
386 context::ResolveTemplateError::UnexpectedBracket { bracket_type, raw_template } => {
387 let (btype, literal) = match bracket_type {
388 context::BracketType::Opening => ("opening", "{"),
389 context::BracketType::Closing => ("closing", "}"),
390 };
391
392 anyhow!(
393 "unexpected {btype} bracket `{literal}` in build.build-dir path `{raw_template}`"
394 )
395 }
396 })?;
397 if !path.ends_with(LOCKFILE_NAME) {
398 bail!("the `resolver.lockfile-path` must be a path to a {LOCKFILE_NAME} file");
399 }
400 if path.is_dir() {
401 bail!(
402 "`resolver.lockfile-path` `{}` is a directory but expected a file",
403 path.display()
404 );
405 }
406 self.requested_lockfile_path = Some(path);
407 } else {
408 self.gctx().shell().warn(
409 "ignoring `resolver.lockfile-path`, pass `-Zlockfile-path` to enable it",
410 )?;
411 }
412 }
413
414 Ok(())
415 }
416
417 pub fn current(&self) -> CargoResult<&Package> {
423 let pkg = self.current_opt().ok_or_else(|| {
424 anyhow::format_err!(
425 "manifest path `{}` is a virtual manifest, but this \
426 command requires running against an actual package in \
427 this workspace",
428 self.current_manifest.display()
429 )
430 })?;
431 Ok(pkg)
432 }
433
434 pub fn current_mut(&mut self) -> CargoResult<&mut Package> {
435 let cm = self.current_manifest.clone();
436 let pkg = self.current_opt_mut().ok_or_else(|| {
437 anyhow::format_err!(
438 "manifest path `{}` is a virtual manifest, but this \
439 command requires running against an actual package in \
440 this workspace",
441 cm.display()
442 )
443 })?;
444 Ok(pkg)
445 }
446
447 pub fn current_opt(&self) -> Option<&Package> {
448 match *self.packages.get(&self.current_manifest) {
449 MaybePackage::Package(ref p) => Some(p),
450 MaybePackage::Virtual(..) => None,
451 }
452 }
453
454 pub fn current_opt_mut(&mut self) -> Option<&mut Package> {
455 match *self.packages.get_mut(&self.current_manifest) {
456 MaybePackage::Package(ref mut p) => Some(p),
457 MaybePackage::Virtual(..) => None,
458 }
459 }
460
461 pub fn is_virtual(&self) -> bool {
462 match *self.packages.get(&self.current_manifest) {
463 MaybePackage::Package(..) => false,
464 MaybePackage::Virtual(..) => true,
465 }
466 }
467
468 pub fn gctx(&self) -> &'gctx GlobalContext {
470 self.gctx
471 }
472
473 pub fn profiles(&self) -> Option<&TomlProfiles> {
474 self.root_maybe().profiles()
475 }
476
477 pub fn root(&self) -> &Path {
482 self.root_manifest().parent().unwrap()
483 }
484
485 pub fn root_manifest(&self) -> &Path {
488 self.root_manifest
489 .as_ref()
490 .unwrap_or(&self.current_manifest)
491 }
492
493 pub fn root_maybe(&self) -> &MaybePackage {
495 self.packages.get(self.root_manifest())
496 }
497
498 pub fn target_dir(&self) -> Filesystem {
499 self.target_dir
500 .clone()
501 .unwrap_or_else(|| self.default_target_dir())
502 }
503
504 pub fn build_dir(&self) -> Filesystem {
505 self.build_dir
506 .clone()
507 .or_else(|| self.target_dir.clone())
508 .unwrap_or_else(|| self.default_build_dir())
509 }
510
511 fn default_target_dir(&self) -> Filesystem {
512 if self.root_maybe().is_embedded() {
513 self.build_dir().join("target")
514 } else {
515 Filesystem::new(self.root().join("target"))
516 }
517 }
518
519 fn default_build_dir(&self) -> Filesystem {
520 if self.root_maybe().is_embedded() {
521 let default = ConfigRelativePath::new(
522 "{cargo-cache-home}/build/{workspace-path-hash}"
523 .to_owned()
524 .into(),
525 );
526 self.gctx()
527 .custom_build_dir(&default, self.root_manifest())
528 .expect("template is correct")
529 } else {
530 self.default_target_dir()
531 }
532 }
533
534 pub fn root_replace(&self) -> &[(PackageIdSpec, Dependency)] {
538 match self.root_maybe() {
539 MaybePackage::Package(p) => p.manifest().replace(),
540 MaybePackage::Virtual(vm) => vm.replace(),
541 }
542 }
543
544 fn config_patch(&self) -> CargoResult<HashMap<Url, Vec<Patch>>> {
545 let config_patch: Option<
546 BTreeMap<String, BTreeMap<String, Value<TomlDependency<ConfigRelativePath>>>>,
547 > = self.gctx.get("patch")?;
548
549 let source = SourceId::for_manifest_path(self.root_manifest())?;
550
551 let mut warnings = Vec::new();
552
553 let mut patch = HashMap::new();
554 for (url, deps) in config_patch.into_iter().flatten() {
555 let url = match &url[..] {
556 CRATES_IO_REGISTRY => CRATES_IO_INDEX.parse().unwrap(),
557 url => self
558 .gctx
559 .get_registry_index(url)
560 .or_else(|_| url.into_url())
561 .with_context(|| {
562 format!("[patch] entry `{}` should be a URL or registry name", url)
563 })?,
564 };
565 patch.insert(
566 url,
567 deps.iter()
568 .map(|(name, dependency_cv)| {
569 crate::util::toml::config_patch_to_dependency(
570 &dependency_cv.val,
571 name,
572 source,
573 self.gctx,
574 &mut warnings,
575 )
576 .map(|dep| Patch {
577 dep,
578 loc: PatchLocation::Config(dependency_cv.definition.clone()),
579 })
580 })
581 .collect::<CargoResult<Vec<_>>>()?,
582 );
583 }
584
585 for message in warnings {
586 self.gctx
587 .shell()
588 .warn(format!("[patch] in cargo config: {}", message))?
589 }
590
591 Ok(patch)
592 }
593
594 pub fn root_patch(&self) -> CargoResult<HashMap<Url, Vec<Patch>>> {
598 let from_manifest = match self.root_maybe() {
599 MaybePackage::Package(p) => p.manifest().patch(),
600 MaybePackage::Virtual(vm) => vm.patch(),
601 };
602
603 let from_config = self.config_patch()?;
604 if from_config.is_empty() {
605 return Ok(from_manifest.clone());
606 }
607 if from_manifest.is_empty() {
608 return Ok(from_config);
609 }
610
611 let mut combined = from_config;
614 for (url, deps_from_manifest) in from_manifest {
615 if let Some(deps_from_config) = combined.get_mut(url) {
616 let mut from_manifest_pruned = deps_from_manifest.clone();
619 for dep_from_config in &mut *deps_from_config {
620 if let Some(i) = from_manifest_pruned.iter().position(|dep_from_manifest| {
621 dep_from_config.dep.name_in_toml() == dep_from_manifest.dep.name_in_toml()
623 }) {
624 from_manifest_pruned.swap_remove(i);
625 }
626 }
627 deps_from_config.extend(from_manifest_pruned);
629 } else {
630 combined.insert(url.clone(), deps_from_manifest.clone());
631 }
632 }
633 Ok(combined)
634 }
635
636 pub fn members(&self) -> impl Iterator<Item = &Package> {
638 let packages = &self.packages;
639 self.members
640 .iter()
641 .filter_map(move |path| match packages.get(path) {
642 MaybePackage::Package(p) => Some(p),
643 _ => None,
644 })
645 }
646
647 pub fn members_mut(&mut self) -> impl Iterator<Item = &mut Package> {
649 let packages = &mut self.packages.packages;
650 let members: HashSet<_> = self.members.iter().map(|path| path).collect();
651
652 packages.iter_mut().filter_map(move |(path, package)| {
653 if members.contains(path) {
654 if let MaybePackage::Package(p) = package {
655 return Some(p);
656 }
657 }
658
659 None
660 })
661 }
662
663 pub fn default_members<'a>(&'a self) -> impl Iterator<Item = &'a Package> {
665 let packages = &self.packages;
666 self.default_members
667 .iter()
668 .filter_map(move |path| match packages.get(path) {
669 MaybePackage::Package(p) => Some(p),
670 _ => None,
671 })
672 }
673
674 pub fn default_members_mut(&mut self) -> impl Iterator<Item = &mut Package> {
676 let packages = &mut self.packages.packages;
677 let members: HashSet<_> = self
678 .default_members
679 .iter()
680 .map(|path| path.parent().unwrap().to_owned())
681 .collect();
682
683 packages.iter_mut().filter_map(move |(path, package)| {
684 if members.contains(path) {
685 if let MaybePackage::Package(p) = package {
686 return Some(p);
687 }
688 }
689
690 None
691 })
692 }
693
694 pub fn is_member(&self, pkg: &Package) -> bool {
696 self.member_ids.contains(&pkg.package_id())
697 }
698
699 pub fn is_member_id(&self, package_id: PackageId) -> bool {
701 self.member_ids.contains(&package_id)
702 }
703
704 pub fn is_ephemeral(&self) -> bool {
705 self.is_ephemeral
706 }
707
708 pub fn require_optional_deps(&self) -> bool {
709 self.require_optional_deps
710 }
711
712 pub fn set_require_optional_deps(
713 &mut self,
714 require_optional_deps: bool,
715 ) -> &mut Workspace<'gctx> {
716 self.require_optional_deps = require_optional_deps;
717 self
718 }
719
720 pub fn ignore_lock(&self) -> bool {
721 self.ignore_lock
722 }
723
724 pub fn set_ignore_lock(&mut self, ignore_lock: bool) -> &mut Workspace<'gctx> {
725 self.ignore_lock = ignore_lock;
726 self
727 }
728
729 pub fn lock_root(&self) -> Filesystem {
731 if let Some(requested) = self.requested_lockfile_path.as_ref() {
732 return Filesystem::new(
733 requested
734 .parent()
735 .expect("Lockfile path can't be root")
736 .to_owned(),
737 );
738 }
739 self.default_lock_root()
740 }
741
742 fn default_lock_root(&self) -> Filesystem {
743 if self.root_maybe().is_embedded() {
744 let workspace_manifest_path = self.root_manifest();
747 let real_path = std::fs::canonicalize(workspace_manifest_path)
748 .unwrap_or_else(|_err| workspace_manifest_path.to_owned());
749 let hash = crate::util::hex::short_hash(&real_path);
750 self.build_dir().join(hash)
751 } else {
752 Filesystem::new(self.root().to_owned())
753 }
754 }
755
756 pub fn set_requested_lockfile_path(&mut self, path: Option<PathBuf>) {
758 self.requested_lockfile_path = path;
759 }
760
761 pub fn requested_lockfile_path(&self) -> Option<&Path> {
762 self.requested_lockfile_path.as_deref()
763 }
764
765 pub fn lowest_rust_version(&self) -> Option<&RustVersion> {
768 self.members().filter_map(|pkg| pkg.rust_version()).min()
769 }
770
771 pub fn set_resolve_honors_rust_version(&mut self, honor_rust_version: Option<bool>) {
772 if let Some(honor_rust_version) = honor_rust_version {
773 self.resolve_honors_rust_version = honor_rust_version;
774 }
775 }
776
777 pub fn resolve_honors_rust_version(&self) -> bool {
778 self.resolve_honors_rust_version
779 }
780
781 pub fn set_resolve_feature_unification(&mut self, feature_unification: FeatureUnification) {
782 self.resolve_feature_unification = feature_unification;
783 }
784
785 pub fn resolve_feature_unification(&self) -> FeatureUnification {
786 self.resolve_feature_unification
787 }
788
789 pub fn set_resolve_publish_time(&mut self, publish_time: jiff::Timestamp) {
790 self.resolve_publish_time = Some(publish_time);
791 }
792
793 pub fn resolve_publish_time(&self) -> Option<jiff::Timestamp> {
794 self.resolve_publish_time
795 }
796
797 pub fn custom_metadata(&self) -> Option<&toml::Value> {
798 self.custom_metadata.as_ref()
799 }
800
801 pub fn load_workspace_config(&mut self) -> CargoResult<Option<WorkspaceRootConfig>> {
802 if let Some(root_path) = &self.root_manifest {
805 let root_package = self.packages.load(root_path)?;
806 match root_package.workspace_config() {
807 WorkspaceConfig::Root(root_config) => {
808 return Ok(Some(root_config.clone()));
809 }
810
811 _ => bail!(
812 "root of a workspace inferred but wasn't a root: {}",
813 root_path.display()
814 ),
815 }
816 }
817
818 Ok(None)
819 }
820
821 fn find_root(&mut self, manifest_path: &Path) -> CargoResult<Option<PathBuf>> {
831 let current = self.packages.load(manifest_path)?;
832 match current
833 .workspace_config()
834 .get_ws_root(manifest_path, manifest_path)
835 {
836 Some(root_path) => {
837 debug!("find_root - is root {}", manifest_path.display());
838 Ok(Some(root_path))
839 }
840 None => find_workspace_root_with_loader(manifest_path, self.gctx, |self_path| {
841 Ok(self
842 .packages
843 .load(self_path)?
844 .workspace_config()
845 .get_ws_root(self_path, manifest_path))
846 }),
847 }
848 }
849
850 #[tracing::instrument(skip_all)]
858 fn find_members(&mut self) -> CargoResult<()> {
859 let Some(workspace_config) = self.load_workspace_config()? else {
860 debug!("find_members - only me as a member");
861 self.members.push(self.current_manifest.clone());
862 self.default_members.push(self.current_manifest.clone());
863 if let Ok(pkg) = self.current() {
864 let id = pkg.package_id();
865 self.member_ids.insert(id);
866 }
867 return Ok(());
868 };
869
870 let root_manifest_path = self.root_manifest.clone().unwrap();
872
873 let members_paths = workspace_config
874 .members_paths(workspace_config.members.as_deref().unwrap_or_default())?;
875 let default_members_paths = if root_manifest_path == self.current_manifest {
876 if let Some(ref default) = workspace_config.default_members {
877 Some(workspace_config.members_paths(default)?)
878 } else {
879 None
880 }
881 } else {
882 None
883 };
884
885 for (path, glob) in &members_paths {
886 self.find_path_deps(&path.join("Cargo.toml"), &root_manifest_path, false)
887 .with_context(|| {
888 format!(
889 "failed to load manifest for workspace member `{}`\n\
890 referenced{} by workspace at `{}`",
891 path.display(),
892 glob.map(|g| format!(" via `{g}`")).unwrap_or_default(),
893 root_manifest_path.display(),
894 )
895 })?;
896 }
897
898 self.find_path_deps(&root_manifest_path, &root_manifest_path, false)?;
899
900 if let Some(default) = default_members_paths {
901 for (path, default_member_glob) in default {
902 let normalized_path = paths::normalize_path(&path);
903 let manifest_path = normalized_path.join("Cargo.toml");
904 if !self.members.contains(&manifest_path) {
905 let exclude = members_paths.iter().any(|(m, _)| *m == normalized_path)
912 && workspace_config.is_excluded(&normalized_path);
913 if exclude {
914 continue;
915 }
916 bail!(
917 "package `{}` is listed in default-members{} but is not a member\n\
918 for workspace at `{}`.",
919 path.display(),
920 default_member_glob
921 .map(|g| format!(" via `{g}`"))
922 .unwrap_or_default(),
923 root_manifest_path.display(),
924 )
925 }
926 self.default_members.push(manifest_path)
927 }
928 } else if self.is_virtual() {
929 self.default_members = self.members.clone()
930 } else {
931 self.default_members.push(self.current_manifest.clone())
932 }
933
934 Ok(())
935 }
936
937 fn find_path_deps(
938 &mut self,
939 manifest_path: &Path,
940 root_manifest: &Path,
941 is_path_dep: bool,
942 ) -> CargoResult<()> {
943 let manifest_path = paths::normalize_path(manifest_path);
944 if self.members.contains(&manifest_path) {
945 return Ok(());
946 }
947 if is_path_dep && self.root_maybe().is_embedded() {
948 return Ok(());
950 }
951 if is_path_dep
952 && !manifest_path.parent().unwrap().starts_with(self.root())
953 && self.find_root(&manifest_path)? != self.root_manifest
954 {
955 return Ok(());
958 }
959
960 if let WorkspaceConfig::Root(ref root_config) =
961 *self.packages.load(root_manifest)?.workspace_config()
962 {
963 if root_config.is_excluded(&manifest_path) {
964 return Ok(());
965 }
966 }
967
968 debug!("find_path_deps - {}", manifest_path.display());
969 self.members.push(manifest_path.clone());
970
971 let candidates = {
972 let pkg = match *self.packages.load(&manifest_path)? {
973 MaybePackage::Package(ref p) => p,
974 MaybePackage::Virtual(_) => return Ok(()),
975 };
976 self.member_ids.insert(pkg.package_id());
977 pkg.dependencies()
978 .iter()
979 .map(|d| (d.source_id(), d.package_name()))
980 .filter(|(s, _)| s.is_path())
981 .filter_map(|(s, n)| s.url().to_file_path().ok().map(|p| (p, n)))
982 .map(|(p, n)| (p.join("Cargo.toml"), n))
983 .collect::<Vec<_>>()
984 };
985 for (path, name) in candidates {
986 self.find_path_deps(&path, root_manifest, true)
987 .with_context(|| format!("failed to load manifest for dependency `{}`", name))
988 .map_err(|err| ManifestError::new(err, manifest_path.clone()))?;
989 }
990 Ok(())
991 }
992
993 pub fn unstable_features(&self) -> &Features {
995 self.root_maybe().unstable_features()
996 }
997
998 pub fn resolve_behavior(&self) -> ResolveBehavior {
999 self.resolve_behavior
1000 }
1001
1002 pub fn allows_new_cli_feature_behavior(&self) -> bool {
1010 self.is_virtual()
1011 || match self.resolve_behavior() {
1012 ResolveBehavior::V1 => false,
1013 ResolveBehavior::V2 | ResolveBehavior::V3 => true,
1014 }
1015 }
1016
1017 #[tracing::instrument(skip_all)]
1023 fn validate(&mut self) -> CargoResult<()> {
1024 if self.root_manifest.is_none() {
1026 return Ok(());
1027 }
1028
1029 self.validate_unique_names()?;
1030 self.validate_workspace_roots()?;
1031 self.validate_members()?;
1032 self.error_if_manifest_not_in_members()?;
1033 self.validate_manifest()
1034 }
1035
1036 fn validate_unique_names(&self) -> CargoResult<()> {
1037 let mut names = BTreeMap::new();
1038 for member in self.members.iter() {
1039 let package = self.packages.get(member);
1040 let name = match *package {
1041 MaybePackage::Package(ref p) => p.name(),
1042 MaybePackage::Virtual(_) => continue,
1043 };
1044 if let Some(prev) = names.insert(name, member) {
1045 bail!(
1046 "two packages named `{}` in this workspace:\n\
1047 - {}\n\
1048 - {}",
1049 name,
1050 prev.display(),
1051 member.display()
1052 );
1053 }
1054 }
1055 Ok(())
1056 }
1057
1058 fn validate_workspace_roots(&self) -> CargoResult<()> {
1059 let roots: Vec<PathBuf> = self
1060 .members
1061 .iter()
1062 .filter(|&member| {
1063 let config = self.packages.get(member).workspace_config();
1064 matches!(config, WorkspaceConfig::Root(_))
1065 })
1066 .map(|member| member.parent().unwrap().to_path_buf())
1067 .collect();
1068 match roots.len() {
1069 1 => Ok(()),
1070 0 => bail!(
1071 "`package.workspace` configuration points to a crate \
1072 which is not configured with [workspace]: \n\
1073 configuration at: {}\n\
1074 points to: {}",
1075 self.current_manifest.display(),
1076 self.root_manifest.as_ref().unwrap().display()
1077 ),
1078 _ => {
1079 bail!(
1080 "multiple workspace roots found in the same workspace:\n{}",
1081 roots
1082 .iter()
1083 .map(|r| format!(" {}", r.display()))
1084 .collect::<Vec<_>>()
1085 .join("\n")
1086 );
1087 }
1088 }
1089 }
1090
1091 #[tracing::instrument(skip_all)]
1092 fn validate_members(&mut self) -> CargoResult<()> {
1093 for member in self.members.clone() {
1094 let root = self.find_root(&member)?;
1095 if root == self.root_manifest {
1096 continue;
1097 }
1098
1099 match root {
1100 Some(root) => {
1101 bail!(
1102 "package `{}` is a member of the wrong workspace\n\
1103 expected: {}\n\
1104 actual: {}",
1105 member.display(),
1106 self.root_manifest.as_ref().unwrap().display(),
1107 root.display()
1108 );
1109 }
1110 None => {
1111 bail!(
1112 "workspace member `{}` is not hierarchically below \
1113 the workspace root `{}`",
1114 member.display(),
1115 self.root_manifest.as_ref().unwrap().display()
1116 );
1117 }
1118 }
1119 }
1120 Ok(())
1121 }
1122
1123 fn error_if_manifest_not_in_members(&mut self) -> CargoResult<()> {
1124 if self.members.contains(&self.current_manifest) {
1125 return Ok(());
1126 }
1127
1128 let root = self.root_manifest.as_ref().unwrap();
1129 let root_dir = root.parent().unwrap();
1130 let current_dir = self.current_manifest.parent().unwrap();
1131 let root_pkg = self.packages.get(root);
1132
1133 let current_dir = paths::normalize_path(current_dir);
1139 let root_dir = paths::normalize_path(root_dir);
1140 let members_msg = match pathdiff::diff_paths(¤t_dir, &root_dir) {
1141 Some(rel) => format!(
1142 "this may be fixable by adding `{}` to the \
1143 `workspace.members` array of the manifest \
1144 located at: {}",
1145 rel.display(),
1146 root.display()
1147 ),
1148 None => format!(
1149 "this may be fixable by adding a member to \
1150 the `workspace.members` array of the \
1151 manifest located at: {}",
1152 root.display()
1153 ),
1154 };
1155 let extra = match *root_pkg {
1156 MaybePackage::Virtual(_) => members_msg,
1157 MaybePackage::Package(ref p) => {
1158 let has_members_list = match *p.manifest().workspace_config() {
1159 WorkspaceConfig::Root(ref root_config) => root_config.has_members_list(),
1160 WorkspaceConfig::Member { .. } => unreachable!(),
1161 };
1162 if !has_members_list {
1163 format!(
1164 "this may be fixable by ensuring that this \
1165 crate is depended on by the workspace \
1166 root: {}",
1167 root.display()
1168 )
1169 } else {
1170 members_msg
1171 }
1172 }
1173 };
1174 bail!(
1175 "current package believes it's in a workspace when it's not:\n\
1176 current: {}\n\
1177 workspace: {}\n\n{}\n\
1178 Alternatively, to keep it out of the workspace, add the package \
1179 to the `workspace.exclude` array, or add an empty `[workspace]` \
1180 table to the package's manifest.",
1181 self.current_manifest.display(),
1182 root.display(),
1183 extra
1184 );
1185 }
1186
1187 fn validate_manifest(&mut self) -> CargoResult<()> {
1188 if let Some(ref root_manifest) = self.root_manifest {
1189 for pkg in self
1190 .members()
1191 .filter(|p| p.manifest_path() != root_manifest)
1192 {
1193 let manifest = pkg.manifest();
1194 let emit_warning = |what| -> CargoResult<()> {
1195 let msg = format!(
1196 "{} for the non root package will be ignored, \
1197 specify {} at the workspace root:\n\
1198 package: {}\n\
1199 workspace: {}",
1200 what,
1201 what,
1202 pkg.manifest_path().display(),
1203 root_manifest.display(),
1204 );
1205 self.gctx.shell().warn(&msg)
1206 };
1207 if manifest.normalized_toml().has_profiles() {
1208 emit_warning("profiles")?;
1209 }
1210 if !manifest.replace().is_empty() {
1211 emit_warning("replace")?;
1212 }
1213 if !manifest.patch().is_empty() {
1214 emit_warning("patch")?;
1215 }
1216 if let Some(behavior) = manifest.resolve_behavior() {
1217 if behavior != self.resolve_behavior {
1218 emit_warning("resolver")?;
1220 }
1221 }
1222 }
1223 if let MaybePackage::Virtual(vm) = self.root_maybe() {
1224 if vm.resolve_behavior().is_none() {
1225 if let Some(edition) = self
1226 .members()
1227 .filter(|p| p.manifest_path() != root_manifest)
1228 .map(|p| p.manifest().edition())
1229 .filter(|&e| e >= Edition::Edition2021)
1230 .max()
1231 {
1232 let resolver = edition.default_resolve_behavior().to_manifest();
1233 let report = &[Level::WARNING
1234 .primary_title(format!(
1235 "virtual workspace defaulting to `resolver = \"1\"` despite one or more workspace members being on edition {edition} which implies `resolver = \"{resolver}\"`"
1236 ))
1237 .elements([
1238 Level::NOTE.message("to keep the current resolver, specify `workspace.resolver = \"1\"` in the workspace root's manifest"),
1239 Level::NOTE.message(
1240 format!("to use the edition {edition} resolver, specify `workspace.resolver = \"{resolver}\"` in the workspace root's manifest"),
1241 ),
1242 Level::NOTE.message("for more details see https://doc.rust-lang.org/cargo/reference/resolver.html#resolver-versions"),
1243 ])];
1244 self.gctx.shell().print_report(report, false)?;
1245 }
1246 }
1247 }
1248 }
1249 Ok(())
1250 }
1251
1252 pub fn load(&self, manifest_path: &Path) -> CargoResult<Package> {
1253 match self.packages.maybe_get(manifest_path) {
1254 Some(MaybePackage::Package(p)) => return Ok(p.clone()),
1255 Some(&MaybePackage::Virtual(_)) => bail!("cannot load workspace root"),
1256 None => {}
1257 }
1258
1259 let mut loaded = self.loaded_packages.borrow_mut();
1260 if let Some(p) = loaded.get(manifest_path).cloned() {
1261 return Ok(p);
1262 }
1263 let source_id = SourceId::for_manifest_path(manifest_path)?;
1264 let package = ops::read_package(manifest_path, source_id, self.gctx)?;
1265 loaded.insert(manifest_path.to_path_buf(), package.clone());
1266 Ok(package)
1267 }
1268
1269 pub fn preload(&self, registry: &mut PackageRegistry<'gctx>) {
1276 if self.is_ephemeral {
1282 return;
1283 }
1284
1285 for pkg in self.packages.packages.values() {
1286 let pkg = match *pkg {
1287 MaybePackage::Package(ref p) => p.clone(),
1288 MaybePackage::Virtual(_) => continue,
1289 };
1290 let src = PathSource::preload_with(pkg, self.gctx);
1291 registry.add_preloaded(Box::new(src));
1292 }
1293 }
1294
1295 pub fn emit_warnings(&self) -> CargoResult<()> {
1296 let mut first_emitted_error = None;
1297
1298 if let Err(e) = self.emit_ws_lints() {
1299 first_emitted_error = Some(e);
1300 }
1301
1302 for (path, maybe_pkg) in &self.packages.packages {
1303 if let MaybePackage::Package(pkg) = maybe_pkg {
1304 if let Err(e) = self.emit_pkg_lints(pkg, &path)
1305 && first_emitted_error.is_none()
1306 {
1307 first_emitted_error = Some(e);
1308 }
1309 }
1310 let warnings = match maybe_pkg {
1311 MaybePackage::Package(pkg) => pkg.manifest().warnings().warnings(),
1312 MaybePackage::Virtual(vm) => vm.warnings().warnings(),
1313 };
1314 for warning in warnings {
1315 if warning.is_critical {
1316 let err = anyhow::format_err!("{}", warning.message);
1317 let cx =
1318 anyhow::format_err!("failed to parse manifest at `{}`", path.display());
1319 if first_emitted_error.is_none() {
1320 first_emitted_error = Some(err.context(cx));
1321 }
1322 } else {
1323 let msg = if self.root_manifest.is_none() {
1324 warning.message.to_string()
1325 } else {
1326 format!("{}: {}", path.display(), warning.message)
1329 };
1330 self.gctx.shell().warn(msg)?
1331 }
1332 }
1333 }
1334
1335 if let Some(error) = first_emitted_error {
1336 Err(error)
1337 } else {
1338 Ok(())
1339 }
1340 }
1341
1342 pub fn emit_pkg_lints(&self, pkg: &Package, path: &Path) -> CargoResult<()> {
1343 let toml_lints = pkg
1344 .manifest()
1345 .normalized_toml()
1346 .lints
1347 .clone()
1348 .map(|lints| lints.lints)
1349 .unwrap_or(manifest::TomlLints::default());
1350 let cargo_lints = toml_lints
1351 .get("cargo")
1352 .cloned()
1353 .unwrap_or(manifest::TomlToolLints::default());
1354
1355 if self.gctx.cli_unstable().cargo_lints {
1356 let mut verify_error_count = 0;
1357
1358 analyze_cargo_lints_table(
1359 pkg.into(),
1360 &path,
1361 &cargo_lints,
1362 &mut verify_error_count,
1363 self.gctx,
1364 )?;
1365
1366 if verify_error_count > 0 {
1367 let plural = if verify_error_count == 1 { "" } else { "s" };
1368 bail!("encountered {verify_error_count} error{plural} while verifying lints")
1369 }
1370
1371 let mut run_error_count = 0;
1372
1373 check_im_a_teapot(pkg, &path, &cargo_lints, &mut run_error_count, self.gctx)?;
1374 implicit_minimum_version_req_pkg(
1375 pkg,
1376 &path,
1377 &cargo_lints,
1378 &mut run_error_count,
1379 self.gctx,
1380 )?;
1381 non_kebab_case_packages(pkg, &path, &cargo_lints, &mut run_error_count, self.gctx)?;
1382 non_snake_case_packages(pkg, &path, &cargo_lints, &mut run_error_count, self.gctx)?;
1383 non_kebab_case_bins(
1384 self,
1385 pkg,
1386 &path,
1387 &cargo_lints,
1388 &mut run_error_count,
1389 self.gctx,
1390 )?;
1391 non_kebab_case_features(pkg, &path, &cargo_lints, &mut run_error_count, self.gctx)?;
1392 non_snake_case_features(pkg, &path, &cargo_lints, &mut run_error_count, self.gctx)?;
1393 unused_build_dependencies_no_build_rs(
1394 pkg,
1395 &path,
1396 &cargo_lints,
1397 &mut run_error_count,
1398 self.gctx,
1399 )?;
1400 redundant_readme(pkg, &path, &cargo_lints, &mut run_error_count, self.gctx)?;
1401 redundant_homepage(pkg, &path, &cargo_lints, &mut run_error_count, self.gctx)?;
1402 missing_lints_inheritance(
1403 self,
1404 pkg,
1405 &path,
1406 &cargo_lints,
1407 &mut run_error_count,
1408 self.gctx,
1409 )?;
1410
1411 if run_error_count > 0 {
1412 let plural = if run_error_count == 1 { "" } else { "s" };
1413 bail!("encountered {run_error_count} error{plural} while running lints")
1414 }
1415 }
1416
1417 Ok(())
1418 }
1419
1420 pub fn emit_ws_lints(&self) -> CargoResult<()> {
1421 let mut run_error_count = 0;
1422
1423 let cargo_lints = match self.root_maybe() {
1424 MaybePackage::Package(pkg) => {
1425 let toml = pkg.manifest().normalized_toml();
1426 if let Some(ws) = &toml.workspace {
1427 ws.lints.as_ref()
1428 } else {
1429 toml.lints.as_ref().map(|l| &l.lints)
1430 }
1431 }
1432 MaybePackage::Virtual(vm) => vm
1433 .normalized_toml()
1434 .workspace
1435 .as_ref()
1436 .unwrap()
1437 .lints
1438 .as_ref(),
1439 }
1440 .and_then(|t| t.get("cargo"))
1441 .cloned()
1442 .unwrap_or(manifest::TomlToolLints::default());
1443
1444 if self.gctx.cli_unstable().cargo_lints {
1445 let mut verify_error_count = 0;
1446
1447 analyze_cargo_lints_table(
1448 (self, self.root_maybe()).into(),
1449 self.root_manifest(),
1450 &cargo_lints,
1451 &mut verify_error_count,
1452 self.gctx,
1453 )?;
1454
1455 if verify_error_count > 0 {
1456 let plural = if verify_error_count == 1 { "" } else { "s" };
1457 bail!("encountered {verify_error_count} error{plural} while verifying lints")
1458 }
1459
1460 unused_workspace_package_fields(
1461 self,
1462 self.root_maybe(),
1463 self.root_manifest(),
1464 &cargo_lints,
1465 &mut run_error_count,
1466 self.gctx,
1467 )?;
1468 unused_workspace_dependencies(
1469 self,
1470 self.root_maybe(),
1471 self.root_manifest(),
1472 &cargo_lints,
1473 &mut run_error_count,
1474 self.gctx,
1475 )?;
1476 implicit_minimum_version_req_ws(
1477 self,
1478 self.root_maybe(),
1479 self.root_manifest(),
1480 &cargo_lints,
1481 &mut run_error_count,
1482 self.gctx,
1483 )?;
1484 }
1485
1486 if self.gctx.cli_unstable().profile_hint_mostly_unused {
1490 blanket_hint_mostly_unused(
1491 self,
1492 self.root_maybe(),
1493 self.root_manifest(),
1494 &cargo_lints,
1495 &mut run_error_count,
1496 self.gctx,
1497 )?;
1498 }
1499
1500 if run_error_count > 0 {
1501 let plural = if run_error_count == 1 { "" } else { "s" };
1502 bail!("encountered {run_error_count} error{plural} while running lints")
1503 } else {
1504 Ok(())
1505 }
1506 }
1507
1508 pub fn set_target_dir(&mut self, target_dir: Filesystem) {
1509 self.target_dir = Some(target_dir);
1510 }
1511
1512 pub fn members_with_features(
1520 &self,
1521 specs: &[PackageIdSpec],
1522 cli_features: &CliFeatures,
1523 ) -> CargoResult<Vec<(&Package, CliFeatures)>> {
1524 assert!(
1525 !specs.is_empty() || cli_features.all_features,
1526 "no specs requires all_features"
1527 );
1528 if specs.is_empty() {
1529 return Ok(self
1532 .members()
1533 .map(|m| (m, CliFeatures::new_all(true)))
1534 .collect());
1535 }
1536 if self.allows_new_cli_feature_behavior() {
1537 self.members_with_features_new(specs, cli_features)
1538 } else {
1539 Ok(self.members_with_features_old(specs, cli_features))
1540 }
1541 }
1542
1543 fn collect_matching_features(
1546 member: &Package,
1547 cli_features: &CliFeatures,
1548 found_features: &mut BTreeSet<FeatureValue>,
1549 ) -> CliFeatures {
1550 if cli_features.features.is_empty() {
1551 return cli_features.clone();
1552 }
1553
1554 let summary = member.summary();
1556
1557 let summary_features = summary.features();
1559
1560 let dependencies: BTreeMap<InternedString, &Dependency> = summary
1562 .dependencies()
1563 .iter()
1564 .map(|dep| (dep.name_in_toml(), dep))
1565 .collect();
1566
1567 let optional_dependency_names: BTreeSet<_> = dependencies
1569 .iter()
1570 .filter(|(_, dep)| dep.is_optional())
1571 .map(|(name, _)| name)
1572 .copied()
1573 .collect();
1574
1575 let mut features = BTreeSet::new();
1576
1577 let summary_or_opt_dependency_feature = |feature: &InternedString| -> bool {
1579 summary_features.contains_key(feature) || optional_dependency_names.contains(feature)
1580 };
1581
1582 for feature in cli_features.features.iter() {
1583 match feature {
1584 FeatureValue::Feature(f) => {
1585 if summary_or_opt_dependency_feature(f) {
1586 features.insert(feature.clone());
1588 found_features.insert(feature.clone());
1589 }
1590 }
1591 FeatureValue::Dep { .. } => panic!("unexpected dep: syntax {}", feature),
1593 FeatureValue::DepFeature {
1594 dep_name,
1595 dep_feature,
1596 weak: _,
1597 } => {
1598 if dependencies.contains_key(dep_name) {
1599 features.insert(feature.clone());
1602 found_features.insert(feature.clone());
1603 } else if *dep_name == member.name()
1604 && summary_or_opt_dependency_feature(dep_feature)
1605 {
1606 features.insert(FeatureValue::Feature(*dep_feature));
1611 found_features.insert(feature.clone());
1612 }
1613 }
1614 }
1615 }
1616 CliFeatures {
1617 features: Rc::new(features),
1618 all_features: cli_features.all_features,
1619 uses_default_features: cli_features.uses_default_features,
1620 }
1621 }
1622
1623 fn missing_feature_spelling_suggestions(
1624 &self,
1625 selected_members: &[&Package],
1626 cli_features: &CliFeatures,
1627 found_features: &BTreeSet<FeatureValue>,
1628 ) -> Vec<String> {
1629 let mut summary_features: Vec<InternedString> = Default::default();
1631
1632 let mut dependencies_features: BTreeMap<InternedString, &[InternedString]> =
1634 Default::default();
1635
1636 let mut optional_dependency_names: Vec<InternedString> = Default::default();
1638
1639 let mut summary_features_per_member: BTreeMap<&Package, BTreeSet<InternedString>> =
1641 Default::default();
1642
1643 let mut optional_dependency_names_per_member: BTreeMap<&Package, BTreeSet<InternedString>> =
1645 Default::default();
1646
1647 for &member in selected_members {
1648 let summary = member.summary();
1650
1651 summary_features.extend(summary.features().keys());
1653 summary_features_per_member
1654 .insert(member, summary.features().keys().copied().collect());
1655
1656 let dependencies: BTreeMap<InternedString, &Dependency> = summary
1658 .dependencies()
1659 .iter()
1660 .map(|dep| (dep.name_in_toml(), dep))
1661 .collect();
1662
1663 dependencies_features.extend(
1664 dependencies
1665 .iter()
1666 .map(|(name, dep)| (*name, dep.features())),
1667 );
1668
1669 let optional_dependency_names_raw: BTreeSet<_> = dependencies
1671 .iter()
1672 .filter(|(_, dep)| dep.is_optional())
1673 .map(|(name, _)| name)
1674 .copied()
1675 .collect();
1676
1677 optional_dependency_names.extend(optional_dependency_names_raw.iter());
1678 optional_dependency_names_per_member.insert(member, optional_dependency_names_raw);
1679 }
1680
1681 let edit_distance_test = |a: InternedString, b: InternedString| {
1682 edit_distance(a.as_str(), b.as_str(), 3).is_some()
1683 };
1684
1685 cli_features
1686 .features
1687 .difference(found_features)
1688 .map(|feature| match feature {
1689 FeatureValue::Feature(typo) => {
1691 let summary_features = summary_features
1693 .iter()
1694 .filter(move |feature| edit_distance_test(**feature, *typo));
1695
1696 let optional_dependency_features = optional_dependency_names
1698 .iter()
1699 .filter(move |feature| edit_distance_test(**feature, *typo));
1700
1701 summary_features
1702 .chain(optional_dependency_features)
1703 .map(|s| s.to_string())
1704 .collect::<Vec<_>>()
1705 }
1706 FeatureValue::Dep { .. } => panic!("unexpected dep: syntax {}", feature),
1707 FeatureValue::DepFeature {
1708 dep_name,
1709 dep_feature,
1710 weak: _,
1711 } => {
1712 let pkg_feat_similar = dependencies_features
1714 .iter()
1715 .filter(|(name, _)| edit_distance_test(**name, *dep_name))
1716 .map(|(name, features)| {
1717 (
1718 name,
1719 features
1720 .iter()
1721 .filter(|feature| edit_distance_test(**feature, *dep_feature))
1722 .collect::<Vec<_>>(),
1723 )
1724 })
1725 .map(|(name, features)| {
1726 features
1727 .into_iter()
1728 .map(move |feature| format!("{}/{}", name, feature))
1729 })
1730 .flatten();
1731
1732 let optional_dependency_features = optional_dependency_names_per_member
1734 .iter()
1735 .filter(|(package, _)| edit_distance_test(package.name(), *dep_name))
1736 .map(|(package, optional_dependencies)| {
1737 optional_dependencies
1738 .into_iter()
1739 .filter(|optional_dependency| {
1740 edit_distance_test(**optional_dependency, *dep_name)
1741 })
1742 .map(move |optional_dependency| {
1743 format!("{}/{}", package.name(), optional_dependency)
1744 })
1745 })
1746 .flatten();
1747
1748 let summary_features = summary_features_per_member
1750 .iter()
1751 .filter(|(package, _)| edit_distance_test(package.name(), *dep_name))
1752 .map(|(package, summary_features)| {
1753 summary_features
1754 .into_iter()
1755 .filter(|summary_feature| {
1756 edit_distance_test(**summary_feature, *dep_feature)
1757 })
1758 .map(move |summary_feature| {
1759 format!("{}/{}", package.name(), summary_feature)
1760 })
1761 })
1762 .flatten();
1763
1764 pkg_feat_similar
1765 .chain(optional_dependency_features)
1766 .chain(summary_features)
1767 .collect::<Vec<_>>()
1768 }
1769 })
1770 .map(|v| v.into_iter())
1771 .flatten()
1772 .unique()
1773 .filter(|element| {
1774 let feature = FeatureValue::new(element.into());
1775 !cli_features.features.contains(&feature) && !found_features.contains(&feature)
1776 })
1777 .sorted()
1778 .take(5)
1779 .collect()
1780 }
1781
1782 fn report_unknown_features_error(
1783 &self,
1784 specs: &[PackageIdSpec],
1785 cli_features: &CliFeatures,
1786 found_features: &BTreeSet<FeatureValue>,
1787 ) -> CargoResult<()> {
1788 let unknown: Vec<_> = cli_features
1789 .features
1790 .difference(found_features)
1791 .map(|feature| feature.to_string())
1792 .sorted()
1793 .collect();
1794
1795 let (selected_members, unselected_members): (Vec<_>, Vec<_>) = self
1796 .members()
1797 .partition(|member| specs.iter().any(|spec| spec.matches(member.package_id())));
1798
1799 let missing_packages_with_the_features = unselected_members
1800 .into_iter()
1801 .filter(|member| {
1802 unknown
1803 .iter()
1804 .any(|feature| member.summary().features().contains_key(&**feature))
1805 })
1806 .map(|m| m.name())
1807 .collect_vec();
1808
1809 let these_features = if unknown.len() == 1 {
1810 "this feature"
1811 } else {
1812 "these features"
1813 };
1814 let mut msg = if let [singular] = &selected_members[..] {
1815 format!(
1816 "the package '{}' does not contain {these_features}: {}",
1817 singular.name(),
1818 unknown.join(", ")
1819 )
1820 } else {
1821 let names = selected_members.iter().map(|m| m.name()).join(", ");
1822 format!(
1823 "none of the selected packages contains {these_features}: {}\nselected packages: {names}",
1824 unknown.join(", ")
1825 )
1826 };
1827
1828 use std::fmt::Write;
1829 if !missing_packages_with_the_features.is_empty() {
1830 write!(
1831 &mut msg,
1832 "\nhelp: package{} with the missing feature{}: {}",
1833 if missing_packages_with_the_features.len() != 1 {
1834 "s"
1835 } else {
1836 ""
1837 },
1838 if unknown.len() != 1 { "s" } else { "" },
1839 missing_packages_with_the_features.join(", ")
1840 )?;
1841 } else {
1842 let suggestions = self.missing_feature_spelling_suggestions(
1843 &selected_members,
1844 cli_features,
1845 found_features,
1846 );
1847 if !suggestions.is_empty() {
1848 write!(
1849 &mut msg,
1850 "\nhelp: there {}: {}",
1851 if suggestions.len() == 1 {
1852 "is a similarly named feature"
1853 } else {
1854 "are similarly named features"
1855 },
1856 suggestions.join(", ")
1857 )?;
1858 }
1859 }
1860
1861 bail!("{msg}")
1862 }
1863
1864 fn members_with_features_new(
1867 &self,
1868 specs: &[PackageIdSpec],
1869 cli_features: &CliFeatures,
1870 ) -> CargoResult<Vec<(&Package, CliFeatures)>> {
1871 let mut found_features = Default::default();
1874
1875 let members: Vec<(&Package, CliFeatures)> = self
1876 .members()
1877 .filter(|m| specs.iter().any(|spec| spec.matches(m.package_id())))
1878 .map(|m| {
1879 (
1880 m,
1881 Workspace::collect_matching_features(m, cli_features, &mut found_features),
1882 )
1883 })
1884 .collect();
1885
1886 if members.is_empty() {
1887 if !(cli_features.features.is_empty()
1890 && !cli_features.all_features
1891 && cli_features.uses_default_features)
1892 {
1893 bail!("cannot specify features for packages outside of workspace");
1894 }
1895 return Ok(self
1898 .members()
1899 .map(|m| (m, CliFeatures::new_all(false)))
1900 .collect());
1901 }
1902 if *cli_features.features != found_features {
1903 self.report_unknown_features_error(specs, cli_features, &found_features)?;
1904 }
1905 Ok(members)
1906 }
1907
1908 fn members_with_features_old(
1911 &self,
1912 specs: &[PackageIdSpec],
1913 cli_features: &CliFeatures,
1914 ) -> Vec<(&Package, CliFeatures)> {
1915 let mut member_specific_features: HashMap<InternedString, BTreeSet<FeatureValue>> =
1918 HashMap::new();
1919 let mut cwd_features = BTreeSet::new();
1921 for feature in cli_features.features.iter() {
1922 match feature {
1923 FeatureValue::Feature(_) => {
1924 cwd_features.insert(feature.clone());
1925 }
1926 FeatureValue::Dep { .. } => panic!("unexpected dep: syntax {}", feature),
1928 FeatureValue::DepFeature {
1929 dep_name,
1930 dep_feature,
1931 weak: _,
1932 } => {
1933 let is_member = self.members().any(|member| {
1939 self.current_opt() != Some(member) && member.name() == *dep_name
1941 });
1942 if is_member && specs.iter().any(|spec| spec.name() == dep_name.as_str()) {
1943 member_specific_features
1944 .entry(*dep_name)
1945 .or_default()
1946 .insert(FeatureValue::Feature(*dep_feature));
1947 } else {
1948 cwd_features.insert(feature.clone());
1949 }
1950 }
1951 }
1952 }
1953
1954 let ms: Vec<_> = self
1955 .members()
1956 .filter_map(|member| {
1957 let member_id = member.package_id();
1958 match self.current_opt() {
1959 Some(current) if member_id == current.package_id() => {
1962 let feats = CliFeatures {
1963 features: Rc::new(cwd_features.clone()),
1964 all_features: cli_features.all_features,
1965 uses_default_features: cli_features.uses_default_features,
1966 };
1967 Some((member, feats))
1968 }
1969 _ => {
1970 if specs.iter().any(|spec| spec.matches(member_id)) {
1972 let feats = CliFeatures {
1982 features: Rc::new(
1983 member_specific_features
1984 .remove(member.name().as_str())
1985 .unwrap_or_default(),
1986 ),
1987 uses_default_features: true,
1988 all_features: cli_features.all_features,
1989 };
1990 Some((member, feats))
1991 } else {
1992 None
1994 }
1995 }
1996 }
1997 })
1998 .collect();
1999
2000 assert!(member_specific_features.is_empty());
2003
2004 ms
2005 }
2006
2007 pub fn unit_needs_doc_scrape(&self, unit: &Unit) -> bool {
2009 self.is_member(&unit.pkg) && !(unit.target.for_host() || unit.pkg.proc_macro())
2014 }
2015
2016 pub fn add_local_overlay(&mut self, id: SourceId, registry_path: PathBuf) {
2020 self.local_overlays.insert(id, registry_path);
2021 }
2022
2023 pub fn package_registry(&self) -> CargoResult<PackageRegistry<'gctx>> {
2025 let source_config =
2026 SourceConfigMap::new_with_overlays(self.gctx(), self.local_overlays()?)?;
2027 PackageRegistry::new_with_source_config(self.gctx(), source_config)
2028 }
2029
2030 fn local_overlays(&self) -> CargoResult<impl Iterator<Item = (SourceId, SourceId)>> {
2032 let mut ret = self
2033 .local_overlays
2034 .iter()
2035 .map(|(id, path)| Ok((*id, SourceId::for_local_registry(path)?)))
2036 .collect::<CargoResult<Vec<_>>>()?;
2037
2038 if let Ok(overlay) = self
2039 .gctx
2040 .get_env("__CARGO_TEST_DEPENDENCY_CONFUSION_VULNERABILITY_DO_NOT_USE_THIS")
2041 {
2042 let (url, path) = overlay.split_once('=').ok_or(anyhow::anyhow!(
2043 "invalid overlay format. I won't tell you why; you shouldn't be using it anyway"
2044 ))?;
2045 ret.push((
2046 SourceId::from_url(url)?,
2047 SourceId::for_local_registry(path.as_ref())?,
2048 ));
2049 }
2050
2051 Ok(ret.into_iter())
2052 }
2053}
2054
2055impl<'gctx> Packages<'gctx> {
2056 fn get(&self, manifest_path: &Path) -> &MaybePackage {
2057 self.maybe_get(manifest_path).unwrap()
2058 }
2059
2060 fn get_mut(&mut self, manifest_path: &Path) -> &mut MaybePackage {
2061 self.maybe_get_mut(manifest_path).unwrap()
2062 }
2063
2064 fn maybe_get(&self, manifest_path: &Path) -> Option<&MaybePackage> {
2065 self.packages.get(manifest_path)
2066 }
2067
2068 fn maybe_get_mut(&mut self, manifest_path: &Path) -> Option<&mut MaybePackage> {
2069 self.packages.get_mut(manifest_path)
2070 }
2071
2072 fn load(&mut self, manifest_path: &Path) -> CargoResult<&MaybePackage> {
2073 match self.packages.entry(manifest_path.to_path_buf()) {
2074 Entry::Occupied(e) => Ok(e.into_mut()),
2075 Entry::Vacant(v) => {
2076 let source_id = SourceId::for_manifest_path(manifest_path)?;
2077 let manifest = read_manifest(manifest_path, source_id, self.gctx)?;
2078 Ok(v.insert(match manifest {
2079 EitherManifest::Real(manifest) => {
2080 MaybePackage::Package(Package::new(manifest, manifest_path))
2081 }
2082 EitherManifest::Virtual(vm) => MaybePackage::Virtual(vm),
2083 }))
2084 }
2085 }
2086 }
2087}
2088
2089impl MaybePackage {
2090 fn workspace_config(&self) -> &WorkspaceConfig {
2091 match *self {
2092 MaybePackage::Package(ref p) => p.manifest().workspace_config(),
2093 MaybePackage::Virtual(ref vm) => vm.workspace_config(),
2094 }
2095 }
2096
2097 pub fn is_embedded(&self) -> bool {
2099 match self {
2100 MaybePackage::Package(p) => p.manifest().is_embedded(),
2101 MaybePackage::Virtual(_) => false,
2102 }
2103 }
2104
2105 pub fn contents(&self) -> Option<&str> {
2106 match self {
2107 MaybePackage::Package(p) => p.manifest().contents(),
2108 MaybePackage::Virtual(v) => v.contents(),
2109 }
2110 }
2111
2112 pub fn document(&self) -> Option<&toml::Spanned<toml::de::DeTable<'static>>> {
2113 match self {
2114 MaybePackage::Package(p) => p.manifest().document(),
2115 MaybePackage::Virtual(v) => v.document(),
2116 }
2117 }
2118
2119 pub fn original_toml(&self) -> Option<&TomlManifest> {
2120 match self {
2121 MaybePackage::Package(p) => p.manifest().original_toml(),
2122 MaybePackage::Virtual(v) => v.original_toml(),
2123 }
2124 }
2125
2126 pub fn normalized_toml(&self) -> &TomlManifest {
2127 match self {
2128 MaybePackage::Package(p) => p.manifest().normalized_toml(),
2129 MaybePackage::Virtual(v) => v.normalized_toml(),
2130 }
2131 }
2132
2133 pub fn edition(&self) -> Edition {
2134 match self {
2135 MaybePackage::Package(p) => p.manifest().edition(),
2136 MaybePackage::Virtual(_) => Edition::default(),
2137 }
2138 }
2139
2140 pub fn profiles(&self) -> Option<&TomlProfiles> {
2141 match self {
2142 MaybePackage::Package(p) => p.manifest().profiles(),
2143 MaybePackage::Virtual(v) => v.profiles(),
2144 }
2145 }
2146
2147 pub fn unstable_features(&self) -> &Features {
2148 match self {
2149 MaybePackage::Package(p) => p.manifest().unstable_features(),
2150 MaybePackage::Virtual(vm) => vm.unstable_features(),
2151 }
2152 }
2153}
2154
2155impl WorkspaceRootConfig {
2156 pub fn new(
2158 root_dir: &Path,
2159 members: &Option<Vec<String>>,
2160 default_members: &Option<Vec<String>>,
2161 exclude: &Option<Vec<String>>,
2162 inheritable: &Option<InheritableFields>,
2163 custom_metadata: &Option<toml::Value>,
2164 ) -> WorkspaceRootConfig {
2165 WorkspaceRootConfig {
2166 root_dir: root_dir.to_path_buf(),
2167 members: members.clone(),
2168 default_members: default_members.clone(),
2169 exclude: exclude.clone().unwrap_or_default(),
2170 inheritable_fields: inheritable.clone().unwrap_or_default(),
2171 custom_metadata: custom_metadata.clone(),
2172 }
2173 }
2174 fn is_excluded(&self, manifest_path: &Path) -> bool {
2178 let excluded = self
2179 .exclude
2180 .iter()
2181 .any(|ex| manifest_path.starts_with(self.root_dir.join(ex)));
2182
2183 let explicit_member = match self.members {
2184 Some(ref members) => members
2185 .iter()
2186 .any(|mem| manifest_path.starts_with(self.root_dir.join(mem))),
2187 None => false,
2188 };
2189
2190 !explicit_member && excluded
2191 }
2192
2193 fn is_explicitly_listed_member(&self, manifest_path: &Path) -> bool {
2204 let root_manifest = self.root_dir.join("Cargo.toml");
2205 if manifest_path == root_manifest {
2206 return true;
2207 }
2208 match self.members {
2209 Some(ref members) => {
2210 let Ok(expanded_members) = self.members_paths(members) else {
2212 return false;
2213 };
2214 let normalized_manifest = paths::normalize_path(manifest_path);
2216 expanded_members.iter().any(|(member_path, _)| {
2217 let normalized_member = paths::normalize_path(member_path);
2219 normalized_manifest.parent() == Some(normalized_member.as_path())
2222 })
2223 }
2224 None => false,
2225 }
2226 }
2227
2228 fn has_members_list(&self) -> bool {
2229 self.members.is_some()
2230 }
2231
2232 fn has_default_members(&self) -> bool {
2234 self.default_members.is_some()
2235 }
2236
2237 #[tracing::instrument(skip_all)]
2240 fn members_paths<'g>(
2241 &self,
2242 globs: &'g [String],
2243 ) -> CargoResult<Vec<(PathBuf, Option<&'g str>)>> {
2244 let mut expanded_list = Vec::new();
2245
2246 for glob in globs {
2247 let pathbuf = self.root_dir.join(glob);
2248 let expanded_paths = Self::expand_member_path(&pathbuf)?;
2249
2250 if expanded_paths.is_empty() {
2253 expanded_list.push((pathbuf, None));
2254 } else {
2255 let used_glob_pattern = expanded_paths.len() > 1 || expanded_paths[0] != pathbuf;
2256 let glob = used_glob_pattern.then_some(glob.as_str());
2257
2258 for expanded_path in expanded_paths {
2264 if expanded_path.is_dir() {
2265 expanded_list.push((expanded_path, glob));
2266 }
2267 }
2268 }
2269 }
2270
2271 Ok(expanded_list)
2272 }
2273
2274 fn expand_member_path(path: &Path) -> CargoResult<Vec<PathBuf>> {
2275 let Some(path) = path.to_str() else {
2276 return Ok(Vec::new());
2277 };
2278 let res = glob(path).with_context(|| format!("could not parse pattern `{}`", &path))?;
2279 let res = res
2280 .map(|p| p.with_context(|| format!("unable to match path to pattern `{}`", &path)))
2281 .collect::<Result<Vec<_>, _>>()?;
2282 Ok(res)
2283 }
2284
2285 pub fn inheritable(&self) -> &InheritableFields {
2286 &self.inheritable_fields
2287 }
2288}
2289
2290pub fn resolve_relative_path(
2291 label: &str,
2292 old_root: &Path,
2293 new_root: &Path,
2294 rel_path: &str,
2295) -> CargoResult<String> {
2296 let joined_path = normalize_path(&old_root.join(rel_path));
2297 match diff_paths(joined_path, new_root) {
2298 None => Err(anyhow!(
2299 "`{}` was defined in {} but could not be resolved with {}",
2300 label,
2301 old_root.display(),
2302 new_root.display()
2303 )),
2304 Some(path) => Ok(path
2305 .to_str()
2306 .ok_or_else(|| {
2307 anyhow!(
2308 "`{}` resolved to non-UTF value (`{}`)",
2309 label,
2310 path.display()
2311 )
2312 })?
2313 .to_owned()),
2314 }
2315}
2316
2317pub fn find_workspace_root(
2319 manifest_path: &Path,
2320 gctx: &GlobalContext,
2321) -> CargoResult<Option<PathBuf>> {
2322 find_workspace_root_with_loader(manifest_path, gctx, |self_path| {
2323 let source_id = SourceId::for_manifest_path(self_path)?;
2324 let manifest = read_manifest(self_path, source_id, gctx)?;
2325 Ok(manifest
2326 .workspace_config()
2327 .get_ws_root(self_path, manifest_path))
2328 })
2329}
2330
2331pub fn find_workspace_root_with_membership_check(
2338 manifest_path: &Path,
2339 gctx: &GlobalContext,
2340) -> CargoResult<Option<PathBuf>> {
2341 let source_id = SourceId::for_manifest_path(manifest_path)?;
2342 let current_manifest = read_manifest(manifest_path, source_id, gctx)?;
2343
2344 match current_manifest.workspace_config() {
2345 WorkspaceConfig::Root(root_config) => {
2346 if root_config.has_default_members() {
2349 Ok(None)
2350 } else {
2351 Ok(Some(manifest_path.to_path_buf()))
2352 }
2353 }
2354 WorkspaceConfig::Member {
2355 root: Some(path_to_root),
2356 } => {
2357 let ws_manifest_path = read_root_pointer(manifest_path, path_to_root);
2359 let ws_source_id = SourceId::for_manifest_path(&ws_manifest_path)?;
2360 let ws_manifest = read_manifest(&ws_manifest_path, ws_source_id, gctx)?;
2361
2362 if let WorkspaceConfig::Root(ref root_config) = *ws_manifest.workspace_config() {
2364 if root_config.is_explicitly_listed_member(manifest_path)
2365 && !root_config.is_excluded(manifest_path)
2366 {
2367 return Ok(Some(ws_manifest_path));
2368 }
2369 }
2370 Ok(None)
2372 }
2373 WorkspaceConfig::Member { root: None } => {
2374 find_workspace_root_with_loader(manifest_path, gctx, |candidate_manifest_path| {
2376 let source_id = SourceId::for_manifest_path(candidate_manifest_path)?;
2377 let manifest = read_manifest(candidate_manifest_path, source_id, gctx)?;
2378 if let WorkspaceConfig::Root(ref root_config) = *manifest.workspace_config() {
2379 if root_config.is_explicitly_listed_member(manifest_path)
2380 && !root_config.is_excluded(manifest_path)
2381 {
2382 return Ok(Some(candidate_manifest_path.to_path_buf()));
2383 }
2384 }
2385 Ok(None)
2386 })
2387 }
2388 }
2389}
2390
2391fn find_workspace_root_with_loader(
2396 manifest_path: &Path,
2397 gctx: &GlobalContext,
2398 mut loader: impl FnMut(&Path) -> CargoResult<Option<PathBuf>>,
2399) -> CargoResult<Option<PathBuf>> {
2400 {
2402 let roots = gctx.ws_roots();
2403 for current in manifest_path.ancestors().skip(1) {
2406 if let Some(ws_config) = roots.get(current) {
2407 if !ws_config.is_excluded(manifest_path) {
2408 return Ok(Some(current.join("Cargo.toml")));
2410 }
2411 }
2412 }
2413 }
2414
2415 for ances_manifest_path in find_root_iter(manifest_path, gctx) {
2416 debug!("find_root - trying {}", ances_manifest_path.display());
2417 let ws_root_path = loader(&ances_manifest_path).with_context(|| {
2418 format!(
2419 "failed searching for potential workspace\n\
2420 package manifest: `{}`\n\
2421 invalid potential workspace manifest: `{}`\n\
2422 \n\
2423 help: to avoid searching for a non-existent workspace, add \
2424 `[workspace]` to the package manifest",
2425 manifest_path.display(),
2426 ances_manifest_path.display(),
2427 )
2428 })?;
2429 if let Some(ws_root_path) = ws_root_path {
2430 return Ok(Some(ws_root_path));
2431 }
2432 }
2433 Ok(None)
2434}
2435
2436fn read_root_pointer(member_manifest: &Path, root_link: &str) -> PathBuf {
2437 let path = member_manifest
2438 .parent()
2439 .unwrap()
2440 .join(root_link)
2441 .join("Cargo.toml");
2442 debug!("find_root - pointer {}", path.display());
2443 paths::normalize_path(&path)
2444}
2445
2446fn find_root_iter<'a>(
2447 manifest_path: &'a Path,
2448 gctx: &'a GlobalContext,
2449) -> impl Iterator<Item = PathBuf> + 'a {
2450 LookBehind::new(paths::ancestors(manifest_path, None).skip(2))
2451 .take_while(|path| !path.curr.ends_with("target/package"))
2452 .take_while(|path| {
2458 if let Some(last) = path.last {
2459 gctx.home() != last
2460 } else {
2461 true
2462 }
2463 })
2464 .map(|path| path.curr.join("Cargo.toml"))
2465 .filter(|ances_manifest_path| ances_manifest_path.exists())
2466}
2467
2468struct LookBehindWindow<'a, T: ?Sized> {
2469 curr: &'a T,
2470 last: Option<&'a T>,
2471}
2472
2473struct LookBehind<'a, T: ?Sized, K: Iterator<Item = &'a T>> {
2474 iter: K,
2475 last: Option<&'a T>,
2476}
2477
2478impl<'a, T: ?Sized, K: Iterator<Item = &'a T>> LookBehind<'a, T, K> {
2479 fn new(items: K) -> Self {
2480 Self {
2481 iter: items,
2482 last: None,
2483 }
2484 }
2485}
2486
2487impl<'a, T: ?Sized, K: Iterator<Item = &'a T>> Iterator for LookBehind<'a, T, K> {
2488 type Item = LookBehindWindow<'a, T>;
2489
2490 fn next(&mut self) -> Option<Self::Item> {
2491 match self.iter.next() {
2492 None => None,
2493 Some(next) => {
2494 let last = self.last;
2495 self.last = Some(next);
2496 Some(LookBehindWindow { curr: next, last })
2497 }
2498 }
2499 }
2500}