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::{anyhow, bail, Context as _};
8use glob::glob;
9use itertools::Itertools;
10use tracing::debug;
11use url::Url;
12
13use crate::core::compiler::Unit;
14use crate::core::features::Features;
15use crate::core::registry::PackageRegistry;
16use crate::core::resolver::features::CliFeatures;
17use crate::core::resolver::ResolveBehavior;
18use crate::core::{
19 Dependency, Edition, FeatureValue, PackageId, PackageIdSpec, PackageIdSpecQuery,
20};
21use crate::core::{EitherManifest, Package, SourceId, VirtualManifest};
22use crate::ops;
23use crate::sources::{PathSource, SourceConfigMap, CRATES_IO_INDEX, CRATES_IO_REGISTRY};
24use crate::util::context::FeatureUnification;
25use crate::util::edit_distance;
26use crate::util::errors::{CargoResult, ManifestError};
27use crate::util::interning::InternedString;
28use crate::util::lints::{analyze_cargo_lints_table, check_im_a_teapot};
29use crate::util::toml::{read_manifest, InheritableFields};
30use crate::util::{
31 context::CargoResolverConfig, context::ConfigRelativePath, context::IncompatibleRustVersions,
32 Filesystem, GlobalContext, IntoUrl,
33};
34use cargo_util::paths;
35use cargo_util::paths::normalize_path;
36use cargo_util_schemas::manifest;
37use cargo_util_schemas::manifest::RustVersion;
38use cargo_util_schemas::manifest::{TomlDependency, TomlProfiles};
39use pathdiff::diff_paths;
40
41#[derive(Debug)]
47pub struct Workspace<'gctx> {
48 gctx: &'gctx GlobalContext,
50
51 current_manifest: PathBuf,
55
56 packages: Packages<'gctx>,
59
60 root_manifest: Option<PathBuf>,
65
66 target_dir: Option<Filesystem>,
69
70 build_dir: Option<Filesystem>,
73
74 members: Vec<PathBuf>,
78 member_ids: HashSet<PackageId>,
80
81 default_members: Vec<PathBuf>,
92
93 is_ephemeral: bool,
96
97 require_optional_deps: bool,
102
103 loaded_packages: RefCell<HashMap<PathBuf, Package>>,
106
107 ignore_lock: bool,
110
111 requested_lockfile_path: Option<PathBuf>,
113
114 resolve_behavior: ResolveBehavior,
116 resolve_honors_rust_version: bool,
120 resolve_feature_unification: FeatureUnification,
122 custom_metadata: Option<toml::Value>,
124
125 local_overlays: HashMap<SourceId, PathBuf>,
127}
128
129#[derive(Debug)]
132struct Packages<'gctx> {
133 gctx: &'gctx GlobalContext,
134 packages: HashMap<PathBuf, MaybePackage>,
135}
136
137#[derive(Debug)]
138pub enum MaybePackage {
139 Package(Package),
140 Virtual(VirtualManifest),
141}
142
143#[derive(Debug, Clone)]
145pub enum WorkspaceConfig {
146 Root(WorkspaceRootConfig),
149
150 Member { root: Option<String> },
153}
154
155impl WorkspaceConfig {
156 pub fn inheritable(&self) -> Option<&InheritableFields> {
157 match self {
158 WorkspaceConfig::Root(root) => Some(&root.inheritable_fields),
159 WorkspaceConfig::Member { .. } => None,
160 }
161 }
162
163 fn get_ws_root(&self, self_path: &Path, look_from: &Path) -> Option<PathBuf> {
171 match self {
172 WorkspaceConfig::Root(ances_root_config) => {
173 debug!("find_root - found a root checking exclusion");
174 if !ances_root_config.is_excluded(look_from) {
175 debug!("find_root - found!");
176 Some(self_path.to_owned())
177 } else {
178 None
179 }
180 }
181 WorkspaceConfig::Member {
182 root: Some(path_to_root),
183 } => {
184 debug!("find_root - found pointer");
185 Some(read_root_pointer(self_path, path_to_root))
186 }
187 WorkspaceConfig::Member { .. } => None,
188 }
189 }
190}
191
192#[derive(Debug, Clone)]
197pub struct WorkspaceRootConfig {
198 root_dir: PathBuf,
199 members: Option<Vec<String>>,
200 default_members: Option<Vec<String>>,
201 exclude: Vec<String>,
202 inheritable_fields: InheritableFields,
203 custom_metadata: Option<toml::Value>,
204}
205
206impl<'gctx> Workspace<'gctx> {
207 pub fn new(manifest_path: &Path, gctx: &'gctx GlobalContext) -> CargoResult<Workspace<'gctx>> {
214 let mut ws = Workspace::new_default(manifest_path.to_path_buf(), gctx);
215 ws.target_dir = gctx.target_dir()?;
216
217 if manifest_path.is_relative() {
218 bail!(
219 "manifest_path:{:?} is not an absolute path. Please provide an absolute path.",
220 manifest_path
221 )
222 } else {
223 ws.root_manifest = ws.find_root(manifest_path)?;
224 }
225
226 ws.build_dir = gctx.build_dir(
227 ws.root_manifest
228 .as_ref()
229 .unwrap_or(&manifest_path.to_path_buf()),
230 )?;
231
232 ws.custom_metadata = ws
233 .load_workspace_config()?
234 .and_then(|cfg| cfg.custom_metadata);
235 ws.find_members()?;
236 ws.set_resolve_behavior()?;
237 ws.validate()?;
238 Ok(ws)
239 }
240
241 fn new_default(current_manifest: PathBuf, gctx: &'gctx GlobalContext) -> Workspace<'gctx> {
242 Workspace {
243 gctx,
244 current_manifest,
245 packages: Packages {
246 gctx,
247 packages: HashMap::new(),
248 },
249 root_manifest: None,
250 target_dir: None,
251 build_dir: None,
252 members: Vec::new(),
253 member_ids: HashSet::new(),
254 default_members: Vec::new(),
255 is_ephemeral: false,
256 require_optional_deps: true,
257 loaded_packages: RefCell::new(HashMap::new()),
258 ignore_lock: false,
259 requested_lockfile_path: None,
260 resolve_behavior: ResolveBehavior::V1,
261 resolve_honors_rust_version: false,
262 resolve_feature_unification: FeatureUnification::Selected,
263 custom_metadata: None,
264 local_overlays: HashMap::new(),
265 }
266 }
267
268 pub fn ephemeral(
278 package: Package,
279 gctx: &'gctx GlobalContext,
280 target_dir: Option<Filesystem>,
281 require_optional_deps: bool,
282 ) -> CargoResult<Workspace<'gctx>> {
283 let mut ws = Workspace::new_default(package.manifest_path().to_path_buf(), gctx);
284 ws.is_ephemeral = true;
285 ws.require_optional_deps = require_optional_deps;
286 let id = package.package_id();
287 let package = MaybePackage::Package(package);
288 ws.packages
289 .packages
290 .insert(ws.current_manifest.clone(), package);
291 ws.target_dir = if let Some(dir) = target_dir {
292 Some(dir)
293 } else {
294 ws.gctx.target_dir()?
295 };
296 ws.build_dir = ws.target_dir.clone();
297 ws.members.push(ws.current_manifest.clone());
298 ws.member_ids.insert(id);
299 ws.default_members.push(ws.current_manifest.clone());
300 ws.set_resolve_behavior()?;
301 Ok(ws)
302 }
303
304 pub fn reload(&self, gctx: &'gctx GlobalContext) -> CargoResult<Workspace<'gctx>> {
309 let mut ws = Workspace::new(self.root_manifest(), gctx)?;
310 ws.set_resolve_honors_rust_version(Some(self.resolve_honors_rust_version));
311 ws.set_resolve_feature_unification(self.resolve_feature_unification);
312 ws.set_requested_lockfile_path(self.requested_lockfile_path.clone());
313 Ok(ws)
314 }
315
316 fn set_resolve_behavior(&mut self) -> CargoResult<()> {
317 self.resolve_behavior = match self.root_maybe() {
322 MaybePackage::Package(p) => p
323 .manifest()
324 .resolve_behavior()
325 .unwrap_or_else(|| p.manifest().edition().default_resolve_behavior()),
326 MaybePackage::Virtual(vm) => vm.resolve_behavior().unwrap_or(ResolveBehavior::V1),
327 };
328
329 match self.resolve_behavior() {
330 ResolveBehavior::V1 | ResolveBehavior::V2 => {}
331 ResolveBehavior::V3 => {
332 if self.resolve_behavior == ResolveBehavior::V3 {
333 self.resolve_honors_rust_version = true;
334 }
335 }
336 }
337 let config = self.gctx().get::<CargoResolverConfig>("resolver")?;
338 if let Some(incompatible_rust_versions) = config.incompatible_rust_versions {
339 self.resolve_honors_rust_version =
340 incompatible_rust_versions == IncompatibleRustVersions::Fallback;
341 }
342 if self.gctx().cli_unstable().feature_unification {
343 self.resolve_feature_unification = config
344 .feature_unification
345 .unwrap_or(FeatureUnification::Selected);
346 } else if config.feature_unification.is_some() {
347 self.gctx()
348 .shell()
349 .warn("ignoring `resolver.feature-unification` without `-Zfeature-unification`")?;
350 };
351
352 Ok(())
353 }
354
355 pub fn current(&self) -> CargoResult<&Package> {
361 let pkg = self.current_opt().ok_or_else(|| {
362 anyhow::format_err!(
363 "manifest path `{}` is a virtual manifest, but this \
364 command requires running against an actual package in \
365 this workspace",
366 self.current_manifest.display()
367 )
368 })?;
369 Ok(pkg)
370 }
371
372 pub fn current_mut(&mut self) -> CargoResult<&mut Package> {
373 let cm = self.current_manifest.clone();
374 let pkg = self.current_opt_mut().ok_or_else(|| {
375 anyhow::format_err!(
376 "manifest path `{}` is a virtual manifest, but this \
377 command requires running against an actual package in \
378 this workspace",
379 cm.display()
380 )
381 })?;
382 Ok(pkg)
383 }
384
385 pub fn current_opt(&self) -> Option<&Package> {
386 match *self.packages.get(&self.current_manifest) {
387 MaybePackage::Package(ref p) => Some(p),
388 MaybePackage::Virtual(..) => None,
389 }
390 }
391
392 pub fn current_opt_mut(&mut self) -> Option<&mut Package> {
393 match *self.packages.get_mut(&self.current_manifest) {
394 MaybePackage::Package(ref mut p) => Some(p),
395 MaybePackage::Virtual(..) => None,
396 }
397 }
398
399 pub fn is_virtual(&self) -> bool {
400 match *self.packages.get(&self.current_manifest) {
401 MaybePackage::Package(..) => false,
402 MaybePackage::Virtual(..) => true,
403 }
404 }
405
406 pub fn gctx(&self) -> &'gctx GlobalContext {
408 self.gctx
409 }
410
411 pub fn profiles(&self) -> Option<&TomlProfiles> {
412 match self.root_maybe() {
413 MaybePackage::Package(p) => p.manifest().profiles(),
414 MaybePackage::Virtual(vm) => vm.profiles(),
415 }
416 }
417
418 pub fn root(&self) -> &Path {
423 self.root_manifest().parent().unwrap()
424 }
425
426 pub fn root_manifest(&self) -> &Path {
429 self.root_manifest
430 .as_ref()
431 .unwrap_or(&self.current_manifest)
432 }
433
434 pub fn root_maybe(&self) -> &MaybePackage {
436 self.packages.get(self.root_manifest())
437 }
438
439 pub fn target_dir(&self) -> Filesystem {
440 self.target_dir
441 .clone()
442 .unwrap_or_else(|| self.default_target_dir())
443 }
444
445 pub fn build_dir(&self) -> Filesystem {
446 if !self.gctx().cli_unstable().build_dir {
447 return self.target_dir();
448 }
449 self.build_dir.clone().unwrap_or_else(|| self.target_dir())
450 }
451
452 fn default_target_dir(&self) -> Filesystem {
453 if self.root_maybe().is_embedded() {
454 let hash = crate::util::hex::short_hash(&self.root_manifest().to_string_lossy());
455 let mut rel_path = PathBuf::new();
456 rel_path.push("target");
457 rel_path.push(&hash[0..2]);
458 rel_path.push(&hash[2..]);
459
460 self.gctx().home().join(rel_path)
461 } else {
462 Filesystem::new(self.root().join("target"))
463 }
464 }
465
466 pub fn root_replace(&self) -> &[(PackageIdSpec, Dependency)] {
470 match self.root_maybe() {
471 MaybePackage::Package(p) => p.manifest().replace(),
472 MaybePackage::Virtual(vm) => vm.replace(),
473 }
474 }
475
476 fn config_patch(&self) -> CargoResult<HashMap<Url, Vec<Dependency>>> {
477 let config_patch: Option<
478 BTreeMap<String, BTreeMap<String, TomlDependency<ConfigRelativePath>>>,
479 > = self.gctx.get("patch")?;
480
481 let source = SourceId::for_manifest_path(self.root_manifest())?;
482
483 let mut warnings = Vec::new();
484
485 let mut patch = HashMap::new();
486 for (url, deps) in config_patch.into_iter().flatten() {
487 let url = match &url[..] {
488 CRATES_IO_REGISTRY => CRATES_IO_INDEX.parse().unwrap(),
489 url => self
490 .gctx
491 .get_registry_index(url)
492 .or_else(|_| url.into_url())
493 .with_context(|| {
494 format!("[patch] entry `{}` should be a URL or registry name", url)
495 })?,
496 };
497 patch.insert(
498 url,
499 deps.iter()
500 .map(|(name, dep)| {
501 crate::util::toml::to_dependency(
502 dep,
503 name,
504 source,
505 self.gctx,
506 &mut warnings,
507 None,
508 Path::new("unused-relative-path"),
511 None,
512 )
513 })
514 .collect::<CargoResult<Vec<_>>>()?,
515 );
516 }
517
518 for message in warnings {
519 self.gctx
520 .shell()
521 .warn(format!("[patch] in cargo config: {}", message))?
522 }
523
524 Ok(patch)
525 }
526
527 pub fn root_patch(&self) -> CargoResult<HashMap<Url, Vec<Dependency>>> {
531 let from_manifest = match self.root_maybe() {
532 MaybePackage::Package(p) => p.manifest().patch(),
533 MaybePackage::Virtual(vm) => vm.patch(),
534 };
535
536 let from_config = self.config_patch()?;
537 if from_config.is_empty() {
538 return Ok(from_manifest.clone());
539 }
540 if from_manifest.is_empty() {
541 return Ok(from_config);
542 }
543
544 let mut combined = from_config;
547 for (url, deps_from_manifest) in from_manifest {
548 if let Some(deps_from_config) = combined.get_mut(url) {
549 let mut from_manifest_pruned = deps_from_manifest.clone();
552 for dep_from_config in &mut *deps_from_config {
553 if let Some(i) = from_manifest_pruned.iter().position(|dep_from_manifest| {
554 dep_from_config.name_in_toml() == dep_from_manifest.name_in_toml()
556 }) {
557 from_manifest_pruned.swap_remove(i);
558 }
559 }
560 deps_from_config.extend(from_manifest_pruned);
562 } else {
563 combined.insert(url.clone(), deps_from_manifest.clone());
564 }
565 }
566 Ok(combined)
567 }
568
569 pub fn members(&self) -> impl Iterator<Item = &Package> {
571 let packages = &self.packages;
572 self.members
573 .iter()
574 .filter_map(move |path| match packages.get(path) {
575 MaybePackage::Package(p) => Some(p),
576 _ => None,
577 })
578 }
579
580 pub fn members_mut(&mut self) -> impl Iterator<Item = &mut Package> {
582 let packages = &mut self.packages.packages;
583 let members: HashSet<_> = self.members.iter().map(|path| path).collect();
584
585 packages.iter_mut().filter_map(move |(path, package)| {
586 if members.contains(path) {
587 if let MaybePackage::Package(ref mut p) = package {
588 return Some(p);
589 }
590 }
591
592 None
593 })
594 }
595
596 pub fn default_members<'a>(&'a self) -> impl Iterator<Item = &'a Package> {
598 let packages = &self.packages;
599 self.default_members
600 .iter()
601 .filter_map(move |path| match packages.get(path) {
602 MaybePackage::Package(p) => Some(p),
603 _ => None,
604 })
605 }
606
607 pub fn default_members_mut(&mut self) -> impl Iterator<Item = &mut Package> {
609 let packages = &mut self.packages.packages;
610 let members: HashSet<_> = self
611 .default_members
612 .iter()
613 .map(|path| path.parent().unwrap().to_owned())
614 .collect();
615
616 packages.iter_mut().filter_map(move |(path, package)| {
617 if members.contains(path) {
618 if let MaybePackage::Package(ref mut p) = package {
619 return Some(p);
620 }
621 }
622
623 None
624 })
625 }
626
627 pub fn is_member(&self, pkg: &Package) -> bool {
629 self.member_ids.contains(&pkg.package_id())
630 }
631
632 pub fn is_member_id(&self, package_id: PackageId) -> bool {
634 self.member_ids.contains(&package_id)
635 }
636
637 pub fn is_ephemeral(&self) -> bool {
638 self.is_ephemeral
639 }
640
641 pub fn require_optional_deps(&self) -> bool {
642 self.require_optional_deps
643 }
644
645 pub fn set_require_optional_deps(
646 &mut self,
647 require_optional_deps: bool,
648 ) -> &mut Workspace<'gctx> {
649 self.require_optional_deps = require_optional_deps;
650 self
651 }
652
653 pub fn ignore_lock(&self) -> bool {
654 self.ignore_lock
655 }
656
657 pub fn set_ignore_lock(&mut self, ignore_lock: bool) -> &mut Workspace<'gctx> {
658 self.ignore_lock = ignore_lock;
659 self
660 }
661
662 pub fn lock_root(&self) -> Filesystem {
664 if let Some(requested) = self.requested_lockfile_path.as_ref() {
665 return Filesystem::new(
666 requested
667 .parent()
668 .expect("Lockfile path can't be root")
669 .to_owned(),
670 );
671 }
672 self.default_lock_root()
673 }
674
675 fn default_lock_root(&self) -> Filesystem {
676 if self.root_maybe().is_embedded() {
677 self.target_dir()
678 } else {
679 Filesystem::new(self.root().to_owned())
680 }
681 }
682
683 pub fn set_requested_lockfile_path(&mut self, path: Option<PathBuf>) {
684 self.requested_lockfile_path = path;
685 }
686
687 pub fn requested_lockfile_path(&self) -> Option<&Path> {
688 self.requested_lockfile_path.as_deref()
689 }
690
691 pub fn lowest_rust_version(&self) -> Option<&RustVersion> {
694 self.members().filter_map(|pkg| pkg.rust_version()).min()
695 }
696
697 pub fn set_resolve_honors_rust_version(&mut self, honor_rust_version: Option<bool>) {
698 if let Some(honor_rust_version) = honor_rust_version {
699 self.resolve_honors_rust_version = honor_rust_version;
700 }
701 }
702
703 pub fn resolve_honors_rust_version(&self) -> bool {
704 self.resolve_honors_rust_version
705 }
706
707 pub fn set_resolve_feature_unification(&mut self, feature_unification: FeatureUnification) {
708 self.resolve_feature_unification = feature_unification;
709 }
710
711 pub fn resolve_feature_unification(&self) -> FeatureUnification {
712 self.resolve_feature_unification
713 }
714
715 pub fn custom_metadata(&self) -> Option<&toml::Value> {
716 self.custom_metadata.as_ref()
717 }
718
719 pub fn load_workspace_config(&mut self) -> CargoResult<Option<WorkspaceRootConfig>> {
720 if let Some(root_path) = &self.root_manifest {
723 let root_package = self.packages.load(root_path)?;
724 match root_package.workspace_config() {
725 WorkspaceConfig::Root(ref root_config) => {
726 return Ok(Some(root_config.clone()));
727 }
728
729 _ => bail!(
730 "root of a workspace inferred but wasn't a root: {}",
731 root_path.display()
732 ),
733 }
734 }
735
736 Ok(None)
737 }
738
739 fn find_root(&mut self, manifest_path: &Path) -> CargoResult<Option<PathBuf>> {
749 let current = self.packages.load(manifest_path)?;
750 match current
751 .workspace_config()
752 .get_ws_root(manifest_path, manifest_path)
753 {
754 Some(root_path) => {
755 debug!("find_root - is root {}", manifest_path.display());
756 Ok(Some(root_path))
757 }
758 None => find_workspace_root_with_loader(manifest_path, self.gctx, |self_path| {
759 Ok(self
760 .packages
761 .load(self_path)?
762 .workspace_config()
763 .get_ws_root(self_path, manifest_path))
764 }),
765 }
766 }
767
768 #[tracing::instrument(skip_all)]
776 fn find_members(&mut self) -> CargoResult<()> {
777 let Some(workspace_config) = self.load_workspace_config()? else {
778 debug!("find_members - only me as a member");
779 self.members.push(self.current_manifest.clone());
780 self.default_members.push(self.current_manifest.clone());
781 if let Ok(pkg) = self.current() {
782 let id = pkg.package_id();
783 self.member_ids.insert(id);
784 }
785 return Ok(());
786 };
787
788 let root_manifest_path = self.root_manifest.clone().unwrap();
790
791 let members_paths = workspace_config
792 .members_paths(workspace_config.members.as_deref().unwrap_or_default())?;
793 let default_members_paths = if root_manifest_path == self.current_manifest {
794 if let Some(ref default) = workspace_config.default_members {
795 Some(workspace_config.members_paths(default)?)
796 } else {
797 None
798 }
799 } else {
800 None
801 };
802
803 for (path, glob) in &members_paths {
804 self.find_path_deps(&path.join("Cargo.toml"), &root_manifest_path, false)
805 .with_context(|| {
806 format!(
807 "failed to load manifest for workspace member `{}`\n\
808 referenced{} by workspace at `{}`",
809 path.display(),
810 glob.map(|g| format!(" via `{g}`")).unwrap_or_default(),
811 root_manifest_path.display(),
812 )
813 })?;
814 }
815
816 self.find_path_deps(&root_manifest_path, &root_manifest_path, false)?;
817
818 if let Some(default) = default_members_paths {
819 for (path, default_member_glob) in default {
820 let normalized_path = paths::normalize_path(&path);
821 let manifest_path = normalized_path.join("Cargo.toml");
822 if !self.members.contains(&manifest_path) {
823 let exclude = members_paths.iter().any(|(m, _)| *m == normalized_path)
830 && workspace_config.is_excluded(&normalized_path);
831 if exclude {
832 continue;
833 }
834 bail!(
835 "package `{}` is listed in default-members{} but is not a member\n\
836 for workspace at `{}`.",
837 path.display(),
838 default_member_glob
839 .map(|g| format!(" via `{g}`"))
840 .unwrap_or_default(),
841 root_manifest_path.display(),
842 )
843 }
844 self.default_members.push(manifest_path)
845 }
846 } else if self.is_virtual() {
847 self.default_members = self.members.clone()
848 } else {
849 self.default_members.push(self.current_manifest.clone())
850 }
851
852 Ok(())
853 }
854
855 fn find_path_deps(
856 &mut self,
857 manifest_path: &Path,
858 root_manifest: &Path,
859 is_path_dep: bool,
860 ) -> CargoResult<()> {
861 let manifest_path = paths::normalize_path(manifest_path);
862 if self.members.contains(&manifest_path) {
863 return Ok(());
864 }
865 if is_path_dep && self.root_maybe().is_embedded() {
866 return Ok(());
868 }
869 if is_path_dep
870 && !manifest_path.parent().unwrap().starts_with(self.root())
871 && self.find_root(&manifest_path)? != self.root_manifest
872 {
873 return Ok(());
876 }
877
878 if let WorkspaceConfig::Root(ref root_config) =
879 *self.packages.load(root_manifest)?.workspace_config()
880 {
881 if root_config.is_excluded(&manifest_path) {
882 return Ok(());
883 }
884 }
885
886 debug!("find_path_deps - {}", manifest_path.display());
887 self.members.push(manifest_path.clone());
888
889 let candidates = {
890 let pkg = match *self.packages.load(&manifest_path)? {
891 MaybePackage::Package(ref p) => p,
892 MaybePackage::Virtual(_) => return Ok(()),
893 };
894 self.member_ids.insert(pkg.package_id());
895 pkg.dependencies()
896 .iter()
897 .map(|d| (d.source_id(), d.package_name()))
898 .filter(|(s, _)| s.is_path())
899 .filter_map(|(s, n)| s.url().to_file_path().ok().map(|p| (p, n)))
900 .map(|(p, n)| (p.join("Cargo.toml"), n))
901 .collect::<Vec<_>>()
902 };
903 for (path, name) in candidates {
904 self.find_path_deps(&path, root_manifest, true)
905 .with_context(|| format!("failed to load manifest for dependency `{}`", name))
906 .map_err(|err| ManifestError::new(err, manifest_path.clone()))?;
907 }
908 Ok(())
909 }
910
911 pub fn unstable_features(&self) -> &Features {
913 match self.root_maybe() {
914 MaybePackage::Package(p) => p.manifest().unstable_features(),
915 MaybePackage::Virtual(vm) => vm.unstable_features(),
916 }
917 }
918
919 pub fn resolve_behavior(&self) -> ResolveBehavior {
920 self.resolve_behavior
921 }
922
923 pub fn allows_new_cli_feature_behavior(&self) -> bool {
931 self.is_virtual()
932 || match self.resolve_behavior() {
933 ResolveBehavior::V1 => false,
934 ResolveBehavior::V2 | ResolveBehavior::V3 => true,
935 }
936 }
937
938 #[tracing::instrument(skip_all)]
944 fn validate(&mut self) -> CargoResult<()> {
945 if self.root_manifest.is_none() {
947 return Ok(());
948 }
949
950 self.validate_unique_names()?;
951 self.validate_workspace_roots()?;
952 self.validate_members()?;
953 self.error_if_manifest_not_in_members()?;
954 self.validate_manifest()
955 }
956
957 fn validate_unique_names(&self) -> CargoResult<()> {
958 let mut names = BTreeMap::new();
959 for member in self.members.iter() {
960 let package = self.packages.get(member);
961 let name = match *package {
962 MaybePackage::Package(ref p) => p.name(),
963 MaybePackage::Virtual(_) => continue,
964 };
965 if let Some(prev) = names.insert(name, member) {
966 bail!(
967 "two packages named `{}` in this workspace:\n\
968 - {}\n\
969 - {}",
970 name,
971 prev.display(),
972 member.display()
973 );
974 }
975 }
976 Ok(())
977 }
978
979 fn validate_workspace_roots(&self) -> CargoResult<()> {
980 let roots: Vec<PathBuf> = self
981 .members
982 .iter()
983 .filter(|&member| {
984 let config = self.packages.get(member).workspace_config();
985 matches!(config, WorkspaceConfig::Root(_))
986 })
987 .map(|member| member.parent().unwrap().to_path_buf())
988 .collect();
989 match roots.len() {
990 1 => Ok(()),
991 0 => bail!(
992 "`package.workspace` configuration points to a crate \
993 which is not configured with [workspace]: \n\
994 configuration at: {}\n\
995 points to: {}",
996 self.current_manifest.display(),
997 self.root_manifest.as_ref().unwrap().display()
998 ),
999 _ => {
1000 bail!(
1001 "multiple workspace roots found in the same workspace:\n{}",
1002 roots
1003 .iter()
1004 .map(|r| format!(" {}", r.display()))
1005 .collect::<Vec<_>>()
1006 .join("\n")
1007 );
1008 }
1009 }
1010 }
1011
1012 #[tracing::instrument(skip_all)]
1013 fn validate_members(&mut self) -> CargoResult<()> {
1014 for member in self.members.clone() {
1015 let root = self.find_root(&member)?;
1016 if root == self.root_manifest {
1017 continue;
1018 }
1019
1020 match root {
1021 Some(root) => {
1022 bail!(
1023 "package `{}` is a member of the wrong workspace\n\
1024 expected: {}\n\
1025 actual: {}",
1026 member.display(),
1027 self.root_manifest.as_ref().unwrap().display(),
1028 root.display()
1029 );
1030 }
1031 None => {
1032 bail!(
1033 "workspace member `{}` is not hierarchically below \
1034 the workspace root `{}`",
1035 member.display(),
1036 self.root_manifest.as_ref().unwrap().display()
1037 );
1038 }
1039 }
1040 }
1041 Ok(())
1042 }
1043
1044 fn error_if_manifest_not_in_members(&mut self) -> CargoResult<()> {
1045 if self.members.contains(&self.current_manifest) {
1046 return Ok(());
1047 }
1048
1049 let root = self.root_manifest.as_ref().unwrap();
1050 let root_dir = root.parent().unwrap();
1051 let current_dir = self.current_manifest.parent().unwrap();
1052 let root_pkg = self.packages.get(root);
1053
1054 let members_msg = match current_dir.strip_prefix(root_dir) {
1056 Ok(rel) => format!(
1057 "this may be fixable by adding `{}` to the \
1058 `workspace.members` array of the manifest \
1059 located at: {}",
1060 rel.display(),
1061 root.display()
1062 ),
1063 Err(_) => format!(
1064 "this may be fixable by adding a member to \
1065 the `workspace.members` array of the \
1066 manifest located at: {}",
1067 root.display()
1068 ),
1069 };
1070 let extra = match *root_pkg {
1071 MaybePackage::Virtual(_) => members_msg,
1072 MaybePackage::Package(ref p) => {
1073 let has_members_list = match *p.manifest().workspace_config() {
1074 WorkspaceConfig::Root(ref root_config) => root_config.has_members_list(),
1075 WorkspaceConfig::Member { .. } => unreachable!(),
1076 };
1077 if !has_members_list {
1078 format!(
1079 "this may be fixable by ensuring that this \
1080 crate is depended on by the workspace \
1081 root: {}",
1082 root.display()
1083 )
1084 } else {
1085 members_msg
1086 }
1087 }
1088 };
1089 bail!(
1090 "current package believes it's in a workspace when it's not:\n\
1091 current: {}\n\
1092 workspace: {}\n\n{}\n\
1093 Alternatively, to keep it out of the workspace, add the package \
1094 to the `workspace.exclude` array, or add an empty `[workspace]` \
1095 table to the package's manifest.",
1096 self.current_manifest.display(),
1097 root.display(),
1098 extra
1099 );
1100 }
1101
1102 fn validate_manifest(&mut self) -> CargoResult<()> {
1103 if let Some(ref root_manifest) = self.root_manifest {
1104 for pkg in self
1105 .members()
1106 .filter(|p| p.manifest_path() != root_manifest)
1107 {
1108 let manifest = pkg.manifest();
1109 let emit_warning = |what| -> CargoResult<()> {
1110 let msg = format!(
1111 "{} for the non root package will be ignored, \
1112 specify {} at the workspace root:\n\
1113 package: {}\n\
1114 workspace: {}",
1115 what,
1116 what,
1117 pkg.manifest_path().display(),
1118 root_manifest.display(),
1119 );
1120 self.gctx.shell().warn(&msg)
1121 };
1122 if manifest.normalized_toml().has_profiles() {
1123 emit_warning("profiles")?;
1124 }
1125 if !manifest.replace().is_empty() {
1126 emit_warning("replace")?;
1127 }
1128 if !manifest.patch().is_empty() {
1129 emit_warning("patch")?;
1130 }
1131 if let Some(behavior) = manifest.resolve_behavior() {
1132 if behavior != self.resolve_behavior {
1133 emit_warning("resolver")?;
1135 }
1136 }
1137 }
1138 if let MaybePackage::Virtual(vm) = self.root_maybe() {
1139 if vm.resolve_behavior().is_none() {
1140 if let Some(edition) = self
1141 .members()
1142 .filter(|p| p.manifest_path() != root_manifest)
1143 .map(|p| p.manifest().edition())
1144 .filter(|&e| e >= Edition::Edition2021)
1145 .max()
1146 {
1147 let resolver = edition.default_resolve_behavior().to_manifest();
1148 self.gctx.shell().warn(format_args!(
1149 "virtual workspace defaulting to `resolver = \"1\"` despite one or more workspace members being on edition {edition} which implies `resolver = \"{resolver}\"`"
1150 ))?;
1151 self.gctx.shell().note(
1152 "to keep the current resolver, specify `workspace.resolver = \"1\"` in the workspace root's manifest",
1153 )?;
1154 self.gctx.shell().note(format_args!(
1155 "to use the edition {edition} resolver, specify `workspace.resolver = \"{resolver}\"` in the workspace root's manifest"
1156 ))?;
1157 self.gctx.shell().note(
1158 "for more details see https://doc.rust-lang.org/cargo/reference/resolver.html#resolver-versions",
1159 )?;
1160 }
1161 }
1162 }
1163 }
1164 Ok(())
1165 }
1166
1167 pub fn load(&self, manifest_path: &Path) -> CargoResult<Package> {
1168 match self.packages.maybe_get(manifest_path) {
1169 Some(MaybePackage::Package(p)) => return Ok(p.clone()),
1170 Some(&MaybePackage::Virtual(_)) => bail!("cannot load workspace root"),
1171 None => {}
1172 }
1173
1174 let mut loaded = self.loaded_packages.borrow_mut();
1175 if let Some(p) = loaded.get(manifest_path).cloned() {
1176 return Ok(p);
1177 }
1178 let source_id = SourceId::for_manifest_path(manifest_path)?;
1179 let package = ops::read_package(manifest_path, source_id, self.gctx)?;
1180 loaded.insert(manifest_path.to_path_buf(), package.clone());
1181 Ok(package)
1182 }
1183
1184 pub fn preload(&self, registry: &mut PackageRegistry<'gctx>) {
1191 if self.is_ephemeral {
1197 return;
1198 }
1199
1200 for pkg in self.packages.packages.values() {
1201 let pkg = match *pkg {
1202 MaybePackage::Package(ref p) => p.clone(),
1203 MaybePackage::Virtual(_) => continue,
1204 };
1205 let src = PathSource::preload_with(pkg, self.gctx);
1206 registry.add_preloaded(Box::new(src));
1207 }
1208 }
1209
1210 pub fn emit_warnings(&self) -> CargoResult<()> {
1211 for (path, maybe_pkg) in &self.packages.packages {
1212 if let MaybePackage::Package(pkg) = maybe_pkg {
1213 if self.gctx.cli_unstable().cargo_lints {
1214 self.emit_lints(pkg, &path)?
1215 }
1216 }
1217 let warnings = match maybe_pkg {
1218 MaybePackage::Package(pkg) => pkg.manifest().warnings().warnings(),
1219 MaybePackage::Virtual(vm) => vm.warnings().warnings(),
1220 };
1221 for warning in warnings {
1222 if warning.is_critical {
1223 let err = anyhow::format_err!("{}", warning.message);
1224 let cx =
1225 anyhow::format_err!("failed to parse manifest at `{}`", path.display());
1226 return Err(err.context(cx));
1227 } else {
1228 let msg = if self.root_manifest.is_none() {
1229 warning.message.to_string()
1230 } else {
1231 format!("{}: {}", path.display(), warning.message)
1234 };
1235 self.gctx.shell().warn(msg)?
1236 }
1237 }
1238 }
1239 Ok(())
1240 }
1241
1242 pub fn emit_lints(&self, pkg: &Package, path: &Path) -> CargoResult<()> {
1243 let mut error_count = 0;
1244 let toml_lints = pkg
1245 .manifest()
1246 .normalized_toml()
1247 .lints
1248 .clone()
1249 .map(|lints| lints.lints)
1250 .unwrap_or(manifest::TomlLints::default());
1251 let cargo_lints = toml_lints
1252 .get("cargo")
1253 .cloned()
1254 .unwrap_or(manifest::TomlToolLints::default());
1255
1256 let ws_contents = match self.root_maybe() {
1257 MaybePackage::Package(pkg) => pkg.manifest().contents(),
1258 MaybePackage::Virtual(v) => v.contents(),
1259 };
1260
1261 let ws_document = match self.root_maybe() {
1262 MaybePackage::Package(pkg) => pkg.manifest().document(),
1263 MaybePackage::Virtual(v) => v.document(),
1264 };
1265
1266 analyze_cargo_lints_table(
1267 pkg,
1268 &path,
1269 &cargo_lints,
1270 ws_contents,
1271 ws_document,
1272 self.root_manifest(),
1273 self.gctx,
1274 )?;
1275 check_im_a_teapot(pkg, &path, &cargo_lints, &mut error_count, self.gctx)?;
1276 if error_count > 0 {
1277 Err(crate::util::errors::AlreadyPrintedError::new(anyhow!(
1278 "encountered {error_count} errors(s) while running lints"
1279 ))
1280 .into())
1281 } else {
1282 Ok(())
1283 }
1284 }
1285
1286 pub fn set_target_dir(&mut self, target_dir: Filesystem) {
1287 self.target_dir = Some(target_dir);
1288 }
1289
1290 pub fn members_with_features(
1298 &self,
1299 specs: &[PackageIdSpec],
1300 cli_features: &CliFeatures,
1301 ) -> CargoResult<Vec<(&Package, CliFeatures)>> {
1302 assert!(
1303 !specs.is_empty() || cli_features.all_features,
1304 "no specs requires all_features"
1305 );
1306 if specs.is_empty() {
1307 return Ok(self
1310 .members()
1311 .map(|m| (m, CliFeatures::new_all(true)))
1312 .collect());
1313 }
1314 if self.allows_new_cli_feature_behavior() {
1315 self.members_with_features_new(specs, cli_features)
1316 } else {
1317 Ok(self.members_with_features_old(specs, cli_features))
1318 }
1319 }
1320
1321 fn collect_matching_features(
1324 member: &Package,
1325 cli_features: &CliFeatures,
1326 found_features: &mut BTreeSet<FeatureValue>,
1327 ) -> CliFeatures {
1328 if cli_features.features.is_empty() {
1329 return cli_features.clone();
1330 }
1331
1332 let summary = member.summary();
1334
1335 let summary_features = summary.features();
1337
1338 let dependencies: BTreeMap<InternedString, &Dependency> = summary
1340 .dependencies()
1341 .iter()
1342 .map(|dep| (dep.name_in_toml(), dep))
1343 .collect();
1344
1345 let optional_dependency_names: BTreeSet<_> = dependencies
1347 .iter()
1348 .filter(|(_, dep)| dep.is_optional())
1349 .map(|(name, _)| name)
1350 .copied()
1351 .collect();
1352
1353 let mut features = BTreeSet::new();
1354
1355 let summary_or_opt_dependency_feature = |feature: &InternedString| -> bool {
1357 summary_features.contains_key(feature) || optional_dependency_names.contains(feature)
1358 };
1359
1360 for feature in cli_features.features.iter() {
1361 match feature {
1362 FeatureValue::Feature(f) => {
1363 if summary_or_opt_dependency_feature(f) {
1364 features.insert(feature.clone());
1366 found_features.insert(feature.clone());
1367 }
1368 }
1369 FeatureValue::Dep { .. } => panic!("unexpected dep: syntax {}", feature),
1371 FeatureValue::DepFeature {
1372 dep_name,
1373 dep_feature,
1374 weak: _,
1375 } => {
1376 if dependencies.contains_key(dep_name) {
1377 features.insert(feature.clone());
1380 found_features.insert(feature.clone());
1381 } else if *dep_name == member.name()
1382 && summary_or_opt_dependency_feature(dep_feature)
1383 {
1384 features.insert(FeatureValue::Feature(*dep_feature));
1389 found_features.insert(feature.clone());
1390 }
1391 }
1392 }
1393 }
1394 CliFeatures {
1395 features: Rc::new(features),
1396 all_features: cli_features.all_features,
1397 uses_default_features: cli_features.uses_default_features,
1398 }
1399 }
1400
1401 fn missing_feature_spelling_suggestions(
1402 &self,
1403 selected_members: &[&Package],
1404 cli_features: &CliFeatures,
1405 found_features: &BTreeSet<FeatureValue>,
1406 ) -> Vec<String> {
1407 let mut summary_features: Vec<InternedString> = Default::default();
1409
1410 let mut dependencies_features: BTreeMap<InternedString, &[InternedString]> =
1412 Default::default();
1413
1414 let mut optional_dependency_names: Vec<InternedString> = Default::default();
1416
1417 let mut summary_features_per_member: BTreeMap<&Package, BTreeSet<InternedString>> =
1419 Default::default();
1420
1421 let mut optional_dependency_names_per_member: BTreeMap<&Package, BTreeSet<InternedString>> =
1423 Default::default();
1424
1425 for &member in selected_members {
1426 let summary = member.summary();
1428
1429 summary_features.extend(summary.features().keys());
1431 summary_features_per_member
1432 .insert(member, summary.features().keys().copied().collect());
1433
1434 let dependencies: BTreeMap<InternedString, &Dependency> = summary
1436 .dependencies()
1437 .iter()
1438 .map(|dep| (dep.name_in_toml(), dep))
1439 .collect();
1440
1441 dependencies_features.extend(
1442 dependencies
1443 .iter()
1444 .map(|(name, dep)| (*name, dep.features())),
1445 );
1446
1447 let optional_dependency_names_raw: BTreeSet<_> = dependencies
1449 .iter()
1450 .filter(|(_, dep)| dep.is_optional())
1451 .map(|(name, _)| name)
1452 .copied()
1453 .collect();
1454
1455 optional_dependency_names.extend(optional_dependency_names_raw.iter());
1456 optional_dependency_names_per_member.insert(member, optional_dependency_names_raw);
1457 }
1458
1459 let edit_distance_test = |a: InternedString, b: InternedString| {
1460 edit_distance(a.as_str(), b.as_str(), 3).is_some()
1461 };
1462
1463 cli_features
1464 .features
1465 .difference(found_features)
1466 .map(|feature| match feature {
1467 FeatureValue::Feature(typo) => {
1469 let summary_features = summary_features
1471 .iter()
1472 .filter(move |feature| edit_distance_test(**feature, *typo));
1473
1474 let optional_dependency_features = optional_dependency_names
1476 .iter()
1477 .filter(move |feature| edit_distance_test(**feature, *typo));
1478
1479 summary_features
1480 .chain(optional_dependency_features)
1481 .map(|s| s.to_string())
1482 .collect::<Vec<_>>()
1483 }
1484 FeatureValue::Dep { .. } => panic!("unexpected dep: syntax {}", feature),
1485 FeatureValue::DepFeature {
1486 dep_name,
1487 dep_feature,
1488 weak: _,
1489 } => {
1490 let pkg_feat_similar = dependencies_features
1492 .iter()
1493 .filter(|(name, _)| edit_distance_test(**name, *dep_name))
1494 .map(|(name, features)| {
1495 (
1496 name,
1497 features
1498 .iter()
1499 .filter(|feature| edit_distance_test(**feature, *dep_feature))
1500 .collect::<Vec<_>>(),
1501 )
1502 })
1503 .map(|(name, features)| {
1504 features
1505 .into_iter()
1506 .map(move |feature| format!("{}/{}", name, feature))
1507 })
1508 .flatten();
1509
1510 let optional_dependency_features = optional_dependency_names_per_member
1512 .iter()
1513 .filter(|(package, _)| edit_distance_test(package.name(), *dep_name))
1514 .map(|(package, optional_dependencies)| {
1515 optional_dependencies
1516 .into_iter()
1517 .filter(|optional_dependency| {
1518 edit_distance_test(**optional_dependency, *dep_name)
1519 })
1520 .map(move |optional_dependency| {
1521 format!("{}/{}", package.name(), optional_dependency)
1522 })
1523 })
1524 .flatten();
1525
1526 let summary_features = summary_features_per_member
1528 .iter()
1529 .filter(|(package, _)| edit_distance_test(package.name(), *dep_name))
1530 .map(|(package, summary_features)| {
1531 summary_features
1532 .into_iter()
1533 .filter(|summary_feature| {
1534 edit_distance_test(**summary_feature, *dep_feature)
1535 })
1536 .map(move |summary_feature| {
1537 format!("{}/{}", package.name(), summary_feature)
1538 })
1539 })
1540 .flatten();
1541
1542 pkg_feat_similar
1543 .chain(optional_dependency_features)
1544 .chain(summary_features)
1545 .collect::<Vec<_>>()
1546 }
1547 })
1548 .map(|v| v.into_iter())
1549 .flatten()
1550 .unique()
1551 .filter(|element| {
1552 let feature = FeatureValue::new(InternedString::new(element));
1553 !cli_features.features.contains(&feature) && !found_features.contains(&feature)
1554 })
1555 .sorted()
1556 .take(5)
1557 .collect()
1558 }
1559
1560 fn report_unknown_features_error(
1561 &self,
1562 specs: &[PackageIdSpec],
1563 cli_features: &CliFeatures,
1564 found_features: &BTreeSet<FeatureValue>,
1565 ) -> CargoResult<()> {
1566 let unknown: Vec<_> = cli_features
1567 .features
1568 .difference(found_features)
1569 .map(|feature| feature.to_string())
1570 .sorted()
1571 .collect();
1572
1573 let (selected_members, unselected_members): (Vec<_>, Vec<_>) = self
1574 .members()
1575 .partition(|member| specs.iter().any(|spec| spec.matches(member.package_id())));
1576
1577 let missing_packages_with_the_features = unselected_members
1578 .into_iter()
1579 .filter(|member| {
1580 unknown
1581 .iter()
1582 .any(|feature| member.summary().features().contains_key(&**feature))
1583 })
1584 .map(|m| m.name())
1585 .collect_vec();
1586
1587 let these_features = if unknown.len() == 1 {
1588 "this feature"
1589 } else {
1590 "these features"
1591 };
1592 let mut msg = if let [singular] = &selected_members[..] {
1593 format!(
1594 "the package '{}' does not contain {these_features}: {}",
1595 singular.name(),
1596 unknown.join(", ")
1597 )
1598 } else {
1599 let names = selected_members.iter().map(|m| m.name()).join(", ");
1600 format!("none of the selected packages contains {these_features}: {}\nselected packages: {names}", unknown.join(", "))
1601 };
1602
1603 use std::fmt::Write;
1604 if !missing_packages_with_the_features.is_empty() {
1605 write!(
1606 &mut msg,
1607 "\nhelp: package{} with the missing feature{}: {}",
1608 if missing_packages_with_the_features.len() != 1 {
1609 "s"
1610 } else {
1611 ""
1612 },
1613 if unknown.len() != 1 { "s" } else { "" },
1614 missing_packages_with_the_features.join(", ")
1615 )?;
1616 } else {
1617 let suggestions = self.missing_feature_spelling_suggestions(
1618 &selected_members,
1619 cli_features,
1620 found_features,
1621 );
1622 if !suggestions.is_empty() {
1623 write!(
1624 &mut msg,
1625 "\nhelp: there {}: {}",
1626 if suggestions.len() == 1 {
1627 "is a similarly named feature"
1628 } else {
1629 "are similarly named features"
1630 },
1631 suggestions.join(", ")
1632 )?;
1633 }
1634 }
1635
1636 bail!("{msg}")
1637 }
1638
1639 fn members_with_features_new(
1642 &self,
1643 specs: &[PackageIdSpec],
1644 cli_features: &CliFeatures,
1645 ) -> CargoResult<Vec<(&Package, CliFeatures)>> {
1646 let mut found_features = Default::default();
1649
1650 let members: Vec<(&Package, CliFeatures)> = self
1651 .members()
1652 .filter(|m| specs.iter().any(|spec| spec.matches(m.package_id())))
1653 .map(|m| {
1654 (
1655 m,
1656 Workspace::collect_matching_features(m, cli_features, &mut found_features),
1657 )
1658 })
1659 .collect();
1660
1661 if members.is_empty() {
1662 if !(cli_features.features.is_empty()
1665 && !cli_features.all_features
1666 && cli_features.uses_default_features)
1667 {
1668 bail!("cannot specify features for packages outside of workspace");
1669 }
1670 return Ok(self
1673 .members()
1674 .map(|m| (m, CliFeatures::new_all(false)))
1675 .collect());
1676 }
1677 if *cli_features.features != found_features {
1678 self.report_unknown_features_error(specs, cli_features, &found_features)?;
1679 }
1680 Ok(members)
1681 }
1682
1683 fn members_with_features_old(
1686 &self,
1687 specs: &[PackageIdSpec],
1688 cli_features: &CliFeatures,
1689 ) -> Vec<(&Package, CliFeatures)> {
1690 let mut member_specific_features: HashMap<InternedString, BTreeSet<FeatureValue>> =
1693 HashMap::new();
1694 let mut cwd_features = BTreeSet::new();
1696 for feature in cli_features.features.iter() {
1697 match feature {
1698 FeatureValue::Feature(_) => {
1699 cwd_features.insert(feature.clone());
1700 }
1701 FeatureValue::Dep { .. } => panic!("unexpected dep: syntax {}", feature),
1703 FeatureValue::DepFeature {
1704 dep_name,
1705 dep_feature,
1706 weak: _,
1707 } => {
1708 let is_member = self.members().any(|member| {
1714 self.current_opt() != Some(member) && member.name() == *dep_name
1716 });
1717 if is_member && specs.iter().any(|spec| spec.name() == dep_name.as_str()) {
1718 member_specific_features
1719 .entry(*dep_name)
1720 .or_default()
1721 .insert(FeatureValue::Feature(*dep_feature));
1722 } else {
1723 cwd_features.insert(feature.clone());
1724 }
1725 }
1726 }
1727 }
1728
1729 let ms: Vec<_> = self
1730 .members()
1731 .filter_map(|member| {
1732 let member_id = member.package_id();
1733 match self.current_opt() {
1734 Some(current) if member_id == current.package_id() => {
1737 let feats = CliFeatures {
1738 features: Rc::new(cwd_features.clone()),
1739 all_features: cli_features.all_features,
1740 uses_default_features: cli_features.uses_default_features,
1741 };
1742 Some((member, feats))
1743 }
1744 _ => {
1745 if specs.iter().any(|spec| spec.matches(member_id)) {
1747 let feats = CliFeatures {
1757 features: Rc::new(
1758 member_specific_features
1759 .remove(member.name().as_str())
1760 .unwrap_or_default(),
1761 ),
1762 uses_default_features: true,
1763 all_features: cli_features.all_features,
1764 };
1765 Some((member, feats))
1766 } else {
1767 None
1769 }
1770 }
1771 }
1772 })
1773 .collect();
1774
1775 assert!(member_specific_features.is_empty());
1778
1779 ms
1780 }
1781
1782 pub fn unit_needs_doc_scrape(&self, unit: &Unit) -> bool {
1784 self.is_member(&unit.pkg) && !(unit.target.for_host() || unit.pkg.proc_macro())
1789 }
1790
1791 pub fn add_local_overlay(&mut self, id: SourceId, registry_path: PathBuf) {
1795 self.local_overlays.insert(id, registry_path);
1796 }
1797
1798 pub fn package_registry(&self) -> CargoResult<PackageRegistry<'gctx>> {
1800 let source_config =
1801 SourceConfigMap::new_with_overlays(self.gctx(), self.local_overlays()?)?;
1802 PackageRegistry::new_with_source_config(self.gctx(), source_config)
1803 }
1804
1805 fn local_overlays(&self) -> CargoResult<impl Iterator<Item = (SourceId, SourceId)>> {
1807 let mut ret = self
1808 .local_overlays
1809 .iter()
1810 .map(|(id, path)| Ok((*id, SourceId::for_local_registry(path)?)))
1811 .collect::<CargoResult<Vec<_>>>()?;
1812
1813 if let Ok(overlay) = self
1814 .gctx
1815 .get_env("__CARGO_TEST_DEPENDENCY_CONFUSION_VULNERABILITY_DO_NOT_USE_THIS")
1816 {
1817 let (url, path) = overlay.split_once('=').ok_or(anyhow::anyhow!(
1818 "invalid overlay format. I won't tell you why; you shouldn't be using it anyway"
1819 ))?;
1820 ret.push((
1821 SourceId::from_url(url)?,
1822 SourceId::for_local_registry(path.as_ref())?,
1823 ));
1824 }
1825
1826 Ok(ret.into_iter())
1827 }
1828}
1829
1830impl<'gctx> Packages<'gctx> {
1831 fn get(&self, manifest_path: &Path) -> &MaybePackage {
1832 self.maybe_get(manifest_path).unwrap()
1833 }
1834
1835 fn get_mut(&mut self, manifest_path: &Path) -> &mut MaybePackage {
1836 self.maybe_get_mut(manifest_path).unwrap()
1837 }
1838
1839 fn maybe_get(&self, manifest_path: &Path) -> Option<&MaybePackage> {
1840 self.packages.get(manifest_path)
1841 }
1842
1843 fn maybe_get_mut(&mut self, manifest_path: &Path) -> Option<&mut MaybePackage> {
1844 self.packages.get_mut(manifest_path)
1845 }
1846
1847 fn load(&mut self, manifest_path: &Path) -> CargoResult<&MaybePackage> {
1848 match self.packages.entry(manifest_path.to_path_buf()) {
1849 Entry::Occupied(e) => Ok(e.into_mut()),
1850 Entry::Vacant(v) => {
1851 let source_id = SourceId::for_manifest_path(manifest_path)?;
1852 let manifest = read_manifest(manifest_path, source_id, self.gctx)?;
1853 Ok(v.insert(match manifest {
1854 EitherManifest::Real(manifest) => {
1855 MaybePackage::Package(Package::new(manifest, manifest_path))
1856 }
1857 EitherManifest::Virtual(vm) => MaybePackage::Virtual(vm),
1858 }))
1859 }
1860 }
1861 }
1862}
1863
1864impl MaybePackage {
1865 fn workspace_config(&self) -> &WorkspaceConfig {
1866 match *self {
1867 MaybePackage::Package(ref p) => p.manifest().workspace_config(),
1868 MaybePackage::Virtual(ref vm) => vm.workspace_config(),
1869 }
1870 }
1871
1872 pub fn is_embedded(&self) -> bool {
1874 match self {
1875 MaybePackage::Package(p) => p.manifest().is_embedded(),
1876 MaybePackage::Virtual(_) => false,
1877 }
1878 }
1879}
1880
1881impl WorkspaceRootConfig {
1882 pub fn new(
1884 root_dir: &Path,
1885 members: &Option<Vec<String>>,
1886 default_members: &Option<Vec<String>>,
1887 exclude: &Option<Vec<String>>,
1888 inheritable: &Option<InheritableFields>,
1889 custom_metadata: &Option<toml::Value>,
1890 ) -> WorkspaceRootConfig {
1891 WorkspaceRootConfig {
1892 root_dir: root_dir.to_path_buf(),
1893 members: members.clone(),
1894 default_members: default_members.clone(),
1895 exclude: exclude.clone().unwrap_or_default(),
1896 inheritable_fields: inheritable.clone().unwrap_or_default(),
1897 custom_metadata: custom_metadata.clone(),
1898 }
1899 }
1900 fn is_excluded(&self, manifest_path: &Path) -> bool {
1904 let excluded = self
1905 .exclude
1906 .iter()
1907 .any(|ex| manifest_path.starts_with(self.root_dir.join(ex)));
1908
1909 let explicit_member = match self.members {
1910 Some(ref members) => members
1911 .iter()
1912 .any(|mem| manifest_path.starts_with(self.root_dir.join(mem))),
1913 None => false,
1914 };
1915
1916 !explicit_member && excluded
1917 }
1918
1919 fn has_members_list(&self) -> bool {
1920 self.members.is_some()
1921 }
1922
1923 #[tracing::instrument(skip_all)]
1926 fn members_paths<'g>(
1927 &self,
1928 globs: &'g [String],
1929 ) -> CargoResult<Vec<(PathBuf, Option<&'g str>)>> {
1930 let mut expanded_list = Vec::new();
1931
1932 for glob in globs {
1933 let pathbuf = self.root_dir.join(glob);
1934 let expanded_paths = Self::expand_member_path(&pathbuf)?;
1935
1936 if expanded_paths.is_empty() {
1939 expanded_list.push((pathbuf, None));
1940 } else {
1941 let used_glob_pattern = expanded_paths.len() > 1 || expanded_paths[0] != pathbuf;
1942 let glob = used_glob_pattern.then_some(glob.as_str());
1943
1944 for expanded_path in expanded_paths {
1950 if expanded_path.is_dir() {
1951 expanded_list.push((expanded_path, glob));
1952 }
1953 }
1954 }
1955 }
1956
1957 Ok(expanded_list)
1958 }
1959
1960 fn expand_member_path(path: &Path) -> CargoResult<Vec<PathBuf>> {
1961 let Some(path) = path.to_str() else {
1962 return Ok(Vec::new());
1963 };
1964 let res = glob(path).with_context(|| format!("could not parse pattern `{}`", &path))?;
1965 let res = res
1966 .map(|p| p.with_context(|| format!("unable to match path to pattern `{}`", &path)))
1967 .collect::<Result<Vec<_>, _>>()?;
1968 Ok(res)
1969 }
1970
1971 pub fn inheritable(&self) -> &InheritableFields {
1972 &self.inheritable_fields
1973 }
1974}
1975
1976pub fn resolve_relative_path(
1977 label: &str,
1978 old_root: &Path,
1979 new_root: &Path,
1980 rel_path: &str,
1981) -> CargoResult<String> {
1982 let joined_path = normalize_path(&old_root.join(rel_path));
1983 match diff_paths(joined_path, new_root) {
1984 None => Err(anyhow!(
1985 "`{}` was defined in {} but could not be resolved with {}",
1986 label,
1987 old_root.display(),
1988 new_root.display()
1989 )),
1990 Some(path) => Ok(path
1991 .to_str()
1992 .ok_or_else(|| {
1993 anyhow!(
1994 "`{}` resolved to non-UTF value (`{}`)",
1995 label,
1996 path.display()
1997 )
1998 })?
1999 .to_owned()),
2000 }
2001}
2002
2003pub fn find_workspace_root(
2005 manifest_path: &Path,
2006 gctx: &GlobalContext,
2007) -> CargoResult<Option<PathBuf>> {
2008 find_workspace_root_with_loader(manifest_path, gctx, |self_path| {
2009 let source_id = SourceId::for_manifest_path(self_path)?;
2010 let manifest = read_manifest(self_path, source_id, gctx)?;
2011 Ok(manifest
2012 .workspace_config()
2013 .get_ws_root(self_path, manifest_path))
2014 })
2015}
2016
2017fn find_workspace_root_with_loader(
2022 manifest_path: &Path,
2023 gctx: &GlobalContext,
2024 mut loader: impl FnMut(&Path) -> CargoResult<Option<PathBuf>>,
2025) -> CargoResult<Option<PathBuf>> {
2026 {
2028 let roots = gctx.ws_roots.borrow();
2029 for current in manifest_path.ancestors().skip(1) {
2032 if let Some(ws_config) = roots.get(current) {
2033 if !ws_config.is_excluded(manifest_path) {
2034 return Ok(Some(current.join("Cargo.toml")));
2036 }
2037 }
2038 }
2039 }
2040
2041 for ances_manifest_path in find_root_iter(manifest_path, gctx) {
2042 debug!("find_root - trying {}", ances_manifest_path.display());
2043 if let Some(ws_root_path) = loader(&ances_manifest_path)? {
2044 return Ok(Some(ws_root_path));
2045 }
2046 }
2047 Ok(None)
2048}
2049
2050fn read_root_pointer(member_manifest: &Path, root_link: &str) -> PathBuf {
2051 let path = member_manifest
2052 .parent()
2053 .unwrap()
2054 .join(root_link)
2055 .join("Cargo.toml");
2056 debug!("find_root - pointer {}", path.display());
2057 paths::normalize_path(&path)
2058}
2059
2060fn find_root_iter<'a>(
2061 manifest_path: &'a Path,
2062 gctx: &'a GlobalContext,
2063) -> impl Iterator<Item = PathBuf> + 'a {
2064 LookBehind::new(paths::ancestors(manifest_path, None).skip(2))
2065 .take_while(|path| !path.curr.ends_with("target/package"))
2066 .take_while(|path| {
2072 if let Some(last) = path.last {
2073 gctx.home() != last
2074 } else {
2075 true
2076 }
2077 })
2078 .map(|path| path.curr.join("Cargo.toml"))
2079 .filter(|ances_manifest_path| ances_manifest_path.exists())
2080}
2081
2082struct LookBehindWindow<'a, T: ?Sized> {
2083 curr: &'a T,
2084 last: Option<&'a T>,
2085}
2086
2087struct LookBehind<'a, T: ?Sized, K: Iterator<Item = &'a T>> {
2088 iter: K,
2089 last: Option<&'a T>,
2090}
2091
2092impl<'a, T: ?Sized, K: Iterator<Item = &'a T>> LookBehind<'a, T, K> {
2093 fn new(items: K) -> Self {
2094 Self {
2095 iter: items,
2096 last: None,
2097 }
2098 }
2099}
2100
2101impl<'a, T: ?Sized, K: Iterator<Item = &'a T>> Iterator for LookBehind<'a, T, K> {
2102 type Item = LookBehindWindow<'a, T>;
2103
2104 fn next(&mut self) -> Option<Self::Item> {
2105 match self.iter.next() {
2106 None => None,
2107 Some(next) => {
2108 let last = self.last;
2109 self.last = Some(next);
2110 Some(LookBehindWindow { curr: next, last })
2111 }
2112 }
2113 }
2114}