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, closest_msg, context::CargoResolverConfig,
51 context::ConfigRelativePath, 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 let replacements: [(&str, &str); 0] = [];
374 let path = lockfile_path
375 .resolve_templated_path(self.gctx(), replacements)
376 .map_err(|e| match e {
377 context::ResolveTemplateError::UnexpectedVariable {
378 variable,
379 raw_template,
380 } => {
381 anyhow!(
382 "unexpected variable `{variable}` in resolver.lockfile-path `{raw_template}`"
383 )
384 }
385 context::ResolveTemplateError::UnexpectedBracket { bracket_type, raw_template } => {
386 let (btype, literal) = match bracket_type {
387 context::BracketType::Opening => ("opening", "{"),
388 context::BracketType::Closing => ("closing", "}"),
389 };
390
391 anyhow!(
392 "unexpected {btype} bracket `{literal}` in build.build-dir path `{raw_template}`"
393 )
394 }
395 })?;
396 if !path.ends_with(LOCKFILE_NAME) {
397 bail!("the `resolver.lockfile-path` must be a path to a {LOCKFILE_NAME} file");
398 }
399 if path.is_dir() {
400 bail!(
401 "`resolver.lockfile-path` `{}` is a directory but expected a file",
402 path.display()
403 );
404 }
405 self.requested_lockfile_path = Some(path);
406 }
407
408 Ok(())
409 }
410
411 pub fn current(&self) -> CargoResult<&Package> {
417 let pkg = self.current_opt().ok_or_else(|| {
418 anyhow::format_err!(
419 "manifest path `{}` is a virtual manifest, but this \
420 command requires running against an actual package in \
421 this workspace",
422 self.current_manifest.display()
423 )
424 })?;
425 Ok(pkg)
426 }
427
428 pub fn current_mut(&mut self) -> CargoResult<&mut Package> {
429 let cm = self.current_manifest.clone();
430 let pkg = self.current_opt_mut().ok_or_else(|| {
431 anyhow::format_err!(
432 "manifest path `{}` is a virtual manifest, but this \
433 command requires running against an actual package in \
434 this workspace",
435 cm.display()
436 )
437 })?;
438 Ok(pkg)
439 }
440
441 pub fn current_opt(&self) -> Option<&Package> {
442 match *self.packages.get(&self.current_manifest) {
443 MaybePackage::Package(ref p) => Some(p),
444 MaybePackage::Virtual(..) => None,
445 }
446 }
447
448 pub fn current_opt_mut(&mut self) -> Option<&mut Package> {
449 match *self.packages.get_mut(&self.current_manifest) {
450 MaybePackage::Package(ref mut p) => Some(p),
451 MaybePackage::Virtual(..) => None,
452 }
453 }
454
455 pub fn is_virtual(&self) -> bool {
456 match *self.packages.get(&self.current_manifest) {
457 MaybePackage::Package(..) => false,
458 MaybePackage::Virtual(..) => true,
459 }
460 }
461
462 pub fn gctx(&self) -> &'gctx GlobalContext {
464 self.gctx
465 }
466
467 pub fn profiles(&self) -> Option<&TomlProfiles> {
468 self.root_maybe().profiles()
469 }
470
471 pub fn root(&self) -> &Path {
476 self.root_manifest().parent().unwrap()
477 }
478
479 pub fn root_manifest(&self) -> &Path {
482 self.root_manifest
483 .as_ref()
484 .unwrap_or(&self.current_manifest)
485 }
486
487 pub fn root_maybe(&self) -> &MaybePackage {
489 self.packages.get(self.root_manifest())
490 }
491
492 pub fn target_dir(&self) -> Filesystem {
493 self.target_dir
494 .clone()
495 .unwrap_or_else(|| self.default_target_dir())
496 }
497
498 pub fn build_dir(&self) -> Filesystem {
499 self.build_dir
500 .clone()
501 .or_else(|| self.target_dir.clone())
502 .unwrap_or_else(|| self.default_build_dir())
503 }
504
505 fn default_target_dir(&self) -> Filesystem {
506 if self.root_maybe().is_embedded() {
507 self.build_dir().join("target")
508 } else {
509 Filesystem::new(self.root().join("target"))
510 }
511 }
512
513 fn default_build_dir(&self) -> Filesystem {
514 if self.root_maybe().is_embedded() {
515 let default = ConfigRelativePath::new(
516 "{cargo-cache-home}/build/{workspace-path-hash}"
517 .to_owned()
518 .into(),
519 );
520 self.gctx()
521 .custom_build_dir(&default, self.root_manifest())
522 .expect("template is correct")
523 } else {
524 self.default_target_dir()
525 }
526 }
527
528 pub fn root_replace(&self) -> &[(PackageIdSpec, Dependency)] {
532 match self.root_maybe() {
533 MaybePackage::Package(p) => p.manifest().replace(),
534 MaybePackage::Virtual(vm) => vm.replace(),
535 }
536 }
537
538 fn config_patch(&self) -> CargoResult<HashMap<Url, Vec<Patch>>> {
539 let config_patch: Option<
540 BTreeMap<String, BTreeMap<String, Value<TomlDependency<ConfigRelativePath>>>>,
541 > = self.gctx.get("patch")?;
542
543 let source = SourceId::for_manifest_path(self.root_manifest())?;
544
545 let mut warnings = Vec::new();
546
547 let mut patch = HashMap::new();
548 for (url, deps) in config_patch.into_iter().flatten() {
549 let url = match &url[..] {
550 CRATES_IO_REGISTRY => CRATES_IO_INDEX.parse().unwrap(),
551 url => self
552 .gctx
553 .get_registry_index(url)
554 .or_else(|_| url.into_url())
555 .with_context(|| {
556 format!("[patch] entry `{}` should be a URL or registry name", url)
557 })?,
558 };
559 patch.insert(
560 url,
561 deps.iter()
562 .map(|(name, dependency_cv)| {
563 crate::util::toml::config_patch_to_dependency(
564 &dependency_cv.val,
565 name,
566 source,
567 self.gctx,
568 &mut warnings,
569 )
570 .map(|dep| Patch {
571 dep,
572 loc: PatchLocation::Config(dependency_cv.definition.clone()),
573 })
574 })
575 .collect::<CargoResult<Vec<_>>>()?,
576 );
577 }
578
579 for message in warnings {
580 self.gctx
581 .shell()
582 .warn(format!("[patch] in cargo config: {}", message))?
583 }
584
585 Ok(patch)
586 }
587
588 pub fn root_patch(&self) -> CargoResult<HashMap<Url, Vec<Patch>>> {
592 let from_manifest = match self.root_maybe() {
593 MaybePackage::Package(p) => p.manifest().patch(),
594 MaybePackage::Virtual(vm) => vm.patch(),
595 };
596
597 let from_config = self.config_patch()?;
598 if from_config.is_empty() {
599 return Ok(from_manifest.clone());
600 }
601 if from_manifest.is_empty() {
602 return Ok(from_config);
603 }
604
605 let mut combined = from_config;
608 for (url, deps_from_manifest) in from_manifest {
609 if let Some(deps_from_config) = combined.get_mut(url) {
610 let mut from_manifest_pruned = deps_from_manifest.clone();
613 for dep_from_config in &mut *deps_from_config {
614 if let Some(i) = from_manifest_pruned.iter().position(|dep_from_manifest| {
615 dep_from_config.dep.name_in_toml() == dep_from_manifest.dep.name_in_toml()
617 }) {
618 from_manifest_pruned.swap_remove(i);
619 }
620 }
621 deps_from_config.extend(from_manifest_pruned);
623 } else {
624 combined.insert(url.clone(), deps_from_manifest.clone());
625 }
626 }
627 Ok(combined)
628 }
629
630 pub fn members(&self) -> impl Iterator<Item = &Package> {
632 let packages = &self.packages;
633 self.members
634 .iter()
635 .filter_map(move |path| match packages.get(path) {
636 MaybePackage::Package(p) => Some(p),
637 _ => None,
638 })
639 }
640
641 pub fn members_mut(&mut self) -> impl Iterator<Item = &mut Package> {
643 let packages = &mut self.packages.packages;
644 let members: HashSet<_> = self.members.iter().map(|path| path).collect();
645
646 packages.iter_mut().filter_map(move |(path, package)| {
647 if members.contains(path) {
648 if let MaybePackage::Package(p) = package {
649 return Some(p);
650 }
651 }
652
653 None
654 })
655 }
656
657 pub fn default_members<'a>(&'a self) -> impl Iterator<Item = &'a Package> {
659 let packages = &self.packages;
660 self.default_members
661 .iter()
662 .filter_map(move |path| match packages.get(path) {
663 MaybePackage::Package(p) => Some(p),
664 _ => None,
665 })
666 }
667
668 pub fn default_members_mut(&mut self) -> impl Iterator<Item = &mut Package> {
670 let packages = &mut self.packages.packages;
671 let members: HashSet<_> = self
672 .default_members
673 .iter()
674 .map(|path| path.parent().unwrap().to_owned())
675 .collect();
676
677 packages.iter_mut().filter_map(move |(path, package)| {
678 if members.contains(path) {
679 if let MaybePackage::Package(p) = package {
680 return Some(p);
681 }
682 }
683
684 None
685 })
686 }
687
688 pub fn is_member(&self, pkg: &Package) -> bool {
690 self.member_ids.contains(&pkg.package_id())
691 }
692
693 pub fn is_member_id(&self, package_id: PackageId) -> bool {
695 self.member_ids.contains(&package_id)
696 }
697
698 pub fn is_ephemeral(&self) -> bool {
699 self.is_ephemeral
700 }
701
702 pub fn require_optional_deps(&self) -> bool {
703 self.require_optional_deps
704 }
705
706 pub fn set_require_optional_deps(
707 &mut self,
708 require_optional_deps: bool,
709 ) -> &mut Workspace<'gctx> {
710 self.require_optional_deps = require_optional_deps;
711 self
712 }
713
714 pub fn ignore_lock(&self) -> bool {
715 self.ignore_lock
716 }
717
718 pub fn set_ignore_lock(&mut self, ignore_lock: bool) -> &mut Workspace<'gctx> {
719 self.ignore_lock = ignore_lock;
720 self
721 }
722
723 pub fn lock_root(&self) -> Filesystem {
725 if let Some(requested) = self.requested_lockfile_path.as_ref() {
726 return Filesystem::new(
727 requested
728 .parent()
729 .expect("Lockfile path can't be root")
730 .to_owned(),
731 );
732 }
733 self.default_lock_root()
734 }
735
736 fn default_lock_root(&self) -> Filesystem {
737 if self.root_maybe().is_embedded() {
738 let workspace_manifest_path = self.root_manifest();
741 let real_path = std::fs::canonicalize(workspace_manifest_path)
742 .unwrap_or_else(|_err| workspace_manifest_path.to_owned());
743 let hash = crate::util::hex::short_hash(&real_path);
744 self.build_dir().join(hash)
745 } else {
746 Filesystem::new(self.root().to_owned())
747 }
748 }
749
750 pub fn set_requested_lockfile_path(&mut self, path: Option<PathBuf>) {
752 self.requested_lockfile_path = path;
753 }
754
755 pub fn requested_lockfile_path(&self) -> Option<&Path> {
756 self.requested_lockfile_path.as_deref()
757 }
758
759 pub fn lowest_rust_version(&self) -> Option<&RustVersion> {
762 self.members().filter_map(|pkg| pkg.rust_version()).min()
763 }
764
765 pub fn set_resolve_honors_rust_version(&mut self, honor_rust_version: Option<bool>) {
766 if let Some(honor_rust_version) = honor_rust_version {
767 self.resolve_honors_rust_version = honor_rust_version;
768 }
769 }
770
771 pub fn resolve_honors_rust_version(&self) -> bool {
772 self.resolve_honors_rust_version
773 }
774
775 pub fn set_resolve_feature_unification(&mut self, feature_unification: FeatureUnification) {
776 self.resolve_feature_unification = feature_unification;
777 }
778
779 pub fn resolve_feature_unification(&self) -> FeatureUnification {
780 self.resolve_feature_unification
781 }
782
783 pub fn set_resolve_publish_time(&mut self, publish_time: jiff::Timestamp) {
784 self.resolve_publish_time = Some(publish_time);
785 }
786
787 pub fn resolve_publish_time(&self) -> Option<jiff::Timestamp> {
788 self.resolve_publish_time
789 }
790
791 pub fn custom_metadata(&self) -> Option<&toml::Value> {
792 self.custom_metadata.as_ref()
793 }
794
795 pub fn load_workspace_config(&mut self) -> CargoResult<Option<WorkspaceRootConfig>> {
796 if let Some(root_path) = &self.root_manifest {
799 let root_package = self.packages.load(root_path)?;
800 match root_package.workspace_config() {
801 WorkspaceConfig::Root(root_config) => {
802 return Ok(Some(root_config.clone()));
803 }
804
805 _ => bail!(
806 "root of a workspace inferred but wasn't a root: {}",
807 root_path.display()
808 ),
809 }
810 }
811
812 Ok(None)
813 }
814
815 fn find_root(&mut self, manifest_path: &Path) -> CargoResult<Option<PathBuf>> {
825 let current = self.packages.load(manifest_path)?;
826 match current
827 .workspace_config()
828 .get_ws_root(manifest_path, manifest_path)
829 {
830 Some(root_path) => {
831 debug!("find_root - is root {}", manifest_path.display());
832 Ok(Some(root_path))
833 }
834 None => find_workspace_root_with_loader(manifest_path, self.gctx, |self_path| {
835 Ok(self
836 .packages
837 .load(self_path)?
838 .workspace_config()
839 .get_ws_root(self_path, manifest_path))
840 }),
841 }
842 }
843
844 #[tracing::instrument(skip_all)]
852 fn find_members(&mut self) -> CargoResult<()> {
853 let Some(workspace_config) = self.load_workspace_config()? else {
854 debug!("find_members - only me as a member");
855 self.members.push(self.current_manifest.clone());
856 self.default_members.push(self.current_manifest.clone());
857 if let Ok(pkg) = self.current() {
858 let id = pkg.package_id();
859 self.member_ids.insert(id);
860 }
861 return Ok(());
862 };
863
864 let root_manifest_path = self.root_manifest.clone().unwrap();
866
867 let members_paths = workspace_config
868 .members_paths(workspace_config.members.as_deref().unwrap_or_default())?;
869 let default_members_paths = if root_manifest_path == self.current_manifest {
870 if let Some(ref default) = workspace_config.default_members {
871 Some(workspace_config.members_paths(default)?)
872 } else {
873 None
874 }
875 } else {
876 None
877 };
878
879 for (path, glob) in &members_paths {
880 self.find_path_deps(&path.join("Cargo.toml"), &root_manifest_path, false)
881 .with_context(|| {
882 format!(
883 "failed to load manifest for workspace member `{}`\n\
884 referenced{} by workspace at `{}`",
885 path.display(),
886 glob.map(|g| format!(" via `{g}`")).unwrap_or_default(),
887 root_manifest_path.display(),
888 )
889 })?;
890 }
891
892 self.find_path_deps(&root_manifest_path, &root_manifest_path, false)?;
893
894 if let Some(default) = default_members_paths {
895 for (path, default_member_glob) in default {
896 let normalized_path = paths::normalize_path(&path);
897 let manifest_path = normalized_path.join("Cargo.toml");
898 if !self.members.contains(&manifest_path) {
899 let exclude = members_paths.iter().any(|(m, _)| *m == normalized_path)
906 && workspace_config.is_excluded(&normalized_path);
907 if exclude {
908 continue;
909 }
910 bail!(
911 "package `{}` is listed in default-members{} but is not a member\n\
912 for workspace at `{}`.",
913 path.display(),
914 default_member_glob
915 .map(|g| format!(" via `{g}`"))
916 .unwrap_or_default(),
917 root_manifest_path.display(),
918 )
919 }
920 self.default_members.push(manifest_path)
921 }
922 } else if self.is_virtual() {
923 self.default_members = self.members.clone()
924 } else {
925 self.default_members.push(self.current_manifest.clone())
926 }
927
928 Ok(())
929 }
930
931 fn find_path_deps(
932 &mut self,
933 manifest_path: &Path,
934 root_manifest: &Path,
935 is_path_dep: bool,
936 ) -> CargoResult<()> {
937 let manifest_path = paths::normalize_path(manifest_path);
938 if self.members.contains(&manifest_path) {
939 return Ok(());
940 }
941 if is_path_dep && self.root_maybe().is_embedded() {
942 return Ok(());
944 }
945 if is_path_dep
946 && !manifest_path.parent().unwrap().starts_with(self.root())
947 && self.find_root(&manifest_path)? != self.root_manifest
948 {
949 return Ok(());
952 }
953
954 if let WorkspaceConfig::Root(ref root_config) =
955 *self.packages.load(root_manifest)?.workspace_config()
956 {
957 if root_config.is_excluded(&manifest_path) {
958 return Ok(());
959 }
960 }
961
962 debug!("find_path_deps - {}", manifest_path.display());
963 self.members.push(manifest_path.clone());
964
965 let candidates = {
966 let pkg = match *self.packages.load(&manifest_path)? {
967 MaybePackage::Package(ref p) => p,
968 MaybePackage::Virtual(_) => return Ok(()),
969 };
970 self.member_ids.insert(pkg.package_id());
971 pkg.dependencies()
972 .iter()
973 .map(|d| (d.source_id(), d.package_name()))
974 .filter(|(s, _)| s.is_path())
975 .filter_map(|(s, n)| s.url().to_file_path().ok().map(|p| (p, n)))
976 .map(|(p, n)| (p.join("Cargo.toml"), n))
977 .collect::<Vec<_>>()
978 };
979 for (path, name) in candidates {
980 self.find_path_deps(&path, root_manifest, true)
981 .with_context(|| format!("failed to load manifest for dependency `{}`", name))
982 .map_err(|err| ManifestError::new(err, manifest_path.clone()))?;
983 }
984 Ok(())
985 }
986
987 pub fn unstable_features(&self) -> &Features {
989 self.root_maybe().unstable_features()
990 }
991
992 pub fn resolve_behavior(&self) -> ResolveBehavior {
993 self.resolve_behavior
994 }
995
996 pub fn allows_new_cli_feature_behavior(&self) -> bool {
1004 self.is_virtual()
1005 || match self.resolve_behavior() {
1006 ResolveBehavior::V1 => false,
1007 ResolveBehavior::V2 | ResolveBehavior::V3 => true,
1008 }
1009 }
1010
1011 #[tracing::instrument(skip_all)]
1017 fn validate(&mut self) -> CargoResult<()> {
1018 if self.root_manifest.is_none() {
1020 return Ok(());
1021 }
1022
1023 self.validate_unique_names()?;
1024 self.validate_workspace_roots()?;
1025 self.validate_members()?;
1026 self.error_if_manifest_not_in_members()?;
1027 self.validate_manifest()
1028 }
1029
1030 fn validate_unique_names(&self) -> CargoResult<()> {
1031 let mut names = BTreeMap::new();
1032 for member in self.members.iter() {
1033 let package = self.packages.get(member);
1034 let name = match *package {
1035 MaybePackage::Package(ref p) => p.name(),
1036 MaybePackage::Virtual(_) => continue,
1037 };
1038 if let Some(prev) = names.insert(name, member) {
1039 bail!(
1040 "two packages named `{}` in this workspace:\n\
1041 - {}\n\
1042 - {}",
1043 name,
1044 prev.display(),
1045 member.display()
1046 );
1047 }
1048 }
1049 Ok(())
1050 }
1051
1052 fn validate_workspace_roots(&self) -> CargoResult<()> {
1053 let roots: Vec<PathBuf> = self
1054 .members
1055 .iter()
1056 .filter(|&member| {
1057 let config = self.packages.get(member).workspace_config();
1058 matches!(config, WorkspaceConfig::Root(_))
1059 })
1060 .map(|member| member.parent().unwrap().to_path_buf())
1061 .collect();
1062 match roots.len() {
1063 1 => Ok(()),
1064 0 => bail!(
1065 "`package.workspace` configuration points to a crate \
1066 which is not configured with [workspace]: \n\
1067 configuration at: {}\n\
1068 points to: {}",
1069 self.current_manifest.display(),
1070 self.root_manifest.as_ref().unwrap().display()
1071 ),
1072 _ => {
1073 bail!(
1074 "multiple workspace roots found in the same workspace:\n{}",
1075 roots
1076 .iter()
1077 .map(|r| format!(" {}", r.display()))
1078 .collect::<Vec<_>>()
1079 .join("\n")
1080 );
1081 }
1082 }
1083 }
1084
1085 #[tracing::instrument(skip_all)]
1086 fn validate_members(&mut self) -> CargoResult<()> {
1087 for member in self.members.clone() {
1088 let root = self.find_root(&member)?;
1089 if root == self.root_manifest {
1090 continue;
1091 }
1092
1093 match root {
1094 Some(root) => {
1095 bail!(
1096 "package `{}` is a member of the wrong workspace\n\
1097 expected: {}\n\
1098 actual: {}",
1099 member.display(),
1100 self.root_manifest.as_ref().unwrap().display(),
1101 root.display()
1102 );
1103 }
1104 None => {
1105 bail!(
1106 "workspace member `{}` is not hierarchically below \
1107 the workspace root `{}`",
1108 member.display(),
1109 self.root_manifest.as_ref().unwrap().display()
1110 );
1111 }
1112 }
1113 }
1114 Ok(())
1115 }
1116
1117 fn error_if_manifest_not_in_members(&mut self) -> CargoResult<()> {
1118 if self.members.contains(&self.current_manifest) {
1119 return Ok(());
1120 }
1121
1122 let root = self.root_manifest.as_ref().unwrap();
1123 let root_dir = root.parent().unwrap();
1124 let current_dir = self.current_manifest.parent().unwrap();
1125 let root_pkg = self.packages.get(root);
1126
1127 let current_dir = paths::normalize_path(current_dir);
1133 let root_dir = paths::normalize_path(root_dir);
1134 let members_msg = match pathdiff::diff_paths(¤t_dir, &root_dir) {
1135 Some(rel) => format!(
1136 "this may be fixable by adding `{}` to the \
1137 `workspace.members` array of the manifest \
1138 located at: {}",
1139 rel.display(),
1140 root.display()
1141 ),
1142 None => format!(
1143 "this may be fixable by adding a member to \
1144 the `workspace.members` array of the \
1145 manifest located at: {}",
1146 root.display()
1147 ),
1148 };
1149 let extra = match *root_pkg {
1150 MaybePackage::Virtual(_) => members_msg,
1151 MaybePackage::Package(ref p) => {
1152 let has_members_list = match *p.manifest().workspace_config() {
1153 WorkspaceConfig::Root(ref root_config) => root_config.has_members_list(),
1154 WorkspaceConfig::Member { .. } => unreachable!(),
1155 };
1156 if !has_members_list {
1157 format!(
1158 "this may be fixable by ensuring that this \
1159 crate is depended on by the workspace \
1160 root: {}",
1161 root.display()
1162 )
1163 } else {
1164 members_msg
1165 }
1166 }
1167 };
1168 bail!(
1169 "current package believes it's in a workspace when it's not:\n\
1170 current: {}\n\
1171 workspace: {}\n\n{}\n\
1172 Alternatively, to keep it out of the workspace, add the package \
1173 to the `workspace.exclude` array, or add an empty `[workspace]` \
1174 table to the package's manifest.",
1175 self.current_manifest.display(),
1176 root.display(),
1177 extra
1178 );
1179 }
1180
1181 fn validate_manifest(&mut self) -> CargoResult<()> {
1182 if let Some(ref root_manifest) = self.root_manifest {
1183 for pkg in self
1184 .members()
1185 .filter(|p| p.manifest_path() != root_manifest)
1186 {
1187 let manifest = pkg.manifest();
1188 let emit_warning = |what| -> CargoResult<()> {
1189 let msg = format!(
1190 "{} for the non root package will be ignored, \
1191 specify {} at the workspace root:\n\
1192 package: {}\n\
1193 workspace: {}",
1194 what,
1195 what,
1196 pkg.manifest_path().display(),
1197 root_manifest.display(),
1198 );
1199 self.gctx.shell().warn(&msg)
1200 };
1201 if manifest.normalized_toml().has_profiles() {
1202 emit_warning("profiles")?;
1203 }
1204 if !manifest.replace().is_empty() {
1205 emit_warning("replace")?;
1206 }
1207 if !manifest.patch().is_empty() {
1208 emit_warning("patch")?;
1209 }
1210 if let Some(behavior) = manifest.resolve_behavior() {
1211 if behavior != self.resolve_behavior {
1212 emit_warning("resolver")?;
1214 }
1215 }
1216 }
1217 if let MaybePackage::Virtual(vm) = self.root_maybe() {
1218 if vm.resolve_behavior().is_none() {
1219 if let Some(edition) = self
1220 .members()
1221 .filter(|p| p.manifest_path() != root_manifest)
1222 .map(|p| p.manifest().edition())
1223 .filter(|&e| e >= Edition::Edition2021)
1224 .max()
1225 {
1226 let resolver = edition.default_resolve_behavior().to_manifest();
1227 let report = &[Level::WARNING
1228 .primary_title(format!(
1229 "virtual workspace defaulting to `resolver = \"1\"` despite one or more workspace members being on edition {edition} which implies `resolver = \"{resolver}\"`"
1230 ))
1231 .elements([
1232 Level::NOTE.message("to keep the current resolver, specify `workspace.resolver = \"1\"` in the workspace root's manifest"),
1233 Level::NOTE.message(
1234 format!("to use the edition {edition} resolver, specify `workspace.resolver = \"{resolver}\"` in the workspace root's manifest"),
1235 ),
1236 Level::NOTE.message("for more details see https://doc.rust-lang.org/cargo/reference/resolver.html#resolver-versions"),
1237 ])];
1238 self.gctx.shell().print_report(report, false)?;
1239 }
1240 }
1241 }
1242 }
1243 Ok(())
1244 }
1245
1246 pub fn load(&self, manifest_path: &Path) -> CargoResult<Package> {
1247 match self.packages.maybe_get(manifest_path) {
1248 Some(MaybePackage::Package(p)) => return Ok(p.clone()),
1249 Some(&MaybePackage::Virtual(_)) => bail!("cannot load workspace root"),
1250 None => {}
1251 }
1252
1253 let mut loaded = self.loaded_packages.borrow_mut();
1254 if let Some(p) = loaded.get(manifest_path).cloned() {
1255 return Ok(p);
1256 }
1257 let source_id = SourceId::for_manifest_path(manifest_path)?;
1258 let package = ops::read_package(manifest_path, source_id, self.gctx)?;
1259 loaded.insert(manifest_path.to_path_buf(), package.clone());
1260 Ok(package)
1261 }
1262
1263 pub fn preload(&self, registry: &mut PackageRegistry<'gctx>) {
1270 if self.is_ephemeral {
1276 return;
1277 }
1278
1279 for pkg in self.packages.packages.values() {
1280 let pkg = match *pkg {
1281 MaybePackage::Package(ref p) => p.clone(),
1282 MaybePackage::Virtual(_) => continue,
1283 };
1284 let src = PathSource::preload_with(pkg, self.gctx);
1285 registry.add_preloaded(Box::new(src));
1286 }
1287 }
1288
1289 pub fn emit_warnings(&self) -> CargoResult<()> {
1290 let mut first_emitted_error = None;
1291
1292 if let Err(e) = self.emit_ws_lints() {
1293 first_emitted_error = Some(e);
1294 }
1295
1296 for (path, maybe_pkg) in &self.packages.packages {
1297 if let MaybePackage::Package(pkg) = maybe_pkg {
1298 if let Err(e) = self.emit_pkg_lints(pkg, &path)
1299 && first_emitted_error.is_none()
1300 {
1301 first_emitted_error = Some(e);
1302 }
1303 }
1304 let warnings = match maybe_pkg {
1305 MaybePackage::Package(pkg) => pkg.manifest().warnings().warnings(),
1306 MaybePackage::Virtual(vm) => vm.warnings().warnings(),
1307 };
1308 for warning in warnings {
1309 if warning.is_critical {
1310 let err = anyhow::format_err!("{}", warning.message);
1311 let cx =
1312 anyhow::format_err!("failed to parse manifest at `{}`", path.display());
1313 if first_emitted_error.is_none() {
1314 first_emitted_error = Some(err.context(cx));
1315 }
1316 } else {
1317 let msg = if self.root_manifest.is_none() {
1318 warning.message.to_string()
1319 } else {
1320 format!("{}: {}", path.display(), warning.message)
1323 };
1324 self.gctx.shell().warn(msg)?
1325 }
1326 }
1327 }
1328
1329 if let Some(error) = first_emitted_error {
1330 Err(error)
1331 } else {
1332 Ok(())
1333 }
1334 }
1335
1336 pub fn emit_pkg_lints(&self, pkg: &Package, path: &Path) -> CargoResult<()> {
1337 let toml_lints = pkg
1338 .manifest()
1339 .normalized_toml()
1340 .lints
1341 .clone()
1342 .map(|lints| lints.lints)
1343 .unwrap_or(manifest::TomlLints::default());
1344 let cargo_lints = toml_lints
1345 .get("cargo")
1346 .cloned()
1347 .unwrap_or(manifest::TomlToolLints::default());
1348
1349 if self.gctx.cli_unstable().cargo_lints {
1350 let mut verify_error_count = 0;
1351
1352 analyze_cargo_lints_table(
1353 pkg.into(),
1354 &path,
1355 &cargo_lints,
1356 &mut verify_error_count,
1357 self.gctx,
1358 )?;
1359
1360 if verify_error_count > 0 {
1361 let plural = if verify_error_count == 1 { "" } else { "s" };
1362 bail!("encountered {verify_error_count} error{plural} while verifying lints")
1363 }
1364
1365 let mut run_error_count = 0;
1366
1367 check_im_a_teapot(pkg, &path, &cargo_lints, &mut run_error_count, self.gctx)?;
1368 implicit_minimum_version_req_pkg(
1369 pkg,
1370 &path,
1371 &cargo_lints,
1372 &mut run_error_count,
1373 self.gctx,
1374 )?;
1375 non_kebab_case_packages(pkg, &path, &cargo_lints, &mut run_error_count, self.gctx)?;
1376 non_snake_case_packages(pkg, &path, &cargo_lints, &mut run_error_count, self.gctx)?;
1377 non_kebab_case_bins(
1378 self,
1379 pkg,
1380 &path,
1381 &cargo_lints,
1382 &mut run_error_count,
1383 self.gctx,
1384 )?;
1385 non_kebab_case_features(pkg, &path, &cargo_lints, &mut run_error_count, self.gctx)?;
1386 non_snake_case_features(pkg, &path, &cargo_lints, &mut run_error_count, self.gctx)?;
1387 unused_build_dependencies_no_build_rs(
1388 pkg,
1389 &path,
1390 &cargo_lints,
1391 &mut run_error_count,
1392 self.gctx,
1393 )?;
1394 redundant_readme(pkg, &path, &cargo_lints, &mut run_error_count, self.gctx)?;
1395 redundant_homepage(pkg, &path, &cargo_lints, &mut run_error_count, self.gctx)?;
1396 missing_lints_inheritance(
1397 self,
1398 pkg,
1399 &path,
1400 &cargo_lints,
1401 &mut run_error_count,
1402 self.gctx,
1403 )?;
1404
1405 if run_error_count > 0 {
1406 let plural = if run_error_count == 1 { "" } else { "s" };
1407 bail!("encountered {run_error_count} error{plural} while running lints")
1408 }
1409 }
1410
1411 Ok(())
1412 }
1413
1414 pub fn emit_ws_lints(&self) -> CargoResult<()> {
1415 let mut run_error_count = 0;
1416
1417 let cargo_lints = match self.root_maybe() {
1418 MaybePackage::Package(pkg) => {
1419 let toml = pkg.manifest().normalized_toml();
1420 if let Some(ws) = &toml.workspace {
1421 ws.lints.as_ref()
1422 } else {
1423 toml.lints.as_ref().map(|l| &l.lints)
1424 }
1425 }
1426 MaybePackage::Virtual(vm) => vm
1427 .normalized_toml()
1428 .workspace
1429 .as_ref()
1430 .unwrap()
1431 .lints
1432 .as_ref(),
1433 }
1434 .and_then(|t| t.get("cargo"))
1435 .cloned()
1436 .unwrap_or(manifest::TomlToolLints::default());
1437
1438 if self.gctx.cli_unstable().cargo_lints {
1439 let mut verify_error_count = 0;
1440
1441 analyze_cargo_lints_table(
1442 (self, self.root_maybe()).into(),
1443 self.root_manifest(),
1444 &cargo_lints,
1445 &mut verify_error_count,
1446 self.gctx,
1447 )?;
1448
1449 if verify_error_count > 0 {
1450 let plural = if verify_error_count == 1 { "" } else { "s" };
1451 bail!("encountered {verify_error_count} error{plural} while verifying lints")
1452 }
1453
1454 unused_workspace_package_fields(
1455 self,
1456 self.root_maybe(),
1457 self.root_manifest(),
1458 &cargo_lints,
1459 &mut run_error_count,
1460 self.gctx,
1461 )?;
1462 unused_workspace_dependencies(
1463 self,
1464 self.root_maybe(),
1465 self.root_manifest(),
1466 &cargo_lints,
1467 &mut run_error_count,
1468 self.gctx,
1469 )?;
1470 implicit_minimum_version_req_ws(
1471 self,
1472 self.root_maybe(),
1473 self.root_manifest(),
1474 &cargo_lints,
1475 &mut run_error_count,
1476 self.gctx,
1477 )?;
1478 }
1479
1480 if self.gctx.cli_unstable().profile_hint_mostly_unused {
1484 blanket_hint_mostly_unused(
1485 self,
1486 self.root_maybe(),
1487 self.root_manifest(),
1488 &cargo_lints,
1489 &mut run_error_count,
1490 self.gctx,
1491 )?;
1492 }
1493
1494 if run_error_count > 0 {
1495 let plural = if run_error_count == 1 { "" } else { "s" };
1496 bail!("encountered {run_error_count} error{plural} while running lints")
1497 } else {
1498 Ok(())
1499 }
1500 }
1501
1502 pub fn set_target_dir(&mut self, target_dir: Filesystem) {
1503 self.target_dir = Some(target_dir);
1504 }
1505
1506 pub fn members_with_features(
1514 &self,
1515 specs: &[PackageIdSpec],
1516 cli_features: &CliFeatures,
1517 ) -> CargoResult<Vec<(&Package, CliFeatures)>> {
1518 assert!(
1519 !specs.is_empty() || cli_features.all_features,
1520 "no specs requires all_features"
1521 );
1522 if specs.is_empty() {
1523 return Ok(self
1526 .members()
1527 .map(|m| (m, CliFeatures::new_all(true)))
1528 .collect());
1529 }
1530 if self.allows_new_cli_feature_behavior() {
1531 self.members_with_features_new(specs, cli_features)
1532 } else {
1533 Ok(self.members_with_features_old(specs, cli_features))
1534 }
1535 }
1536
1537 fn collect_matching_features(
1540 member: &Package,
1541 cli_features: &CliFeatures,
1542 found_features: &mut BTreeSet<FeatureValue>,
1543 ) -> CliFeatures {
1544 if cli_features.features.is_empty() {
1545 return cli_features.clone();
1546 }
1547
1548 let summary = member.summary();
1550
1551 let summary_features = summary.features();
1553
1554 let dependencies: BTreeMap<InternedString, &Dependency> = summary
1556 .dependencies()
1557 .iter()
1558 .map(|dep| (dep.name_in_toml(), dep))
1559 .collect();
1560
1561 let optional_dependency_names: BTreeSet<_> = dependencies
1563 .iter()
1564 .filter(|(_, dep)| dep.is_optional())
1565 .map(|(name, _)| name)
1566 .copied()
1567 .collect();
1568
1569 let mut features = BTreeSet::new();
1570
1571 let summary_or_opt_dependency_feature = |feature: &InternedString| -> bool {
1573 summary_features.contains_key(feature) || optional_dependency_names.contains(feature)
1574 };
1575
1576 for feature in cli_features.features.iter() {
1577 match feature {
1578 FeatureValue::Feature(f) => {
1579 if summary_or_opt_dependency_feature(f) {
1580 features.insert(feature.clone());
1582 found_features.insert(feature.clone());
1583 }
1584 }
1585 FeatureValue::Dep { .. } => panic!("unexpected dep: syntax {}", feature),
1587 FeatureValue::DepFeature {
1588 dep_name,
1589 dep_feature,
1590 weak: _,
1591 } => {
1592 if dependencies.contains_key(dep_name) {
1593 features.insert(feature.clone());
1596 found_features.insert(feature.clone());
1597 } else if *dep_name == member.name()
1598 && summary_or_opt_dependency_feature(dep_feature)
1599 {
1600 features.insert(FeatureValue::Feature(*dep_feature));
1605 found_features.insert(feature.clone());
1606 }
1607 }
1608 }
1609 }
1610 CliFeatures {
1611 features: Rc::new(features),
1612 all_features: cli_features.all_features,
1613 uses_default_features: cli_features.uses_default_features,
1614 }
1615 }
1616
1617 fn missing_feature_spelling_suggestions(
1618 &self,
1619 selected_members: &[&Package],
1620 cli_features: &CliFeatures,
1621 found_features: &BTreeSet<FeatureValue>,
1622 ) -> Vec<String> {
1623 let mut summary_features: Vec<InternedString> = Default::default();
1625
1626 let mut dependencies_features: BTreeMap<InternedString, &[InternedString]> =
1628 Default::default();
1629
1630 let mut optional_dependency_names: Vec<InternedString> = Default::default();
1632
1633 let mut summary_features_per_member: BTreeMap<&Package, BTreeSet<InternedString>> =
1635 Default::default();
1636
1637 let mut optional_dependency_names_per_member: BTreeMap<&Package, BTreeSet<InternedString>> =
1639 Default::default();
1640
1641 for &member in selected_members {
1642 let summary = member.summary();
1644
1645 summary_features.extend(summary.features().keys());
1647 summary_features_per_member
1648 .insert(member, summary.features().keys().copied().collect());
1649
1650 let dependencies: BTreeMap<InternedString, &Dependency> = summary
1652 .dependencies()
1653 .iter()
1654 .map(|dep| (dep.name_in_toml(), dep))
1655 .collect();
1656
1657 dependencies_features.extend(
1658 dependencies
1659 .iter()
1660 .map(|(name, dep)| (*name, dep.features())),
1661 );
1662
1663 let optional_dependency_names_raw: BTreeSet<_> = dependencies
1665 .iter()
1666 .filter(|(_, dep)| dep.is_optional())
1667 .map(|(name, _)| name)
1668 .copied()
1669 .collect();
1670
1671 optional_dependency_names.extend(optional_dependency_names_raw.iter());
1672 optional_dependency_names_per_member.insert(member, optional_dependency_names_raw);
1673 }
1674
1675 let edit_distance_test = |a: InternedString, b: InternedString| {
1676 edit_distance(a.as_str(), b.as_str(), 3).is_some()
1677 };
1678
1679 cli_features
1680 .features
1681 .difference(found_features)
1682 .map(|feature| match feature {
1683 FeatureValue::Feature(typo) => {
1685 let summary_features = summary_features
1687 .iter()
1688 .filter(move |feature| edit_distance_test(**feature, *typo));
1689
1690 let optional_dependency_features = optional_dependency_names
1692 .iter()
1693 .filter(move |feature| edit_distance_test(**feature, *typo));
1694
1695 summary_features
1696 .chain(optional_dependency_features)
1697 .map(|s| s.to_string())
1698 .collect::<Vec<_>>()
1699 }
1700 FeatureValue::Dep { .. } => panic!("unexpected dep: syntax {}", feature),
1701 FeatureValue::DepFeature {
1702 dep_name,
1703 dep_feature,
1704 weak: _,
1705 } => {
1706 let pkg_feat_similar = dependencies_features
1708 .iter()
1709 .filter(|(name, _)| edit_distance_test(**name, *dep_name))
1710 .map(|(name, features)| {
1711 (
1712 name,
1713 features
1714 .iter()
1715 .filter(|feature| edit_distance_test(**feature, *dep_feature))
1716 .collect::<Vec<_>>(),
1717 )
1718 })
1719 .map(|(name, features)| {
1720 features
1721 .into_iter()
1722 .map(move |feature| format!("{}/{}", name, feature))
1723 })
1724 .flatten();
1725
1726 let optional_dependency_features = optional_dependency_names_per_member
1728 .iter()
1729 .filter(|(package, _)| edit_distance_test(package.name(), *dep_name))
1730 .map(|(package, optional_dependencies)| {
1731 optional_dependencies
1732 .into_iter()
1733 .filter(|optional_dependency| {
1734 edit_distance_test(**optional_dependency, *dep_name)
1735 })
1736 .map(move |optional_dependency| {
1737 format!("{}/{}", package.name(), optional_dependency)
1738 })
1739 })
1740 .flatten();
1741
1742 let summary_features = summary_features_per_member
1744 .iter()
1745 .filter(|(package, _)| edit_distance_test(package.name(), *dep_name))
1746 .map(|(package, summary_features)| {
1747 summary_features
1748 .into_iter()
1749 .filter(|summary_feature| {
1750 edit_distance_test(**summary_feature, *dep_feature)
1751 })
1752 .map(move |summary_feature| {
1753 format!("{}/{}", package.name(), summary_feature)
1754 })
1755 })
1756 .flatten();
1757
1758 pkg_feat_similar
1759 .chain(optional_dependency_features)
1760 .chain(summary_features)
1761 .collect::<Vec<_>>()
1762 }
1763 })
1764 .map(|v| v.into_iter())
1765 .flatten()
1766 .unique()
1767 .filter(|element| {
1768 let feature = FeatureValue::new(element.into());
1769 !cli_features.features.contains(&feature) && !found_features.contains(&feature)
1770 })
1771 .sorted()
1772 .take(5)
1773 .collect()
1774 }
1775
1776 fn report_unknown_features_error(
1777 &self,
1778 specs: &[PackageIdSpec],
1779 cli_features: &CliFeatures,
1780 found_features: &BTreeSet<FeatureValue>,
1781 ) -> CargoResult<()> {
1782 let unknown: Vec<_> = cli_features
1783 .features
1784 .difference(found_features)
1785 .map(|feature| feature.to_string())
1786 .sorted()
1787 .collect();
1788
1789 let (selected_members, unselected_members): (Vec<_>, Vec<_>) = self
1790 .members()
1791 .partition(|member| specs.iter().any(|spec| spec.matches(member.package_id())));
1792
1793 let missing_packages_with_the_features = unselected_members
1794 .into_iter()
1795 .filter(|member| {
1796 unknown
1797 .iter()
1798 .any(|feature| member.summary().features().contains_key(&**feature))
1799 })
1800 .map(|m| m.name())
1801 .collect_vec();
1802
1803 let these_features = if unknown.len() == 1 {
1804 "this feature"
1805 } else {
1806 "these features"
1807 };
1808 let mut msg = if let [singular] = &selected_members[..] {
1809 format!(
1810 "the package '{}' does not contain {these_features}: {}",
1811 singular.name(),
1812 unknown.join(", ")
1813 )
1814 } else {
1815 let names = selected_members.iter().map(|m| m.name()).join(", ");
1816 format!(
1817 "none of the selected packages contains {these_features}: {}\nselected packages: {names}",
1818 unknown.join(", ")
1819 )
1820 };
1821
1822 use std::fmt::Write;
1823 if !missing_packages_with_the_features.is_empty() {
1824 write!(
1825 &mut msg,
1826 "\nhelp: package{} with the missing feature{}: {}",
1827 if missing_packages_with_the_features.len() != 1 {
1828 "s"
1829 } else {
1830 ""
1831 },
1832 if unknown.len() != 1 { "s" } else { "" },
1833 missing_packages_with_the_features.join(", ")
1834 )?;
1835 } else {
1836 let suggestions = self.missing_feature_spelling_suggestions(
1837 &selected_members,
1838 cli_features,
1839 found_features,
1840 );
1841 if !suggestions.is_empty() {
1842 write!(
1843 &mut msg,
1844 "\nhelp: there {}: {}",
1845 if suggestions.len() == 1 {
1846 "is a similarly named feature"
1847 } else {
1848 "are similarly named features"
1849 },
1850 suggestions.join(", ")
1851 )?;
1852 }
1853 }
1854
1855 bail!("{msg}")
1856 }
1857
1858 fn members_with_features_new(
1861 &self,
1862 specs: &[PackageIdSpec],
1863 cli_features: &CliFeatures,
1864 ) -> CargoResult<Vec<(&Package, CliFeatures)>> {
1865 let mut found_features = Default::default();
1868
1869 let members: Vec<(&Package, CliFeatures)> = self
1870 .members()
1871 .filter(|m| specs.iter().any(|spec| spec.matches(m.package_id())))
1872 .map(|m| {
1873 (
1874 m,
1875 Workspace::collect_matching_features(m, cli_features, &mut found_features),
1876 )
1877 })
1878 .collect();
1879
1880 if members.is_empty() {
1881 if !(cli_features.features.is_empty()
1884 && !cli_features.all_features
1885 && cli_features.uses_default_features)
1886 {
1887 let hint = specs
1888 .iter()
1889 .map(|spec| {
1890 closest_msg(
1891 spec.name(),
1892 self.members(),
1893 |m| m.name().as_str(),
1894 "workspace member",
1895 )
1896 })
1897 .find(|msg| !msg.is_empty())
1898 .unwrap_or_default();
1899 bail!("cannot specify features for packages outside of workspace{hint}");
1900 }
1901 return Ok(self
1904 .members()
1905 .map(|m| (m, CliFeatures::new_all(false)))
1906 .collect());
1907 }
1908 if *cli_features.features != found_features {
1909 self.report_unknown_features_error(specs, cli_features, &found_features)?;
1910 }
1911 Ok(members)
1912 }
1913
1914 fn members_with_features_old(
1917 &self,
1918 specs: &[PackageIdSpec],
1919 cli_features: &CliFeatures,
1920 ) -> Vec<(&Package, CliFeatures)> {
1921 let mut member_specific_features: HashMap<InternedString, BTreeSet<FeatureValue>> =
1924 HashMap::new();
1925 let mut cwd_features = BTreeSet::new();
1927 for feature in cli_features.features.iter() {
1928 match feature {
1929 FeatureValue::Feature(_) => {
1930 cwd_features.insert(feature.clone());
1931 }
1932 FeatureValue::Dep { .. } => panic!("unexpected dep: syntax {}", feature),
1934 FeatureValue::DepFeature {
1935 dep_name,
1936 dep_feature,
1937 weak: _,
1938 } => {
1939 let is_member = self.members().any(|member| {
1945 self.current_opt() != Some(member) && member.name() == *dep_name
1947 });
1948 if is_member && specs.iter().any(|spec| spec.name() == dep_name.as_str()) {
1949 member_specific_features
1950 .entry(*dep_name)
1951 .or_default()
1952 .insert(FeatureValue::Feature(*dep_feature));
1953 } else {
1954 cwd_features.insert(feature.clone());
1955 }
1956 }
1957 }
1958 }
1959
1960 let ms: Vec<_> = self
1961 .members()
1962 .filter_map(|member| {
1963 let member_id = member.package_id();
1964 match self.current_opt() {
1965 Some(current) if member_id == current.package_id() => {
1968 let feats = CliFeatures {
1969 features: Rc::new(cwd_features.clone()),
1970 all_features: cli_features.all_features,
1971 uses_default_features: cli_features.uses_default_features,
1972 };
1973 Some((member, feats))
1974 }
1975 _ => {
1976 if specs.iter().any(|spec| spec.matches(member_id)) {
1978 let feats = CliFeatures {
1988 features: Rc::new(
1989 member_specific_features
1990 .remove(member.name().as_str())
1991 .unwrap_or_default(),
1992 ),
1993 uses_default_features: true,
1994 all_features: cli_features.all_features,
1995 };
1996 Some((member, feats))
1997 } else {
1998 None
2000 }
2001 }
2002 }
2003 })
2004 .collect();
2005
2006 assert!(member_specific_features.is_empty());
2009
2010 ms
2011 }
2012
2013 pub fn unit_needs_doc_scrape(&self, unit: &Unit) -> bool {
2015 self.is_member(&unit.pkg) && !(unit.target.for_host() || unit.pkg.proc_macro())
2020 }
2021
2022 pub fn add_local_overlay(&mut self, id: SourceId, registry_path: PathBuf) {
2026 self.local_overlays.insert(id, registry_path);
2027 }
2028
2029 pub fn package_registry(&self) -> CargoResult<PackageRegistry<'gctx>> {
2031 let source_config =
2032 SourceConfigMap::new_with_overlays(self.gctx(), self.local_overlays()?)?;
2033 PackageRegistry::new_with_source_config(self.gctx(), source_config)
2034 }
2035
2036 fn local_overlays(&self) -> CargoResult<impl Iterator<Item = (SourceId, SourceId)>> {
2038 let mut ret = self
2039 .local_overlays
2040 .iter()
2041 .map(|(id, path)| Ok((*id, SourceId::for_local_registry(path)?)))
2042 .collect::<CargoResult<Vec<_>>>()?;
2043
2044 if let Ok(overlay) = self
2045 .gctx
2046 .get_env("__CARGO_TEST_DEPENDENCY_CONFUSION_VULNERABILITY_DO_NOT_USE_THIS")
2047 {
2048 let (url, path) = overlay.split_once('=').ok_or(anyhow::anyhow!(
2049 "invalid overlay format. I won't tell you why; you shouldn't be using it anyway"
2050 ))?;
2051 ret.push((
2052 SourceId::from_url(url)?,
2053 SourceId::for_local_registry(path.as_ref())?,
2054 ));
2055 }
2056
2057 Ok(ret.into_iter())
2058 }
2059}
2060
2061impl<'gctx> Packages<'gctx> {
2062 fn get(&self, manifest_path: &Path) -> &MaybePackage {
2063 self.maybe_get(manifest_path).unwrap()
2064 }
2065
2066 fn get_mut(&mut self, manifest_path: &Path) -> &mut MaybePackage {
2067 self.maybe_get_mut(manifest_path).unwrap()
2068 }
2069
2070 fn maybe_get(&self, manifest_path: &Path) -> Option<&MaybePackage> {
2071 self.packages.get(manifest_path)
2072 }
2073
2074 fn maybe_get_mut(&mut self, manifest_path: &Path) -> Option<&mut MaybePackage> {
2075 self.packages.get_mut(manifest_path)
2076 }
2077
2078 fn load(&mut self, manifest_path: &Path) -> CargoResult<&MaybePackage> {
2079 match self.packages.entry(manifest_path.to_path_buf()) {
2080 Entry::Occupied(e) => Ok(e.into_mut()),
2081 Entry::Vacant(v) => {
2082 let source_id = SourceId::for_manifest_path(manifest_path)?;
2083 let manifest = read_manifest(manifest_path, source_id, self.gctx)?;
2084 Ok(v.insert(match manifest {
2085 EitherManifest::Real(manifest) => {
2086 MaybePackage::Package(Package::new(manifest, manifest_path))
2087 }
2088 EitherManifest::Virtual(vm) => MaybePackage::Virtual(vm),
2089 }))
2090 }
2091 }
2092 }
2093}
2094
2095impl MaybePackage {
2096 fn workspace_config(&self) -> &WorkspaceConfig {
2097 match *self {
2098 MaybePackage::Package(ref p) => p.manifest().workspace_config(),
2099 MaybePackage::Virtual(ref vm) => vm.workspace_config(),
2100 }
2101 }
2102
2103 pub fn is_embedded(&self) -> bool {
2105 match self {
2106 MaybePackage::Package(p) => p.manifest().is_embedded(),
2107 MaybePackage::Virtual(_) => false,
2108 }
2109 }
2110
2111 pub fn contents(&self) -> Option<&str> {
2112 match self {
2113 MaybePackage::Package(p) => p.manifest().contents(),
2114 MaybePackage::Virtual(v) => v.contents(),
2115 }
2116 }
2117
2118 pub fn document(&self) -> Option<&toml::Spanned<toml::de::DeTable<'static>>> {
2119 match self {
2120 MaybePackage::Package(p) => p.manifest().document(),
2121 MaybePackage::Virtual(v) => v.document(),
2122 }
2123 }
2124
2125 pub fn original_toml(&self) -> Option<&TomlManifest> {
2126 match self {
2127 MaybePackage::Package(p) => p.manifest().original_toml(),
2128 MaybePackage::Virtual(v) => v.original_toml(),
2129 }
2130 }
2131
2132 pub fn normalized_toml(&self) -> &TomlManifest {
2133 match self {
2134 MaybePackage::Package(p) => p.manifest().normalized_toml(),
2135 MaybePackage::Virtual(v) => v.normalized_toml(),
2136 }
2137 }
2138
2139 pub fn edition(&self) -> Edition {
2140 match self {
2141 MaybePackage::Package(p) => p.manifest().edition(),
2142 MaybePackage::Virtual(_) => Edition::default(),
2143 }
2144 }
2145
2146 pub fn profiles(&self) -> Option<&TomlProfiles> {
2147 match self {
2148 MaybePackage::Package(p) => p.manifest().profiles(),
2149 MaybePackage::Virtual(v) => v.profiles(),
2150 }
2151 }
2152
2153 pub fn unstable_features(&self) -> &Features {
2154 match self {
2155 MaybePackage::Package(p) => p.manifest().unstable_features(),
2156 MaybePackage::Virtual(vm) => vm.unstable_features(),
2157 }
2158 }
2159}
2160
2161impl WorkspaceRootConfig {
2162 pub fn new(
2164 root_dir: &Path,
2165 members: &Option<Vec<String>>,
2166 default_members: &Option<Vec<String>>,
2167 exclude: &Option<Vec<String>>,
2168 inheritable: &Option<InheritableFields>,
2169 custom_metadata: &Option<toml::Value>,
2170 ) -> WorkspaceRootConfig {
2171 WorkspaceRootConfig {
2172 root_dir: root_dir.to_path_buf(),
2173 members: members.clone(),
2174 default_members: default_members.clone(),
2175 exclude: exclude.clone().unwrap_or_default(),
2176 inheritable_fields: inheritable.clone().unwrap_or_default(),
2177 custom_metadata: custom_metadata.clone(),
2178 }
2179 }
2180 fn is_excluded(&self, manifest_path: &Path) -> bool {
2184 let excluded = self
2185 .exclude
2186 .iter()
2187 .any(|ex| manifest_path.starts_with(self.root_dir.join(ex)));
2188
2189 let explicit_member = match self.members {
2190 Some(ref members) => members
2191 .iter()
2192 .any(|mem| manifest_path.starts_with(self.root_dir.join(mem))),
2193 None => false,
2194 };
2195
2196 !explicit_member && excluded
2197 }
2198
2199 fn is_explicitly_listed_member(&self, manifest_path: &Path) -> bool {
2210 let root_manifest = self.root_dir.join("Cargo.toml");
2211 if manifest_path == root_manifest {
2212 return true;
2213 }
2214 match self.members {
2215 Some(ref members) => {
2216 let Ok(expanded_members) = self.members_paths(members) else {
2218 return false;
2219 };
2220 let normalized_manifest = paths::normalize_path(manifest_path);
2222 expanded_members.iter().any(|(member_path, _)| {
2223 let normalized_member = paths::normalize_path(member_path);
2225 normalized_manifest.parent() == Some(normalized_member.as_path())
2228 })
2229 }
2230 None => false,
2231 }
2232 }
2233
2234 fn has_members_list(&self) -> bool {
2235 self.members.is_some()
2236 }
2237
2238 fn has_default_members(&self) -> bool {
2240 self.default_members.is_some()
2241 }
2242
2243 #[tracing::instrument(skip_all)]
2246 fn members_paths<'g>(
2247 &self,
2248 globs: &'g [String],
2249 ) -> CargoResult<Vec<(PathBuf, Option<&'g str>)>> {
2250 let mut expanded_list = Vec::new();
2251
2252 for glob in globs {
2253 let pathbuf = self.root_dir.join(glob);
2254 let expanded_paths = Self::expand_member_path(&pathbuf)?;
2255
2256 if expanded_paths.is_empty() {
2259 expanded_list.push((pathbuf, None));
2260 } else {
2261 let used_glob_pattern = expanded_paths.len() > 1 || expanded_paths[0] != pathbuf;
2262 let glob = used_glob_pattern.then_some(glob.as_str());
2263
2264 for expanded_path in expanded_paths {
2270 if expanded_path.is_dir() {
2271 expanded_list.push((expanded_path, glob));
2272 }
2273 }
2274 }
2275 }
2276
2277 Ok(expanded_list)
2278 }
2279
2280 fn expand_member_path(path: &Path) -> CargoResult<Vec<PathBuf>> {
2281 let Some(path) = path.to_str() else {
2282 return Ok(Vec::new());
2283 };
2284 let res = glob(path).with_context(|| format!("could not parse pattern `{}`", &path))?;
2285 let res = res
2286 .map(|p| p.with_context(|| format!("unable to match path to pattern `{}`", &path)))
2287 .collect::<Result<Vec<_>, _>>()?;
2288 Ok(res)
2289 }
2290
2291 pub fn inheritable(&self) -> &InheritableFields {
2292 &self.inheritable_fields
2293 }
2294}
2295
2296pub fn resolve_relative_path(
2297 label: &str,
2298 old_root: &Path,
2299 new_root: &Path,
2300 rel_path: &str,
2301) -> CargoResult<String> {
2302 let joined_path = normalize_path(&old_root.join(rel_path));
2303 match diff_paths(joined_path, new_root) {
2304 None => Err(anyhow!(
2305 "`{}` was defined in {} but could not be resolved with {}",
2306 label,
2307 old_root.display(),
2308 new_root.display()
2309 )),
2310 Some(path) => Ok(path
2311 .to_str()
2312 .ok_or_else(|| {
2313 anyhow!(
2314 "`{}` resolved to non-UTF value (`{}`)",
2315 label,
2316 path.display()
2317 )
2318 })?
2319 .to_owned()),
2320 }
2321}
2322
2323pub fn find_workspace_root(
2325 manifest_path: &Path,
2326 gctx: &GlobalContext,
2327) -> CargoResult<Option<PathBuf>> {
2328 find_workspace_root_with_loader(manifest_path, gctx, |self_path| {
2329 let source_id = SourceId::for_manifest_path(self_path)?;
2330 let manifest = read_manifest(self_path, source_id, gctx)?;
2331 Ok(manifest
2332 .workspace_config()
2333 .get_ws_root(self_path, manifest_path))
2334 })
2335}
2336
2337pub fn find_workspace_root_with_membership_check(
2344 manifest_path: &Path,
2345 gctx: &GlobalContext,
2346) -> CargoResult<Option<PathBuf>> {
2347 let source_id = SourceId::for_manifest_path(manifest_path)?;
2348 let current_manifest = read_manifest(manifest_path, source_id, gctx)?;
2349
2350 match current_manifest.workspace_config() {
2351 WorkspaceConfig::Root(root_config) => {
2352 if root_config.has_default_members() {
2355 Ok(None)
2356 } else {
2357 Ok(Some(manifest_path.to_path_buf()))
2358 }
2359 }
2360 WorkspaceConfig::Member {
2361 root: Some(path_to_root),
2362 } => {
2363 let ws_manifest_path = read_root_pointer(manifest_path, path_to_root);
2365 let ws_source_id = SourceId::for_manifest_path(&ws_manifest_path)?;
2366 let ws_manifest = read_manifest(&ws_manifest_path, ws_source_id, gctx)?;
2367
2368 if let WorkspaceConfig::Root(ref root_config) = *ws_manifest.workspace_config() {
2370 if root_config.is_explicitly_listed_member(manifest_path)
2371 && !root_config.is_excluded(manifest_path)
2372 {
2373 return Ok(Some(ws_manifest_path));
2374 }
2375 }
2376 Ok(None)
2378 }
2379 WorkspaceConfig::Member { root: None } => {
2380 find_workspace_root_with_loader(manifest_path, gctx, |candidate_manifest_path| {
2382 let source_id = SourceId::for_manifest_path(candidate_manifest_path)?;
2383 let manifest = read_manifest(candidate_manifest_path, source_id, gctx)?;
2384 if let WorkspaceConfig::Root(ref root_config) = *manifest.workspace_config() {
2385 if root_config.is_explicitly_listed_member(manifest_path)
2386 && !root_config.is_excluded(manifest_path)
2387 {
2388 return Ok(Some(candidate_manifest_path.to_path_buf()));
2389 }
2390 }
2391 Ok(None)
2392 })
2393 }
2394 }
2395}
2396
2397fn find_workspace_root_with_loader(
2402 manifest_path: &Path,
2403 gctx: &GlobalContext,
2404 mut loader: impl FnMut(&Path) -> CargoResult<Option<PathBuf>>,
2405) -> CargoResult<Option<PathBuf>> {
2406 {
2408 let roots = gctx.ws_roots();
2409 for current in manifest_path.ancestors().skip(1) {
2412 if let Some(ws_config) = roots.get(current) {
2413 if !ws_config.is_excluded(manifest_path) {
2414 return Ok(Some(current.join("Cargo.toml")));
2416 }
2417 }
2418 }
2419 }
2420
2421 for ances_manifest_path in find_root_iter(manifest_path, gctx) {
2422 debug!("find_root - trying {}", ances_manifest_path.display());
2423 let ws_root_path = loader(&ances_manifest_path).with_context(|| {
2424 format!(
2425 "failed searching for potential workspace\n\
2426 package manifest: `{}`\n\
2427 invalid potential workspace manifest: `{}`\n\
2428 \n\
2429 help: to avoid searching for a non-existent workspace, add \
2430 `[workspace]` to the package manifest",
2431 manifest_path.display(),
2432 ances_manifest_path.display(),
2433 )
2434 })?;
2435 if let Some(ws_root_path) = ws_root_path {
2436 return Ok(Some(ws_root_path));
2437 }
2438 }
2439 Ok(None)
2440}
2441
2442fn read_root_pointer(member_manifest: &Path, root_link: &str) -> PathBuf {
2443 let path = member_manifest
2444 .parent()
2445 .unwrap()
2446 .join(root_link)
2447 .join("Cargo.toml");
2448 debug!("find_root - pointer {}", path.display());
2449 paths::normalize_path(&path)
2450}
2451
2452fn find_root_iter<'a>(
2453 manifest_path: &'a Path,
2454 gctx: &'a GlobalContext,
2455) -> impl Iterator<Item = PathBuf> + 'a {
2456 LookBehind::new(paths::ancestors(manifest_path, None).skip(2))
2457 .take_while(|path| !path.curr.ends_with("target/package"))
2458 .take_while(|path| {
2464 if let Some(last) = path.last {
2465 gctx.home() != last
2466 } else {
2467 true
2468 }
2469 })
2470 .map(|path| path.curr.join("Cargo.toml"))
2471 .filter(|ances_manifest_path| ances_manifest_path.exists())
2472}
2473
2474struct LookBehindWindow<'a, T: ?Sized> {
2475 curr: &'a T,
2476 last: Option<&'a T>,
2477}
2478
2479struct LookBehind<'a, T: ?Sized, K: Iterator<Item = &'a T>> {
2480 iter: K,
2481 last: Option<&'a T>,
2482}
2483
2484impl<'a, T: ?Sized, K: Iterator<Item = &'a T>> LookBehind<'a, T, K> {
2485 fn new(items: K) -> Self {
2486 Self {
2487 iter: items,
2488 last: None,
2489 }
2490 }
2491}
2492
2493impl<'a, T: ?Sized, K: Iterator<Item = &'a T>> Iterator for LookBehind<'a, T, K> {
2494 type Item = LookBehindWindow<'a, T>;
2495
2496 fn next(&mut self) -> Option<Self::Item> {
2497 match self.iter.next() {
2498 None => None,
2499 Some(next) => {
2500 let last = self.last;
2501 self.last = Some(next);
2502 Some(LookBehindWindow { curr: next, last })
2503 }
2504 }
2505 }
2506}