1use std::cell::RefCell;
2use std::collections::hash_map::{Entry, HashMap};
3use std::collections::{BTreeMap, BTreeSet, HashSet};
4use std::path::{Path, PathBuf};
5use std::rc::Rc;
6
7use anyhow::{Context as _, anyhow, bail};
8use cargo_util_terminal::report::Level;
9use glob::glob;
10use itertools::Itertools;
11use tracing::debug;
12use url::Url;
13
14use crate::core::compiler::Unit;
15use crate::core::features::Features;
16use crate::core::registry::PackageRegistry;
17use crate::core::resolver::ResolveBehavior;
18use crate::core::resolver::features::CliFeatures;
19use crate::core::{
20 Dependency, Edition, FeatureValue, PackageId, PackageIdSpec, PackageIdSpecQuery, Patch,
21 PatchLocation,
22};
23use crate::core::{EitherManifest, Package, SourceId, VirtualManifest};
24use crate::diagnostics::rules::blanket_hint_mostly_unused;
25use crate::diagnostics::rules::check_im_a_teapot;
26use crate::diagnostics::rules::implicit_minimum_version_req_pkg;
27use crate::diagnostics::rules::implicit_minimum_version_req_ws;
28use crate::diagnostics::rules::missing_lints_features;
29use crate::diagnostics::rules::missing_lints_inheritance;
30use crate::diagnostics::rules::non_kebab_case_bins;
31use crate::diagnostics::rules::non_kebab_case_features;
32use crate::diagnostics::rules::non_kebab_case_packages;
33use crate::diagnostics::rules::non_snake_case_features;
34use crate::diagnostics::rules::non_snake_case_packages;
35use crate::diagnostics::rules::redundant_homepage;
36use crate::diagnostics::rules::redundant_readme;
37use crate::diagnostics::rules::text_direction_codepoint_in_comment;
38use crate::diagnostics::rules::text_direction_codepoint_in_literal;
39use crate::diagnostics::rules::unknown_lints;
40use crate::diagnostics::rules::unused_build_dependencies_no_build_rs;
41use crate::diagnostics::rules::unused_workspace_dependencies;
42use crate::diagnostics::rules::unused_workspace_package_fields;
43use crate::ops;
44use crate::ops::lockfile::LOCKFILE_NAME;
45use crate::sources::{CRATES_IO_INDEX, CRATES_IO_REGISTRY, PathSource, SourceConfigMap};
46use crate::util::context;
47use crate::util::context::{FeatureUnification, Value};
48use crate::util::edit_distance;
49use crate::util::errors::{CargoResult, ManifestError};
50use crate::util::interning::InternedString;
51use crate::util::toml::{InheritableFields, read_manifest};
52use crate::util::{
53 Filesystem, GlobalContext, IntoUrl, closest_msg, context::CargoResolverConfig,
54 context::ConfigRelativePath, context::IncompatibleRustVersions,
55};
56
57use cargo_util::paths;
58use cargo_util::paths::normalize_path;
59use cargo_util_schemas::manifest;
60use cargo_util_schemas::manifest::RustVersion;
61use cargo_util_schemas::manifest::{TomlDependency, TomlManifest, TomlProfiles};
62use pathdiff::diff_paths;
63
64#[derive(Debug)]
70pub struct Workspace<'gctx> {
71 gctx: &'gctx GlobalContext,
73
74 current_manifest: PathBuf,
78
79 packages: Packages<'gctx>,
82
83 root_manifest: Option<PathBuf>,
88
89 target_dir: Option<Filesystem>,
92
93 build_dir: Option<Filesystem>,
96
97 members: Vec<PathBuf>,
101 member_ids: HashSet<PackageId>,
103
104 default_members: Vec<PathBuf>,
115
116 is_ephemeral: bool,
119
120 require_optional_deps: bool,
125
126 loaded_packages: RefCell<HashMap<PathBuf, Package>>,
129
130 ignore_lock: bool,
133
134 requested_lockfile_path: Option<PathBuf>,
136
137 resolve_behavior: ResolveBehavior,
139 resolve_honors_rust_version: bool,
143 resolve_feature_unification: FeatureUnification,
145 resolve_publish_time: Option<jiff::Timestamp>,
147 custom_metadata: Option<toml::Value>,
149
150 local_overlays: HashMap<SourceId, PathBuf>,
152}
153
154#[derive(Debug)]
157struct Packages<'gctx> {
158 gctx: &'gctx GlobalContext,
159 packages: HashMap<PathBuf, MaybePackage>,
160}
161
162#[derive(Debug)]
163pub enum MaybePackage {
164 Package(Package),
165 Virtual(VirtualManifest),
166}
167
168#[derive(Debug, Clone)]
170pub enum WorkspaceConfig {
171 Root(WorkspaceRootConfig),
174
175 Member { root: Option<String> },
178}
179
180impl WorkspaceConfig {
181 pub fn inheritable(&self) -> Option<&InheritableFields> {
182 match self {
183 WorkspaceConfig::Root(root) => Some(&root.inheritable_fields),
184 WorkspaceConfig::Member { .. } => None,
185 }
186 }
187
188 fn get_ws_root(&self, self_path: &Path, look_from: &Path) -> Option<PathBuf> {
196 match self {
197 WorkspaceConfig::Root(ances_root_config) => {
198 debug!("find_root - found a root checking exclusion");
199 if !ances_root_config.is_excluded(look_from) {
200 debug!("find_root - found!");
201 Some(self_path.to_owned())
202 } else {
203 None
204 }
205 }
206 WorkspaceConfig::Member {
207 root: Some(path_to_root),
208 } => {
209 debug!("find_root - found pointer");
210 Some(read_root_pointer(self_path, path_to_root))
211 }
212 WorkspaceConfig::Member { .. } => None,
213 }
214 }
215}
216
217#[derive(Debug, Clone)]
222pub struct WorkspaceRootConfig {
223 root_dir: PathBuf,
224 members: Option<Vec<String>>,
225 default_members: Option<Vec<String>>,
226 exclude: Vec<String>,
227 inheritable_fields: InheritableFields,
228 custom_metadata: Option<toml::Value>,
229}
230
231impl<'gctx> Workspace<'gctx> {
232 pub fn new(manifest_path: &Path, gctx: &'gctx GlobalContext) -> CargoResult<Workspace<'gctx>> {
239 let mut ws = Workspace::new_default(manifest_path.to_path_buf(), gctx);
240
241 if manifest_path.is_relative() {
242 bail!(
243 "manifest_path:{:?} is not an absolute path. Please provide an absolute path.",
244 manifest_path
245 )
246 } else {
247 ws.root_manifest = ws.find_root(manifest_path)?;
248 }
249
250 ws.target_dir = gctx.target_dir()?;
251 ws.build_dir = gctx.build_dir(ws.root_manifest())?;
252
253 ws.custom_metadata = ws
254 .load_workspace_config()?
255 .and_then(|cfg| cfg.custom_metadata);
256 ws.find_members()?;
257 ws.set_resolve_behavior()?;
258 ws.validate()?;
259 Ok(ws)
260 }
261
262 fn new_default(current_manifest: PathBuf, gctx: &'gctx GlobalContext) -> Workspace<'gctx> {
263 Workspace {
264 gctx,
265 current_manifest,
266 packages: Packages {
267 gctx,
268 packages: HashMap::new(),
269 },
270 root_manifest: None,
271 target_dir: None,
272 build_dir: None,
273 members: Vec::new(),
274 member_ids: HashSet::new(),
275 default_members: Vec::new(),
276 is_ephemeral: false,
277 require_optional_deps: true,
278 loaded_packages: RefCell::new(HashMap::new()),
279 ignore_lock: false,
280 requested_lockfile_path: None,
281 resolve_behavior: ResolveBehavior::V1,
282 resolve_honors_rust_version: false,
283 resolve_feature_unification: FeatureUnification::Selected,
284 resolve_publish_time: None,
285 custom_metadata: None,
286 local_overlays: HashMap::new(),
287 }
288 }
289
290 pub fn ephemeral(
300 package: Package,
301 gctx: &'gctx GlobalContext,
302 target_dir: Option<Filesystem>,
303 require_optional_deps: bool,
304 ) -> CargoResult<Workspace<'gctx>> {
305 let mut ws = Workspace::new_default(package.manifest_path().to_path_buf(), gctx);
306 ws.is_ephemeral = true;
307 ws.require_optional_deps = require_optional_deps;
308 let id = package.package_id();
309 let package = MaybePackage::Package(package);
310 ws.packages
311 .packages
312 .insert(ws.current_manifest.clone(), package);
313 ws.target_dir = if let Some(dir) = target_dir {
314 Some(dir)
315 } else {
316 ws.gctx.target_dir()?
317 };
318 ws.build_dir = ws.target_dir.clone();
319 ws.members.push(ws.current_manifest.clone());
320 ws.member_ids.insert(id);
321 ws.default_members.push(ws.current_manifest.clone());
322 ws.set_resolve_behavior()?;
323 Ok(ws)
324 }
325
326 pub fn reload(&self, gctx: &'gctx GlobalContext) -> CargoResult<Workspace<'gctx>> {
331 let mut ws = Workspace::new(&self.current_manifest, gctx)?;
332 ws.set_resolve_honors_rust_version(Some(self.resolve_honors_rust_version));
333 ws.set_resolve_feature_unification(self.resolve_feature_unification);
334 ws.set_requested_lockfile_path(self.requested_lockfile_path.clone());
335 Ok(ws)
336 }
337
338 fn set_resolve_behavior(&mut self) -> CargoResult<()> {
339 self.resolve_behavior = match self.root_maybe() {
344 MaybePackage::Package(p) => p
345 .manifest()
346 .resolve_behavior()
347 .unwrap_or_else(|| p.manifest().edition().default_resolve_behavior()),
348 MaybePackage::Virtual(vm) => vm.resolve_behavior().unwrap_or(ResolveBehavior::V1),
349 };
350
351 match self.resolve_behavior() {
352 ResolveBehavior::V1 | ResolveBehavior::V2 => {}
353 ResolveBehavior::V3 => {
354 if self.resolve_behavior == ResolveBehavior::V3 {
355 self.resolve_honors_rust_version = true;
356 }
357 }
358 }
359 let config = self.gctx().get::<CargoResolverConfig>("resolver")?;
360 if let Some(incompatible_rust_versions) = config.incompatible_rust_versions {
361 self.resolve_honors_rust_version =
362 incompatible_rust_versions == IncompatibleRustVersions::Fallback;
363 }
364 if self.gctx().cli_unstable().feature_unification {
365 self.resolve_feature_unification = config
366 .feature_unification
367 .unwrap_or(FeatureUnification::Selected);
368 } else if config.feature_unification.is_some() {
369 self.gctx()
370 .shell()
371 .warn("ignoring `resolver.feature-unification` without `-Zfeature-unification`")?;
372 };
373
374 if let Some(lockfile_path) = config.lockfile_path {
375 let replacements: [(&str, &str); 0] = [];
377 let path = lockfile_path
378 .resolve_templated_path(self.gctx(), replacements)
379 .map_err(|e| match e {
380 context::ResolveTemplateError::UnexpectedVariable {
381 variable,
382 raw_template,
383 } => {
384 anyhow!(
385 "unexpected variable `{variable}` in resolver.lockfile-path `{raw_template}`"
386 )
387 }
388 context::ResolveTemplateError::UnexpectedBracket { bracket_type, raw_template } => {
389 let (btype, literal) = match bracket_type {
390 context::BracketType::Opening => ("opening", "{"),
391 context::BracketType::Closing => ("closing", "}"),
392 };
393
394 anyhow!(
395 "unexpected {btype} bracket `{literal}` in build.build-dir path `{raw_template}`"
396 )
397 }
398 })?;
399 if !path.ends_with(LOCKFILE_NAME) {
400 bail!("the `resolver.lockfile-path` must be a path to a {LOCKFILE_NAME} file");
401 }
402 if path.is_dir() {
403 bail!(
404 "`resolver.lockfile-path` `{}` is a directory but expected a file",
405 path.display()
406 );
407 }
408 self.requested_lockfile_path = Some(path);
409 }
410
411 Ok(())
412 }
413
414 pub fn current(&self) -> CargoResult<&Package> {
420 let pkg = self.current_opt().ok_or_else(|| {
421 anyhow::format_err!(
422 "manifest path `{}` is a virtual manifest, but this \
423 command requires running against an actual package in \
424 this workspace",
425 self.current_manifest.display()
426 )
427 })?;
428 Ok(pkg)
429 }
430
431 pub fn current_mut(&mut self) -> CargoResult<&mut Package> {
432 let cm = self.current_manifest.clone();
433 let pkg = self.current_opt_mut().ok_or_else(|| {
434 anyhow::format_err!(
435 "manifest path `{}` is a virtual manifest, but this \
436 command requires running against an actual package in \
437 this workspace",
438 cm.display()
439 )
440 })?;
441 Ok(pkg)
442 }
443
444 pub fn current_opt(&self) -> Option<&Package> {
445 match *self.packages.get(&self.current_manifest) {
446 MaybePackage::Package(ref p) => Some(p),
447 MaybePackage::Virtual(..) => None,
448 }
449 }
450
451 pub fn current_opt_mut(&mut self) -> Option<&mut Package> {
452 match *self.packages.get_mut(&self.current_manifest) {
453 MaybePackage::Package(ref mut p) => Some(p),
454 MaybePackage::Virtual(..) => None,
455 }
456 }
457
458 pub fn is_virtual(&self) -> bool {
459 match *self.packages.get(&self.current_manifest) {
460 MaybePackage::Package(..) => false,
461 MaybePackage::Virtual(..) => true,
462 }
463 }
464
465 pub fn gctx(&self) -> &'gctx GlobalContext {
467 self.gctx
468 }
469
470 pub fn profiles(&self) -> Option<&TomlProfiles> {
471 self.root_maybe().profiles()
472 }
473
474 pub fn root(&self) -> &Path {
479 self.root_manifest().parent().unwrap()
480 }
481
482 pub fn root_manifest(&self) -> &Path {
485 self.root_manifest
486 .as_ref()
487 .unwrap_or(&self.current_manifest)
488 }
489
490 pub fn root_maybe(&self) -> &MaybePackage {
492 self.packages.get(self.root_manifest())
493 }
494
495 pub fn target_dir(&self) -> Filesystem {
496 self.target_dir
497 .clone()
498 .unwrap_or_else(|| self.default_target_dir())
499 }
500
501 pub fn build_dir(&self) -> Filesystem {
502 self.build_dir
503 .clone()
504 .or_else(|| self.target_dir.clone())
505 .unwrap_or_else(|| self.default_build_dir())
506 }
507
508 fn default_target_dir(&self) -> Filesystem {
509 if self.root_maybe().is_embedded() {
510 self.build_dir().join("target")
511 } else {
512 Filesystem::new(self.root().join("target"))
513 }
514 }
515
516 fn default_build_dir(&self) -> Filesystem {
517 if self.root_maybe().is_embedded() {
518 let default = ConfigRelativePath::new(
519 "{cargo-cache-home}/build/{workspace-path-hash}"
520 .to_owned()
521 .into(),
522 );
523 self.gctx()
524 .custom_build_dir(&default, self.root_manifest())
525 .expect("template is correct")
526 } else {
527 self.default_target_dir()
528 }
529 }
530
531 pub fn root_replace(&self) -> &[(PackageIdSpec, Dependency)] {
535 match self.root_maybe() {
536 MaybePackage::Package(p) => p.manifest().replace(),
537 MaybePackage::Virtual(vm) => vm.replace(),
538 }
539 }
540
541 fn config_patch(&self) -> CargoResult<HashMap<Url, Vec<Patch>>> {
542 let config_patch: Option<
543 BTreeMap<String, BTreeMap<String, Value<TomlDependency<ConfigRelativePath>>>>,
544 > = self.gctx.get("patch")?;
545
546 let source = SourceId::for_manifest_path(self.root_manifest())?;
547
548 let mut warnings = Vec::new();
549
550 let mut patch = HashMap::new();
551 for (url, deps) in config_patch.into_iter().flatten() {
552 let url = match &url[..] {
553 CRATES_IO_REGISTRY => CRATES_IO_INDEX.parse().unwrap(),
554 url => self
555 .gctx
556 .get_registry_index(url)
557 .or_else(|_| url.into_url())
558 .with_context(|| {
559 format!("[patch] entry `{}` should be a URL or registry name", url)
560 })?,
561 };
562 patch.insert(
563 url,
564 deps.iter()
565 .map(|(name, dependency_cv)| {
566 crate::util::toml::config_patch_to_dependency(
567 &dependency_cv.val,
568 name,
569 source,
570 self.gctx,
571 &mut warnings,
572 )
573 .map(|dep| Patch {
574 dep,
575 loc: PatchLocation::Config(dependency_cv.definition.clone()),
576 })
577 })
578 .collect::<CargoResult<Vec<_>>>()?,
579 );
580 }
581
582 for message in warnings {
583 self.gctx
584 .shell()
585 .warn(format!("[patch] in cargo config: {}", message))?
586 }
587
588 Ok(patch)
589 }
590
591 pub fn root_patch(&self) -> CargoResult<HashMap<Url, Vec<Patch>>> {
595 let from_manifest = match self.root_maybe() {
596 MaybePackage::Package(p) => p.manifest().patch(),
597 MaybePackage::Virtual(vm) => vm.patch(),
598 };
599
600 let from_config = self.config_patch()?;
601 if from_config.is_empty() {
602 return Ok(from_manifest.clone());
603 }
604 if from_manifest.is_empty() {
605 return Ok(from_config);
606 }
607
608 let mut combined = from_config;
611 for (url, deps_from_manifest) in from_manifest {
612 if let Some(deps_from_config) = combined.get_mut(url) {
613 let mut from_manifest_pruned = deps_from_manifest.clone();
616 for dep_from_config in &mut *deps_from_config {
617 if let Some(i) = from_manifest_pruned.iter().position(|dep_from_manifest| {
618 dep_from_config.dep.name_in_toml() == dep_from_manifest.dep.name_in_toml()
620 }) {
621 from_manifest_pruned.swap_remove(i);
622 }
623 }
624 deps_from_config.extend(from_manifest_pruned);
626 } else {
627 combined.insert(url.clone(), deps_from_manifest.clone());
628 }
629 }
630 Ok(combined)
631 }
632
633 pub fn members(&self) -> impl Iterator<Item = &Package> {
635 let packages = &self.packages;
636 self.members
637 .iter()
638 .filter_map(move |path| match packages.get(path) {
639 MaybePackage::Package(p) => Some(p),
640 _ => None,
641 })
642 }
643
644 pub fn members_mut(&mut self) -> impl Iterator<Item = &mut Package> {
646 let packages = &mut self.packages.packages;
647 let members: HashSet<_> = self.members.iter().map(|path| path).collect();
648
649 packages.iter_mut().filter_map(move |(path, package)| {
650 if members.contains(path) {
651 if let MaybePackage::Package(p) = package {
652 return Some(p);
653 }
654 }
655
656 None
657 })
658 }
659
660 pub fn default_members<'a>(&'a self) -> impl Iterator<Item = &'a Package> {
662 let packages = &self.packages;
663 self.default_members
664 .iter()
665 .filter_map(move |path| match packages.get(path) {
666 MaybePackage::Package(p) => Some(p),
667 _ => None,
668 })
669 }
670
671 pub fn default_members_mut(&mut self) -> impl Iterator<Item = &mut Package> {
673 let packages = &mut self.packages.packages;
674 let members: HashSet<_> = self
675 .default_members
676 .iter()
677 .map(|path| path.parent().unwrap().to_owned())
678 .collect();
679
680 packages.iter_mut().filter_map(move |(path, package)| {
681 if members.contains(path) {
682 if let MaybePackage::Package(p) = package {
683 return Some(p);
684 }
685 }
686
687 None
688 })
689 }
690
691 pub fn is_member(&self, pkg: &Package) -> bool {
693 self.member_ids.contains(&pkg.package_id())
694 }
695
696 pub fn is_member_id(&self, package_id: PackageId) -> bool {
698 self.member_ids.contains(&package_id)
699 }
700
701 pub fn is_ephemeral(&self) -> bool {
702 self.is_ephemeral
703 }
704
705 pub fn require_optional_deps(&self) -> bool {
706 self.require_optional_deps
707 }
708
709 pub fn set_require_optional_deps(
710 &mut self,
711 require_optional_deps: bool,
712 ) -> &mut Workspace<'gctx> {
713 self.require_optional_deps = require_optional_deps;
714 self
715 }
716
717 pub fn ignore_lock(&self) -> bool {
718 self.ignore_lock
719 }
720
721 pub fn set_ignore_lock(&mut self, ignore_lock: bool) -> &mut Workspace<'gctx> {
722 self.ignore_lock = ignore_lock;
723 self
724 }
725
726 pub fn lock_root(&self) -> Filesystem {
728 if let Some(requested) = self.requested_lockfile_path.as_ref() {
729 return Filesystem::new(
730 requested
731 .parent()
732 .expect("Lockfile path can't be root")
733 .to_owned(),
734 );
735 }
736 self.default_lock_root()
737 }
738
739 fn default_lock_root(&self) -> Filesystem {
740 if self.root_maybe().is_embedded() {
741 let workspace_manifest_path = self.root_manifest();
744 let real_path = std::fs::canonicalize(workspace_manifest_path)
745 .unwrap_or_else(|_err| workspace_manifest_path.to_owned());
746 let hash = crate::util::hex::short_hash(&real_path);
747 self.build_dir().join(hash)
748 } else {
749 Filesystem::new(self.root().to_owned())
750 }
751 }
752
753 pub fn set_requested_lockfile_path(&mut self, path: Option<PathBuf>) {
755 self.requested_lockfile_path = path;
756 }
757
758 pub fn requested_lockfile_path(&self) -> Option<&Path> {
759 self.requested_lockfile_path.as_deref()
760 }
761
762 pub fn lowest_rust_version(&self) -> Option<&RustVersion> {
765 self.members().filter_map(|pkg| pkg.rust_version()).min()
766 }
767
768 pub fn set_resolve_honors_rust_version(&mut self, honor_rust_version: Option<bool>) {
769 if let Some(honor_rust_version) = honor_rust_version {
770 self.resolve_honors_rust_version = honor_rust_version;
771 }
772 }
773
774 pub fn resolve_honors_rust_version(&self) -> bool {
775 self.resolve_honors_rust_version
776 }
777
778 pub fn set_resolve_feature_unification(&mut self, feature_unification: FeatureUnification) {
779 self.resolve_feature_unification = feature_unification;
780 }
781
782 pub fn resolve_feature_unification(&self) -> FeatureUnification {
783 self.resolve_feature_unification
784 }
785
786 pub fn set_resolve_publish_time(&mut self, publish_time: jiff::Timestamp) {
787 self.resolve_publish_time = Some(publish_time);
788 }
789
790 pub fn resolve_publish_time(&self) -> Option<jiff::Timestamp> {
791 self.resolve_publish_time
792 }
793
794 pub fn custom_metadata(&self) -> Option<&toml::Value> {
795 self.custom_metadata.as_ref()
796 }
797
798 pub fn load_workspace_config(&mut self) -> CargoResult<Option<WorkspaceRootConfig>> {
799 if let Some(root_path) = &self.root_manifest {
802 let root_package = self.packages.load(root_path)?;
803 match root_package.workspace_config() {
804 WorkspaceConfig::Root(root_config) => {
805 return Ok(Some(root_config.clone()));
806 }
807
808 _ => bail!(
809 "root of a workspace inferred but wasn't a root: {}",
810 root_path.display()
811 ),
812 }
813 }
814
815 Ok(None)
816 }
817
818 fn find_root(&mut self, manifest_path: &Path) -> CargoResult<Option<PathBuf>> {
828 let current = self.packages.load(manifest_path)?;
829 match current
830 .workspace_config()
831 .get_ws_root(manifest_path, manifest_path)
832 {
833 Some(root_path) => {
834 debug!("find_root - is root {}", manifest_path.display());
835 Ok(Some(root_path))
836 }
837 None => find_workspace_root_with_loader(manifest_path, self.gctx, |self_path| {
838 Ok(self
839 .packages
840 .load(self_path)?
841 .workspace_config()
842 .get_ws_root(self_path, manifest_path))
843 }),
844 }
845 }
846
847 #[tracing::instrument(skip_all)]
855 fn find_members(&mut self) -> CargoResult<()> {
856 let Some(workspace_config) = self.load_workspace_config()? else {
857 debug!("find_members - only me as a member");
858 self.members.push(self.current_manifest.clone());
859 self.default_members.push(self.current_manifest.clone());
860 if let Ok(pkg) = self.current() {
861 let id = pkg.package_id();
862 self.member_ids.insert(id);
863 }
864 return Ok(());
865 };
866
867 let root_manifest_path = self.root_manifest.clone().unwrap();
869
870 let members_paths = workspace_config
871 .members_paths(workspace_config.members.as_deref().unwrap_or_default())?;
872 let default_members_paths = if root_manifest_path == self.current_manifest {
873 if let Some(ref default) = workspace_config.default_members {
874 Some(workspace_config.members_paths(default)?)
875 } else {
876 None
877 }
878 } else {
879 None
880 };
881
882 for (path, glob) in &members_paths {
883 self.find_path_deps(&path.join("Cargo.toml"), &root_manifest_path, false)
884 .with_context(|| {
885 format!(
886 "failed to load manifest for workspace member `{}`\n\
887 referenced{} by workspace at `{}`",
888 path.display(),
889 glob.map(|g| format!(" via `{g}`")).unwrap_or_default(),
890 root_manifest_path.display(),
891 )
892 })?;
893 }
894
895 self.find_path_deps(&root_manifest_path, &root_manifest_path, false)?;
896
897 if let Some(default) = default_members_paths {
898 for (path, default_member_glob) in default {
899 let normalized_path = paths::normalize_path(&path);
900 let manifest_path = normalized_path.join("Cargo.toml");
901 if !self.members.contains(&manifest_path) {
902 let exclude = members_paths.iter().any(|(m, _)| *m == normalized_path)
909 && workspace_config.is_excluded(&normalized_path);
910 if exclude {
911 continue;
912 }
913 bail!(
914 "package `{}` is listed in default-members{} but is not a member\n\
915 for workspace at `{}`.",
916 path.display(),
917 default_member_glob
918 .map(|g| format!(" via `{g}`"))
919 .unwrap_or_default(),
920 root_manifest_path.display(),
921 )
922 }
923 self.default_members.push(manifest_path)
924 }
925 } else if self.is_virtual() {
926 self.default_members = self.members.clone()
927 } else {
928 self.default_members.push(self.current_manifest.clone())
929 }
930
931 Ok(())
932 }
933
934 fn find_path_deps(
935 &mut self,
936 manifest_path: &Path,
937 root_manifest: &Path,
938 is_path_dep: bool,
939 ) -> CargoResult<()> {
940 let manifest_path = paths::normalize_path(manifest_path);
941 if self.members.contains(&manifest_path) {
942 return Ok(());
943 }
944 if is_path_dep && self.root_maybe().is_embedded() {
945 return Ok(());
947 }
948 if is_path_dep
949 && !manifest_path.parent().unwrap().starts_with(self.root())
950 && self.find_root(&manifest_path)? != self.root_manifest
951 {
952 return Ok(());
955 }
956
957 if let WorkspaceConfig::Root(ref root_config) =
958 *self.packages.load(root_manifest)?.workspace_config()
959 {
960 if root_config.is_excluded(&manifest_path) {
961 return Ok(());
962 }
963 }
964
965 debug!("find_path_deps - {}", manifest_path.display());
966 self.members.push(manifest_path.clone());
967
968 let candidates = {
969 let pkg = match *self.packages.load(&manifest_path)? {
970 MaybePackage::Package(ref p) => p,
971 MaybePackage::Virtual(_) => return Ok(()),
972 };
973 self.member_ids.insert(pkg.package_id());
974 pkg.dependencies()
975 .iter()
976 .map(|d| (d.source_id(), d.package_name()))
977 .filter(|(s, _)| s.is_path())
978 .filter_map(|(s, n)| s.url().to_file_path().ok().map(|p| (p, n)))
979 .map(|(p, n)| (p.join("Cargo.toml"), n))
980 .collect::<Vec<_>>()
981 };
982 for (path, name) in candidates {
983 self.find_path_deps(&path, root_manifest, true)
984 .with_context(|| format!("failed to load manifest for dependency `{}`", name))
985 .map_err(|err| ManifestError::new(err, manifest_path.clone()))?;
986 }
987 Ok(())
988 }
989
990 pub fn unstable_features(&self) -> &Features {
992 self.root_maybe().unstable_features()
993 }
994
995 pub fn resolve_behavior(&self) -> ResolveBehavior {
996 self.resolve_behavior
997 }
998
999 pub fn allows_new_cli_feature_behavior(&self) -> bool {
1007 self.is_virtual()
1008 || match self.resolve_behavior() {
1009 ResolveBehavior::V1 => false,
1010 ResolveBehavior::V2 | ResolveBehavior::V3 => true,
1011 }
1012 }
1013
1014 #[tracing::instrument(skip_all)]
1020 fn validate(&mut self) -> CargoResult<()> {
1021 if self.root_manifest.is_none() {
1023 return Ok(());
1024 }
1025
1026 self.validate_unique_names()?;
1027 self.validate_workspace_roots()?;
1028 self.validate_members()?;
1029 self.error_if_manifest_not_in_members()?;
1030 self.validate_manifest()
1031 }
1032
1033 fn validate_unique_names(&self) -> CargoResult<()> {
1034 let mut names = BTreeMap::new();
1035 for member in self.members.iter() {
1036 let package = self.packages.get(member);
1037 let name = match *package {
1038 MaybePackage::Package(ref p) => p.name(),
1039 MaybePackage::Virtual(_) => continue,
1040 };
1041 if let Some(prev) = names.insert(name, member) {
1042 bail!(
1043 "two packages named `{}` in this workspace:\n\
1044 - {}\n\
1045 - {}",
1046 name,
1047 prev.display(),
1048 member.display()
1049 );
1050 }
1051 }
1052 Ok(())
1053 }
1054
1055 fn validate_workspace_roots(&self) -> CargoResult<()> {
1056 let roots: Vec<PathBuf> = self
1057 .members
1058 .iter()
1059 .filter(|&member| {
1060 let config = self.packages.get(member).workspace_config();
1061 matches!(config, WorkspaceConfig::Root(_))
1062 })
1063 .map(|member| member.parent().unwrap().to_path_buf())
1064 .collect();
1065 match roots.len() {
1066 1 => Ok(()),
1067 0 => bail!(
1068 "`package.workspace` configuration points to a crate \
1069 which is not configured with [workspace]: \n\
1070 configuration at: {}\n\
1071 points to: {}",
1072 self.current_manifest.display(),
1073 self.root_manifest.as_ref().unwrap().display()
1074 ),
1075 _ => {
1076 bail!(
1077 "multiple workspace roots found in the same workspace:\n{}",
1078 roots
1079 .iter()
1080 .map(|r| format!(" {}", r.display()))
1081 .collect::<Vec<_>>()
1082 .join("\n")
1083 );
1084 }
1085 }
1086 }
1087
1088 #[tracing::instrument(skip_all)]
1089 fn validate_members(&mut self) -> CargoResult<()> {
1090 for member in self.members.clone() {
1091 let root = self.find_root(&member)?;
1092 if root == self.root_manifest {
1093 continue;
1094 }
1095
1096 match root {
1097 Some(root) => {
1098 bail!(
1099 "package `{}` is a member of the wrong workspace\n\
1100 expected: {}\n\
1101 actual: {}",
1102 member.display(),
1103 self.root_manifest.as_ref().unwrap().display(),
1104 root.display()
1105 );
1106 }
1107 None => {
1108 bail!(
1109 "workspace member `{}` is not hierarchically below \
1110 the workspace root `{}`",
1111 member.display(),
1112 self.root_manifest.as_ref().unwrap().display()
1113 );
1114 }
1115 }
1116 }
1117 Ok(())
1118 }
1119
1120 fn error_if_manifest_not_in_members(&mut self) -> CargoResult<()> {
1121 if self.members.contains(&self.current_manifest) {
1122 return Ok(());
1123 }
1124
1125 let root = self.root_manifest.as_ref().unwrap();
1126 let root_dir = root.parent().unwrap();
1127 let current_dir = self.current_manifest.parent().unwrap();
1128 let root_pkg = self.packages.get(root);
1129
1130 let current_dir = paths::normalize_path(current_dir);
1136 let root_dir = paths::normalize_path(root_dir);
1137 let members_msg = match pathdiff::diff_paths(¤t_dir, &root_dir) {
1138 Some(rel) => format!(
1139 "this may be fixable by adding `{}` to the \
1140 `workspace.members` array of the manifest \
1141 located at: {}",
1142 rel.display(),
1143 root.display()
1144 ),
1145 None => format!(
1146 "this may be fixable by adding a member to \
1147 the `workspace.members` array of the \
1148 manifest located at: {}",
1149 root.display()
1150 ),
1151 };
1152 let extra = match *root_pkg {
1153 MaybePackage::Virtual(_) => members_msg,
1154 MaybePackage::Package(ref p) => {
1155 let has_members_list = match *p.manifest().workspace_config() {
1156 WorkspaceConfig::Root(ref root_config) => root_config.has_members_list(),
1157 WorkspaceConfig::Member { .. } => unreachable!(),
1158 };
1159 if !has_members_list {
1160 format!(
1161 "this may be fixable by ensuring that this \
1162 crate is depended on by the workspace \
1163 root: {}",
1164 root.display()
1165 )
1166 } else {
1167 members_msg
1168 }
1169 }
1170 };
1171 bail!(
1172 "current package believes it's in a workspace when it's not:\n\
1173 current: {}\n\
1174 workspace: {}\n\n{}\n\
1175 Alternatively, to keep it out of the workspace, add the package \
1176 to the `workspace.exclude` array, or add an empty `[workspace]` \
1177 table to the package's manifest.",
1178 self.current_manifest.display(),
1179 root.display(),
1180 extra
1181 );
1182 }
1183
1184 fn validate_manifest(&mut self) -> CargoResult<()> {
1185 if let Some(ref root_manifest) = self.root_manifest {
1186 for pkg in self
1187 .members()
1188 .filter(|p| p.manifest_path() != root_manifest)
1189 {
1190 let manifest = pkg.manifest();
1191 let emit_warning = |what| -> CargoResult<()> {
1192 let msg = format!(
1193 "{} for the non root package will be ignored, \
1194 specify {} at the workspace root:\n\
1195 package: {}\n\
1196 workspace: {}",
1197 what,
1198 what,
1199 pkg.manifest_path().display(),
1200 root_manifest.display(),
1201 );
1202 self.gctx.shell().warn(&msg)
1203 };
1204 if manifest.normalized_toml().has_profiles() {
1205 emit_warning("profiles")?;
1206 }
1207 if !manifest.replace().is_empty() {
1208 emit_warning("replace")?;
1209 }
1210 if !manifest.patch().is_empty() {
1211 emit_warning("patch")?;
1212 }
1213 if let Some(behavior) = manifest.resolve_behavior() {
1214 if behavior != self.resolve_behavior {
1215 emit_warning("resolver")?;
1217 }
1218 }
1219 }
1220 if let MaybePackage::Virtual(vm) = self.root_maybe() {
1221 if vm.resolve_behavior().is_none() {
1222 if let Some(edition) = self
1223 .members()
1224 .filter(|p| p.manifest_path() != root_manifest)
1225 .map(|p| p.manifest().edition())
1226 .filter(|&e| e >= Edition::Edition2021)
1227 .max()
1228 {
1229 let resolver = edition.default_resolve_behavior().to_manifest();
1230 let report = &[Level::WARNING
1231 .primary_title(format!(
1232 "virtual workspace defaulting to `resolver = \"1\"` despite one or more workspace members being on edition {edition} which implies `resolver = \"{resolver}\"`"
1233 ))
1234 .elements([
1235 Level::NOTE.message("to keep the current resolver, specify `workspace.resolver = \"1\"` in the workspace root's manifest"),
1236 Level::NOTE.message(
1237 format!("to use the edition {edition} resolver, specify `workspace.resolver = \"{resolver}\"` in the workspace root's manifest"),
1238 ),
1239 Level::NOTE.message("for more details see https://doc.rust-lang.org/cargo/reference/resolver.html#resolver-versions"),
1240 ])];
1241 self.gctx.shell().print_report(report, false)?;
1242 }
1243 }
1244 }
1245 }
1246 Ok(())
1247 }
1248
1249 pub fn load(&self, manifest_path: &Path) -> CargoResult<Package> {
1250 match self.packages.maybe_get(manifest_path) {
1251 Some(MaybePackage::Package(p)) => return Ok(p.clone()),
1252 Some(&MaybePackage::Virtual(_)) => bail!("cannot load workspace root"),
1253 None => {}
1254 }
1255
1256 let mut loaded = self.loaded_packages.borrow_mut();
1257 if let Some(p) = loaded.get(manifest_path).cloned() {
1258 return Ok(p);
1259 }
1260 let source_id = SourceId::for_manifest_path(manifest_path)?;
1261 let package = ops::read_package(manifest_path, source_id, self.gctx)?;
1262 loaded.insert(manifest_path.to_path_buf(), package.clone());
1263 Ok(package)
1264 }
1265
1266 pub fn preload(&self, registry: &mut PackageRegistry<'gctx>) {
1273 if self.is_ephemeral {
1279 return;
1280 }
1281
1282 for pkg in self.packages.packages.values() {
1283 let pkg = match *pkg {
1284 MaybePackage::Package(ref p) => p.clone(),
1285 MaybePackage::Virtual(_) => continue,
1286 };
1287 let src = PathSource::preload_with(pkg, self.gctx);
1288 registry.add_preloaded(Box::new(src));
1289 }
1290 }
1291
1292 pub fn emit_warnings(&self) -> CargoResult<()> {
1293 let mut first_emitted_error = None;
1294
1295 if let Err(e) = self.emit_ws_lints() {
1296 first_emitted_error = Some(e);
1297 }
1298
1299 for (path, maybe_pkg) in &self.packages.packages {
1300 if let MaybePackage::Package(pkg) = maybe_pkg {
1301 if let Err(e) = self.emit_pkg_lints(pkg, &path)
1302 && first_emitted_error.is_none()
1303 {
1304 first_emitted_error = Some(e);
1305 }
1306 }
1307 let warnings = match maybe_pkg {
1308 MaybePackage::Package(pkg) => pkg.manifest().warnings().warnings(),
1309 MaybePackage::Virtual(vm) => vm.warnings().warnings(),
1310 };
1311 for warning in warnings {
1312 if warning.is_critical {
1313 let err = anyhow::format_err!("{}", warning.message);
1314 let cx =
1315 anyhow::format_err!("failed to parse manifest at `{}`", path.display());
1316 if first_emitted_error.is_none() {
1317 first_emitted_error = Some(err.context(cx));
1318 }
1319 } else {
1320 let msg = if self.root_manifest.is_none() {
1321 warning.message.to_string()
1322 } else {
1323 format!("{}: {}", path.display(), warning.message)
1326 };
1327 self.gctx.shell().warn(msg)?
1328 }
1329 }
1330 }
1331
1332 if let Some(error) = first_emitted_error {
1333 Err(error)
1334 } else {
1335 Ok(())
1336 }
1337 }
1338
1339 pub fn emit_pkg_lints(&self, pkg: &Package, path: &Path) -> CargoResult<()> {
1340 let toml_lints = pkg
1341 .manifest()
1342 .normalized_toml()
1343 .lints
1344 .clone()
1345 .map(|lints| lints.lints)
1346 .unwrap_or(manifest::TomlLints::default());
1347 let cargo_lints = toml_lints
1348 .get("cargo")
1349 .cloned()
1350 .unwrap_or(manifest::TomlToolLints::default());
1351
1352 if self.gctx.cli_unstable().cargo_lints {
1353 let mut verify_error_count = 0;
1354
1355 missing_lints_features(
1356 pkg.into(),
1357 &path,
1358 &cargo_lints,
1359 &mut verify_error_count,
1360 self.gctx,
1361 )?;
1362 unknown_lints(
1363 pkg.into(),
1364 &path,
1365 &cargo_lints,
1366 &mut verify_error_count,
1367 self.gctx,
1368 )?;
1369
1370 if verify_error_count > 0 {
1371 let plural = if verify_error_count == 1 { "" } else { "s" };
1372 bail!("encountered {verify_error_count} error{plural} while verifying lints")
1373 }
1374
1375 let mut run_error_count = 0;
1376
1377 check_im_a_teapot(pkg, &path, &cargo_lints, &mut run_error_count, self.gctx)?;
1378 implicit_minimum_version_req_pkg(
1379 pkg,
1380 &path,
1381 &cargo_lints,
1382 &mut run_error_count,
1383 self.gctx,
1384 )?;
1385 non_kebab_case_packages(pkg, &path, &cargo_lints, &mut run_error_count, self.gctx)?;
1386 non_snake_case_packages(pkg, &path, &cargo_lints, &mut run_error_count, self.gctx)?;
1387 non_kebab_case_bins(
1388 self,
1389 pkg,
1390 &path,
1391 &cargo_lints,
1392 &mut run_error_count,
1393 self.gctx,
1394 )?;
1395 non_kebab_case_features(pkg, &path, &cargo_lints, &mut run_error_count, self.gctx)?;
1396 non_snake_case_features(pkg, &path, &cargo_lints, &mut run_error_count, self.gctx)?;
1397 unused_build_dependencies_no_build_rs(
1398 pkg,
1399 &path,
1400 &cargo_lints,
1401 &mut run_error_count,
1402 self.gctx,
1403 )?;
1404 redundant_readme(pkg, &path, &cargo_lints, &mut run_error_count, self.gctx)?;
1405 redundant_homepage(pkg, &path, &cargo_lints, &mut run_error_count, self.gctx)?;
1406 missing_lints_inheritance(
1407 self,
1408 pkg,
1409 &path,
1410 &cargo_lints,
1411 &mut run_error_count,
1412 self.gctx,
1413 )?;
1414 text_direction_codepoint_in_comment(
1415 pkg.into(),
1416 &path,
1417 &cargo_lints,
1418 &mut run_error_count,
1419 self.gctx,
1420 )?;
1421 text_direction_codepoint_in_literal(
1422 pkg.into(),
1423 &path,
1424 &cargo_lints,
1425 &mut run_error_count,
1426 self.gctx,
1427 )?;
1428
1429 if run_error_count > 0 {
1430 let plural = if run_error_count == 1 { "" } else { "s" };
1431 bail!("encountered {run_error_count} error{plural} while running lints")
1432 }
1433 }
1434
1435 Ok(())
1436 }
1437
1438 pub fn emit_ws_lints(&self) -> CargoResult<()> {
1439 let mut run_error_count = 0;
1440
1441 let cargo_lints = match self.root_maybe() {
1442 MaybePackage::Package(pkg) => {
1443 let toml = pkg.manifest().normalized_toml();
1444 if let Some(ws) = &toml.workspace {
1445 ws.lints.as_ref()
1446 } else {
1447 toml.lints.as_ref().map(|l| &l.lints)
1448 }
1449 }
1450 MaybePackage::Virtual(vm) => vm
1451 .normalized_toml()
1452 .workspace
1453 .as_ref()
1454 .unwrap()
1455 .lints
1456 .as_ref(),
1457 }
1458 .and_then(|t| t.get("cargo"))
1459 .cloned()
1460 .unwrap_or(manifest::TomlToolLints::default());
1461
1462 if self.gctx.cli_unstable().cargo_lints {
1463 let mut verify_error_count = 0;
1464
1465 missing_lints_features(
1466 (self, self.root_maybe()).into(),
1467 self.root_manifest(),
1468 &cargo_lints,
1469 &mut verify_error_count,
1470 self.gctx,
1471 )?;
1472 unknown_lints(
1473 (self, self.root_maybe()).into(),
1474 self.root_manifest(),
1475 &cargo_lints,
1476 &mut verify_error_count,
1477 self.gctx,
1478 )?;
1479
1480 if verify_error_count > 0 {
1481 let plural = if verify_error_count == 1 { "" } else { "s" };
1482 bail!("encountered {verify_error_count} error{plural} while verifying lints")
1483 }
1484
1485 unused_workspace_package_fields(
1486 self,
1487 self.root_maybe(),
1488 self.root_manifest(),
1489 &cargo_lints,
1490 &mut run_error_count,
1491 self.gctx,
1492 )?;
1493 unused_workspace_dependencies(
1494 self,
1495 self.root_maybe(),
1496 self.root_manifest(),
1497 &cargo_lints,
1498 &mut run_error_count,
1499 self.gctx,
1500 )?;
1501 implicit_minimum_version_req_ws(
1502 self,
1503 self.root_maybe(),
1504 self.root_manifest(),
1505 &cargo_lints,
1506 &mut run_error_count,
1507 self.gctx,
1508 )?;
1509 text_direction_codepoint_in_comment(
1510 (self, self.root_maybe()).into(),
1511 self.root_manifest(),
1512 &cargo_lints,
1513 &mut run_error_count,
1514 self.gctx,
1515 )?;
1516 text_direction_codepoint_in_literal(
1517 (self, self.root_maybe()).into(),
1518 self.root_manifest(),
1519 &cargo_lints,
1520 &mut run_error_count,
1521 self.gctx,
1522 )?;
1523 }
1524
1525 if self.gctx.cli_unstable().profile_hint_mostly_unused {
1529 blanket_hint_mostly_unused(
1530 self,
1531 self.root_maybe(),
1532 self.root_manifest(),
1533 &cargo_lints,
1534 &mut run_error_count,
1535 self.gctx,
1536 )?;
1537 }
1538
1539 if run_error_count > 0 {
1540 let plural = if run_error_count == 1 { "" } else { "s" };
1541 bail!("encountered {run_error_count} error{plural} while running lints")
1542 } else {
1543 Ok(())
1544 }
1545 }
1546
1547 pub fn set_target_dir(&mut self, target_dir: Filesystem) {
1548 self.target_dir = Some(target_dir);
1549 }
1550
1551 pub fn members_with_features(
1559 &self,
1560 specs: &[PackageIdSpec],
1561 cli_features: &CliFeatures,
1562 ) -> CargoResult<Vec<(&Package, CliFeatures)>> {
1563 assert!(
1564 !specs.is_empty() || cli_features.all_features,
1565 "no specs requires all_features"
1566 );
1567 if specs.is_empty() {
1568 return Ok(self
1571 .members()
1572 .map(|m| (m, CliFeatures::new_all(true)))
1573 .collect());
1574 }
1575 if self.allows_new_cli_feature_behavior() {
1576 self.members_with_features_new(specs, cli_features)
1577 } else {
1578 Ok(self.members_with_features_old(specs, cli_features))
1579 }
1580 }
1581
1582 fn collect_matching_features(
1585 member: &Package,
1586 cli_features: &CliFeatures,
1587 found_features: &mut BTreeSet<FeatureValue>,
1588 ) -> CliFeatures {
1589 if cli_features.features.is_empty() {
1590 return cli_features.clone();
1591 }
1592
1593 let summary = member.summary();
1595
1596 let summary_features = summary.features();
1598
1599 let dependencies: BTreeMap<InternedString, &Dependency> = summary
1601 .dependencies()
1602 .iter()
1603 .map(|dep| (dep.name_in_toml(), dep))
1604 .collect();
1605
1606 let optional_dependency_names: BTreeSet<_> = dependencies
1608 .iter()
1609 .filter(|(_, dep)| dep.is_optional())
1610 .map(|(name, _)| name)
1611 .copied()
1612 .collect();
1613
1614 let mut features = BTreeSet::new();
1615
1616 let summary_or_opt_dependency_feature = |feature: &InternedString| -> bool {
1618 summary_features.contains_key(feature) || optional_dependency_names.contains(feature)
1619 };
1620
1621 for feature in cli_features.features.iter() {
1622 match feature {
1623 FeatureValue::Feature(f) => {
1624 if summary_or_opt_dependency_feature(f) {
1625 features.insert(feature.clone());
1627 found_features.insert(feature.clone());
1628 }
1629 }
1630 FeatureValue::Dep { .. } => panic!("unexpected dep: syntax {}", feature),
1632 FeatureValue::DepFeature {
1633 dep_name,
1634 dep_feature,
1635 weak: _,
1636 } => {
1637 if dependencies.contains_key(dep_name) {
1638 features.insert(feature.clone());
1641 found_features.insert(feature.clone());
1642 } else if *dep_name == member.name()
1643 && summary_or_opt_dependency_feature(dep_feature)
1644 {
1645 features.insert(FeatureValue::Feature(*dep_feature));
1650 found_features.insert(feature.clone());
1651 }
1652 }
1653 }
1654 }
1655 CliFeatures {
1656 features: Rc::new(features),
1657 all_features: cli_features.all_features,
1658 uses_default_features: cli_features.uses_default_features,
1659 }
1660 }
1661
1662 fn missing_feature_spelling_suggestions(
1663 &self,
1664 selected_members: &[&Package],
1665 cli_features: &CliFeatures,
1666 found_features: &BTreeSet<FeatureValue>,
1667 ) -> Vec<String> {
1668 let mut summary_features: Vec<InternedString> = Default::default();
1670
1671 let mut dependencies_features: BTreeMap<InternedString, &[InternedString]> =
1673 Default::default();
1674
1675 let mut optional_dependency_names: Vec<InternedString> = Default::default();
1677
1678 let mut summary_features_per_member: BTreeMap<&Package, BTreeSet<InternedString>> =
1680 Default::default();
1681
1682 let mut optional_dependency_names_per_member: BTreeMap<&Package, BTreeSet<InternedString>> =
1684 Default::default();
1685
1686 for &member in selected_members {
1687 let summary = member.summary();
1689
1690 summary_features.extend(summary.features().keys());
1692 summary_features_per_member
1693 .insert(member, summary.features().keys().copied().collect());
1694
1695 let dependencies: BTreeMap<InternedString, &Dependency> = summary
1697 .dependencies()
1698 .iter()
1699 .map(|dep| (dep.name_in_toml(), dep))
1700 .collect();
1701
1702 dependencies_features.extend(
1703 dependencies
1704 .iter()
1705 .map(|(name, dep)| (*name, dep.features())),
1706 );
1707
1708 let optional_dependency_names_raw: BTreeSet<_> = dependencies
1710 .iter()
1711 .filter(|(_, dep)| dep.is_optional())
1712 .map(|(name, _)| name)
1713 .copied()
1714 .collect();
1715
1716 optional_dependency_names.extend(optional_dependency_names_raw.iter());
1717 optional_dependency_names_per_member.insert(member, optional_dependency_names_raw);
1718 }
1719
1720 let edit_distance_test = |a: InternedString, b: InternedString| {
1721 edit_distance(a.as_str(), b.as_str(), 3).is_some()
1722 };
1723
1724 cli_features
1725 .features
1726 .difference(found_features)
1727 .map(|feature| match feature {
1728 FeatureValue::Feature(typo) => {
1730 let summary_features = summary_features
1732 .iter()
1733 .filter(move |feature| edit_distance_test(**feature, *typo));
1734
1735 let optional_dependency_features = optional_dependency_names
1737 .iter()
1738 .filter(move |feature| edit_distance_test(**feature, *typo));
1739
1740 summary_features
1741 .chain(optional_dependency_features)
1742 .map(|s| s.to_string())
1743 .collect::<Vec<_>>()
1744 }
1745 FeatureValue::Dep { .. } => panic!("unexpected dep: syntax {}", feature),
1746 FeatureValue::DepFeature {
1747 dep_name,
1748 dep_feature,
1749 weak: _,
1750 } => {
1751 let pkg_feat_similar = dependencies_features
1753 .iter()
1754 .filter(|(name, _)| edit_distance_test(**name, *dep_name))
1755 .map(|(name, features)| {
1756 (
1757 name,
1758 features
1759 .iter()
1760 .filter(|feature| edit_distance_test(**feature, *dep_feature))
1761 .collect::<Vec<_>>(),
1762 )
1763 })
1764 .map(|(name, features)| {
1765 features
1766 .into_iter()
1767 .map(move |feature| format!("{}/{}", name, feature))
1768 })
1769 .flatten();
1770
1771 let optional_dependency_features = optional_dependency_names_per_member
1773 .iter()
1774 .filter(|(package, _)| edit_distance_test(package.name(), *dep_name))
1775 .map(|(package, optional_dependencies)| {
1776 optional_dependencies
1777 .into_iter()
1778 .filter(|optional_dependency| {
1779 edit_distance_test(**optional_dependency, *dep_name)
1780 })
1781 .map(move |optional_dependency| {
1782 format!("{}/{}", package.name(), optional_dependency)
1783 })
1784 })
1785 .flatten();
1786
1787 let summary_features = summary_features_per_member
1789 .iter()
1790 .filter(|(package, _)| edit_distance_test(package.name(), *dep_name))
1791 .map(|(package, summary_features)| {
1792 summary_features
1793 .into_iter()
1794 .filter(|summary_feature| {
1795 edit_distance_test(**summary_feature, *dep_feature)
1796 })
1797 .map(move |summary_feature| {
1798 format!("{}/{}", package.name(), summary_feature)
1799 })
1800 })
1801 .flatten();
1802
1803 pkg_feat_similar
1804 .chain(optional_dependency_features)
1805 .chain(summary_features)
1806 .collect::<Vec<_>>()
1807 }
1808 })
1809 .map(|v| v.into_iter())
1810 .flatten()
1811 .unique()
1812 .filter(|element| {
1813 let feature = FeatureValue::new(element.into());
1814 !cli_features.features.contains(&feature) && !found_features.contains(&feature)
1815 })
1816 .sorted()
1817 .take(5)
1818 .collect()
1819 }
1820
1821 fn report_unknown_features_error(
1822 &self,
1823 specs: &[PackageIdSpec],
1824 cli_features: &CliFeatures,
1825 found_features: &BTreeSet<FeatureValue>,
1826 ) -> CargoResult<()> {
1827 let unknown: Vec<_> = cli_features
1828 .features
1829 .difference(found_features)
1830 .map(|feature| feature.to_string())
1831 .sorted()
1832 .collect();
1833
1834 let (selected_members, unselected_members): (Vec<_>, Vec<_>) = self
1835 .members()
1836 .partition(|member| specs.iter().any(|spec| spec.matches(member.package_id())));
1837
1838 let missing_packages_with_the_features = unselected_members
1839 .into_iter()
1840 .filter(|member| {
1841 unknown
1842 .iter()
1843 .any(|feature| member.summary().features().contains_key(&**feature))
1844 })
1845 .map(|m| m.name())
1846 .collect_vec();
1847
1848 let these_features = if unknown.len() == 1 {
1849 "this feature"
1850 } else {
1851 "these features"
1852 };
1853 let mut msg = if let [singular] = &selected_members[..] {
1854 format!(
1855 "the package '{}' does not contain {these_features}: {}",
1856 singular.name(),
1857 unknown.join(", ")
1858 )
1859 } else {
1860 let names = selected_members.iter().map(|m| m.name()).join(", ");
1861 format!(
1862 "none of the selected packages contains {these_features}: {}\nselected packages: {names}",
1863 unknown.join(", ")
1864 )
1865 };
1866
1867 use std::fmt::Write;
1868 if !missing_packages_with_the_features.is_empty() {
1869 write!(
1870 &mut msg,
1871 "\nhelp: package{} with the missing feature{}: {}",
1872 if missing_packages_with_the_features.len() != 1 {
1873 "s"
1874 } else {
1875 ""
1876 },
1877 if unknown.len() != 1 { "s" } else { "" },
1878 missing_packages_with_the_features.join(", ")
1879 )?;
1880 } else {
1881 let suggestions = self.missing_feature_spelling_suggestions(
1882 &selected_members,
1883 cli_features,
1884 found_features,
1885 );
1886 if !suggestions.is_empty() {
1887 write!(
1888 &mut msg,
1889 "\nhelp: there {}: {}",
1890 if suggestions.len() == 1 {
1891 "is a similarly named feature"
1892 } else {
1893 "are similarly named features"
1894 },
1895 suggestions.join(", ")
1896 )?;
1897 }
1898 }
1899
1900 bail!("{msg}")
1901 }
1902
1903 fn members_with_features_new(
1906 &self,
1907 specs: &[PackageIdSpec],
1908 cli_features: &CliFeatures,
1909 ) -> CargoResult<Vec<(&Package, CliFeatures)>> {
1910 let mut found_features = Default::default();
1913
1914 let members: Vec<(&Package, CliFeatures)> = self
1915 .members()
1916 .filter(|m| specs.iter().any(|spec| spec.matches(m.package_id())))
1917 .map(|m| {
1918 (
1919 m,
1920 Workspace::collect_matching_features(m, cli_features, &mut found_features),
1921 )
1922 })
1923 .collect();
1924
1925 if members.is_empty() {
1926 if !(cli_features.features.is_empty()
1929 && !cli_features.all_features
1930 && cli_features.uses_default_features)
1931 {
1932 let hint = specs
1933 .iter()
1934 .map(|spec| {
1935 closest_msg(
1936 spec.name(),
1937 self.members(),
1938 |m| m.name().as_str(),
1939 "workspace member",
1940 )
1941 })
1942 .find(|msg| !msg.is_empty())
1943 .unwrap_or_default();
1944 bail!("cannot specify features for packages outside of workspace{hint}");
1945 }
1946 return Ok(self
1949 .members()
1950 .map(|m| (m, CliFeatures::new_all(false)))
1951 .collect());
1952 }
1953 if *cli_features.features != found_features {
1954 self.report_unknown_features_error(specs, cli_features, &found_features)?;
1955 }
1956 Ok(members)
1957 }
1958
1959 fn members_with_features_old(
1962 &self,
1963 specs: &[PackageIdSpec],
1964 cli_features: &CliFeatures,
1965 ) -> Vec<(&Package, CliFeatures)> {
1966 let mut member_specific_features: HashMap<InternedString, BTreeSet<FeatureValue>> =
1969 HashMap::new();
1970 let mut cwd_features = BTreeSet::new();
1972 for feature in cli_features.features.iter() {
1973 match feature {
1974 FeatureValue::Feature(_) => {
1975 cwd_features.insert(feature.clone());
1976 }
1977 FeatureValue::Dep { .. } => panic!("unexpected dep: syntax {}", feature),
1979 FeatureValue::DepFeature {
1980 dep_name,
1981 dep_feature,
1982 weak: _,
1983 } => {
1984 let is_member = self.members().any(|member| {
1990 self.current_opt() != Some(member) && member.name() == *dep_name
1992 });
1993 if is_member && specs.iter().any(|spec| spec.name() == dep_name.as_str()) {
1994 member_specific_features
1995 .entry(*dep_name)
1996 .or_default()
1997 .insert(FeatureValue::Feature(*dep_feature));
1998 } else {
1999 cwd_features.insert(feature.clone());
2000 }
2001 }
2002 }
2003 }
2004
2005 let ms: Vec<_> = self
2006 .members()
2007 .filter_map(|member| {
2008 let member_id = member.package_id();
2009 match self.current_opt() {
2010 Some(current) if member_id == current.package_id() => {
2013 let feats = CliFeatures {
2014 features: Rc::new(cwd_features.clone()),
2015 all_features: cli_features.all_features,
2016 uses_default_features: cli_features.uses_default_features,
2017 };
2018 Some((member, feats))
2019 }
2020 _ => {
2021 if specs.iter().any(|spec| spec.matches(member_id)) {
2023 let feats = CliFeatures {
2033 features: Rc::new(
2034 member_specific_features
2035 .remove(member.name().as_str())
2036 .unwrap_or_default(),
2037 ),
2038 uses_default_features: true,
2039 all_features: cli_features.all_features,
2040 };
2041 Some((member, feats))
2042 } else {
2043 None
2045 }
2046 }
2047 }
2048 })
2049 .collect();
2050
2051 assert!(member_specific_features.is_empty());
2054
2055 ms
2056 }
2057
2058 pub fn unit_needs_doc_scrape(&self, unit: &Unit) -> bool {
2060 self.is_member(&unit.pkg) && !(unit.target.for_host() || unit.pkg.proc_macro())
2065 }
2066
2067 pub fn add_local_overlay(&mut self, id: SourceId, registry_path: PathBuf) {
2071 self.local_overlays.insert(id, registry_path);
2072 }
2073
2074 pub fn package_registry(&self) -> CargoResult<PackageRegistry<'gctx>> {
2076 let source_config =
2077 SourceConfigMap::new_with_overlays(self.gctx(), self.local_overlays()?)?;
2078 PackageRegistry::new_with_source_config(self.gctx(), source_config)
2079 }
2080
2081 fn local_overlays(&self) -> CargoResult<impl Iterator<Item = (SourceId, SourceId)>> {
2083 let mut ret = self
2084 .local_overlays
2085 .iter()
2086 .map(|(id, path)| Ok((*id, SourceId::for_local_registry(path)?)))
2087 .collect::<CargoResult<Vec<_>>>()?;
2088
2089 if let Ok(overlay) = self
2090 .gctx
2091 .get_env("__CARGO_TEST_DEPENDENCY_CONFUSION_VULNERABILITY_DO_NOT_USE_THIS")
2092 {
2093 let (url, path) = overlay.split_once('=').ok_or(anyhow::anyhow!(
2094 "invalid overlay format. I won't tell you why; you shouldn't be using it anyway"
2095 ))?;
2096 ret.push((
2097 SourceId::from_url(url)?,
2098 SourceId::for_local_registry(path.as_ref())?,
2099 ));
2100 }
2101
2102 Ok(ret.into_iter())
2103 }
2104}
2105
2106impl<'gctx> Packages<'gctx> {
2107 fn get(&self, manifest_path: &Path) -> &MaybePackage {
2108 self.maybe_get(manifest_path).unwrap()
2109 }
2110
2111 fn get_mut(&mut self, manifest_path: &Path) -> &mut MaybePackage {
2112 self.maybe_get_mut(manifest_path).unwrap()
2113 }
2114
2115 fn maybe_get(&self, manifest_path: &Path) -> Option<&MaybePackage> {
2116 self.packages.get(manifest_path)
2117 }
2118
2119 fn maybe_get_mut(&mut self, manifest_path: &Path) -> Option<&mut MaybePackage> {
2120 self.packages.get_mut(manifest_path)
2121 }
2122
2123 fn load(&mut self, manifest_path: &Path) -> CargoResult<&MaybePackage> {
2124 match self.packages.entry(manifest_path.to_path_buf()) {
2125 Entry::Occupied(e) => Ok(e.into_mut()),
2126 Entry::Vacant(v) => {
2127 let source_id = SourceId::for_manifest_path(manifest_path)?;
2128 let manifest = read_manifest(manifest_path, source_id, self.gctx)?;
2129 Ok(v.insert(match manifest {
2130 EitherManifest::Real(manifest) => {
2131 MaybePackage::Package(Package::new(manifest, manifest_path))
2132 }
2133 EitherManifest::Virtual(vm) => MaybePackage::Virtual(vm),
2134 }))
2135 }
2136 }
2137 }
2138}
2139
2140impl MaybePackage {
2141 fn workspace_config(&self) -> &WorkspaceConfig {
2142 match *self {
2143 MaybePackage::Package(ref p) => p.manifest().workspace_config(),
2144 MaybePackage::Virtual(ref vm) => vm.workspace_config(),
2145 }
2146 }
2147
2148 pub fn is_embedded(&self) -> bool {
2150 match self {
2151 MaybePackage::Package(p) => p.manifest().is_embedded(),
2152 MaybePackage::Virtual(_) => false,
2153 }
2154 }
2155
2156 pub fn contents(&self) -> Option<&str> {
2157 match self {
2158 MaybePackage::Package(p) => p.manifest().contents(),
2159 MaybePackage::Virtual(v) => v.contents(),
2160 }
2161 }
2162
2163 pub fn document(&self) -> Option<&toml::Spanned<toml::de::DeTable<'static>>> {
2164 match self {
2165 MaybePackage::Package(p) => p.manifest().document(),
2166 MaybePackage::Virtual(v) => v.document(),
2167 }
2168 }
2169
2170 pub fn original_toml(&self) -> Option<&TomlManifest> {
2171 match self {
2172 MaybePackage::Package(p) => p.manifest().original_toml(),
2173 MaybePackage::Virtual(v) => v.original_toml(),
2174 }
2175 }
2176
2177 pub fn normalized_toml(&self) -> &TomlManifest {
2178 match self {
2179 MaybePackage::Package(p) => p.manifest().normalized_toml(),
2180 MaybePackage::Virtual(v) => v.normalized_toml(),
2181 }
2182 }
2183
2184 pub fn edition(&self) -> Edition {
2185 match self {
2186 MaybePackage::Package(p) => p.manifest().edition(),
2187 MaybePackage::Virtual(_) => Edition::default(),
2188 }
2189 }
2190
2191 pub fn profiles(&self) -> Option<&TomlProfiles> {
2192 match self {
2193 MaybePackage::Package(p) => p.manifest().profiles(),
2194 MaybePackage::Virtual(v) => v.profiles(),
2195 }
2196 }
2197
2198 pub fn unstable_features(&self) -> &Features {
2199 match self {
2200 MaybePackage::Package(p) => p.manifest().unstable_features(),
2201 MaybePackage::Virtual(vm) => vm.unstable_features(),
2202 }
2203 }
2204}
2205
2206impl WorkspaceRootConfig {
2207 pub fn new(
2209 root_dir: &Path,
2210 members: &Option<Vec<String>>,
2211 default_members: &Option<Vec<String>>,
2212 exclude: &Option<Vec<String>>,
2213 inheritable: &Option<InheritableFields>,
2214 custom_metadata: &Option<toml::Value>,
2215 ) -> WorkspaceRootConfig {
2216 WorkspaceRootConfig {
2217 root_dir: root_dir.to_path_buf(),
2218 members: members.clone(),
2219 default_members: default_members.clone(),
2220 exclude: exclude.clone().unwrap_or_default(),
2221 inheritable_fields: inheritable.clone().unwrap_or_default(),
2222 custom_metadata: custom_metadata.clone(),
2223 }
2224 }
2225 fn is_excluded(&self, manifest_path: &Path) -> bool {
2229 let excluded = self
2230 .exclude
2231 .iter()
2232 .any(|ex| manifest_path.starts_with(self.root_dir.join(ex)));
2233
2234 let explicit_member = match self.members {
2235 Some(ref members) => members
2236 .iter()
2237 .any(|mem| manifest_path.starts_with(self.root_dir.join(mem))),
2238 None => false,
2239 };
2240
2241 !explicit_member && excluded
2242 }
2243
2244 fn is_explicitly_listed_member(&self, manifest_path: &Path) -> bool {
2255 let root_manifest = self.root_dir.join("Cargo.toml");
2256 if manifest_path == root_manifest {
2257 return true;
2258 }
2259 match self.members {
2260 Some(ref members) => {
2261 let Ok(expanded_members) = self.members_paths(members) else {
2263 return false;
2264 };
2265 let normalized_manifest = paths::normalize_path(manifest_path);
2267 expanded_members.iter().any(|(member_path, _)| {
2268 let normalized_member = paths::normalize_path(member_path);
2270 normalized_manifest.parent() == Some(normalized_member.as_path())
2273 })
2274 }
2275 None => false,
2276 }
2277 }
2278
2279 fn has_members_list(&self) -> bool {
2280 self.members.is_some()
2281 }
2282
2283 fn has_default_members(&self) -> bool {
2285 self.default_members.is_some()
2286 }
2287
2288 #[tracing::instrument(skip_all)]
2291 fn members_paths<'g>(
2292 &self,
2293 globs: &'g [String],
2294 ) -> CargoResult<Vec<(PathBuf, Option<&'g str>)>> {
2295 let mut expanded_list = Vec::new();
2296
2297 for glob in globs {
2298 let pathbuf = self.root_dir.join(glob);
2299 let expanded_paths = Self::expand_member_path(&pathbuf)?;
2300
2301 if expanded_paths.is_empty() {
2304 expanded_list.push((pathbuf, None));
2305 } else {
2306 let used_glob_pattern = expanded_paths.len() > 1 || expanded_paths[0] != pathbuf;
2307 let glob = used_glob_pattern.then_some(glob.as_str());
2308
2309 for expanded_path in expanded_paths {
2315 if expanded_path.is_dir() {
2316 expanded_list.push((expanded_path, glob));
2317 }
2318 }
2319 }
2320 }
2321
2322 Ok(expanded_list)
2323 }
2324
2325 fn expand_member_path(path: &Path) -> CargoResult<Vec<PathBuf>> {
2326 let Some(path) = path.to_str() else {
2327 return Ok(Vec::new());
2328 };
2329 let res = glob(path).with_context(|| format!("could not parse pattern `{}`", &path))?;
2330 let res = res
2331 .map(|p| p.with_context(|| format!("unable to match path to pattern `{}`", &path)))
2332 .collect::<Result<Vec<_>, _>>()?;
2333 Ok(res)
2334 }
2335
2336 pub fn inheritable(&self) -> &InheritableFields {
2337 &self.inheritable_fields
2338 }
2339}
2340
2341pub fn resolve_relative_path(
2342 label: &str,
2343 old_root: &Path,
2344 new_root: &Path,
2345 rel_path: &str,
2346) -> CargoResult<String> {
2347 let joined_path = normalize_path(&old_root.join(rel_path));
2348 match diff_paths(joined_path, new_root) {
2349 None => Err(anyhow!(
2350 "`{}` was defined in {} but could not be resolved with {}",
2351 label,
2352 old_root.display(),
2353 new_root.display()
2354 )),
2355 Some(path) => Ok(path
2356 .to_str()
2357 .ok_or_else(|| {
2358 anyhow!(
2359 "`{}` resolved to non-UTF value (`{}`)",
2360 label,
2361 path.display()
2362 )
2363 })?
2364 .to_owned()),
2365 }
2366}
2367
2368pub fn find_workspace_root(
2370 manifest_path: &Path,
2371 gctx: &GlobalContext,
2372) -> CargoResult<Option<PathBuf>> {
2373 find_workspace_root_with_loader(manifest_path, gctx, |self_path| {
2374 let source_id = SourceId::for_manifest_path(self_path)?;
2375 let manifest = read_manifest(self_path, source_id, gctx)?;
2376 Ok(manifest
2377 .workspace_config()
2378 .get_ws_root(self_path, manifest_path))
2379 })
2380}
2381
2382pub fn find_workspace_root_with_membership_check(
2389 manifest_path: &Path,
2390 gctx: &GlobalContext,
2391) -> CargoResult<Option<PathBuf>> {
2392 let source_id = SourceId::for_manifest_path(manifest_path)?;
2393 let current_manifest = read_manifest(manifest_path, source_id, gctx)?;
2394
2395 match current_manifest.workspace_config() {
2396 WorkspaceConfig::Root(root_config) => {
2397 if root_config.has_default_members() {
2400 Ok(None)
2401 } else {
2402 Ok(Some(manifest_path.to_path_buf()))
2403 }
2404 }
2405 WorkspaceConfig::Member {
2406 root: Some(path_to_root),
2407 } => {
2408 let ws_manifest_path = read_root_pointer(manifest_path, path_to_root);
2410 let ws_source_id = SourceId::for_manifest_path(&ws_manifest_path)?;
2411 let ws_manifest = read_manifest(&ws_manifest_path, ws_source_id, gctx)?;
2412
2413 if let WorkspaceConfig::Root(ref root_config) = *ws_manifest.workspace_config() {
2415 if root_config.is_explicitly_listed_member(manifest_path)
2416 && !root_config.is_excluded(manifest_path)
2417 {
2418 return Ok(Some(ws_manifest_path));
2419 }
2420 }
2421 Ok(None)
2423 }
2424 WorkspaceConfig::Member { root: None } => {
2425 find_workspace_root_with_loader(manifest_path, gctx, |candidate_manifest_path| {
2427 let source_id = SourceId::for_manifest_path(candidate_manifest_path)?;
2428 let manifest = read_manifest(candidate_manifest_path, source_id, gctx)?;
2429 if let WorkspaceConfig::Root(ref root_config) = *manifest.workspace_config() {
2430 if root_config.is_explicitly_listed_member(manifest_path)
2431 && !root_config.is_excluded(manifest_path)
2432 {
2433 return Ok(Some(candidate_manifest_path.to_path_buf()));
2434 }
2435 }
2436 Ok(None)
2437 })
2438 }
2439 }
2440}
2441
2442fn find_workspace_root_with_loader(
2447 manifest_path: &Path,
2448 gctx: &GlobalContext,
2449 mut loader: impl FnMut(&Path) -> CargoResult<Option<PathBuf>>,
2450) -> CargoResult<Option<PathBuf>> {
2451 {
2453 let roots = gctx.ws_roots();
2454 for current in manifest_path.ancestors().skip(1) {
2457 if let Some(ws_config) = roots.get(current) {
2458 if !ws_config.is_excluded(manifest_path) {
2459 return Ok(Some(current.join("Cargo.toml")));
2461 }
2462 }
2463 }
2464 }
2465
2466 for ances_manifest_path in find_root_iter(manifest_path, gctx) {
2467 debug!("find_root - trying {}", ances_manifest_path.display());
2468 let ws_root_path = loader(&ances_manifest_path).with_context(|| {
2469 format!(
2470 "failed searching for potential workspace\n\
2471 package manifest: `{}`\n\
2472 invalid potential workspace manifest: `{}`\n\
2473 \n\
2474 help: to avoid searching for a non-existent workspace, add \
2475 `[workspace]` to the package manifest",
2476 manifest_path.display(),
2477 ances_manifest_path.display(),
2478 )
2479 })?;
2480 if let Some(ws_root_path) = ws_root_path {
2481 return Ok(Some(ws_root_path));
2482 }
2483 }
2484 Ok(None)
2485}
2486
2487fn read_root_pointer(member_manifest: &Path, root_link: &str) -> PathBuf {
2488 let path = member_manifest
2489 .parent()
2490 .unwrap()
2491 .join(root_link)
2492 .join("Cargo.toml");
2493 debug!("find_root - pointer {}", path.display());
2494 paths::normalize_path(&path)
2495}
2496
2497fn find_root_iter<'a>(
2498 manifest_path: &'a Path,
2499 gctx: &'a GlobalContext,
2500) -> impl Iterator<Item = PathBuf> + 'a {
2501 LookBehind::new(paths::ancestors(manifest_path, None).skip(2))
2502 .take_while(|path| !path.curr.ends_with("target/package"))
2503 .take_while(|path| {
2509 if let Some(last) = path.last {
2510 gctx.home() != last
2511 } else {
2512 true
2513 }
2514 })
2515 .map(|path| path.curr.join("Cargo.toml"))
2516 .filter(|ances_manifest_path| ances_manifest_path.exists())
2517}
2518
2519struct LookBehindWindow<'a, T: ?Sized> {
2520 curr: &'a T,
2521 last: Option<&'a T>,
2522}
2523
2524struct LookBehind<'a, T: ?Sized, K: Iterator<Item = &'a T>> {
2525 iter: K,
2526 last: Option<&'a T>,
2527}
2528
2529impl<'a, T: ?Sized, K: Iterator<Item = &'a T>> LookBehind<'a, T, K> {
2530 fn new(items: K) -> Self {
2531 Self {
2532 iter: items,
2533 last: None,
2534 }
2535 }
2536}
2537
2538impl<'a, T: ?Sized, K: Iterator<Item = &'a T>> Iterator for LookBehind<'a, T, K> {
2539 type Item = LookBehindWindow<'a, T>;
2540
2541 fn next(&mut self) -> Option<Self::Item> {
2542 match self.iter.next() {
2543 None => None,
2544 Some(next) => {
2545 let last = self.last;
2546 self.last = Some(next);
2547 Some(LookBehindWindow { curr: next, last })
2548 }
2549 }
2550 }
2551}