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