1use std::cell::RefCell;
2use std::collections::hash_map::{Entry, HashMap};
3use std::collections::{BTreeMap, BTreeSet, HashSet};
4use std::path::{Path, PathBuf};
5use std::rc::Rc;
6
7use annotate_snippets::Level;
8use anyhow::{Context as _, anyhow, bail};
9use glob::glob;
10use itertools::Itertools;
11use tracing::debug;
12use url::Url;
13
14use crate::core::compiler::Unit;
15use crate::core::features::Features;
16use crate::core::registry::PackageRegistry;
17use crate::core::resolver::ResolveBehavior;
18use crate::core::resolver::features::CliFeatures;
19use crate::core::{
20 Dependency, Edition, FeatureValue, PackageId, PackageIdSpec, PackageIdSpecQuery,
21};
22use crate::core::{EitherManifest, Package, SourceId, VirtualManifest};
23use crate::ops;
24use crate::sources::{CRATES_IO_INDEX, CRATES_IO_REGISTRY, PathSource, SourceConfigMap};
25use crate::util::context::FeatureUnification;
26use crate::util::edit_distance;
27use crate::util::errors::{CargoResult, ManifestError};
28use crate::util::interning::InternedString;
29use crate::util::lints::{
30 analyze_cargo_lints_table, blanket_hint_mostly_unused, check_im_a_teapot,
31};
32use crate::util::toml::{InheritableFields, read_manifest};
33use crate::util::{
34 Filesystem, GlobalContext, IntoUrl, context::CargoResolverConfig, context::ConfigRelativePath,
35 context::IncompatibleRustVersions,
36};
37use cargo_util::paths;
38use cargo_util::paths::normalize_path;
39use cargo_util_schemas::manifest;
40use cargo_util_schemas::manifest::RustVersion;
41use cargo_util_schemas::manifest::{TomlDependency, TomlProfiles};
42use pathdiff::diff_paths;
43
44#[derive(Debug)]
50pub struct Workspace<'gctx> {
51 gctx: &'gctx GlobalContext,
53
54 current_manifest: PathBuf,
58
59 packages: Packages<'gctx>,
62
63 root_manifest: Option<PathBuf>,
68
69 target_dir: Option<Filesystem>,
72
73 build_dir: Option<Filesystem>,
76
77 members: Vec<PathBuf>,
81 member_ids: HashSet<PackageId>,
83
84 default_members: Vec<PathBuf>,
95
96 is_ephemeral: bool,
99
100 require_optional_deps: bool,
105
106 loaded_packages: RefCell<HashMap<PathBuf, Package>>,
109
110 ignore_lock: bool,
113
114 requested_lockfile_path: Option<PathBuf>,
116
117 resolve_behavior: ResolveBehavior,
119 resolve_honors_rust_version: bool,
123 resolve_feature_unification: FeatureUnification,
125 resolve_publish_time: Option<jiff::Timestamp>,
127 custom_metadata: Option<toml::Value>,
129
130 local_overlays: HashMap<SourceId, PathBuf>,
132}
133
134#[derive(Debug)]
137struct Packages<'gctx> {
138 gctx: &'gctx GlobalContext,
139 packages: HashMap<PathBuf, MaybePackage>,
140}
141
142#[derive(Debug)]
143pub enum MaybePackage {
144 Package(Package),
145 Virtual(VirtualManifest),
146}
147
148#[derive(Debug, Clone)]
150pub enum WorkspaceConfig {
151 Root(WorkspaceRootConfig),
154
155 Member { root: Option<String> },
158}
159
160impl WorkspaceConfig {
161 pub fn inheritable(&self) -> Option<&InheritableFields> {
162 match self {
163 WorkspaceConfig::Root(root) => Some(&root.inheritable_fields),
164 WorkspaceConfig::Member { .. } => None,
165 }
166 }
167
168 fn get_ws_root(&self, self_path: &Path, look_from: &Path) -> Option<PathBuf> {
176 match self {
177 WorkspaceConfig::Root(ances_root_config) => {
178 debug!("find_root - found a root checking exclusion");
179 if !ances_root_config.is_excluded(look_from) {
180 debug!("find_root - found!");
181 Some(self_path.to_owned())
182 } else {
183 None
184 }
185 }
186 WorkspaceConfig::Member {
187 root: Some(path_to_root),
188 } => {
189 debug!("find_root - found pointer");
190 Some(read_root_pointer(self_path, path_to_root))
191 }
192 WorkspaceConfig::Member { .. } => None,
193 }
194 }
195}
196
197#[derive(Debug, Clone)]
202pub struct WorkspaceRootConfig {
203 root_dir: PathBuf,
204 members: Option<Vec<String>>,
205 default_members: Option<Vec<String>>,
206 exclude: Vec<String>,
207 inheritable_fields: InheritableFields,
208 custom_metadata: Option<toml::Value>,
209}
210
211impl<'gctx> Workspace<'gctx> {
212 pub fn new(manifest_path: &Path, gctx: &'gctx GlobalContext) -> CargoResult<Workspace<'gctx>> {
219 let mut ws = Workspace::new_default(manifest_path.to_path_buf(), gctx);
220
221 if manifest_path.is_relative() {
222 bail!(
223 "manifest_path:{:?} is not an absolute path. Please provide an absolute path.",
224 manifest_path
225 )
226 } else {
227 ws.root_manifest = ws.find_root(manifest_path)?;
228 }
229
230 ws.target_dir = gctx.target_dir()?;
231 ws.build_dir = gctx.build_dir(ws.root_manifest())?;
232
233 ws.custom_metadata = ws
234 .load_workspace_config()?
235 .and_then(|cfg| cfg.custom_metadata);
236 ws.find_members()?;
237 ws.set_resolve_behavior()?;
238 ws.validate()?;
239 Ok(ws)
240 }
241
242 fn new_default(current_manifest: PathBuf, gctx: &'gctx GlobalContext) -> Workspace<'gctx> {
243 Workspace {
244 gctx,
245 current_manifest,
246 packages: Packages {
247 gctx,
248 packages: HashMap::new(),
249 },
250 root_manifest: None,
251 target_dir: None,
252 build_dir: None,
253 members: Vec::new(),
254 member_ids: HashSet::new(),
255 default_members: Vec::new(),
256 is_ephemeral: false,
257 require_optional_deps: true,
258 loaded_packages: RefCell::new(HashMap::new()),
259 ignore_lock: false,
260 requested_lockfile_path: None,
261 resolve_behavior: ResolveBehavior::V1,
262 resolve_honors_rust_version: false,
263 resolve_feature_unification: FeatureUnification::Selected,
264 resolve_publish_time: None,
265 custom_metadata: None,
266 local_overlays: HashMap::new(),
267 }
268 }
269
270 pub fn ephemeral(
280 package: Package,
281 gctx: &'gctx GlobalContext,
282 target_dir: Option<Filesystem>,
283 require_optional_deps: bool,
284 ) -> CargoResult<Workspace<'gctx>> {
285 let mut ws = Workspace::new_default(package.manifest_path().to_path_buf(), gctx);
286 ws.is_ephemeral = true;
287 ws.require_optional_deps = require_optional_deps;
288 let id = package.package_id();
289 let package = MaybePackage::Package(package);
290 ws.packages
291 .packages
292 .insert(ws.current_manifest.clone(), package);
293 ws.target_dir = if let Some(dir) = target_dir {
294 Some(dir)
295 } else {
296 ws.gctx.target_dir()?
297 };
298 ws.build_dir = ws.target_dir.clone();
299 ws.members.push(ws.current_manifest.clone());
300 ws.member_ids.insert(id);
301 ws.default_members.push(ws.current_manifest.clone());
302 ws.set_resolve_behavior()?;
303 Ok(ws)
304 }
305
306 pub fn reload(&self, gctx: &'gctx GlobalContext) -> CargoResult<Workspace<'gctx>> {
311 let mut ws = Workspace::new(&self.current_manifest, gctx)?;
312 ws.set_resolve_honors_rust_version(Some(self.resolve_honors_rust_version));
313 ws.set_resolve_feature_unification(self.resolve_feature_unification);
314 ws.set_requested_lockfile_path(self.requested_lockfile_path.clone());
315 Ok(ws)
316 }
317
318 fn set_resolve_behavior(&mut self) -> CargoResult<()> {
319 self.resolve_behavior = match self.root_maybe() {
324 MaybePackage::Package(p) => p
325 .manifest()
326 .resolve_behavior()
327 .unwrap_or_else(|| p.manifest().edition().default_resolve_behavior()),
328 MaybePackage::Virtual(vm) => vm.resolve_behavior().unwrap_or(ResolveBehavior::V1),
329 };
330
331 match self.resolve_behavior() {
332 ResolveBehavior::V1 | ResolveBehavior::V2 => {}
333 ResolveBehavior::V3 => {
334 if self.resolve_behavior == ResolveBehavior::V3 {
335 self.resolve_honors_rust_version = true;
336 }
337 }
338 }
339 let config = self.gctx().get::<CargoResolverConfig>("resolver")?;
340 if let Some(incompatible_rust_versions) = config.incompatible_rust_versions {
341 self.resolve_honors_rust_version =
342 incompatible_rust_versions == IncompatibleRustVersions::Fallback;
343 }
344 if self.gctx().cli_unstable().feature_unification {
345 self.resolve_feature_unification = config
346 .feature_unification
347 .unwrap_or(FeatureUnification::Selected);
348 } else if config.feature_unification.is_some() {
349 self.gctx()
350 .shell()
351 .warn("ignoring `resolver.feature-unification` without `-Zfeature-unification`")?;
352 };
353
354 Ok(())
355 }
356
357 pub fn current(&self) -> CargoResult<&Package> {
363 let pkg = self.current_opt().ok_or_else(|| {
364 anyhow::format_err!(
365 "manifest path `{}` is a virtual manifest, but this \
366 command requires running against an actual package in \
367 this workspace",
368 self.current_manifest.display()
369 )
370 })?;
371 Ok(pkg)
372 }
373
374 pub fn current_mut(&mut self) -> CargoResult<&mut Package> {
375 let cm = self.current_manifest.clone();
376 let pkg = self.current_opt_mut().ok_or_else(|| {
377 anyhow::format_err!(
378 "manifest path `{}` is a virtual manifest, but this \
379 command requires running against an actual package in \
380 this workspace",
381 cm.display()
382 )
383 })?;
384 Ok(pkg)
385 }
386
387 pub fn current_opt(&self) -> Option<&Package> {
388 match *self.packages.get(&self.current_manifest) {
389 MaybePackage::Package(ref p) => Some(p),
390 MaybePackage::Virtual(..) => None,
391 }
392 }
393
394 pub fn current_opt_mut(&mut self) -> Option<&mut Package> {
395 match *self.packages.get_mut(&self.current_manifest) {
396 MaybePackage::Package(ref mut p) => Some(p),
397 MaybePackage::Virtual(..) => None,
398 }
399 }
400
401 pub fn is_virtual(&self) -> bool {
402 match *self.packages.get(&self.current_manifest) {
403 MaybePackage::Package(..) => false,
404 MaybePackage::Virtual(..) => true,
405 }
406 }
407
408 pub fn gctx(&self) -> &'gctx GlobalContext {
410 self.gctx
411 }
412
413 pub fn profiles(&self) -> Option<&TomlProfiles> {
414 self.root_maybe().profiles()
415 }
416
417 pub fn root(&self) -> &Path {
422 self.root_manifest().parent().unwrap()
423 }
424
425 pub fn root_manifest(&self) -> &Path {
428 self.root_manifest
429 .as_ref()
430 .unwrap_or(&self.current_manifest)
431 }
432
433 pub fn root_maybe(&self) -> &MaybePackage {
435 self.packages.get(self.root_manifest())
436 }
437
438 pub fn target_dir(&self) -> Filesystem {
439 self.target_dir
440 .clone()
441 .unwrap_or_else(|| self.default_target_dir())
442 }
443
444 pub fn build_dir(&self) -> Filesystem {
445 self.build_dir
446 .clone()
447 .or_else(|| self.target_dir.clone())
448 .unwrap_or_else(|| self.default_build_dir())
449 }
450
451 fn default_target_dir(&self) -> Filesystem {
452 if self.root_maybe().is_embedded() {
453 self.build_dir().join("target")
454 } else {
455 Filesystem::new(self.root().join("target"))
456 }
457 }
458
459 fn default_build_dir(&self) -> Filesystem {
460 if self.root_maybe().is_embedded() {
461 let default = ConfigRelativePath::new(
462 "{cargo-cache-home}/build/{workspace-path-hash}"
463 .to_owned()
464 .into(),
465 );
466 self.gctx()
467 .custom_build_dir(&default, self.root_manifest())
468 .expect("template is correct")
469 } else {
470 self.default_target_dir()
471 }
472 }
473
474 pub fn root_replace(&self) -> &[(PackageIdSpec, Dependency)] {
478 match self.root_maybe() {
479 MaybePackage::Package(p) => p.manifest().replace(),
480 MaybePackage::Virtual(vm) => vm.replace(),
481 }
482 }
483
484 fn config_patch(&self) -> CargoResult<HashMap<Url, Vec<Dependency>>> {
485 let config_patch: Option<
486 BTreeMap<String, BTreeMap<String, TomlDependency<ConfigRelativePath>>>,
487 > = self.gctx.get("patch")?;
488
489 let source = SourceId::for_manifest_path(self.root_manifest())?;
490
491 let mut warnings = Vec::new();
492
493 let mut patch = HashMap::new();
494 for (url, deps) in config_patch.into_iter().flatten() {
495 let url = match &url[..] {
496 CRATES_IO_REGISTRY => CRATES_IO_INDEX.parse().unwrap(),
497 url => self
498 .gctx
499 .get_registry_index(url)
500 .or_else(|_| url.into_url())
501 .with_context(|| {
502 format!("[patch] entry `{}` should be a URL or registry name", url)
503 })?,
504 };
505 patch.insert(
506 url,
507 deps.iter()
508 .map(|(name, dep)| {
509 crate::util::toml::to_dependency(
510 dep,
511 name,
512 source,
513 self.gctx,
514 &mut warnings,
515 None,
516 Path::new("unused-relative-path"),
519 None,
520 )
521 })
522 .collect::<CargoResult<Vec<_>>>()?,
523 );
524 }
525
526 for message in warnings {
527 self.gctx
528 .shell()
529 .warn(format!("[patch] in cargo config: {}", message))?
530 }
531
532 Ok(patch)
533 }
534
535 pub fn root_patch(&self) -> CargoResult<HashMap<Url, Vec<Dependency>>> {
539 let from_manifest = match self.root_maybe() {
540 MaybePackage::Package(p) => p.manifest().patch(),
541 MaybePackage::Virtual(vm) => vm.patch(),
542 };
543
544 let from_config = self.config_patch()?;
545 if from_config.is_empty() {
546 return Ok(from_manifest.clone());
547 }
548 if from_manifest.is_empty() {
549 return Ok(from_config);
550 }
551
552 let mut combined = from_config;
555 for (url, deps_from_manifest) in from_manifest {
556 if let Some(deps_from_config) = combined.get_mut(url) {
557 let mut from_manifest_pruned = deps_from_manifest.clone();
560 for dep_from_config in &mut *deps_from_config {
561 if let Some(i) = from_manifest_pruned.iter().position(|dep_from_manifest| {
562 dep_from_config.name_in_toml() == dep_from_manifest.name_in_toml()
564 }) {
565 from_manifest_pruned.swap_remove(i);
566 }
567 }
568 deps_from_config.extend(from_manifest_pruned);
570 } else {
571 combined.insert(url.clone(), deps_from_manifest.clone());
572 }
573 }
574 Ok(combined)
575 }
576
577 pub fn members(&self) -> impl Iterator<Item = &Package> {
579 let packages = &self.packages;
580 self.members
581 .iter()
582 .filter_map(move |path| match packages.get(path) {
583 MaybePackage::Package(p) => Some(p),
584 _ => None,
585 })
586 }
587
588 pub fn members_mut(&mut self) -> impl Iterator<Item = &mut Package> {
590 let packages = &mut self.packages.packages;
591 let members: HashSet<_> = self.members.iter().map(|path| path).collect();
592
593 packages.iter_mut().filter_map(move |(path, package)| {
594 if members.contains(path) {
595 if let MaybePackage::Package(p) = package {
596 return Some(p);
597 }
598 }
599
600 None
601 })
602 }
603
604 pub fn default_members<'a>(&'a self) -> impl Iterator<Item = &'a Package> {
606 let packages = &self.packages;
607 self.default_members
608 .iter()
609 .filter_map(move |path| match packages.get(path) {
610 MaybePackage::Package(p) => Some(p),
611 _ => None,
612 })
613 }
614
615 pub fn default_members_mut(&mut self) -> impl Iterator<Item = &mut Package> {
617 let packages = &mut self.packages.packages;
618 let members: HashSet<_> = self
619 .default_members
620 .iter()
621 .map(|path| path.parent().unwrap().to_owned())
622 .collect();
623
624 packages.iter_mut().filter_map(move |(path, package)| {
625 if members.contains(path) {
626 if let MaybePackage::Package(p) = package {
627 return Some(p);
628 }
629 }
630
631 None
632 })
633 }
634
635 pub fn is_member(&self, pkg: &Package) -> bool {
637 self.member_ids.contains(&pkg.package_id())
638 }
639
640 pub fn is_member_id(&self, package_id: PackageId) -> bool {
642 self.member_ids.contains(&package_id)
643 }
644
645 pub fn is_ephemeral(&self) -> bool {
646 self.is_ephemeral
647 }
648
649 pub fn require_optional_deps(&self) -> bool {
650 self.require_optional_deps
651 }
652
653 pub fn set_require_optional_deps(
654 &mut self,
655 require_optional_deps: bool,
656 ) -> &mut Workspace<'gctx> {
657 self.require_optional_deps = require_optional_deps;
658 self
659 }
660
661 pub fn ignore_lock(&self) -> bool {
662 self.ignore_lock
663 }
664
665 pub fn set_ignore_lock(&mut self, ignore_lock: bool) -> &mut Workspace<'gctx> {
666 self.ignore_lock = ignore_lock;
667 self
668 }
669
670 pub fn lock_root(&self) -> Filesystem {
672 if let Some(requested) = self.requested_lockfile_path.as_ref() {
673 return Filesystem::new(
674 requested
675 .parent()
676 .expect("Lockfile path can't be root")
677 .to_owned(),
678 );
679 }
680 self.default_lock_root()
681 }
682
683 fn default_lock_root(&self) -> Filesystem {
684 if self.root_maybe().is_embedded() {
685 self.build_dir()
686 } else {
687 Filesystem::new(self.root().to_owned())
688 }
689 }
690
691 pub fn set_requested_lockfile_path(&mut self, path: Option<PathBuf>) {
692 self.requested_lockfile_path = path;
693 }
694
695 pub fn requested_lockfile_path(&self) -> Option<&Path> {
696 self.requested_lockfile_path.as_deref()
697 }
698
699 pub fn lowest_rust_version(&self) -> Option<&RustVersion> {
702 self.members().filter_map(|pkg| pkg.rust_version()).min()
703 }
704
705 pub fn set_resolve_honors_rust_version(&mut self, honor_rust_version: Option<bool>) {
706 if let Some(honor_rust_version) = honor_rust_version {
707 self.resolve_honors_rust_version = honor_rust_version;
708 }
709 }
710
711 pub fn resolve_honors_rust_version(&self) -> bool {
712 self.resolve_honors_rust_version
713 }
714
715 pub fn set_resolve_feature_unification(&mut self, feature_unification: FeatureUnification) {
716 self.resolve_feature_unification = feature_unification;
717 }
718
719 pub fn resolve_feature_unification(&self) -> FeatureUnification {
720 self.resolve_feature_unification
721 }
722
723 pub fn set_resolve_publish_time(&mut self, publish_time: jiff::Timestamp) {
724 self.resolve_publish_time = Some(publish_time);
725 }
726
727 pub fn resolve_publish_time(&self) -> Option<jiff::Timestamp> {
728 self.resolve_publish_time
729 }
730
731 pub fn custom_metadata(&self) -> Option<&toml::Value> {
732 self.custom_metadata.as_ref()
733 }
734
735 pub fn load_workspace_config(&mut self) -> CargoResult<Option<WorkspaceRootConfig>> {
736 if let Some(root_path) = &self.root_manifest {
739 let root_package = self.packages.load(root_path)?;
740 match root_package.workspace_config() {
741 WorkspaceConfig::Root(root_config) => {
742 return Ok(Some(root_config.clone()));
743 }
744
745 _ => bail!(
746 "root of a workspace inferred but wasn't a root: {}",
747 root_path.display()
748 ),
749 }
750 }
751
752 Ok(None)
753 }
754
755 fn find_root(&mut self, manifest_path: &Path) -> CargoResult<Option<PathBuf>> {
765 let current = self.packages.load(manifest_path)?;
766 match current
767 .workspace_config()
768 .get_ws_root(manifest_path, manifest_path)
769 {
770 Some(root_path) => {
771 debug!("find_root - is root {}", manifest_path.display());
772 Ok(Some(root_path))
773 }
774 None => find_workspace_root_with_loader(manifest_path, self.gctx, |self_path| {
775 Ok(self
776 .packages
777 .load(self_path)?
778 .workspace_config()
779 .get_ws_root(self_path, manifest_path))
780 }),
781 }
782 }
783
784 #[tracing::instrument(skip_all)]
792 fn find_members(&mut self) -> CargoResult<()> {
793 let Some(workspace_config) = self.load_workspace_config()? else {
794 debug!("find_members - only me as a member");
795 self.members.push(self.current_manifest.clone());
796 self.default_members.push(self.current_manifest.clone());
797 if let Ok(pkg) = self.current() {
798 let id = pkg.package_id();
799 self.member_ids.insert(id);
800 }
801 return Ok(());
802 };
803
804 let root_manifest_path = self.root_manifest.clone().unwrap();
806
807 let members_paths = workspace_config
808 .members_paths(workspace_config.members.as_deref().unwrap_or_default())?;
809 let default_members_paths = if root_manifest_path == self.current_manifest {
810 if let Some(ref default) = workspace_config.default_members {
811 Some(workspace_config.members_paths(default)?)
812 } else {
813 None
814 }
815 } else {
816 None
817 };
818
819 for (path, glob) in &members_paths {
820 self.find_path_deps(&path.join("Cargo.toml"), &root_manifest_path, false)
821 .with_context(|| {
822 format!(
823 "failed to load manifest for workspace member `{}`\n\
824 referenced{} by workspace at `{}`",
825 path.display(),
826 glob.map(|g| format!(" via `{g}`")).unwrap_or_default(),
827 root_manifest_path.display(),
828 )
829 })?;
830 }
831
832 self.find_path_deps(&root_manifest_path, &root_manifest_path, false)?;
833
834 if let Some(default) = default_members_paths {
835 for (path, default_member_glob) in default {
836 let normalized_path = paths::normalize_path(&path);
837 let manifest_path = normalized_path.join("Cargo.toml");
838 if !self.members.contains(&manifest_path) {
839 let exclude = members_paths.iter().any(|(m, _)| *m == normalized_path)
846 && workspace_config.is_excluded(&normalized_path);
847 if exclude {
848 continue;
849 }
850 bail!(
851 "package `{}` is listed in default-members{} but is not a member\n\
852 for workspace at `{}`.",
853 path.display(),
854 default_member_glob
855 .map(|g| format!(" via `{g}`"))
856 .unwrap_or_default(),
857 root_manifest_path.display(),
858 )
859 }
860 self.default_members.push(manifest_path)
861 }
862 } else if self.is_virtual() {
863 self.default_members = self.members.clone()
864 } else {
865 self.default_members.push(self.current_manifest.clone())
866 }
867
868 Ok(())
869 }
870
871 fn find_path_deps(
872 &mut self,
873 manifest_path: &Path,
874 root_manifest: &Path,
875 is_path_dep: bool,
876 ) -> CargoResult<()> {
877 let manifest_path = paths::normalize_path(manifest_path);
878 if self.members.contains(&manifest_path) {
879 return Ok(());
880 }
881 if is_path_dep && self.root_maybe().is_embedded() {
882 return Ok(());
884 }
885 if is_path_dep
886 && !manifest_path.parent().unwrap().starts_with(self.root())
887 && self.find_root(&manifest_path)? != self.root_manifest
888 {
889 return Ok(());
892 }
893
894 if let WorkspaceConfig::Root(ref root_config) =
895 *self.packages.load(root_manifest)?.workspace_config()
896 {
897 if root_config.is_excluded(&manifest_path) {
898 return Ok(());
899 }
900 }
901
902 debug!("find_path_deps - {}", manifest_path.display());
903 self.members.push(manifest_path.clone());
904
905 let candidates = {
906 let pkg = match *self.packages.load(&manifest_path)? {
907 MaybePackage::Package(ref p) => p,
908 MaybePackage::Virtual(_) => return Ok(()),
909 };
910 self.member_ids.insert(pkg.package_id());
911 pkg.dependencies()
912 .iter()
913 .map(|d| (d.source_id(), d.package_name()))
914 .filter(|(s, _)| s.is_path())
915 .filter_map(|(s, n)| s.url().to_file_path().ok().map(|p| (p, n)))
916 .map(|(p, n)| (p.join("Cargo.toml"), n))
917 .collect::<Vec<_>>()
918 };
919 for (path, name) in candidates {
920 self.find_path_deps(&path, root_manifest, true)
921 .with_context(|| format!("failed to load manifest for dependency `{}`", name))
922 .map_err(|err| ManifestError::new(err, manifest_path.clone()))?;
923 }
924 Ok(())
925 }
926
927 pub fn unstable_features(&self) -> &Features {
929 self.root_maybe().unstable_features()
930 }
931
932 pub fn resolve_behavior(&self) -> ResolveBehavior {
933 self.resolve_behavior
934 }
935
936 pub fn allows_new_cli_feature_behavior(&self) -> bool {
944 self.is_virtual()
945 || match self.resolve_behavior() {
946 ResolveBehavior::V1 => false,
947 ResolveBehavior::V2 | ResolveBehavior::V3 => true,
948 }
949 }
950
951 #[tracing::instrument(skip_all)]
957 fn validate(&mut self) -> CargoResult<()> {
958 if self.root_manifest.is_none() {
960 return Ok(());
961 }
962
963 self.validate_unique_names()?;
964 self.validate_workspace_roots()?;
965 self.validate_members()?;
966 self.error_if_manifest_not_in_members()?;
967 self.validate_manifest()
968 }
969
970 fn validate_unique_names(&self) -> CargoResult<()> {
971 let mut names = BTreeMap::new();
972 for member in self.members.iter() {
973 let package = self.packages.get(member);
974 let name = match *package {
975 MaybePackage::Package(ref p) => p.name(),
976 MaybePackage::Virtual(_) => continue,
977 };
978 if let Some(prev) = names.insert(name, member) {
979 bail!(
980 "two packages named `{}` in this workspace:\n\
981 - {}\n\
982 - {}",
983 name,
984 prev.display(),
985 member.display()
986 );
987 }
988 }
989 Ok(())
990 }
991
992 fn validate_workspace_roots(&self) -> CargoResult<()> {
993 let roots: Vec<PathBuf> = self
994 .members
995 .iter()
996 .filter(|&member| {
997 let config = self.packages.get(member).workspace_config();
998 matches!(config, WorkspaceConfig::Root(_))
999 })
1000 .map(|member| member.parent().unwrap().to_path_buf())
1001 .collect();
1002 match roots.len() {
1003 1 => Ok(()),
1004 0 => bail!(
1005 "`package.workspace` configuration points to a crate \
1006 which is not configured with [workspace]: \n\
1007 configuration at: {}\n\
1008 points to: {}",
1009 self.current_manifest.display(),
1010 self.root_manifest.as_ref().unwrap().display()
1011 ),
1012 _ => {
1013 bail!(
1014 "multiple workspace roots found in the same workspace:\n{}",
1015 roots
1016 .iter()
1017 .map(|r| format!(" {}", r.display()))
1018 .collect::<Vec<_>>()
1019 .join("\n")
1020 );
1021 }
1022 }
1023 }
1024
1025 #[tracing::instrument(skip_all)]
1026 fn validate_members(&mut self) -> CargoResult<()> {
1027 for member in self.members.clone() {
1028 let root = self.find_root(&member)?;
1029 if root == self.root_manifest {
1030 continue;
1031 }
1032
1033 match root {
1034 Some(root) => {
1035 bail!(
1036 "package `{}` is a member of the wrong workspace\n\
1037 expected: {}\n\
1038 actual: {}",
1039 member.display(),
1040 self.root_manifest.as_ref().unwrap().display(),
1041 root.display()
1042 );
1043 }
1044 None => {
1045 bail!(
1046 "workspace member `{}` is not hierarchically below \
1047 the workspace root `{}`",
1048 member.display(),
1049 self.root_manifest.as_ref().unwrap().display()
1050 );
1051 }
1052 }
1053 }
1054 Ok(())
1055 }
1056
1057 fn error_if_manifest_not_in_members(&mut self) -> CargoResult<()> {
1058 if self.members.contains(&self.current_manifest) {
1059 return Ok(());
1060 }
1061
1062 let root = self.root_manifest.as_ref().unwrap();
1063 let root_dir = root.parent().unwrap();
1064 let current_dir = self.current_manifest.parent().unwrap();
1065 let root_pkg = self.packages.get(root);
1066
1067 let members_msg = match current_dir.strip_prefix(root_dir) {
1069 Ok(rel) => format!(
1070 "this may be fixable by adding `{}` to the \
1071 `workspace.members` array of the manifest \
1072 located at: {}",
1073 rel.display(),
1074 root.display()
1075 ),
1076 Err(_) => format!(
1077 "this may be fixable by adding a member to \
1078 the `workspace.members` array of the \
1079 manifest located at: {}",
1080 root.display()
1081 ),
1082 };
1083 let extra = match *root_pkg {
1084 MaybePackage::Virtual(_) => members_msg,
1085 MaybePackage::Package(ref p) => {
1086 let has_members_list = match *p.manifest().workspace_config() {
1087 WorkspaceConfig::Root(ref root_config) => root_config.has_members_list(),
1088 WorkspaceConfig::Member { .. } => unreachable!(),
1089 };
1090 if !has_members_list {
1091 format!(
1092 "this may be fixable by ensuring that this \
1093 crate is depended on by the workspace \
1094 root: {}",
1095 root.display()
1096 )
1097 } else {
1098 members_msg
1099 }
1100 }
1101 };
1102 bail!(
1103 "current package believes it's in a workspace when it's not:\n\
1104 current: {}\n\
1105 workspace: {}\n\n{}\n\
1106 Alternatively, to keep it out of the workspace, add the package \
1107 to the `workspace.exclude` array, or add an empty `[workspace]` \
1108 table to the package's manifest.",
1109 self.current_manifest.display(),
1110 root.display(),
1111 extra
1112 );
1113 }
1114
1115 fn validate_manifest(&mut self) -> CargoResult<()> {
1116 if let Some(ref root_manifest) = self.root_manifest {
1117 for pkg in self
1118 .members()
1119 .filter(|p| p.manifest_path() != root_manifest)
1120 {
1121 let manifest = pkg.manifest();
1122 let emit_warning = |what| -> CargoResult<()> {
1123 let msg = format!(
1124 "{} for the non root package will be ignored, \
1125 specify {} at the workspace root:\n\
1126 package: {}\n\
1127 workspace: {}",
1128 what,
1129 what,
1130 pkg.manifest_path().display(),
1131 root_manifest.display(),
1132 );
1133 self.gctx.shell().warn(&msg)
1134 };
1135 if manifest.normalized_toml().has_profiles() {
1136 emit_warning("profiles")?;
1137 }
1138 if !manifest.replace().is_empty() {
1139 emit_warning("replace")?;
1140 }
1141 if !manifest.patch().is_empty() {
1142 emit_warning("patch")?;
1143 }
1144 if let Some(behavior) = manifest.resolve_behavior() {
1145 if behavior != self.resolve_behavior {
1146 emit_warning("resolver")?;
1148 }
1149 }
1150 }
1151 if let MaybePackage::Virtual(vm) = self.root_maybe() {
1152 if vm.resolve_behavior().is_none() {
1153 if let Some(edition) = self
1154 .members()
1155 .filter(|p| p.manifest_path() != root_manifest)
1156 .map(|p| p.manifest().edition())
1157 .filter(|&e| e >= Edition::Edition2021)
1158 .max()
1159 {
1160 let resolver = edition.default_resolve_behavior().to_manifest();
1161 let report = &[Level::WARNING
1162 .primary_title(format!(
1163 "virtual workspace defaulting to `resolver = \"1\"` despite one or more workspace members being on edition {edition} which implies `resolver = \"{resolver}\"`"
1164 ))
1165 .elements([
1166 Level::NOTE.message("to keep the current resolver, specify `workspace.resolver = \"1\"` in the workspace root's manifest"),
1167 Level::NOTE.message(
1168 format!("to use the edition {edition} resolver, specify `workspace.resolver = \"{resolver}\"` in the workspace root's manifest"),
1169 ),
1170 Level::NOTE.message("for more details see https://doc.rust-lang.org/cargo/reference/resolver.html#resolver-versions"),
1171 ])];
1172 self.gctx.shell().print_report(report, false)?;
1173 }
1174 }
1175 }
1176 }
1177 Ok(())
1178 }
1179
1180 pub fn load(&self, manifest_path: &Path) -> CargoResult<Package> {
1181 match self.packages.maybe_get(manifest_path) {
1182 Some(MaybePackage::Package(p)) => return Ok(p.clone()),
1183 Some(&MaybePackage::Virtual(_)) => bail!("cannot load workspace root"),
1184 None => {}
1185 }
1186
1187 let mut loaded = self.loaded_packages.borrow_mut();
1188 if let Some(p) = loaded.get(manifest_path).cloned() {
1189 return Ok(p);
1190 }
1191 let source_id = SourceId::for_manifest_path(manifest_path)?;
1192 let package = ops::read_package(manifest_path, source_id, self.gctx)?;
1193 loaded.insert(manifest_path.to_path_buf(), package.clone());
1194 Ok(package)
1195 }
1196
1197 pub fn preload(&self, registry: &mut PackageRegistry<'gctx>) {
1204 if self.is_ephemeral {
1210 return;
1211 }
1212
1213 for pkg in self.packages.packages.values() {
1214 let pkg = match *pkg {
1215 MaybePackage::Package(ref p) => p.clone(),
1216 MaybePackage::Virtual(_) => continue,
1217 };
1218 let src = PathSource::preload_with(pkg, self.gctx);
1219 registry.add_preloaded(Box::new(src));
1220 }
1221 }
1222
1223 pub fn emit_warnings(&self) -> CargoResult<()> {
1224 let mut first_emitted_error = None;
1225
1226 if let Err(e) = self.emit_ws_lints() {
1227 first_emitted_error = Some(e);
1228 }
1229
1230 for (path, maybe_pkg) in &self.packages.packages {
1231 if let MaybePackage::Package(pkg) = maybe_pkg {
1232 if let Err(e) = self.emit_pkg_lints(pkg, &path)
1233 && first_emitted_error.is_none()
1234 {
1235 first_emitted_error = Some(e);
1236 }
1237 }
1238 let warnings = match maybe_pkg {
1239 MaybePackage::Package(pkg) => pkg.manifest().warnings().warnings(),
1240 MaybePackage::Virtual(vm) => vm.warnings().warnings(),
1241 };
1242 for warning in warnings {
1243 if warning.is_critical {
1244 let err = anyhow::format_err!("{}", warning.message);
1245 let cx =
1246 anyhow::format_err!("failed to parse manifest at `{}`", path.display());
1247 if first_emitted_error.is_none() {
1248 first_emitted_error = Some(err.context(cx));
1249 }
1250 } else {
1251 let msg = if self.root_manifest.is_none() {
1252 warning.message.to_string()
1253 } else {
1254 format!("{}: {}", path.display(), warning.message)
1257 };
1258 self.gctx.shell().warn(msg)?
1259 }
1260 }
1261 }
1262
1263 if let Some(error) = first_emitted_error {
1264 Err(error)
1265 } else {
1266 Ok(())
1267 }
1268 }
1269
1270 pub fn emit_pkg_lints(&self, pkg: &Package, path: &Path) -> CargoResult<()> {
1271 let mut error_count = 0;
1272 let toml_lints = pkg
1273 .manifest()
1274 .normalized_toml()
1275 .lints
1276 .clone()
1277 .map(|lints| lints.lints)
1278 .unwrap_or(manifest::TomlLints::default());
1279 let cargo_lints = toml_lints
1280 .get("cargo")
1281 .cloned()
1282 .unwrap_or(manifest::TomlToolLints::default());
1283
1284 let ws_contents = self.root_maybe().contents();
1285
1286 let ws_document = self.root_maybe().document();
1287
1288 if self.gctx.cli_unstable().cargo_lints {
1289 analyze_cargo_lints_table(
1290 pkg,
1291 &path,
1292 &cargo_lints,
1293 ws_contents,
1294 ws_document,
1295 self.root_manifest(),
1296 self.gctx,
1297 )?;
1298 check_im_a_teapot(pkg, &path, &cargo_lints, &mut error_count, self.gctx)?;
1299 }
1300
1301 if error_count > 0 {
1302 Err(crate::util::errors::AlreadyPrintedError::new(anyhow!(
1303 "encountered {error_count} errors(s) while running lints"
1304 ))
1305 .into())
1306 } else {
1307 Ok(())
1308 }
1309 }
1310
1311 pub fn emit_ws_lints(&self) -> CargoResult<()> {
1312 let mut error_count = 0;
1313
1314 let cargo_lints = match self.root_maybe() {
1315 MaybePackage::Package(pkg) => {
1316 let toml = pkg.manifest().normalized_toml();
1317 if let Some(ws) = &toml.workspace {
1318 ws.lints.as_ref()
1319 } else {
1320 toml.lints.as_ref().map(|l| &l.lints)
1321 }
1322 }
1323 MaybePackage::Virtual(vm) => vm
1324 .normalized_toml()
1325 .workspace
1326 .as_ref()
1327 .unwrap()
1328 .lints
1329 .as_ref(),
1330 }
1331 .and_then(|t| t.get("cargo"))
1332 .cloned()
1333 .unwrap_or(manifest::TomlToolLints::default());
1334
1335 if self.gctx.cli_unstable().cargo_lints {
1336 }
1338
1339 if self.gctx.cli_unstable().profile_hint_mostly_unused {
1343 blanket_hint_mostly_unused(
1344 self.root_maybe(),
1345 self.root_manifest(),
1346 &cargo_lints,
1347 &mut error_count,
1348 self.gctx,
1349 )?;
1350 }
1351
1352 if error_count > 0 {
1353 Err(crate::util::errors::AlreadyPrintedError::new(anyhow!(
1354 "encountered {error_count} errors(s) while running lints"
1355 ))
1356 .into())
1357 } else {
1358 Ok(())
1359 }
1360 }
1361
1362 pub fn set_target_dir(&mut self, target_dir: Filesystem) {
1363 self.target_dir = Some(target_dir);
1364 }
1365
1366 pub fn members_with_features(
1374 &self,
1375 specs: &[PackageIdSpec],
1376 cli_features: &CliFeatures,
1377 ) -> CargoResult<Vec<(&Package, CliFeatures)>> {
1378 assert!(
1379 !specs.is_empty() || cli_features.all_features,
1380 "no specs requires all_features"
1381 );
1382 if specs.is_empty() {
1383 return Ok(self
1386 .members()
1387 .map(|m| (m, CliFeatures::new_all(true)))
1388 .collect());
1389 }
1390 if self.allows_new_cli_feature_behavior() {
1391 self.members_with_features_new(specs, cli_features)
1392 } else {
1393 Ok(self.members_with_features_old(specs, cli_features))
1394 }
1395 }
1396
1397 fn collect_matching_features(
1400 member: &Package,
1401 cli_features: &CliFeatures,
1402 found_features: &mut BTreeSet<FeatureValue>,
1403 ) -> CliFeatures {
1404 if cli_features.features.is_empty() {
1405 return cli_features.clone();
1406 }
1407
1408 let summary = member.summary();
1410
1411 let summary_features = summary.features();
1413
1414 let dependencies: BTreeMap<InternedString, &Dependency> = summary
1416 .dependencies()
1417 .iter()
1418 .map(|dep| (dep.name_in_toml(), dep))
1419 .collect();
1420
1421 let optional_dependency_names: BTreeSet<_> = dependencies
1423 .iter()
1424 .filter(|(_, dep)| dep.is_optional())
1425 .map(|(name, _)| name)
1426 .copied()
1427 .collect();
1428
1429 let mut features = BTreeSet::new();
1430
1431 let summary_or_opt_dependency_feature = |feature: &InternedString| -> bool {
1433 summary_features.contains_key(feature) || optional_dependency_names.contains(feature)
1434 };
1435
1436 for feature in cli_features.features.iter() {
1437 match feature {
1438 FeatureValue::Feature(f) => {
1439 if summary_or_opt_dependency_feature(f) {
1440 features.insert(feature.clone());
1442 found_features.insert(feature.clone());
1443 }
1444 }
1445 FeatureValue::Dep { .. } => panic!("unexpected dep: syntax {}", feature),
1447 FeatureValue::DepFeature {
1448 dep_name,
1449 dep_feature,
1450 weak: _,
1451 } => {
1452 if dependencies.contains_key(dep_name) {
1453 features.insert(feature.clone());
1456 found_features.insert(feature.clone());
1457 } else if *dep_name == member.name()
1458 && summary_or_opt_dependency_feature(dep_feature)
1459 {
1460 features.insert(FeatureValue::Feature(*dep_feature));
1465 found_features.insert(feature.clone());
1466 }
1467 }
1468 }
1469 }
1470 CliFeatures {
1471 features: Rc::new(features),
1472 all_features: cli_features.all_features,
1473 uses_default_features: cli_features.uses_default_features,
1474 }
1475 }
1476
1477 fn missing_feature_spelling_suggestions(
1478 &self,
1479 selected_members: &[&Package],
1480 cli_features: &CliFeatures,
1481 found_features: &BTreeSet<FeatureValue>,
1482 ) -> Vec<String> {
1483 let mut summary_features: Vec<InternedString> = Default::default();
1485
1486 let mut dependencies_features: BTreeMap<InternedString, &[InternedString]> =
1488 Default::default();
1489
1490 let mut optional_dependency_names: Vec<InternedString> = Default::default();
1492
1493 let mut summary_features_per_member: BTreeMap<&Package, BTreeSet<InternedString>> =
1495 Default::default();
1496
1497 let mut optional_dependency_names_per_member: BTreeMap<&Package, BTreeSet<InternedString>> =
1499 Default::default();
1500
1501 for &member in selected_members {
1502 let summary = member.summary();
1504
1505 summary_features.extend(summary.features().keys());
1507 summary_features_per_member
1508 .insert(member, summary.features().keys().copied().collect());
1509
1510 let dependencies: BTreeMap<InternedString, &Dependency> = summary
1512 .dependencies()
1513 .iter()
1514 .map(|dep| (dep.name_in_toml(), dep))
1515 .collect();
1516
1517 dependencies_features.extend(
1518 dependencies
1519 .iter()
1520 .map(|(name, dep)| (*name, dep.features())),
1521 );
1522
1523 let optional_dependency_names_raw: BTreeSet<_> = dependencies
1525 .iter()
1526 .filter(|(_, dep)| dep.is_optional())
1527 .map(|(name, _)| name)
1528 .copied()
1529 .collect();
1530
1531 optional_dependency_names.extend(optional_dependency_names_raw.iter());
1532 optional_dependency_names_per_member.insert(member, optional_dependency_names_raw);
1533 }
1534
1535 let edit_distance_test = |a: InternedString, b: InternedString| {
1536 edit_distance(a.as_str(), b.as_str(), 3).is_some()
1537 };
1538
1539 cli_features
1540 .features
1541 .difference(found_features)
1542 .map(|feature| match feature {
1543 FeatureValue::Feature(typo) => {
1545 let summary_features = summary_features
1547 .iter()
1548 .filter(move |feature| edit_distance_test(**feature, *typo));
1549
1550 let optional_dependency_features = optional_dependency_names
1552 .iter()
1553 .filter(move |feature| edit_distance_test(**feature, *typo));
1554
1555 summary_features
1556 .chain(optional_dependency_features)
1557 .map(|s| s.to_string())
1558 .collect::<Vec<_>>()
1559 }
1560 FeatureValue::Dep { .. } => panic!("unexpected dep: syntax {}", feature),
1561 FeatureValue::DepFeature {
1562 dep_name,
1563 dep_feature,
1564 weak: _,
1565 } => {
1566 let pkg_feat_similar = dependencies_features
1568 .iter()
1569 .filter(|(name, _)| edit_distance_test(**name, *dep_name))
1570 .map(|(name, features)| {
1571 (
1572 name,
1573 features
1574 .iter()
1575 .filter(|feature| edit_distance_test(**feature, *dep_feature))
1576 .collect::<Vec<_>>(),
1577 )
1578 })
1579 .map(|(name, features)| {
1580 features
1581 .into_iter()
1582 .map(move |feature| format!("{}/{}", name, feature))
1583 })
1584 .flatten();
1585
1586 let optional_dependency_features = optional_dependency_names_per_member
1588 .iter()
1589 .filter(|(package, _)| edit_distance_test(package.name(), *dep_name))
1590 .map(|(package, optional_dependencies)| {
1591 optional_dependencies
1592 .into_iter()
1593 .filter(|optional_dependency| {
1594 edit_distance_test(**optional_dependency, *dep_name)
1595 })
1596 .map(move |optional_dependency| {
1597 format!("{}/{}", package.name(), optional_dependency)
1598 })
1599 })
1600 .flatten();
1601
1602 let summary_features = summary_features_per_member
1604 .iter()
1605 .filter(|(package, _)| edit_distance_test(package.name(), *dep_name))
1606 .map(|(package, summary_features)| {
1607 summary_features
1608 .into_iter()
1609 .filter(|summary_feature| {
1610 edit_distance_test(**summary_feature, *dep_feature)
1611 })
1612 .map(move |summary_feature| {
1613 format!("{}/{}", package.name(), summary_feature)
1614 })
1615 })
1616 .flatten();
1617
1618 pkg_feat_similar
1619 .chain(optional_dependency_features)
1620 .chain(summary_features)
1621 .collect::<Vec<_>>()
1622 }
1623 })
1624 .map(|v| v.into_iter())
1625 .flatten()
1626 .unique()
1627 .filter(|element| {
1628 let feature = FeatureValue::new(element.into());
1629 !cli_features.features.contains(&feature) && !found_features.contains(&feature)
1630 })
1631 .sorted()
1632 .take(5)
1633 .collect()
1634 }
1635
1636 fn report_unknown_features_error(
1637 &self,
1638 specs: &[PackageIdSpec],
1639 cli_features: &CliFeatures,
1640 found_features: &BTreeSet<FeatureValue>,
1641 ) -> CargoResult<()> {
1642 let unknown: Vec<_> = cli_features
1643 .features
1644 .difference(found_features)
1645 .map(|feature| feature.to_string())
1646 .sorted()
1647 .collect();
1648
1649 let (selected_members, unselected_members): (Vec<_>, Vec<_>) = self
1650 .members()
1651 .partition(|member| specs.iter().any(|spec| spec.matches(member.package_id())));
1652
1653 let missing_packages_with_the_features = unselected_members
1654 .into_iter()
1655 .filter(|member| {
1656 unknown
1657 .iter()
1658 .any(|feature| member.summary().features().contains_key(&**feature))
1659 })
1660 .map(|m| m.name())
1661 .collect_vec();
1662
1663 let these_features = if unknown.len() == 1 {
1664 "this feature"
1665 } else {
1666 "these features"
1667 };
1668 let mut msg = if let [singular] = &selected_members[..] {
1669 format!(
1670 "the package '{}' does not contain {these_features}: {}",
1671 singular.name(),
1672 unknown.join(", ")
1673 )
1674 } else {
1675 let names = selected_members.iter().map(|m| m.name()).join(", ");
1676 format!(
1677 "none of the selected packages contains {these_features}: {}\nselected packages: {names}",
1678 unknown.join(", ")
1679 )
1680 };
1681
1682 use std::fmt::Write;
1683 if !missing_packages_with_the_features.is_empty() {
1684 write!(
1685 &mut msg,
1686 "\nhelp: package{} with the missing feature{}: {}",
1687 if missing_packages_with_the_features.len() != 1 {
1688 "s"
1689 } else {
1690 ""
1691 },
1692 if unknown.len() != 1 { "s" } else { "" },
1693 missing_packages_with_the_features.join(", ")
1694 )?;
1695 } else {
1696 let suggestions = self.missing_feature_spelling_suggestions(
1697 &selected_members,
1698 cli_features,
1699 found_features,
1700 );
1701 if !suggestions.is_empty() {
1702 write!(
1703 &mut msg,
1704 "\nhelp: there {}: {}",
1705 if suggestions.len() == 1 {
1706 "is a similarly named feature"
1707 } else {
1708 "are similarly named features"
1709 },
1710 suggestions.join(", ")
1711 )?;
1712 }
1713 }
1714
1715 bail!("{msg}")
1716 }
1717
1718 fn members_with_features_new(
1721 &self,
1722 specs: &[PackageIdSpec],
1723 cli_features: &CliFeatures,
1724 ) -> CargoResult<Vec<(&Package, CliFeatures)>> {
1725 let mut found_features = Default::default();
1728
1729 let members: Vec<(&Package, CliFeatures)> = self
1730 .members()
1731 .filter(|m| specs.iter().any(|spec| spec.matches(m.package_id())))
1732 .map(|m| {
1733 (
1734 m,
1735 Workspace::collect_matching_features(m, cli_features, &mut found_features),
1736 )
1737 })
1738 .collect();
1739
1740 if members.is_empty() {
1741 if !(cli_features.features.is_empty()
1744 && !cli_features.all_features
1745 && cli_features.uses_default_features)
1746 {
1747 bail!("cannot specify features for packages outside of workspace");
1748 }
1749 return Ok(self
1752 .members()
1753 .map(|m| (m, CliFeatures::new_all(false)))
1754 .collect());
1755 }
1756 if *cli_features.features != found_features {
1757 self.report_unknown_features_error(specs, cli_features, &found_features)?;
1758 }
1759 Ok(members)
1760 }
1761
1762 fn members_with_features_old(
1765 &self,
1766 specs: &[PackageIdSpec],
1767 cli_features: &CliFeatures,
1768 ) -> Vec<(&Package, CliFeatures)> {
1769 let mut member_specific_features: HashMap<InternedString, BTreeSet<FeatureValue>> =
1772 HashMap::new();
1773 let mut cwd_features = BTreeSet::new();
1775 for feature in cli_features.features.iter() {
1776 match feature {
1777 FeatureValue::Feature(_) => {
1778 cwd_features.insert(feature.clone());
1779 }
1780 FeatureValue::Dep { .. } => panic!("unexpected dep: syntax {}", feature),
1782 FeatureValue::DepFeature {
1783 dep_name,
1784 dep_feature,
1785 weak: _,
1786 } => {
1787 let is_member = self.members().any(|member| {
1793 self.current_opt() != Some(member) && member.name() == *dep_name
1795 });
1796 if is_member && specs.iter().any(|spec| spec.name() == dep_name.as_str()) {
1797 member_specific_features
1798 .entry(*dep_name)
1799 .or_default()
1800 .insert(FeatureValue::Feature(*dep_feature));
1801 } else {
1802 cwd_features.insert(feature.clone());
1803 }
1804 }
1805 }
1806 }
1807
1808 let ms: Vec<_> = self
1809 .members()
1810 .filter_map(|member| {
1811 let member_id = member.package_id();
1812 match self.current_opt() {
1813 Some(current) if member_id == current.package_id() => {
1816 let feats = CliFeatures {
1817 features: Rc::new(cwd_features.clone()),
1818 all_features: cli_features.all_features,
1819 uses_default_features: cli_features.uses_default_features,
1820 };
1821 Some((member, feats))
1822 }
1823 _ => {
1824 if specs.iter().any(|spec| spec.matches(member_id)) {
1826 let feats = CliFeatures {
1836 features: Rc::new(
1837 member_specific_features
1838 .remove(member.name().as_str())
1839 .unwrap_or_default(),
1840 ),
1841 uses_default_features: true,
1842 all_features: cli_features.all_features,
1843 };
1844 Some((member, feats))
1845 } else {
1846 None
1848 }
1849 }
1850 }
1851 })
1852 .collect();
1853
1854 assert!(member_specific_features.is_empty());
1857
1858 ms
1859 }
1860
1861 pub fn unit_needs_doc_scrape(&self, unit: &Unit) -> bool {
1863 self.is_member(&unit.pkg) && !(unit.target.for_host() || unit.pkg.proc_macro())
1868 }
1869
1870 pub fn add_local_overlay(&mut self, id: SourceId, registry_path: PathBuf) {
1874 self.local_overlays.insert(id, registry_path);
1875 }
1876
1877 pub fn package_registry(&self) -> CargoResult<PackageRegistry<'gctx>> {
1879 let source_config =
1880 SourceConfigMap::new_with_overlays(self.gctx(), self.local_overlays()?)?;
1881 PackageRegistry::new_with_source_config(self.gctx(), source_config)
1882 }
1883
1884 fn local_overlays(&self) -> CargoResult<impl Iterator<Item = (SourceId, SourceId)>> {
1886 let mut ret = self
1887 .local_overlays
1888 .iter()
1889 .map(|(id, path)| Ok((*id, SourceId::for_local_registry(path)?)))
1890 .collect::<CargoResult<Vec<_>>>()?;
1891
1892 if let Ok(overlay) = self
1893 .gctx
1894 .get_env("__CARGO_TEST_DEPENDENCY_CONFUSION_VULNERABILITY_DO_NOT_USE_THIS")
1895 {
1896 let (url, path) = overlay.split_once('=').ok_or(anyhow::anyhow!(
1897 "invalid overlay format. I won't tell you why; you shouldn't be using it anyway"
1898 ))?;
1899 ret.push((
1900 SourceId::from_url(url)?,
1901 SourceId::for_local_registry(path.as_ref())?,
1902 ));
1903 }
1904
1905 Ok(ret.into_iter())
1906 }
1907}
1908
1909impl<'gctx> Packages<'gctx> {
1910 fn get(&self, manifest_path: &Path) -> &MaybePackage {
1911 self.maybe_get(manifest_path).unwrap()
1912 }
1913
1914 fn get_mut(&mut self, manifest_path: &Path) -> &mut MaybePackage {
1915 self.maybe_get_mut(manifest_path).unwrap()
1916 }
1917
1918 fn maybe_get(&self, manifest_path: &Path) -> Option<&MaybePackage> {
1919 self.packages.get(manifest_path)
1920 }
1921
1922 fn maybe_get_mut(&mut self, manifest_path: &Path) -> Option<&mut MaybePackage> {
1923 self.packages.get_mut(manifest_path)
1924 }
1925
1926 fn load(&mut self, manifest_path: &Path) -> CargoResult<&MaybePackage> {
1927 match self.packages.entry(manifest_path.to_path_buf()) {
1928 Entry::Occupied(e) => Ok(e.into_mut()),
1929 Entry::Vacant(v) => {
1930 let source_id = SourceId::for_manifest_path(manifest_path)?;
1931 let manifest = read_manifest(manifest_path, source_id, self.gctx)?;
1932 Ok(v.insert(match manifest {
1933 EitherManifest::Real(manifest) => {
1934 MaybePackage::Package(Package::new(manifest, manifest_path))
1935 }
1936 EitherManifest::Virtual(vm) => MaybePackage::Virtual(vm),
1937 }))
1938 }
1939 }
1940 }
1941}
1942
1943impl MaybePackage {
1944 fn workspace_config(&self) -> &WorkspaceConfig {
1945 match *self {
1946 MaybePackage::Package(ref p) => p.manifest().workspace_config(),
1947 MaybePackage::Virtual(ref vm) => vm.workspace_config(),
1948 }
1949 }
1950
1951 pub fn is_embedded(&self) -> bool {
1953 match self {
1954 MaybePackage::Package(p) => p.manifest().is_embedded(),
1955 MaybePackage::Virtual(_) => false,
1956 }
1957 }
1958
1959 pub fn contents(&self) -> &str {
1960 match self {
1961 MaybePackage::Package(p) => p.manifest().contents(),
1962 MaybePackage::Virtual(v) => v.contents(),
1963 }
1964 }
1965
1966 pub fn document(&self) -> &toml::Spanned<toml::de::DeTable<'static>> {
1967 match self {
1968 MaybePackage::Package(p) => p.manifest().document(),
1969 MaybePackage::Virtual(v) => v.document(),
1970 }
1971 }
1972
1973 pub fn edition(&self) -> Edition {
1974 match self {
1975 MaybePackage::Package(p) => p.manifest().edition(),
1976 MaybePackage::Virtual(_) => Edition::default(),
1977 }
1978 }
1979
1980 pub fn profiles(&self) -> Option<&TomlProfiles> {
1981 match self {
1982 MaybePackage::Package(p) => p.manifest().profiles(),
1983 MaybePackage::Virtual(v) => v.profiles(),
1984 }
1985 }
1986
1987 pub fn unstable_features(&self) -> &Features {
1988 match self {
1989 MaybePackage::Package(p) => p.manifest().unstable_features(),
1990 MaybePackage::Virtual(vm) => vm.unstable_features(),
1991 }
1992 }
1993}
1994
1995impl WorkspaceRootConfig {
1996 pub fn new(
1998 root_dir: &Path,
1999 members: &Option<Vec<String>>,
2000 default_members: &Option<Vec<String>>,
2001 exclude: &Option<Vec<String>>,
2002 inheritable: &Option<InheritableFields>,
2003 custom_metadata: &Option<toml::Value>,
2004 ) -> WorkspaceRootConfig {
2005 WorkspaceRootConfig {
2006 root_dir: root_dir.to_path_buf(),
2007 members: members.clone(),
2008 default_members: default_members.clone(),
2009 exclude: exclude.clone().unwrap_or_default(),
2010 inheritable_fields: inheritable.clone().unwrap_or_default(),
2011 custom_metadata: custom_metadata.clone(),
2012 }
2013 }
2014 fn is_excluded(&self, manifest_path: &Path) -> bool {
2018 let excluded = self
2019 .exclude
2020 .iter()
2021 .any(|ex| manifest_path.starts_with(self.root_dir.join(ex)));
2022
2023 let explicit_member = match self.members {
2024 Some(ref members) => members
2025 .iter()
2026 .any(|mem| manifest_path.starts_with(self.root_dir.join(mem))),
2027 None => false,
2028 };
2029
2030 !explicit_member && excluded
2031 }
2032
2033 fn has_members_list(&self) -> bool {
2034 self.members.is_some()
2035 }
2036
2037 #[tracing::instrument(skip_all)]
2040 fn members_paths<'g>(
2041 &self,
2042 globs: &'g [String],
2043 ) -> CargoResult<Vec<(PathBuf, Option<&'g str>)>> {
2044 let mut expanded_list = Vec::new();
2045
2046 for glob in globs {
2047 let pathbuf = self.root_dir.join(glob);
2048 let expanded_paths = Self::expand_member_path(&pathbuf)?;
2049
2050 if expanded_paths.is_empty() {
2053 expanded_list.push((pathbuf, None));
2054 } else {
2055 let used_glob_pattern = expanded_paths.len() > 1 || expanded_paths[0] != pathbuf;
2056 let glob = used_glob_pattern.then_some(glob.as_str());
2057
2058 for expanded_path in expanded_paths {
2064 if expanded_path.is_dir() {
2065 expanded_list.push((expanded_path, glob));
2066 }
2067 }
2068 }
2069 }
2070
2071 Ok(expanded_list)
2072 }
2073
2074 fn expand_member_path(path: &Path) -> CargoResult<Vec<PathBuf>> {
2075 let Some(path) = path.to_str() else {
2076 return Ok(Vec::new());
2077 };
2078 let res = glob(path).with_context(|| format!("could not parse pattern `{}`", &path))?;
2079 let res = res
2080 .map(|p| p.with_context(|| format!("unable to match path to pattern `{}`", &path)))
2081 .collect::<Result<Vec<_>, _>>()?;
2082 Ok(res)
2083 }
2084
2085 pub fn inheritable(&self) -> &InheritableFields {
2086 &self.inheritable_fields
2087 }
2088}
2089
2090pub fn resolve_relative_path(
2091 label: &str,
2092 old_root: &Path,
2093 new_root: &Path,
2094 rel_path: &str,
2095) -> CargoResult<String> {
2096 let joined_path = normalize_path(&old_root.join(rel_path));
2097 match diff_paths(joined_path, new_root) {
2098 None => Err(anyhow!(
2099 "`{}` was defined in {} but could not be resolved with {}",
2100 label,
2101 old_root.display(),
2102 new_root.display()
2103 )),
2104 Some(path) => Ok(path
2105 .to_str()
2106 .ok_or_else(|| {
2107 anyhow!(
2108 "`{}` resolved to non-UTF value (`{}`)",
2109 label,
2110 path.display()
2111 )
2112 })?
2113 .to_owned()),
2114 }
2115}
2116
2117pub fn find_workspace_root(
2119 manifest_path: &Path,
2120 gctx: &GlobalContext,
2121) -> CargoResult<Option<PathBuf>> {
2122 find_workspace_root_with_loader(manifest_path, gctx, |self_path| {
2123 let source_id = SourceId::for_manifest_path(self_path)?;
2124 let manifest = read_manifest(self_path, source_id, gctx)?;
2125 Ok(manifest
2126 .workspace_config()
2127 .get_ws_root(self_path, manifest_path))
2128 })
2129}
2130
2131fn find_workspace_root_with_loader(
2136 manifest_path: &Path,
2137 gctx: &GlobalContext,
2138 mut loader: impl FnMut(&Path) -> CargoResult<Option<PathBuf>>,
2139) -> CargoResult<Option<PathBuf>> {
2140 {
2142 let roots = gctx.ws_roots();
2143 for current in manifest_path.ancestors().skip(1) {
2146 if let Some(ws_config) = roots.get(current) {
2147 if !ws_config.is_excluded(manifest_path) {
2148 return Ok(Some(current.join("Cargo.toml")));
2150 }
2151 }
2152 }
2153 }
2154
2155 for ances_manifest_path in find_root_iter(manifest_path, gctx) {
2156 debug!("find_root - trying {}", ances_manifest_path.display());
2157 if let Some(ws_root_path) = loader(&ances_manifest_path)? {
2158 return Ok(Some(ws_root_path));
2159 }
2160 }
2161 Ok(None)
2162}
2163
2164fn read_root_pointer(member_manifest: &Path, root_link: &str) -> PathBuf {
2165 let path = member_manifest
2166 .parent()
2167 .unwrap()
2168 .join(root_link)
2169 .join("Cargo.toml");
2170 debug!("find_root - pointer {}", path.display());
2171 paths::normalize_path(&path)
2172}
2173
2174fn find_root_iter<'a>(
2175 manifest_path: &'a Path,
2176 gctx: &'a GlobalContext,
2177) -> impl Iterator<Item = PathBuf> + 'a {
2178 LookBehind::new(paths::ancestors(manifest_path, None).skip(2))
2179 .take_while(|path| !path.curr.ends_with("target/package"))
2180 .take_while(|path| {
2186 if let Some(last) = path.last {
2187 gctx.home() != last
2188 } else {
2189 true
2190 }
2191 })
2192 .map(|path| path.curr.join("Cargo.toml"))
2193 .filter(|ances_manifest_path| ances_manifest_path.exists())
2194}
2195
2196struct LookBehindWindow<'a, T: ?Sized> {
2197 curr: &'a T,
2198 last: Option<&'a T>,
2199}
2200
2201struct LookBehind<'a, T: ?Sized, K: Iterator<Item = &'a T>> {
2202 iter: K,
2203 last: Option<&'a T>,
2204}
2205
2206impl<'a, T: ?Sized, K: Iterator<Item = &'a T>> LookBehind<'a, T, K> {
2207 fn new(items: K) -> Self {
2208 Self {
2209 iter: items,
2210 last: None,
2211 }
2212 }
2213}
2214
2215impl<'a, T: ?Sized, K: Iterator<Item = &'a T>> Iterator for LookBehind<'a, T, K> {
2216 type Item = LookBehindWindow<'a, T>;
2217
2218 fn next(&mut self) -> Option<Self::Item> {
2219 match self.iter.next() {
2220 None => None,
2221 Some(next) => {
2222 let last = self.last;
2223 self.last = Some(next);
2224 Some(LookBehindWindow { curr: next, last })
2225 }
2226 }
2227 }
2228}