1use std::cell::RefCell;
2use std::collections::hash_map::{Entry, HashMap};
3use std::collections::{BTreeMap, BTreeSet, HashSet};
4use std::path::{Path, PathBuf};
5use std::rc::Rc;
6
7use annotate_snippets::Level;
8use anyhow::{Context as _, anyhow, bail};
9use glob::glob;
10use itertools::Itertools;
11use tracing::debug;
12use url::Url;
13
14use crate::core::compiler::Unit;
15use crate::core::features::Features;
16use crate::core::registry::PackageRegistry;
17use crate::core::resolver::ResolveBehavior;
18use crate::core::resolver::features::CliFeatures;
19use crate::core::{
20 Dependency, Edition, FeatureValue, PackageId, PackageIdSpec, PackageIdSpecQuery, Patch,
21 PatchLocation,
22};
23use crate::core::{EitherManifest, Package, SourceId, VirtualManifest};
24use crate::lints::analyze_cargo_lints_table;
25use crate::lints::rules::blanket_hint_mostly_unused;
26use crate::lints::rules::check_im_a_teapot;
27use crate::lints::rules::implicit_minimum_version_req;
28use crate::lints::rules::non_kebab_case_bins;
29use crate::lints::rules::non_kebab_case_features;
30use crate::lints::rules::non_kebab_case_packages;
31use crate::lints::rules::non_snake_case_features;
32use crate::lints::rules::non_snake_case_packages;
33use crate::lints::rules::redundant_homepage;
34use crate::lints::rules::redundant_readme;
35use crate::lints::rules::unused_workspace_dependencies;
36use crate::ops;
37use crate::ops::lockfile::LOCKFILE_NAME;
38use crate::sources::{CRATES_IO_INDEX, CRATES_IO_REGISTRY, PathSource, SourceConfigMap};
39use crate::util::context;
40use crate::util::context::{FeatureUnification, Value};
41use crate::util::edit_distance;
42use crate::util::errors::{CargoResult, ManifestError};
43use crate::util::interning::InternedString;
44use crate::util::toml::{InheritableFields, read_manifest};
45use crate::util::{
46 Filesystem, GlobalContext, IntoUrl, context::CargoResolverConfig, context::ConfigRelativePath,
47 context::IncompatibleRustVersions,
48};
49
50use cargo_util::paths;
51use cargo_util::paths::normalize_path;
52use cargo_util_schemas::manifest;
53use cargo_util_schemas::manifest::RustVersion;
54use cargo_util_schemas::manifest::{TomlDependency, TomlManifest, TomlProfiles};
55use pathdiff::diff_paths;
56
57#[derive(Debug)]
63pub struct Workspace<'gctx> {
64 gctx: &'gctx GlobalContext,
66
67 current_manifest: PathBuf,
71
72 packages: Packages<'gctx>,
75
76 root_manifest: Option<PathBuf>,
81
82 target_dir: Option<Filesystem>,
85
86 build_dir: Option<Filesystem>,
89
90 members: Vec<PathBuf>,
94 member_ids: HashSet<PackageId>,
96
97 default_members: Vec<PathBuf>,
108
109 is_ephemeral: bool,
112
113 require_optional_deps: bool,
118
119 loaded_packages: RefCell<HashMap<PathBuf, Package>>,
122
123 ignore_lock: bool,
126
127 requested_lockfile_path: Option<PathBuf>,
129
130 resolve_behavior: ResolveBehavior,
132 resolve_honors_rust_version: bool,
136 resolve_feature_unification: FeatureUnification,
138 resolve_publish_time: Option<jiff::Timestamp>,
140 custom_metadata: Option<toml::Value>,
142
143 local_overlays: HashMap<SourceId, PathBuf>,
145}
146
147#[derive(Debug)]
150struct Packages<'gctx> {
151 gctx: &'gctx GlobalContext,
152 packages: HashMap<PathBuf, MaybePackage>,
153}
154
155#[derive(Debug)]
156pub enum MaybePackage {
157 Package(Package),
158 Virtual(VirtualManifest),
159}
160
161#[derive(Debug, Clone)]
163pub enum WorkspaceConfig {
164 Root(WorkspaceRootConfig),
167
168 Member { root: Option<String> },
171}
172
173impl WorkspaceConfig {
174 pub fn inheritable(&self) -> Option<&InheritableFields> {
175 match self {
176 WorkspaceConfig::Root(root) => Some(&root.inheritable_fields),
177 WorkspaceConfig::Member { .. } => None,
178 }
179 }
180
181 fn get_ws_root(&self, self_path: &Path, look_from: &Path) -> Option<PathBuf> {
189 match self {
190 WorkspaceConfig::Root(ances_root_config) => {
191 debug!("find_root - found a root checking exclusion");
192 if !ances_root_config.is_excluded(look_from) {
193 debug!("find_root - found!");
194 Some(self_path.to_owned())
195 } else {
196 None
197 }
198 }
199 WorkspaceConfig::Member {
200 root: Some(path_to_root),
201 } => {
202 debug!("find_root - found pointer");
203 Some(read_root_pointer(self_path, path_to_root))
204 }
205 WorkspaceConfig::Member { .. } => None,
206 }
207 }
208}
209
210#[derive(Debug, Clone)]
215pub struct WorkspaceRootConfig {
216 root_dir: PathBuf,
217 members: Option<Vec<String>>,
218 default_members: Option<Vec<String>>,
219 exclude: Vec<String>,
220 inheritable_fields: InheritableFields,
221 custom_metadata: Option<toml::Value>,
222}
223
224impl<'gctx> Workspace<'gctx> {
225 pub fn new(manifest_path: &Path, gctx: &'gctx GlobalContext) -> CargoResult<Workspace<'gctx>> {
232 let mut ws = Workspace::new_default(manifest_path.to_path_buf(), gctx);
233
234 if manifest_path.is_relative() {
235 bail!(
236 "manifest_path:{:?} is not an absolute path. Please provide an absolute path.",
237 manifest_path
238 )
239 } else {
240 ws.root_manifest = ws.find_root(manifest_path)?;
241 }
242
243 ws.target_dir = gctx.target_dir()?;
244 ws.build_dir = gctx.build_dir(ws.root_manifest())?;
245
246 ws.custom_metadata = ws
247 .load_workspace_config()?
248 .and_then(|cfg| cfg.custom_metadata);
249 ws.find_members()?;
250 ws.set_resolve_behavior()?;
251 ws.validate()?;
252 Ok(ws)
253 }
254
255 fn new_default(current_manifest: PathBuf, gctx: &'gctx GlobalContext) -> Workspace<'gctx> {
256 Workspace {
257 gctx,
258 current_manifest,
259 packages: Packages {
260 gctx,
261 packages: HashMap::new(),
262 },
263 root_manifest: None,
264 target_dir: None,
265 build_dir: None,
266 members: Vec::new(),
267 member_ids: HashSet::new(),
268 default_members: Vec::new(),
269 is_ephemeral: false,
270 require_optional_deps: true,
271 loaded_packages: RefCell::new(HashMap::new()),
272 ignore_lock: false,
273 requested_lockfile_path: None,
274 resolve_behavior: ResolveBehavior::V1,
275 resolve_honors_rust_version: false,
276 resolve_feature_unification: FeatureUnification::Selected,
277 resolve_publish_time: None,
278 custom_metadata: None,
279 local_overlays: HashMap::new(),
280 }
281 }
282
283 pub fn ephemeral(
293 package: Package,
294 gctx: &'gctx GlobalContext,
295 target_dir: Option<Filesystem>,
296 require_optional_deps: bool,
297 ) -> CargoResult<Workspace<'gctx>> {
298 let mut ws = Workspace::new_default(package.manifest_path().to_path_buf(), gctx);
299 ws.is_ephemeral = true;
300 ws.require_optional_deps = require_optional_deps;
301 let id = package.package_id();
302 let package = MaybePackage::Package(package);
303 ws.packages
304 .packages
305 .insert(ws.current_manifest.clone(), package);
306 ws.target_dir = if let Some(dir) = target_dir {
307 Some(dir)
308 } else {
309 ws.gctx.target_dir()?
310 };
311 ws.build_dir = ws.target_dir.clone();
312 ws.members.push(ws.current_manifest.clone());
313 ws.member_ids.insert(id);
314 ws.default_members.push(ws.current_manifest.clone());
315 ws.set_resolve_behavior()?;
316 Ok(ws)
317 }
318
319 pub fn reload(&self, gctx: &'gctx GlobalContext) -> CargoResult<Workspace<'gctx>> {
324 let mut ws = Workspace::new(&self.current_manifest, gctx)?;
325 ws.set_resolve_honors_rust_version(Some(self.resolve_honors_rust_version));
326 ws.set_resolve_feature_unification(self.resolve_feature_unification);
327 ws.set_requested_lockfile_path(self.requested_lockfile_path.clone());
328 Ok(ws)
329 }
330
331 fn set_resolve_behavior(&mut self) -> CargoResult<()> {
332 self.resolve_behavior = match self.root_maybe() {
337 MaybePackage::Package(p) => p
338 .manifest()
339 .resolve_behavior()
340 .unwrap_or_else(|| p.manifest().edition().default_resolve_behavior()),
341 MaybePackage::Virtual(vm) => vm.resolve_behavior().unwrap_or(ResolveBehavior::V1),
342 };
343
344 match self.resolve_behavior() {
345 ResolveBehavior::V1 | ResolveBehavior::V2 => {}
346 ResolveBehavior::V3 => {
347 if self.resolve_behavior == ResolveBehavior::V3 {
348 self.resolve_honors_rust_version = true;
349 }
350 }
351 }
352 let config = self.gctx().get::<CargoResolverConfig>("resolver")?;
353 if let Some(incompatible_rust_versions) = config.incompatible_rust_versions {
354 self.resolve_honors_rust_version =
355 incompatible_rust_versions == IncompatibleRustVersions::Fallback;
356 }
357 if self.gctx().cli_unstable().feature_unification {
358 self.resolve_feature_unification = config
359 .feature_unification
360 .unwrap_or(FeatureUnification::Selected);
361 } else if config.feature_unification.is_some() {
362 self.gctx()
363 .shell()
364 .warn("ignoring `resolver.feature-unification` without `-Zfeature-unification`")?;
365 };
366
367 if let Some(lockfile_path) = config.lockfile_path {
368 if self.gctx().cli_unstable().lockfile_path {
369 let replacements: [(&str, &str); 0] = [];
371 let path = lockfile_path
372 .resolve_templated_path(self.gctx(), replacements)
373 .map_err(|e| match e {
374 context::ResolveTemplateError::UnexpectedVariable {
375 variable,
376 raw_template,
377 } => {
378 anyhow!(
379 "unexpected variable `{variable}` in resolver.lockfile-path `{raw_template}`"
380 )
381 }
382 context::ResolveTemplateError::UnexpectedBracket { bracket_type, raw_template } => {
383 let (btype, literal) = match bracket_type {
384 context::BracketType::Opening => ("opening", "{"),
385 context::BracketType::Closing => ("closing", "}"),
386 };
387
388 anyhow!(
389 "unexpected {btype} bracket `{literal}` in build.build-dir path `{raw_template}`"
390 )
391 }
392 })?;
393 if !path.ends_with(LOCKFILE_NAME) {
394 bail!("the `resolver.lockfile-path` must be a path to a {LOCKFILE_NAME} file");
395 }
396 if path.is_dir() {
397 bail!(
398 "`resolver.lockfile-path` `{}` is a directory but expected a file",
399 path.display()
400 );
401 }
402 self.requested_lockfile_path = Some(path);
403 } else {
404 self.gctx().shell().warn(
405 "ignoring `resolver.lockfile-path`, pass `-Zlockfile-path` to enable it",
406 )?;
407 }
408 }
409
410 Ok(())
411 }
412
413 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 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 pub fn root(&self) -> &Path {
478 self.root_manifest().parent().unwrap()
479 }
480
481 pub fn root_manifest(&self) -> &Path {
484 self.root_manifest
485 .as_ref()
486 .unwrap_or(&self.current_manifest)
487 }
488
489 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 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 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 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 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 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 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 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 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 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 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 pub fn is_member(&self, pkg: &Package) -> bool {
692 self.member_ids.contains(&pkg.package_id())
693 }
694
695 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 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 self.build_dir()
741 } else {
742 Filesystem::new(self.root().to_owned())
743 }
744 }
745
746 pub fn set_requested_lockfile_path(&mut self, path: Option<PathBuf>) {
748 self.requested_lockfile_path = path;
749 }
750
751 pub fn requested_lockfile_path(&self) -> Option<&Path> {
752 self.requested_lockfile_path.as_deref()
753 }
754
755 pub fn lowest_rust_version(&self) -> Option<&RustVersion> {
758 self.members().filter_map(|pkg| pkg.rust_version()).min()
759 }
760
761 pub fn set_resolve_honors_rust_version(&mut self, honor_rust_version: Option<bool>) {
762 if let Some(honor_rust_version) = honor_rust_version {
763 self.resolve_honors_rust_version = honor_rust_version;
764 }
765 }
766
767 pub fn resolve_honors_rust_version(&self) -> bool {
768 self.resolve_honors_rust_version
769 }
770
771 pub fn set_resolve_feature_unification(&mut self, feature_unification: FeatureUnification) {
772 self.resolve_feature_unification = feature_unification;
773 }
774
775 pub fn resolve_feature_unification(&self) -> FeatureUnification {
776 self.resolve_feature_unification
777 }
778
779 pub fn set_resolve_publish_time(&mut self, publish_time: jiff::Timestamp) {
780 self.resolve_publish_time = Some(publish_time);
781 }
782
783 pub fn resolve_publish_time(&self) -> Option<jiff::Timestamp> {
784 self.resolve_publish_time
785 }
786
787 pub fn custom_metadata(&self) -> Option<&toml::Value> {
788 self.custom_metadata.as_ref()
789 }
790
791 pub fn load_workspace_config(&mut self) -> CargoResult<Option<WorkspaceRootConfig>> {
792 if let Some(root_path) = &self.root_manifest {
795 let root_package = self.packages.load(root_path)?;
796 match root_package.workspace_config() {
797 WorkspaceConfig::Root(root_config) => {
798 return Ok(Some(root_config.clone()));
799 }
800
801 _ => bail!(
802 "root of a workspace inferred but wasn't a root: {}",
803 root_path.display()
804 ),
805 }
806 }
807
808 Ok(None)
809 }
810
811 fn find_root(&mut self, manifest_path: &Path) -> CargoResult<Option<PathBuf>> {
821 let current = self.packages.load(manifest_path)?;
822 match current
823 .workspace_config()
824 .get_ws_root(manifest_path, manifest_path)
825 {
826 Some(root_path) => {
827 debug!("find_root - is root {}", manifest_path.display());
828 Ok(Some(root_path))
829 }
830 None => find_workspace_root_with_loader(manifest_path, self.gctx, |self_path| {
831 Ok(self
832 .packages
833 .load(self_path)?
834 .workspace_config()
835 .get_ws_root(self_path, manifest_path))
836 }),
837 }
838 }
839
840 #[tracing::instrument(skip_all)]
848 fn find_members(&mut self) -> CargoResult<()> {
849 let Some(workspace_config) = self.load_workspace_config()? else {
850 debug!("find_members - only me as a member");
851 self.members.push(self.current_manifest.clone());
852 self.default_members.push(self.current_manifest.clone());
853 if let Ok(pkg) = self.current() {
854 let id = pkg.package_id();
855 self.member_ids.insert(id);
856 }
857 return Ok(());
858 };
859
860 let root_manifest_path = self.root_manifest.clone().unwrap();
862
863 let members_paths = workspace_config
864 .members_paths(workspace_config.members.as_deref().unwrap_or_default())?;
865 let default_members_paths = if root_manifest_path == self.current_manifest {
866 if let Some(ref default) = workspace_config.default_members {
867 Some(workspace_config.members_paths(default)?)
868 } else {
869 None
870 }
871 } else {
872 None
873 };
874
875 for (path, glob) in &members_paths {
876 self.find_path_deps(&path.join("Cargo.toml"), &root_manifest_path, false)
877 .with_context(|| {
878 format!(
879 "failed to load manifest for workspace member `{}`\n\
880 referenced{} by workspace at `{}`",
881 path.display(),
882 glob.map(|g| format!(" via `{g}`")).unwrap_or_default(),
883 root_manifest_path.display(),
884 )
885 })?;
886 }
887
888 self.find_path_deps(&root_manifest_path, &root_manifest_path, false)?;
889
890 if let Some(default) = default_members_paths {
891 for (path, default_member_glob) in default {
892 let normalized_path = paths::normalize_path(&path);
893 let manifest_path = normalized_path.join("Cargo.toml");
894 if !self.members.contains(&manifest_path) {
895 let exclude = members_paths.iter().any(|(m, _)| *m == normalized_path)
902 && workspace_config.is_excluded(&normalized_path);
903 if exclude {
904 continue;
905 }
906 bail!(
907 "package `{}` is listed in default-members{} but is not a member\n\
908 for workspace at `{}`.",
909 path.display(),
910 default_member_glob
911 .map(|g| format!(" via `{g}`"))
912 .unwrap_or_default(),
913 root_manifest_path.display(),
914 )
915 }
916 self.default_members.push(manifest_path)
917 }
918 } else if self.is_virtual() {
919 self.default_members = self.members.clone()
920 } else {
921 self.default_members.push(self.current_manifest.clone())
922 }
923
924 Ok(())
925 }
926
927 fn find_path_deps(
928 &mut self,
929 manifest_path: &Path,
930 root_manifest: &Path,
931 is_path_dep: bool,
932 ) -> CargoResult<()> {
933 let manifest_path = paths::normalize_path(manifest_path);
934 if self.members.contains(&manifest_path) {
935 return Ok(());
936 }
937 if is_path_dep && self.root_maybe().is_embedded() {
938 return Ok(());
940 }
941 if is_path_dep
942 && !manifest_path.parent().unwrap().starts_with(self.root())
943 && self.find_root(&manifest_path)? != self.root_manifest
944 {
945 return Ok(());
948 }
949
950 if let WorkspaceConfig::Root(ref root_config) =
951 *self.packages.load(root_manifest)?.workspace_config()
952 {
953 if root_config.is_excluded(&manifest_path) {
954 return Ok(());
955 }
956 }
957
958 debug!("find_path_deps - {}", manifest_path.display());
959 self.members.push(manifest_path.clone());
960
961 let candidates = {
962 let pkg = match *self.packages.load(&manifest_path)? {
963 MaybePackage::Package(ref p) => p,
964 MaybePackage::Virtual(_) => return Ok(()),
965 };
966 self.member_ids.insert(pkg.package_id());
967 pkg.dependencies()
968 .iter()
969 .map(|d| (d.source_id(), d.package_name()))
970 .filter(|(s, _)| s.is_path())
971 .filter_map(|(s, n)| s.url().to_file_path().ok().map(|p| (p, n)))
972 .map(|(p, n)| (p.join("Cargo.toml"), n))
973 .collect::<Vec<_>>()
974 };
975 for (path, name) in candidates {
976 self.find_path_deps(&path, root_manifest, true)
977 .with_context(|| format!("failed to load manifest for dependency `{}`", name))
978 .map_err(|err| ManifestError::new(err, manifest_path.clone()))?;
979 }
980 Ok(())
981 }
982
983 pub fn unstable_features(&self) -> &Features {
985 self.root_maybe().unstable_features()
986 }
987
988 pub fn resolve_behavior(&self) -> ResolveBehavior {
989 self.resolve_behavior
990 }
991
992 pub fn allows_new_cli_feature_behavior(&self) -> bool {
1000 self.is_virtual()
1001 || match self.resolve_behavior() {
1002 ResolveBehavior::V1 => false,
1003 ResolveBehavior::V2 | ResolveBehavior::V3 => true,
1004 }
1005 }
1006
1007 #[tracing::instrument(skip_all)]
1013 fn validate(&mut self) -> CargoResult<()> {
1014 if self.root_manifest.is_none() {
1016 return Ok(());
1017 }
1018
1019 self.validate_unique_names()?;
1020 self.validate_workspace_roots()?;
1021 self.validate_members()?;
1022 self.error_if_manifest_not_in_members()?;
1023 self.validate_manifest()
1024 }
1025
1026 fn validate_unique_names(&self) -> CargoResult<()> {
1027 let mut names = BTreeMap::new();
1028 for member in self.members.iter() {
1029 let package = self.packages.get(member);
1030 let name = match *package {
1031 MaybePackage::Package(ref p) => p.name(),
1032 MaybePackage::Virtual(_) => continue,
1033 };
1034 if let Some(prev) = names.insert(name, member) {
1035 bail!(
1036 "two packages named `{}` in this workspace:\n\
1037 - {}\n\
1038 - {}",
1039 name,
1040 prev.display(),
1041 member.display()
1042 );
1043 }
1044 }
1045 Ok(())
1046 }
1047
1048 fn validate_workspace_roots(&self) -> CargoResult<()> {
1049 let roots: Vec<PathBuf> = self
1050 .members
1051 .iter()
1052 .filter(|&member| {
1053 let config = self.packages.get(member).workspace_config();
1054 matches!(config, WorkspaceConfig::Root(_))
1055 })
1056 .map(|member| member.parent().unwrap().to_path_buf())
1057 .collect();
1058 match roots.len() {
1059 1 => Ok(()),
1060 0 => bail!(
1061 "`package.workspace` configuration points to a crate \
1062 which is not configured with [workspace]: \n\
1063 configuration at: {}\n\
1064 points to: {}",
1065 self.current_manifest.display(),
1066 self.root_manifest.as_ref().unwrap().display()
1067 ),
1068 _ => {
1069 bail!(
1070 "multiple workspace roots found in the same workspace:\n{}",
1071 roots
1072 .iter()
1073 .map(|r| format!(" {}", r.display()))
1074 .collect::<Vec<_>>()
1075 .join("\n")
1076 );
1077 }
1078 }
1079 }
1080
1081 #[tracing::instrument(skip_all)]
1082 fn validate_members(&mut self) -> CargoResult<()> {
1083 for member in self.members.clone() {
1084 let root = self.find_root(&member)?;
1085 if root == self.root_manifest {
1086 continue;
1087 }
1088
1089 match root {
1090 Some(root) => {
1091 bail!(
1092 "package `{}` is a member of the wrong workspace\n\
1093 expected: {}\n\
1094 actual: {}",
1095 member.display(),
1096 self.root_manifest.as_ref().unwrap().display(),
1097 root.display()
1098 );
1099 }
1100 None => {
1101 bail!(
1102 "workspace member `{}` is not hierarchically below \
1103 the workspace root `{}`",
1104 member.display(),
1105 self.root_manifest.as_ref().unwrap().display()
1106 );
1107 }
1108 }
1109 }
1110 Ok(())
1111 }
1112
1113 fn error_if_manifest_not_in_members(&mut self) -> CargoResult<()> {
1114 if self.members.contains(&self.current_manifest) {
1115 return Ok(());
1116 }
1117
1118 let root = self.root_manifest.as_ref().unwrap();
1119 let root_dir = root.parent().unwrap();
1120 let current_dir = self.current_manifest.parent().unwrap();
1121 let root_pkg = self.packages.get(root);
1122
1123 let members_msg = match current_dir.strip_prefix(root_dir) {
1125 Ok(rel) => format!(
1126 "this may be fixable by adding `{}` to the \
1127 `workspace.members` array of the manifest \
1128 located at: {}",
1129 rel.display(),
1130 root.display()
1131 ),
1132 Err(_) => format!(
1133 "this may be fixable by adding a member to \
1134 the `workspace.members` array of the \
1135 manifest located at: {}",
1136 root.display()
1137 ),
1138 };
1139 let extra = match *root_pkg {
1140 MaybePackage::Virtual(_) => members_msg,
1141 MaybePackage::Package(ref p) => {
1142 let has_members_list = match *p.manifest().workspace_config() {
1143 WorkspaceConfig::Root(ref root_config) => root_config.has_members_list(),
1144 WorkspaceConfig::Member { .. } => unreachable!(),
1145 };
1146 if !has_members_list {
1147 format!(
1148 "this may be fixable by ensuring that this \
1149 crate is depended on by the workspace \
1150 root: {}",
1151 root.display()
1152 )
1153 } else {
1154 members_msg
1155 }
1156 }
1157 };
1158 bail!(
1159 "current package believes it's in a workspace when it's not:\n\
1160 current: {}\n\
1161 workspace: {}\n\n{}\n\
1162 Alternatively, to keep it out of the workspace, add the package \
1163 to the `workspace.exclude` array, or add an empty `[workspace]` \
1164 table to the package's manifest.",
1165 self.current_manifest.display(),
1166 root.display(),
1167 extra
1168 );
1169 }
1170
1171 fn validate_manifest(&mut self) -> CargoResult<()> {
1172 if let Some(ref root_manifest) = self.root_manifest {
1173 for pkg in self
1174 .members()
1175 .filter(|p| p.manifest_path() != root_manifest)
1176 {
1177 let manifest = pkg.manifest();
1178 let emit_warning = |what| -> CargoResult<()> {
1179 let msg = format!(
1180 "{} for the non root package will be ignored, \
1181 specify {} at the workspace root:\n\
1182 package: {}\n\
1183 workspace: {}",
1184 what,
1185 what,
1186 pkg.manifest_path().display(),
1187 root_manifest.display(),
1188 );
1189 self.gctx.shell().warn(&msg)
1190 };
1191 if manifest.normalized_toml().has_profiles() {
1192 emit_warning("profiles")?;
1193 }
1194 if !manifest.replace().is_empty() {
1195 emit_warning("replace")?;
1196 }
1197 if !manifest.patch().is_empty() {
1198 emit_warning("patch")?;
1199 }
1200 if let Some(behavior) = manifest.resolve_behavior() {
1201 if behavior != self.resolve_behavior {
1202 emit_warning("resolver")?;
1204 }
1205 }
1206 }
1207 if let MaybePackage::Virtual(vm) = self.root_maybe() {
1208 if vm.resolve_behavior().is_none() {
1209 if let Some(edition) = self
1210 .members()
1211 .filter(|p| p.manifest_path() != root_manifest)
1212 .map(|p| p.manifest().edition())
1213 .filter(|&e| e >= Edition::Edition2021)
1214 .max()
1215 {
1216 let resolver = edition.default_resolve_behavior().to_manifest();
1217 let report = &[Level::WARNING
1218 .primary_title(format!(
1219 "virtual workspace defaulting to `resolver = \"1\"` despite one or more workspace members being on edition {edition} which implies `resolver = \"{resolver}\"`"
1220 ))
1221 .elements([
1222 Level::NOTE.message("to keep the current resolver, specify `workspace.resolver = \"1\"` in the workspace root's manifest"),
1223 Level::NOTE.message(
1224 format!("to use the edition {edition} resolver, specify `workspace.resolver = \"{resolver}\"` in the workspace root's manifest"),
1225 ),
1226 Level::NOTE.message("for more details see https://doc.rust-lang.org/cargo/reference/resolver.html#resolver-versions"),
1227 ])];
1228 self.gctx.shell().print_report(report, false)?;
1229 }
1230 }
1231 }
1232 }
1233 Ok(())
1234 }
1235
1236 pub fn load(&self, manifest_path: &Path) -> CargoResult<Package> {
1237 match self.packages.maybe_get(manifest_path) {
1238 Some(MaybePackage::Package(p)) => return Ok(p.clone()),
1239 Some(&MaybePackage::Virtual(_)) => bail!("cannot load workspace root"),
1240 None => {}
1241 }
1242
1243 let mut loaded = self.loaded_packages.borrow_mut();
1244 if let Some(p) = loaded.get(manifest_path).cloned() {
1245 return Ok(p);
1246 }
1247 let source_id = SourceId::for_manifest_path(manifest_path)?;
1248 let package = ops::read_package(manifest_path, source_id, self.gctx)?;
1249 loaded.insert(manifest_path.to_path_buf(), package.clone());
1250 Ok(package)
1251 }
1252
1253 pub fn preload(&self, registry: &mut PackageRegistry<'gctx>) {
1260 if self.is_ephemeral {
1266 return;
1267 }
1268
1269 for pkg in self.packages.packages.values() {
1270 let pkg = match *pkg {
1271 MaybePackage::Package(ref p) => p.clone(),
1272 MaybePackage::Virtual(_) => continue,
1273 };
1274 let src = PathSource::preload_with(pkg, self.gctx);
1275 registry.add_preloaded(Box::new(src));
1276 }
1277 }
1278
1279 pub fn emit_warnings(&self) -> CargoResult<()> {
1280 let mut first_emitted_error = None;
1281
1282 if let Err(e) = self.emit_ws_lints() {
1283 first_emitted_error = Some(e);
1284 }
1285
1286 for (path, maybe_pkg) in &self.packages.packages {
1287 if let MaybePackage::Package(pkg) = maybe_pkg {
1288 if let Err(e) = self.emit_pkg_lints(pkg, &path)
1289 && first_emitted_error.is_none()
1290 {
1291 first_emitted_error = Some(e);
1292 }
1293 }
1294 let warnings = match maybe_pkg {
1295 MaybePackage::Package(pkg) => pkg.manifest().warnings().warnings(),
1296 MaybePackage::Virtual(vm) => vm.warnings().warnings(),
1297 };
1298 for warning in warnings {
1299 if warning.is_critical {
1300 let err = anyhow::format_err!("{}", warning.message);
1301 let cx =
1302 anyhow::format_err!("failed to parse manifest at `{}`", path.display());
1303 if first_emitted_error.is_none() {
1304 first_emitted_error = Some(err.context(cx));
1305 }
1306 } else {
1307 let msg = if self.root_manifest.is_none() {
1308 warning.message.to_string()
1309 } else {
1310 format!("{}: {}", path.display(), warning.message)
1313 };
1314 self.gctx.shell().warn(msg)?
1315 }
1316 }
1317 }
1318
1319 if let Some(error) = first_emitted_error {
1320 Err(error)
1321 } else {
1322 Ok(())
1323 }
1324 }
1325
1326 pub fn emit_pkg_lints(&self, pkg: &Package, path: &Path) -> CargoResult<()> {
1327 let toml_lints = pkg
1328 .manifest()
1329 .normalized_toml()
1330 .lints
1331 .clone()
1332 .map(|lints| lints.lints)
1333 .unwrap_or(manifest::TomlLints::default());
1334 let cargo_lints = toml_lints
1335 .get("cargo")
1336 .cloned()
1337 .unwrap_or(manifest::TomlToolLints::default());
1338
1339 if self.gctx.cli_unstable().cargo_lints {
1340 let mut verify_error_count = 0;
1341
1342 analyze_cargo_lints_table(
1343 pkg.into(),
1344 &path,
1345 &cargo_lints,
1346 &mut verify_error_count,
1347 self.gctx,
1348 )?;
1349
1350 if verify_error_count > 0 {
1351 let plural = if verify_error_count == 1 { "" } else { "s" };
1352 bail!("encountered {verify_error_count} error{plural} while verifying lints")
1353 }
1354
1355 let mut run_error_count = 0;
1356
1357 check_im_a_teapot(pkg, &path, &cargo_lints, &mut run_error_count, self.gctx)?;
1358 implicit_minimum_version_req(
1359 pkg.into(),
1360 &path,
1361 &cargo_lints,
1362 &mut run_error_count,
1363 self.gctx,
1364 )?;
1365 non_kebab_case_packages(
1366 pkg.into(),
1367 &path,
1368 &cargo_lints,
1369 &mut run_error_count,
1370 self.gctx,
1371 )?;
1372 non_snake_case_packages(
1373 pkg.into(),
1374 &path,
1375 &cargo_lints,
1376 &mut run_error_count,
1377 self.gctx,
1378 )?;
1379 non_kebab_case_bins(
1380 self,
1381 pkg.into(),
1382 &path,
1383 &cargo_lints,
1384 &mut run_error_count,
1385 self.gctx,
1386 )?;
1387 non_kebab_case_features(
1388 pkg.into(),
1389 &path,
1390 &cargo_lints,
1391 &mut run_error_count,
1392 self.gctx,
1393 )?;
1394 non_snake_case_features(
1395 pkg.into(),
1396 &path,
1397 &cargo_lints,
1398 &mut run_error_count,
1399 self.gctx,
1400 )?;
1401 redundant_readme(
1402 pkg.into(),
1403 &path,
1404 &cargo_lints,
1405 &mut run_error_count,
1406 self.gctx,
1407 )?;
1408 redundant_homepage(
1409 pkg.into(),
1410 &path,
1411 &cargo_lints,
1412 &mut run_error_count,
1413 self.gctx,
1414 )?;
1415
1416 if run_error_count > 0 {
1417 let plural = if run_error_count == 1 { "" } else { "s" };
1418 bail!("encountered {run_error_count} error{plural} while running lints")
1419 }
1420 }
1421
1422 Ok(())
1423 }
1424
1425 pub fn emit_ws_lints(&self) -> CargoResult<()> {
1426 let mut run_error_count = 0;
1427
1428 let cargo_lints = match self.root_maybe() {
1429 MaybePackage::Package(pkg) => {
1430 let toml = pkg.manifest().normalized_toml();
1431 if let Some(ws) = &toml.workspace {
1432 ws.lints.as_ref()
1433 } else {
1434 toml.lints.as_ref().map(|l| &l.lints)
1435 }
1436 }
1437 MaybePackage::Virtual(vm) => vm
1438 .normalized_toml()
1439 .workspace
1440 .as_ref()
1441 .unwrap()
1442 .lints
1443 .as_ref(),
1444 }
1445 .and_then(|t| t.get("cargo"))
1446 .cloned()
1447 .unwrap_or(manifest::TomlToolLints::default());
1448
1449 if self.gctx.cli_unstable().cargo_lints {
1450 let mut verify_error_count = 0;
1451
1452 analyze_cargo_lints_table(
1453 self.root_maybe().into(),
1454 self.root_manifest(),
1455 &cargo_lints,
1456 &mut verify_error_count,
1457 self.gctx,
1458 )?;
1459
1460 if verify_error_count > 0 {
1461 let plural = if verify_error_count == 1 { "" } else { "s" };
1462 bail!("encountered {verify_error_count} error{plural} while verifying lints")
1463 }
1464
1465 unused_workspace_dependencies(
1466 self,
1467 self.root_maybe(),
1468 self.root_manifest(),
1469 &cargo_lints,
1470 &mut run_error_count,
1471 self.gctx,
1472 )?;
1473 implicit_minimum_version_req(
1474 self.root_maybe().into(),
1475 self.root_manifest(),
1476 &cargo_lints,
1477 &mut run_error_count,
1478 self.gctx,
1479 )?;
1480 }
1481
1482 if self.gctx.cli_unstable().profile_hint_mostly_unused {
1486 blanket_hint_mostly_unused(
1487 self.root_maybe(),
1488 self.root_manifest(),
1489 &cargo_lints,
1490 &mut run_error_count,
1491 self.gctx,
1492 )?;
1493 }
1494
1495 if run_error_count > 0 {
1496 let plural = if run_error_count == 1 { "" } else { "s" };
1497 bail!("encountered {run_error_count} error{plural} while running lints")
1498 } else {
1499 Ok(())
1500 }
1501 }
1502
1503 pub fn set_target_dir(&mut self, target_dir: Filesystem) {
1504 self.target_dir = Some(target_dir);
1505 }
1506
1507 pub fn members_with_features(
1515 &self,
1516 specs: &[PackageIdSpec],
1517 cli_features: &CliFeatures,
1518 ) -> CargoResult<Vec<(&Package, CliFeatures)>> {
1519 assert!(
1520 !specs.is_empty() || cli_features.all_features,
1521 "no specs requires all_features"
1522 );
1523 if specs.is_empty() {
1524 return Ok(self
1527 .members()
1528 .map(|m| (m, CliFeatures::new_all(true)))
1529 .collect());
1530 }
1531 if self.allows_new_cli_feature_behavior() {
1532 self.members_with_features_new(specs, cli_features)
1533 } else {
1534 Ok(self.members_with_features_old(specs, cli_features))
1535 }
1536 }
1537
1538 fn collect_matching_features(
1541 member: &Package,
1542 cli_features: &CliFeatures,
1543 found_features: &mut BTreeSet<FeatureValue>,
1544 ) -> CliFeatures {
1545 if cli_features.features.is_empty() {
1546 return cli_features.clone();
1547 }
1548
1549 let summary = member.summary();
1551
1552 let summary_features = summary.features();
1554
1555 let dependencies: BTreeMap<InternedString, &Dependency> = summary
1557 .dependencies()
1558 .iter()
1559 .map(|dep| (dep.name_in_toml(), dep))
1560 .collect();
1561
1562 let optional_dependency_names: BTreeSet<_> = dependencies
1564 .iter()
1565 .filter(|(_, dep)| dep.is_optional())
1566 .map(|(name, _)| name)
1567 .copied()
1568 .collect();
1569
1570 let mut features = BTreeSet::new();
1571
1572 let summary_or_opt_dependency_feature = |feature: &InternedString| -> bool {
1574 summary_features.contains_key(feature) || optional_dependency_names.contains(feature)
1575 };
1576
1577 for feature in cli_features.features.iter() {
1578 match feature {
1579 FeatureValue::Feature(f) => {
1580 if summary_or_opt_dependency_feature(f) {
1581 features.insert(feature.clone());
1583 found_features.insert(feature.clone());
1584 }
1585 }
1586 FeatureValue::Dep { .. } => panic!("unexpected dep: syntax {}", feature),
1588 FeatureValue::DepFeature {
1589 dep_name,
1590 dep_feature,
1591 weak: _,
1592 } => {
1593 if dependencies.contains_key(dep_name) {
1594 features.insert(feature.clone());
1597 found_features.insert(feature.clone());
1598 } else if *dep_name == member.name()
1599 && summary_or_opt_dependency_feature(dep_feature)
1600 {
1601 features.insert(FeatureValue::Feature(*dep_feature));
1606 found_features.insert(feature.clone());
1607 }
1608 }
1609 }
1610 }
1611 CliFeatures {
1612 features: Rc::new(features),
1613 all_features: cli_features.all_features,
1614 uses_default_features: cli_features.uses_default_features,
1615 }
1616 }
1617
1618 fn missing_feature_spelling_suggestions(
1619 &self,
1620 selected_members: &[&Package],
1621 cli_features: &CliFeatures,
1622 found_features: &BTreeSet<FeatureValue>,
1623 ) -> Vec<String> {
1624 let mut summary_features: Vec<InternedString> = Default::default();
1626
1627 let mut dependencies_features: BTreeMap<InternedString, &[InternedString]> =
1629 Default::default();
1630
1631 let mut optional_dependency_names: Vec<InternedString> = Default::default();
1633
1634 let mut summary_features_per_member: BTreeMap<&Package, BTreeSet<InternedString>> =
1636 Default::default();
1637
1638 let mut optional_dependency_names_per_member: BTreeMap<&Package, BTreeSet<InternedString>> =
1640 Default::default();
1641
1642 for &member in selected_members {
1643 let summary = member.summary();
1645
1646 summary_features.extend(summary.features().keys());
1648 summary_features_per_member
1649 .insert(member, summary.features().keys().copied().collect());
1650
1651 let dependencies: BTreeMap<InternedString, &Dependency> = summary
1653 .dependencies()
1654 .iter()
1655 .map(|dep| (dep.name_in_toml(), dep))
1656 .collect();
1657
1658 dependencies_features.extend(
1659 dependencies
1660 .iter()
1661 .map(|(name, dep)| (*name, dep.features())),
1662 );
1663
1664 let optional_dependency_names_raw: BTreeSet<_> = dependencies
1666 .iter()
1667 .filter(|(_, dep)| dep.is_optional())
1668 .map(|(name, _)| name)
1669 .copied()
1670 .collect();
1671
1672 optional_dependency_names.extend(optional_dependency_names_raw.iter());
1673 optional_dependency_names_per_member.insert(member, optional_dependency_names_raw);
1674 }
1675
1676 let edit_distance_test = |a: InternedString, b: InternedString| {
1677 edit_distance(a.as_str(), b.as_str(), 3).is_some()
1678 };
1679
1680 cli_features
1681 .features
1682 .difference(found_features)
1683 .map(|feature| match feature {
1684 FeatureValue::Feature(typo) => {
1686 let summary_features = summary_features
1688 .iter()
1689 .filter(move |feature| edit_distance_test(**feature, *typo));
1690
1691 let optional_dependency_features = optional_dependency_names
1693 .iter()
1694 .filter(move |feature| edit_distance_test(**feature, *typo));
1695
1696 summary_features
1697 .chain(optional_dependency_features)
1698 .map(|s| s.to_string())
1699 .collect::<Vec<_>>()
1700 }
1701 FeatureValue::Dep { .. } => panic!("unexpected dep: syntax {}", feature),
1702 FeatureValue::DepFeature {
1703 dep_name,
1704 dep_feature,
1705 weak: _,
1706 } => {
1707 let pkg_feat_similar = dependencies_features
1709 .iter()
1710 .filter(|(name, _)| edit_distance_test(**name, *dep_name))
1711 .map(|(name, features)| {
1712 (
1713 name,
1714 features
1715 .iter()
1716 .filter(|feature| edit_distance_test(**feature, *dep_feature))
1717 .collect::<Vec<_>>(),
1718 )
1719 })
1720 .map(|(name, features)| {
1721 features
1722 .into_iter()
1723 .map(move |feature| format!("{}/{}", name, feature))
1724 })
1725 .flatten();
1726
1727 let optional_dependency_features = optional_dependency_names_per_member
1729 .iter()
1730 .filter(|(package, _)| edit_distance_test(package.name(), *dep_name))
1731 .map(|(package, optional_dependencies)| {
1732 optional_dependencies
1733 .into_iter()
1734 .filter(|optional_dependency| {
1735 edit_distance_test(**optional_dependency, *dep_name)
1736 })
1737 .map(move |optional_dependency| {
1738 format!("{}/{}", package.name(), optional_dependency)
1739 })
1740 })
1741 .flatten();
1742
1743 let summary_features = summary_features_per_member
1745 .iter()
1746 .filter(|(package, _)| edit_distance_test(package.name(), *dep_name))
1747 .map(|(package, summary_features)| {
1748 summary_features
1749 .into_iter()
1750 .filter(|summary_feature| {
1751 edit_distance_test(**summary_feature, *dep_feature)
1752 })
1753 .map(move |summary_feature| {
1754 format!("{}/{}", package.name(), summary_feature)
1755 })
1756 })
1757 .flatten();
1758
1759 pkg_feat_similar
1760 .chain(optional_dependency_features)
1761 .chain(summary_features)
1762 .collect::<Vec<_>>()
1763 }
1764 })
1765 .map(|v| v.into_iter())
1766 .flatten()
1767 .unique()
1768 .filter(|element| {
1769 let feature = FeatureValue::new(element.into());
1770 !cli_features.features.contains(&feature) && !found_features.contains(&feature)
1771 })
1772 .sorted()
1773 .take(5)
1774 .collect()
1775 }
1776
1777 fn report_unknown_features_error(
1778 &self,
1779 specs: &[PackageIdSpec],
1780 cli_features: &CliFeatures,
1781 found_features: &BTreeSet<FeatureValue>,
1782 ) -> CargoResult<()> {
1783 let unknown: Vec<_> = cli_features
1784 .features
1785 .difference(found_features)
1786 .map(|feature| feature.to_string())
1787 .sorted()
1788 .collect();
1789
1790 let (selected_members, unselected_members): (Vec<_>, Vec<_>) = self
1791 .members()
1792 .partition(|member| specs.iter().any(|spec| spec.matches(member.package_id())));
1793
1794 let missing_packages_with_the_features = unselected_members
1795 .into_iter()
1796 .filter(|member| {
1797 unknown
1798 .iter()
1799 .any(|feature| member.summary().features().contains_key(&**feature))
1800 })
1801 .map(|m| m.name())
1802 .collect_vec();
1803
1804 let these_features = if unknown.len() == 1 {
1805 "this feature"
1806 } else {
1807 "these features"
1808 };
1809 let mut msg = if let [singular] = &selected_members[..] {
1810 format!(
1811 "the package '{}' does not contain {these_features}: {}",
1812 singular.name(),
1813 unknown.join(", ")
1814 )
1815 } else {
1816 let names = selected_members.iter().map(|m| m.name()).join(", ");
1817 format!(
1818 "none of the selected packages contains {these_features}: {}\nselected packages: {names}",
1819 unknown.join(", ")
1820 )
1821 };
1822
1823 use std::fmt::Write;
1824 if !missing_packages_with_the_features.is_empty() {
1825 write!(
1826 &mut msg,
1827 "\nhelp: package{} with the missing feature{}: {}",
1828 if missing_packages_with_the_features.len() != 1 {
1829 "s"
1830 } else {
1831 ""
1832 },
1833 if unknown.len() != 1 { "s" } else { "" },
1834 missing_packages_with_the_features.join(", ")
1835 )?;
1836 } else {
1837 let suggestions = self.missing_feature_spelling_suggestions(
1838 &selected_members,
1839 cli_features,
1840 found_features,
1841 );
1842 if !suggestions.is_empty() {
1843 write!(
1844 &mut msg,
1845 "\nhelp: there {}: {}",
1846 if suggestions.len() == 1 {
1847 "is a similarly named feature"
1848 } else {
1849 "are similarly named features"
1850 },
1851 suggestions.join(", ")
1852 )?;
1853 }
1854 }
1855
1856 bail!("{msg}")
1857 }
1858
1859 fn members_with_features_new(
1862 &self,
1863 specs: &[PackageIdSpec],
1864 cli_features: &CliFeatures,
1865 ) -> CargoResult<Vec<(&Package, CliFeatures)>> {
1866 let mut found_features = Default::default();
1869
1870 let members: Vec<(&Package, CliFeatures)> = self
1871 .members()
1872 .filter(|m| specs.iter().any(|spec| spec.matches(m.package_id())))
1873 .map(|m| {
1874 (
1875 m,
1876 Workspace::collect_matching_features(m, cli_features, &mut found_features),
1877 )
1878 })
1879 .collect();
1880
1881 if members.is_empty() {
1882 if !(cli_features.features.is_empty()
1885 && !cli_features.all_features
1886 && cli_features.uses_default_features)
1887 {
1888 bail!("cannot specify features for packages outside of workspace");
1889 }
1890 return Ok(self
1893 .members()
1894 .map(|m| (m, CliFeatures::new_all(false)))
1895 .collect());
1896 }
1897 if *cli_features.features != found_features {
1898 self.report_unknown_features_error(specs, cli_features, &found_features)?;
1899 }
1900 Ok(members)
1901 }
1902
1903 fn members_with_features_old(
1906 &self,
1907 specs: &[PackageIdSpec],
1908 cli_features: &CliFeatures,
1909 ) -> Vec<(&Package, CliFeatures)> {
1910 let mut member_specific_features: HashMap<InternedString, BTreeSet<FeatureValue>> =
1913 HashMap::new();
1914 let mut cwd_features = BTreeSet::new();
1916 for feature in cli_features.features.iter() {
1917 match feature {
1918 FeatureValue::Feature(_) => {
1919 cwd_features.insert(feature.clone());
1920 }
1921 FeatureValue::Dep { .. } => panic!("unexpected dep: syntax {}", feature),
1923 FeatureValue::DepFeature {
1924 dep_name,
1925 dep_feature,
1926 weak: _,
1927 } => {
1928 let is_member = self.members().any(|member| {
1934 self.current_opt() != Some(member) && member.name() == *dep_name
1936 });
1937 if is_member && specs.iter().any(|spec| spec.name() == dep_name.as_str()) {
1938 member_specific_features
1939 .entry(*dep_name)
1940 .or_default()
1941 .insert(FeatureValue::Feature(*dep_feature));
1942 } else {
1943 cwd_features.insert(feature.clone());
1944 }
1945 }
1946 }
1947 }
1948
1949 let ms: Vec<_> = self
1950 .members()
1951 .filter_map(|member| {
1952 let member_id = member.package_id();
1953 match self.current_opt() {
1954 Some(current) if member_id == current.package_id() => {
1957 let feats = CliFeatures {
1958 features: Rc::new(cwd_features.clone()),
1959 all_features: cli_features.all_features,
1960 uses_default_features: cli_features.uses_default_features,
1961 };
1962 Some((member, feats))
1963 }
1964 _ => {
1965 if specs.iter().any(|spec| spec.matches(member_id)) {
1967 let feats = CliFeatures {
1977 features: Rc::new(
1978 member_specific_features
1979 .remove(member.name().as_str())
1980 .unwrap_or_default(),
1981 ),
1982 uses_default_features: true,
1983 all_features: cli_features.all_features,
1984 };
1985 Some((member, feats))
1986 } else {
1987 None
1989 }
1990 }
1991 }
1992 })
1993 .collect();
1994
1995 assert!(member_specific_features.is_empty());
1998
1999 ms
2000 }
2001
2002 pub fn unit_needs_doc_scrape(&self, unit: &Unit) -> bool {
2004 self.is_member(&unit.pkg) && !(unit.target.for_host() || unit.pkg.proc_macro())
2009 }
2010
2011 pub fn add_local_overlay(&mut self, id: SourceId, registry_path: PathBuf) {
2015 self.local_overlays.insert(id, registry_path);
2016 }
2017
2018 pub fn package_registry(&self) -> CargoResult<PackageRegistry<'gctx>> {
2020 let source_config =
2021 SourceConfigMap::new_with_overlays(self.gctx(), self.local_overlays()?)?;
2022 PackageRegistry::new_with_source_config(self.gctx(), source_config)
2023 }
2024
2025 fn local_overlays(&self) -> CargoResult<impl Iterator<Item = (SourceId, SourceId)>> {
2027 let mut ret = self
2028 .local_overlays
2029 .iter()
2030 .map(|(id, path)| Ok((*id, SourceId::for_local_registry(path)?)))
2031 .collect::<CargoResult<Vec<_>>>()?;
2032
2033 if let Ok(overlay) = self
2034 .gctx
2035 .get_env("__CARGO_TEST_DEPENDENCY_CONFUSION_VULNERABILITY_DO_NOT_USE_THIS")
2036 {
2037 let (url, path) = overlay.split_once('=').ok_or(anyhow::anyhow!(
2038 "invalid overlay format. I won't tell you why; you shouldn't be using it anyway"
2039 ))?;
2040 ret.push((
2041 SourceId::from_url(url)?,
2042 SourceId::for_local_registry(path.as_ref())?,
2043 ));
2044 }
2045
2046 Ok(ret.into_iter())
2047 }
2048}
2049
2050impl<'gctx> Packages<'gctx> {
2051 fn get(&self, manifest_path: &Path) -> &MaybePackage {
2052 self.maybe_get(manifest_path).unwrap()
2053 }
2054
2055 fn get_mut(&mut self, manifest_path: &Path) -> &mut MaybePackage {
2056 self.maybe_get_mut(manifest_path).unwrap()
2057 }
2058
2059 fn maybe_get(&self, manifest_path: &Path) -> Option<&MaybePackage> {
2060 self.packages.get(manifest_path)
2061 }
2062
2063 fn maybe_get_mut(&mut self, manifest_path: &Path) -> Option<&mut MaybePackage> {
2064 self.packages.get_mut(manifest_path)
2065 }
2066
2067 fn load(&mut self, manifest_path: &Path) -> CargoResult<&MaybePackage> {
2068 match self.packages.entry(manifest_path.to_path_buf()) {
2069 Entry::Occupied(e) => Ok(e.into_mut()),
2070 Entry::Vacant(v) => {
2071 let source_id = SourceId::for_manifest_path(manifest_path)?;
2072 let manifest = read_manifest(manifest_path, source_id, self.gctx)?;
2073 Ok(v.insert(match manifest {
2074 EitherManifest::Real(manifest) => {
2075 MaybePackage::Package(Package::new(manifest, manifest_path))
2076 }
2077 EitherManifest::Virtual(vm) => MaybePackage::Virtual(vm),
2078 }))
2079 }
2080 }
2081 }
2082}
2083
2084impl MaybePackage {
2085 fn workspace_config(&self) -> &WorkspaceConfig {
2086 match *self {
2087 MaybePackage::Package(ref p) => p.manifest().workspace_config(),
2088 MaybePackage::Virtual(ref vm) => vm.workspace_config(),
2089 }
2090 }
2091
2092 pub fn is_embedded(&self) -> bool {
2094 match self {
2095 MaybePackage::Package(p) => p.manifest().is_embedded(),
2096 MaybePackage::Virtual(_) => false,
2097 }
2098 }
2099
2100 pub fn contents(&self) -> Option<&str> {
2101 match self {
2102 MaybePackage::Package(p) => p.manifest().contents(),
2103 MaybePackage::Virtual(v) => v.contents(),
2104 }
2105 }
2106
2107 pub fn document(&self) -> Option<&toml::Spanned<toml::de::DeTable<'static>>> {
2108 match self {
2109 MaybePackage::Package(p) => p.manifest().document(),
2110 MaybePackage::Virtual(v) => v.document(),
2111 }
2112 }
2113
2114 pub fn original_toml(&self) -> Option<&TomlManifest> {
2115 match self {
2116 MaybePackage::Package(p) => p.manifest().original_toml(),
2117 MaybePackage::Virtual(v) => v.original_toml(),
2118 }
2119 }
2120
2121 pub fn normalized_toml(&self) -> &TomlManifest {
2122 match self {
2123 MaybePackage::Package(p) => p.manifest().normalized_toml(),
2124 MaybePackage::Virtual(v) => v.normalized_toml(),
2125 }
2126 }
2127
2128 pub fn edition(&self) -> Edition {
2129 match self {
2130 MaybePackage::Package(p) => p.manifest().edition(),
2131 MaybePackage::Virtual(_) => Edition::default(),
2132 }
2133 }
2134
2135 pub fn profiles(&self) -> Option<&TomlProfiles> {
2136 match self {
2137 MaybePackage::Package(p) => p.manifest().profiles(),
2138 MaybePackage::Virtual(v) => v.profiles(),
2139 }
2140 }
2141
2142 pub fn unstable_features(&self) -> &Features {
2143 match self {
2144 MaybePackage::Package(p) => p.manifest().unstable_features(),
2145 MaybePackage::Virtual(vm) => vm.unstable_features(),
2146 }
2147 }
2148}
2149
2150impl WorkspaceRootConfig {
2151 pub fn new(
2153 root_dir: &Path,
2154 members: &Option<Vec<String>>,
2155 default_members: &Option<Vec<String>>,
2156 exclude: &Option<Vec<String>>,
2157 inheritable: &Option<InheritableFields>,
2158 custom_metadata: &Option<toml::Value>,
2159 ) -> WorkspaceRootConfig {
2160 WorkspaceRootConfig {
2161 root_dir: root_dir.to_path_buf(),
2162 members: members.clone(),
2163 default_members: default_members.clone(),
2164 exclude: exclude.clone().unwrap_or_default(),
2165 inheritable_fields: inheritable.clone().unwrap_or_default(),
2166 custom_metadata: custom_metadata.clone(),
2167 }
2168 }
2169 fn is_excluded(&self, manifest_path: &Path) -> bool {
2173 let excluded = self
2174 .exclude
2175 .iter()
2176 .any(|ex| manifest_path.starts_with(self.root_dir.join(ex)));
2177
2178 let explicit_member = match self.members {
2179 Some(ref members) => members
2180 .iter()
2181 .any(|mem| manifest_path.starts_with(self.root_dir.join(mem))),
2182 None => false,
2183 };
2184
2185 !explicit_member && excluded
2186 }
2187
2188 fn is_explicitly_listed_member(&self, manifest_path: &Path) -> bool {
2199 let root_manifest = self.root_dir.join("Cargo.toml");
2200 if manifest_path == root_manifest {
2201 return true;
2202 }
2203 match self.members {
2204 Some(ref members) => {
2205 let Ok(expanded_members) = self.members_paths(members) else {
2207 return false;
2208 };
2209 let normalized_manifest = paths::normalize_path(manifest_path);
2211 expanded_members.iter().any(|(member_path, _)| {
2212 let normalized_member = paths::normalize_path(member_path);
2214 normalized_manifest.parent() == Some(normalized_member.as_path())
2217 })
2218 }
2219 None => false,
2220 }
2221 }
2222
2223 fn has_members_list(&self) -> bool {
2224 self.members.is_some()
2225 }
2226
2227 fn has_default_members(&self) -> bool {
2229 self.default_members.is_some()
2230 }
2231
2232 #[tracing::instrument(skip_all)]
2235 fn members_paths<'g>(
2236 &self,
2237 globs: &'g [String],
2238 ) -> CargoResult<Vec<(PathBuf, Option<&'g str>)>> {
2239 let mut expanded_list = Vec::new();
2240
2241 for glob in globs {
2242 let pathbuf = self.root_dir.join(glob);
2243 let expanded_paths = Self::expand_member_path(&pathbuf)?;
2244
2245 if expanded_paths.is_empty() {
2248 expanded_list.push((pathbuf, None));
2249 } else {
2250 let used_glob_pattern = expanded_paths.len() > 1 || expanded_paths[0] != pathbuf;
2251 let glob = used_glob_pattern.then_some(glob.as_str());
2252
2253 for expanded_path in expanded_paths {
2259 if expanded_path.is_dir() {
2260 expanded_list.push((expanded_path, glob));
2261 }
2262 }
2263 }
2264 }
2265
2266 Ok(expanded_list)
2267 }
2268
2269 fn expand_member_path(path: &Path) -> CargoResult<Vec<PathBuf>> {
2270 let Some(path) = path.to_str() else {
2271 return Ok(Vec::new());
2272 };
2273 let res = glob(path).with_context(|| format!("could not parse pattern `{}`", &path))?;
2274 let res = res
2275 .map(|p| p.with_context(|| format!("unable to match path to pattern `{}`", &path)))
2276 .collect::<Result<Vec<_>, _>>()?;
2277 Ok(res)
2278 }
2279
2280 pub fn inheritable(&self) -> &InheritableFields {
2281 &self.inheritable_fields
2282 }
2283}
2284
2285pub fn resolve_relative_path(
2286 label: &str,
2287 old_root: &Path,
2288 new_root: &Path,
2289 rel_path: &str,
2290) -> CargoResult<String> {
2291 let joined_path = normalize_path(&old_root.join(rel_path));
2292 match diff_paths(joined_path, new_root) {
2293 None => Err(anyhow!(
2294 "`{}` was defined in {} but could not be resolved with {}",
2295 label,
2296 old_root.display(),
2297 new_root.display()
2298 )),
2299 Some(path) => Ok(path
2300 .to_str()
2301 .ok_or_else(|| {
2302 anyhow!(
2303 "`{}` resolved to non-UTF value (`{}`)",
2304 label,
2305 path.display()
2306 )
2307 })?
2308 .to_owned()),
2309 }
2310}
2311
2312pub fn find_workspace_root(
2314 manifest_path: &Path,
2315 gctx: &GlobalContext,
2316) -> CargoResult<Option<PathBuf>> {
2317 find_workspace_root_with_loader(manifest_path, gctx, |self_path| {
2318 let source_id = SourceId::for_manifest_path(self_path)?;
2319 let manifest = read_manifest(self_path, source_id, gctx)?;
2320 Ok(manifest
2321 .workspace_config()
2322 .get_ws_root(self_path, manifest_path))
2323 })
2324}
2325
2326pub fn find_workspace_root_with_membership_check(
2333 manifest_path: &Path,
2334 gctx: &GlobalContext,
2335) -> CargoResult<Option<PathBuf>> {
2336 let source_id = SourceId::for_manifest_path(manifest_path)?;
2337 let current_manifest = read_manifest(manifest_path, source_id, gctx)?;
2338
2339 match current_manifest.workspace_config() {
2340 WorkspaceConfig::Root(root_config) => {
2341 if root_config.has_default_members() {
2344 Ok(None)
2345 } else {
2346 Ok(Some(manifest_path.to_path_buf()))
2347 }
2348 }
2349 WorkspaceConfig::Member {
2350 root: Some(path_to_root),
2351 } => {
2352 let ws_manifest_path = read_root_pointer(manifest_path, path_to_root);
2354 let ws_source_id = SourceId::for_manifest_path(&ws_manifest_path)?;
2355 let ws_manifest = read_manifest(&ws_manifest_path, ws_source_id, gctx)?;
2356
2357 if let WorkspaceConfig::Root(ref root_config) = *ws_manifest.workspace_config() {
2359 if root_config.is_explicitly_listed_member(manifest_path)
2360 && !root_config.is_excluded(manifest_path)
2361 {
2362 return Ok(Some(ws_manifest_path));
2363 }
2364 }
2365 Ok(None)
2367 }
2368 WorkspaceConfig::Member { root: None } => {
2369 find_workspace_root_with_loader(manifest_path, gctx, |candidate_manifest_path| {
2371 let source_id = SourceId::for_manifest_path(candidate_manifest_path)?;
2372 let manifest = read_manifest(candidate_manifest_path, source_id, gctx)?;
2373 if let WorkspaceConfig::Root(ref root_config) = *manifest.workspace_config() {
2374 if root_config.is_explicitly_listed_member(manifest_path)
2375 && !root_config.is_excluded(manifest_path)
2376 {
2377 return Ok(Some(candidate_manifest_path.to_path_buf()));
2378 }
2379 }
2380 Ok(None)
2381 })
2382 }
2383 }
2384}
2385
2386fn find_workspace_root_with_loader(
2391 manifest_path: &Path,
2392 gctx: &GlobalContext,
2393 mut loader: impl FnMut(&Path) -> CargoResult<Option<PathBuf>>,
2394) -> CargoResult<Option<PathBuf>> {
2395 {
2397 let roots = gctx.ws_roots();
2398 for current in manifest_path.ancestors().skip(1) {
2401 if let Some(ws_config) = roots.get(current) {
2402 if !ws_config.is_excluded(manifest_path) {
2403 return Ok(Some(current.join("Cargo.toml")));
2405 }
2406 }
2407 }
2408 }
2409
2410 for ances_manifest_path in find_root_iter(manifest_path, gctx) {
2411 debug!("find_root - trying {}", ances_manifest_path.display());
2412 if let Some(ws_root_path) = loader(&ances_manifest_path)? {
2413 return Ok(Some(ws_root_path));
2414 }
2415 }
2416 Ok(None)
2417}
2418
2419fn read_root_pointer(member_manifest: &Path, root_link: &str) -> PathBuf {
2420 let path = member_manifest
2421 .parent()
2422 .unwrap()
2423 .join(root_link)
2424 .join("Cargo.toml");
2425 debug!("find_root - pointer {}", path.display());
2426 paths::normalize_path(&path)
2427}
2428
2429fn find_root_iter<'a>(
2430 manifest_path: &'a Path,
2431 gctx: &'a GlobalContext,
2432) -> impl Iterator<Item = PathBuf> + 'a {
2433 LookBehind::new(paths::ancestors(manifest_path, None).skip(2))
2434 .take_while(|path| !path.curr.ends_with("target/package"))
2435 .take_while(|path| {
2441 if let Some(last) = path.last {
2442 gctx.home() != last
2443 } else {
2444 true
2445 }
2446 })
2447 .map(|path| path.curr.join("Cargo.toml"))
2448 .filter(|ances_manifest_path| ances_manifest_path.exists())
2449}
2450
2451struct LookBehindWindow<'a, T: ?Sized> {
2452 curr: &'a T,
2453 last: Option<&'a T>,
2454}
2455
2456struct LookBehind<'a, T: ?Sized, K: Iterator<Item = &'a T>> {
2457 iter: K,
2458 last: Option<&'a T>,
2459}
2460
2461impl<'a, T: ?Sized, K: Iterator<Item = &'a T>> LookBehind<'a, T, K> {
2462 fn new(items: K) -> Self {
2463 Self {
2464 iter: items,
2465 last: None,
2466 }
2467 }
2468}
2469
2470impl<'a, T: ?Sized, K: Iterator<Item = &'a T>> Iterator for LookBehind<'a, T, K> {
2471 type Item = LookBehindWindow<'a, T>;
2472
2473 fn next(&mut self) -> Option<Self::Item> {
2474 match self.iter.next() {
2475 None => None,
2476 Some(next) => {
2477 let last = self.last;
2478 self.last = Some(next);
2479 Some(LookBehindWindow { curr: next, last })
2480 }
2481 }
2482 }
2483}