1use crate::{
49 compilers::{Compiler, CompilerVersion, ParsedSource},
50 project::VersionedSources,
51 resolver::parse::SolParser,
52 ArtifactOutput, CompilerSettings, Project, ProjectPathsConfig, SourceParser,
53};
54use core::fmt;
55use foundry_compilers_artifacts::sources::{Source, Sources};
56use foundry_compilers_core::{
57 error::{Result, SolcError},
58 utils,
59};
60use semver::{Version, VersionReq};
61use std::{
62 collections::{BTreeSet, HashMap, HashSet, VecDeque},
63 io,
64 path::{Path, PathBuf},
65};
66use yansi::{Color, Paint};
67
68pub mod parse;
69mod tree;
70
71pub use parse::SolImportAlias;
72pub use tree::{print, Charset, TreeOptions};
73
74#[derive(Debug)]
76pub struct ResolvedSources<'a, C: Compiler> {
77 pub sources: VersionedSources<'a, C::Language, C::Settings>,
82 pub primary_profiles: HashMap<PathBuf, &'a str>,
90 pub edges: GraphEdges<C::Parser>,
92}
93
94#[derive(Clone, Debug)]
99pub struct GraphEdges<P: SourceParser> {
100 edges: Vec<Vec<usize>>,
103 rev_edges: Vec<Vec<usize>>,
105 indices: HashMap<PathBuf, usize>,
107 rev_indices: HashMap<usize, PathBuf>,
109 versions: HashMap<usize, Option<VersionReq>>,
111 data: Vec<P::ParsedSource>,
113 parser: Option<P>,
115 num_input_files: usize,
121 unresolved_imports: HashSet<(PathBuf, PathBuf)>,
123 resolved_solc_include_paths: BTreeSet<PathBuf>,
129}
130
131impl<P: SourceParser> Default for GraphEdges<P> {
132 fn default() -> Self {
133 Self {
134 edges: Default::default(),
135 rev_edges: Default::default(),
136 indices: Default::default(),
137 rev_indices: Default::default(),
138 versions: Default::default(),
139 data: Default::default(),
140 parser: Default::default(),
141 num_input_files: Default::default(),
142 unresolved_imports: Default::default(),
143 resolved_solc_include_paths: Default::default(),
144 }
145 }
146}
147
148impl<P: SourceParser> GraphEdges<P> {
149 pub fn parser(&self) -> &P {
151 self.parser.as_ref().unwrap()
152 }
153
154 pub fn parser_mut(&mut self) -> &mut P {
156 self.parser.as_mut().unwrap()
157 }
158
159 pub fn num_source_files(&self) -> usize {
161 self.num_input_files
162 }
163
164 pub fn files(&self) -> impl Iterator<Item = usize> + '_ {
166 0..self.edges.len()
167 }
168
169 pub fn source_files(&self) -> impl Iterator<Item = usize> + '_ {
171 0..self.num_input_files
172 }
173
174 pub fn library_files(&self) -> impl Iterator<Item = usize> + '_ {
176 self.files().skip(self.num_input_files)
177 }
178
179 pub fn include_paths(&self) -> &BTreeSet<PathBuf> {
181 &self.resolved_solc_include_paths
182 }
183
184 pub fn unresolved_imports(&self) -> &HashSet<(PathBuf, PathBuf)> {
186 &self.unresolved_imports
187 }
188
189 pub fn imported_nodes(&self, from: usize) -> &[usize] {
191 &self.edges[from]
192 }
193
194 pub fn all_imported_nodes(&self, from: usize) -> impl Iterator<Item = usize> + '_ {
196 NodesIter::new(from, self).skip(1)
197 }
198
199 pub fn imports(&self, file: &Path) -> HashSet<&Path> {
201 if let Some(start) = self.indices.get(file).copied() {
202 NodesIter::new(start, self).skip(1).map(move |idx| &*self.rev_indices[&idx]).collect()
203 } else {
204 HashSet::new()
205 }
206 }
207
208 pub fn importers(&self, file: &Path) -> HashSet<&Path> {
210 if let Some(start) = self.indices.get(file).copied() {
211 self.rev_edges[start].iter().map(move |idx| &*self.rev_indices[idx]).collect()
212 } else {
213 HashSet::new()
214 }
215 }
216
217 pub fn node_id(&self, file: &Path) -> usize {
219 self.indices[file]
220 }
221
222 pub fn node_path(&self, id: usize) -> &Path {
224 &self.rev_indices[&id]
225 }
226
227 pub fn is_input_file(&self, file: &Path) -> bool {
230 if let Some(idx) = self.indices.get(file).copied() {
231 idx < self.num_input_files
232 } else {
233 false
234 }
235 }
236
237 pub fn version_requirement(&self, file: &Path) -> Option<&VersionReq> {
239 self.indices.get(file).and_then(|idx| self.versions.get(idx)).and_then(Option::as_ref)
240 }
241
242 pub fn get_parsed_source(&self, file: &Path) -> Option<&P::ParsedSource>
244 where
245 P: SourceParser,
246 {
247 self.indices.get(file).and_then(|idx| self.data.get(*idx))
248 }
249}
250
251#[derive(Debug)]
257pub struct Graph<P: SourceParser = SolParser> {
258 pub nodes: Vec<Node<P::ParsedSource>>,
260 edges: GraphEdges<P>,
262 root: PathBuf,
264}
265
266type L<P> = <<P as SourceParser>::ParsedSource as ParsedSource>::Language;
267
268impl<P: SourceParser> Graph<P> {
269 pub fn parser(&self) -> &P {
271 self.edges.parser()
272 }
273
274 pub fn print(&self) {
276 self.print_with_options(Default::default())
277 }
278
279 pub fn print_with_options(&self, opts: TreeOptions) {
281 let stdout = io::stdout();
282 let mut out = stdout.lock();
283 tree::print(self, &opts, &mut out).expect("failed to write to stdout.")
284 }
285
286 pub fn imported_nodes(&self, from: usize) -> &[usize] {
288 self.edges.imported_nodes(from)
289 }
290
291 pub fn all_imported_nodes(&self, from: usize) -> impl Iterator<Item = usize> + '_ {
293 self.edges.all_imported_nodes(from)
294 }
295
296 pub(crate) fn has_outgoing_edges(&self, index: usize) -> bool {
298 !self.edges.edges[index].is_empty()
299 }
300
301 pub fn files(&self) -> &HashMap<PathBuf, usize> {
303 &self.edges.indices
304 }
305
306 pub fn is_empty(&self) -> bool {
308 self.nodes.is_empty()
309 }
310
311 pub fn node(&self, index: usize) -> &Node<P::ParsedSource> {
317 &self.nodes[index]
318 }
319
320 pub(crate) fn display_node(&self, index: usize) -> DisplayNode<'_, P::ParsedSource> {
321 DisplayNode { node: self.node(index), root: &self.root }
322 }
323
324 pub fn node_ids(&self, start: usize) -> impl Iterator<Item = usize> + '_ {
331 NodesIter::new(start, &self.edges)
332 }
333
334 pub fn nodes(&self, start: usize) -> impl Iterator<Item = &Node<P::ParsedSource>> + '_ {
336 self.node_ids(start).map(move |idx| self.node(idx))
337 }
338
339 fn split(self) -> (Vec<(PathBuf, Source)>, GraphEdges<P>) {
340 let Self { nodes, mut edges, .. } = self;
341 let mut sources = Vec::new();
344 for (idx, node) in nodes.into_iter().enumerate() {
345 let Node { path, source, data } = node;
346 sources.push((path, source));
347 let idx2 = edges.data.len();
348 edges.data.push(data);
349 assert_eq!(idx, idx2);
350 }
351
352 (sources, edges)
353 }
354
355 pub fn into_sources(self) -> (Sources, GraphEdges<P>) {
358 let (sources, edges) = self.split();
359 (sources.into_iter().collect(), edges)
360 }
361
362 pub fn input_nodes(&self) -> impl Iterator<Item = &Node<P::ParsedSource>> {
366 self.nodes.iter().take(self.edges.num_input_files)
367 }
368
369 pub fn imports(&self, path: &Path) -> HashSet<&Path> {
371 self.edges.imports(path)
372 }
373
374 #[instrument(name = "Graph::resolve_sources", skip_all)]
376 pub fn resolve_sources(
377 paths: &ProjectPathsConfig<<P::ParsedSource as ParsedSource>::Language>,
378 mut sources: Sources,
379 ) -> Result<Self> {
380 fn add_node<P: SourceParser>(
384 parser: &mut P,
385 unresolved: &mut VecDeque<(PathBuf, Node<P::ParsedSource>)>,
386 index: &mut HashMap<PathBuf, usize>,
387 resolved_imports: &mut Vec<usize>,
388 target: PathBuf,
389 ) -> Result<()> {
390 if let Some(idx) = index.get(&target).copied() {
391 resolved_imports.push(idx);
392 } else {
393 let node = parser.read(&target)?;
395 unresolved.push_back((target.clone(), node));
396 let idx = index.len();
397 index.insert(target, idx);
398 resolved_imports.push(idx);
399 }
400 Ok(())
401 }
402
403 sources.make_absolute(&paths.root);
405
406 let mut parser = P::new(paths.with_language_ref());
407
408 let mut unresolved: VecDeque<_> = parser.parse_sources(&mut sources)?.into();
411
412 let mut index: HashMap<_, _> =
414 unresolved.iter().enumerate().map(|(idx, (p, _))| (p.clone(), idx)).collect();
415
416 let num_input_files = unresolved.len();
417
418 let mut nodes = Vec::with_capacity(unresolved.len());
420 let mut edges = Vec::with_capacity(unresolved.len());
421 let mut rev_edges = Vec::with_capacity(unresolved.len());
422
423 let mut resolved_solc_include_paths = BTreeSet::new();
426 resolved_solc_include_paths.insert(paths.root.clone());
427
428 let mut unresolved_imports = HashSet::new();
431
432 while let Some((path, node)) = unresolved.pop_front() {
435 let mut resolved_imports = Vec::new();
436 let cwd = match path.parent() {
438 Some(inner) => inner,
439 None => continue,
440 };
441
442 for import_path in node.data.resolve_imports(paths, &mut resolved_solc_include_paths)? {
443 if let Some(err) = match paths.resolve_import_and_include_paths(
444 cwd,
445 &import_path,
446 &mut resolved_solc_include_paths,
447 ) {
448 Ok(import) => add_node(
449 &mut parser,
450 &mut unresolved,
451 &mut index,
452 &mut resolved_imports,
453 import,
454 )
455 .err(),
456 Err(err) => Some(err),
457 } {
458 unresolved_imports.insert((import_path.to_path_buf(), node.path.clone()));
459 trace!("failed to resolve import component \"{:?}\" for {:?}", err, node.path)
460 }
461 }
462
463 nodes.push(node);
464 edges.push(resolved_imports);
465 rev_edges.push(Vec::new());
467 }
468
469 for (idx, edges) in edges.iter().enumerate() {
471 for &edge in edges.iter() {
472 rev_edges[edge].push(idx);
473 }
474 }
475
476 if !unresolved_imports.is_empty() {
477 crate::report::unresolved_imports(
479 &unresolved_imports
480 .iter()
481 .map(|(i, f)| (i.as_path(), f.as_path()))
482 .collect::<Vec<_>>(),
483 &paths.remappings,
484 );
485 }
486
487 let edges = GraphEdges {
488 edges,
489 rev_edges,
490 rev_indices: index.iter().map(|(k, v)| (*v, k.clone())).collect(),
491 indices: index,
492 num_input_files,
493 versions: nodes
494 .iter()
495 .enumerate()
496 .map(|(idx, node)| (idx, node.data.version_req().cloned()))
497 .collect(),
498 data: Default::default(),
499 parser: Some(parser),
500 unresolved_imports,
501 resolved_solc_include_paths,
502 };
503 Ok(Self { nodes, edges, root: paths.root.clone() })
504 }
505
506 pub fn resolve(
508 paths: &ProjectPathsConfig<<P::ParsedSource as ParsedSource>::Language>,
509 ) -> Result<Self> {
510 Self::resolve_sources(paths, paths.read_input_files()?)
511 }
512
513 pub fn into_sources_by_version<C, T>(
519 self,
520 project: &Project<C, T>,
521 ) -> Result<ResolvedSources<'_, C>>
522 where
523 T: ArtifactOutput<CompilerContract = C::CompilerContract>,
524 C: Compiler<Parser = P, Language = <P::ParsedSource as ParsedSource>::Language>,
525 {
526 fn insert_imports(
534 idx: usize,
535 all_nodes: &mut HashMap<usize, (PathBuf, Source)>,
536 sources: &mut Sources,
537 edges: &[Vec<usize>],
538 processed_sources: &mut HashSet<usize>,
539 ) {
540 for dep in edges[idx].iter().copied() {
542 if !processed_sources.insert(dep) {
545 continue;
546 }
547
548 if let Some((path, source)) = all_nodes.get(&dep).cloned() {
550 sources.insert(path, source);
551 insert_imports(dep, all_nodes, sources, edges, processed_sources);
552 }
553 }
554 }
555
556 let versioned_nodes = self.get_input_node_versions(project)?;
557 let versioned_nodes = self.resolve_settings(project, versioned_nodes)?;
558 let (nodes, edges) = self.split();
559
560 let mut all_nodes = nodes.into_iter().enumerate().collect::<HashMap<_, _>>();
561
562 let mut resulted_sources = HashMap::new();
563 let mut default_profiles = HashMap::new();
564
565 let profiles = project.settings_profiles().collect::<Vec<_>>();
566
567 for (language, versioned_nodes) in versioned_nodes {
569 let mut versioned_sources = Vec::with_capacity(versioned_nodes.len());
570
571 for (version, profile_to_nodes) in versioned_nodes {
572 for (profile_idx, input_node_indexes) in profile_to_nodes {
573 let mut sources = Sources::new();
574
575 let mut processed_sources = input_node_indexes.iter().copied().collect();
577
578 for idx in input_node_indexes {
580 let (path, source) =
583 all_nodes.get(&idx).cloned().expect("node is preset. qed");
584
585 default_profiles.insert(path.clone(), profiles[profile_idx].0);
586 sources.insert(path, source);
587 insert_imports(
588 idx,
589 &mut all_nodes,
590 &mut sources,
591 &edges.edges,
592 &mut processed_sources,
593 );
594 }
595 versioned_sources.push((version.clone(), sources, profiles[profile_idx]));
596 }
597 }
598
599 resulted_sources.insert(language, versioned_sources);
600 }
601
602 Ok(ResolvedSources { sources: resulted_sources, primary_profiles: default_profiles, edges })
603 }
604
605 fn format_imports_list<
614 C: Compiler,
615 T: ArtifactOutput<CompilerContract = C::CompilerContract>,
616 W: std::fmt::Write,
617 >(
618 &self,
619 idx: usize,
620 incompatible: HashSet<usize>,
621 project: &Project<C, T>,
622 f: &mut W,
623 ) -> std::result::Result<(), std::fmt::Error> {
624 let format_node = |idx, f: &mut W| {
625 let node = self.node(idx);
626 let color = if incompatible.contains(&idx) { Color::Red } else { Color::White };
627
628 let mut line = utils::source_name(&node.path, &self.root).display().to_string();
629 if let Some(req) = self.version_requirement(idx, project) {
630 line.push_str(&format!(" {req}"));
631 }
632
633 write!(f, "{}", line.paint(color))
634 };
635 format_node(idx, f)?;
636 write!(f, " imports:")?;
637 for dep in self.node_ids(idx).skip(1) {
638 write!(f, "\n ")?;
639 format_node(dep, f)?;
640 }
641
642 Ok(())
643 }
644
645 fn version_requirement<
647 C: Compiler,
648 T: ArtifactOutput<CompilerContract = C::CompilerContract>,
649 >(
650 &self,
651 idx: usize,
652 project: &Project<C, T>,
653 ) -> Option<VersionReq> {
654 let node = self.node(idx);
655 let parsed_req = node.data.version_req();
656 let other_req = project.restrictions.get(&node.path).and_then(|r| r.version.as_ref());
657
658 match (parsed_req, other_req) {
659 (Some(parsed_req), Some(other_req)) => {
660 let mut req = parsed_req.clone();
661 req.comparators.extend(other_req.comparators.clone());
662 Some(req)
663 }
664 (Some(parsed_req), None) => Some(parsed_req.clone()),
665 (None, Some(other_req)) => Some(other_req.clone()),
666 _ => None,
667 }
668 }
669
670 fn check_available_version<
675 C: Compiler,
676 T: ArtifactOutput<CompilerContract = C::CompilerContract>,
677 >(
678 &self,
679 idx: usize,
680 all_versions: &[&CompilerVersion],
681 project: &Project<C, T>,
682 ) -> std::result::Result<(), SourceVersionError> {
683 let Some(req) = self.version_requirement(idx, project) else { return Ok(()) };
684
685 if !all_versions.iter().any(|v| req.matches(v.as_ref())) {
686 return if project.offline {
687 Err(SourceVersionError::NoMatchingVersionOffline(req))
688 } else {
689 Err(SourceVersionError::NoMatchingVersion(req))
690 };
691 }
692
693 Ok(())
694 }
695
696 fn retain_compatible_versions<
699 C: Compiler,
700 T: ArtifactOutput<CompilerContract = C::CompilerContract>,
701 >(
702 &self,
703 idx: usize,
704 candidates: &mut Vec<&CompilerVersion>,
705 project: &Project<C, T>,
706 ) -> Result<(), String> {
707 let mut all_versions = candidates.clone();
708
709 let nodes: Vec<_> = self.node_ids(idx).collect();
710 let mut failed_node_idx = None;
711 for node in nodes.iter() {
712 if let Some(req) = self.version_requirement(*node, project) {
713 candidates.retain(|v| req.matches(v.as_ref()));
714
715 if candidates.is_empty() {
716 failed_node_idx = Some(*node);
717 break;
718 }
719 }
720 }
721
722 let Some(failed_node_idx) = failed_node_idx else {
723 return Ok(());
725 };
726
727 let failed_node = self.node(failed_node_idx);
731
732 if let Err(version_err) =
733 self.check_available_version(failed_node_idx, &all_versions, project)
734 {
735 let f = utils::source_name(&failed_node.path, &self.root).display();
737 return Err(format!("Encountered invalid solc version in {f}: {version_err}"));
738 } else {
739 if let Some(req) = self.version_requirement(failed_node_idx, project) {
744 all_versions.retain(|v| req.matches(v.as_ref()));
745 }
746
747 for node in &nodes {
749 if self.check_available_version(*node, &all_versions, project).is_err() {
750 let mut msg = "Found incompatible versions:\n".white().to_string();
751
752 self.format_imports_list(
753 idx,
754 [*node, failed_node_idx].into(),
755 project,
756 &mut msg,
757 )
758 .unwrap();
759 return Err(msg);
760 }
761 }
762 }
763
764 let mut msg = "Found incompatible versions:\n".white().to_string();
765 self.format_imports_list(idx, nodes.into_iter().collect(), project, &mut msg).unwrap();
766 Err(msg)
767 }
768
769 fn retain_compatible_profiles<
771 C: Compiler,
772 T: ArtifactOutput<CompilerContract = C::CompilerContract>,
773 >(
774 &self,
775 idx: usize,
776 project: &Project<C, T>,
777 candidates: &mut Vec<(usize, (&str, &C::Settings))>,
778 ) -> Result<(), String> {
779 let mut all_profiles = candidates.clone();
780
781 let nodes: Vec<_> = self.node_ids(idx).collect();
782 let mut failed_node_idx = None;
783 for node in nodes.iter() {
784 if let Some(req) = project.restrictions.get(&self.node(*node).path) {
785 candidates.retain(|(_, (_, settings))| settings.satisfies_restrictions(&**req));
786 if candidates.is_empty() {
787 failed_node_idx = Some(*node);
788 break;
789 }
790 }
791 }
792
793 let Some(failed_node_idx) = failed_node_idx else {
794 return Ok(());
796 };
797
798 let failed_node = self.node(failed_node_idx);
799
800 if let Some(req) = project.restrictions.get(&failed_node.path) {
802 all_profiles.retain(|(_, (_, settings))| settings.satisfies_restrictions(&**req));
803 }
804
805 if all_profiles.is_empty() {
806 let f = utils::source_name(&failed_node.path, &self.root).display();
807 return Err(format!("Missing profile satisfying settings restrictions for {f}"));
808 }
809
810 for node in &nodes {
812 if let Some(req) = project.restrictions.get(&self.node(*node).path) {
813 if !all_profiles
814 .iter()
815 .any(|(_, (_, settings))| settings.satisfies_restrictions(&**req))
816 {
817 let mut msg = "Found incompatible settings restrictions:\n".white().to_string();
818
819 self.format_imports_list(
820 idx,
821 [*node, failed_node_idx].into(),
822 project,
823 &mut msg,
824 )
825 .unwrap();
826 return Err(msg);
827 }
828 }
829 }
830
831 let mut msg = "Found incompatible settings restrictions:\n".white().to_string();
832 self.format_imports_list(idx, nodes.into_iter().collect(), project, &mut msg).unwrap();
833 Err(msg)
834 }
835
836 fn input_nodes_by_language(&self) -> HashMap<L<P>, Vec<usize>> {
837 let mut nodes = HashMap::new();
838
839 for idx in 0..self.edges.num_input_files {
840 nodes.entry(self.nodes[idx].data.language()).or_insert_with(Vec::new).push(idx);
841 }
842
843 nodes
844 }
845
846 #[allow(clippy::type_complexity)]
857 fn get_input_node_versions<
858 C: Compiler<Language = L<P>>,
859 T: ArtifactOutput<CompilerContract = C::CompilerContract>,
860 >(
861 &self,
862 project: &Project<C, T>,
863 ) -> Result<HashMap<L<P>, HashMap<Version, Vec<usize>>>> {
864 trace!("resolving input node versions");
865
866 let mut resulted_nodes = HashMap::new();
867
868 for (language, nodes) in self.input_nodes_by_language() {
869 let mut errors = Vec::new();
873
874 let all_versions = if project.offline {
876 project
877 .compiler
878 .available_versions(&language)
879 .into_iter()
880 .filter(|v| v.is_installed())
881 .collect()
882 } else {
883 project.compiler.available_versions(&language)
884 };
885
886 if all_versions.is_empty() && !nodes.is_empty() {
887 return Err(SolcError::msg(format!(
888 "Found {language} sources, but no compiler versions are available for it"
889 )));
890 }
891
892 let mut versioned_nodes = HashMap::new();
894
895 let mut all_candidates = Vec::with_capacity(self.edges.num_input_files);
897 for idx in nodes {
899 let mut candidates = all_versions.iter().collect::<Vec<_>>();
900 if let Err(err) = self.retain_compatible_versions(idx, &mut candidates, project) {
903 errors.push(err);
904 } else {
905 let candidate =
908 if let Some(pos) = candidates.iter().rposition(|v| v.is_installed()) {
909 candidates[pos]
910 } else {
911 candidates.last().expect("not empty; qed.")
912 }
913 .clone();
914
915 all_candidates.push((idx, candidates.into_iter().collect::<HashSet<_>>()));
917
918 versioned_nodes
919 .entry(candidate)
920 .or_insert_with(|| Vec::with_capacity(1))
921 .push(idx);
922 }
923 }
924
925 if versioned_nodes.len() > 1 {
928 versioned_nodes = Self::resolve_multiple_versions(all_candidates);
929 }
930
931 if versioned_nodes.len() == 1 {
932 trace!(
933 "found exact solc version for all sources \"{}\"",
934 versioned_nodes.keys().next().unwrap()
935 );
936 }
937
938 if errors.is_empty() {
939 trace!("resolved {} versions {:?}", versioned_nodes.len(), versioned_nodes.keys());
940 resulted_nodes.insert(
941 language,
942 versioned_nodes
943 .into_iter()
944 .map(|(v, nodes)| (Version::from(v), nodes))
945 .collect(),
946 );
947 } else {
948 let s = errors.join("\n");
949 debug!("failed to resolve versions: {s}");
950 return Err(SolcError::msg(s));
951 }
952 }
953
954 Ok(resulted_nodes)
955 }
956
957 #[allow(clippy::complexity)]
958 fn resolve_settings<
959 C: Compiler<Language = L<P>>,
960 T: ArtifactOutput<CompilerContract = C::CompilerContract>,
961 >(
962 &self,
963 project: &Project<C, T>,
964 input_nodes_versions: HashMap<L<P>, HashMap<Version, Vec<usize>>>,
965 ) -> Result<HashMap<L<P>, HashMap<Version, HashMap<usize, Vec<usize>>>>> {
966 let mut resulted_sources = HashMap::new();
967 let mut errors = Vec::new();
968 for (language, versions) in input_nodes_versions {
969 let mut versioned_sources = HashMap::new();
970 for (version, nodes) in versions {
971 let mut profile_to_nodes = HashMap::new();
972 for idx in nodes {
973 let mut profile_candidates =
974 project.settings_profiles().enumerate().collect::<Vec<_>>();
975 if let Err(err) =
976 self.retain_compatible_profiles(idx, project, &mut profile_candidates)
977 {
978 errors.push(err);
979 } else {
980 let (profile_idx, _) = profile_candidates.first().expect("exists");
981 profile_to_nodes.entry(*profile_idx).or_insert_with(Vec::new).push(idx);
982 }
983 }
984 versioned_sources.insert(version, profile_to_nodes);
985 }
986 resulted_sources.insert(language, versioned_sources);
987 }
988
989 if errors.is_empty() {
990 Ok(resulted_sources)
991 } else {
992 let s = errors.join("\n");
993 debug!("failed to resolve settings: {s}");
994 Err(SolcError::msg(s))
995 }
996 }
997
998 fn resolve_multiple_versions(
1004 all_candidates: Vec<(usize, HashSet<&CompilerVersion>)>,
1005 ) -> HashMap<CompilerVersion, Vec<usize>> {
1006 fn intersection<'a>(
1008 mut sets: Vec<&HashSet<&'a CompilerVersion>>,
1009 ) -> Vec<&'a CompilerVersion> {
1010 if sets.is_empty() {
1011 return Vec::new();
1012 }
1013
1014 let mut result = sets.pop().cloned().expect("not empty; qed.");
1015 if !sets.is_empty() {
1016 result.retain(|item| sets.iter().all(|set| set.contains(item)));
1017 }
1018
1019 let mut v = result.into_iter().collect::<Vec<_>>();
1020 v.sort_unstable();
1021 v
1022 }
1023
1024 fn remove_candidate(candidates: &mut Vec<&CompilerVersion>) -> CompilerVersion {
1028 debug_assert!(!candidates.is_empty());
1029
1030 if let Some(pos) = candidates.iter().rposition(|v| v.is_installed()) {
1031 candidates.remove(pos)
1032 } else {
1033 candidates.pop().expect("not empty; qed.")
1034 }
1035 .clone()
1036 }
1037
1038 let all_sets = all_candidates.iter().map(|(_, versions)| versions).collect();
1039
1040 let mut intersection = intersection(all_sets);
1042 if !intersection.is_empty() {
1043 let exact_version = remove_candidate(&mut intersection);
1044 let all_nodes = all_candidates.into_iter().map(|(node, _)| node).collect();
1045 trace!("resolved solc version compatible with all sources \"{}\"", exact_version);
1046 return HashMap::from([(exact_version, all_nodes)]);
1047 }
1048
1049 let mut versioned_nodes: HashMap<_, _> = HashMap::new();
1051
1052 for (node, versions) in all_candidates {
1055 let mut versions = versions.into_iter().collect::<Vec<_>>();
1057 versions.sort_unstable();
1058
1059 let candidate = if let Some(idx) =
1060 versions.iter().rposition(|v| versioned_nodes.contains_key(*v))
1061 {
1062 versions.remove(idx).clone()
1064 } else {
1065 remove_candidate(&mut versions)
1067 };
1068
1069 versioned_nodes.entry(candidate).or_insert_with(|| Vec::with_capacity(1)).push(node);
1070 }
1071
1072 trace!(
1073 "no solc version can satisfy all source files, resolved multiple versions \"{:?}\"",
1074 versioned_nodes.keys()
1075 );
1076
1077 versioned_nodes
1078 }
1079}
1080
1081#[derive(Debug)]
1083pub struct NodesIter<'a, P: SourceParser> {
1084 stack: VecDeque<usize>,
1086 visited: HashSet<usize>,
1087 graph: &'a GraphEdges<P>,
1088}
1089
1090impl<'a, P: SourceParser> NodesIter<'a, P> {
1091 fn new(start: usize, graph: &'a GraphEdges<P>) -> Self {
1092 Self { stack: VecDeque::from([start]), visited: HashSet::new(), graph }
1093 }
1094}
1095
1096impl<P: SourceParser> Iterator for NodesIter<'_, P> {
1097 type Item = usize;
1098 fn next(&mut self) -> Option<Self::Item> {
1099 let node = self.stack.pop_front()?;
1100
1101 if self.visited.insert(node) {
1102 self.stack.extend(self.graph.imported_nodes(node).iter().copied());
1104 }
1105 Some(node)
1106 }
1107}
1108
1109#[derive(Debug)]
1110pub struct Node<S> {
1111 path: PathBuf,
1113 source: Source,
1115 pub data: S,
1117}
1118
1119impl<S> Node<S> {
1120 pub fn new(path: PathBuf, source: Source, data: S) -> Self {
1121 Self { path, source, data }
1122 }
1123
1124 pub fn map_data<T>(self, f: impl FnOnce(S) -> T) -> Node<T> {
1125 Node::new(self.path, self.source, f(self.data))
1126 }
1127}
1128
1129impl<S: ParsedSource> Node<S> {
1130 pub fn read(file: &Path) -> Result<Self> {
1132 let source = Source::read_(file)?;
1133 Self::parse(file, source)
1134 }
1135
1136 pub fn parse(file: &Path, source: Source) -> Result<Self> {
1137 let data = S::parse(source.as_ref(), file)?;
1138 Ok(Self::new(file.to_path_buf(), source, data))
1139 }
1140
1141 pub fn path(&self) -> &Path {
1143 &self.path
1144 }
1145
1146 pub fn content(&self) -> &str {
1148 &self.source.content
1149 }
1150
1151 pub fn unpack(&self) -> (&Path, &Source) {
1152 (&self.path, &self.source)
1153 }
1154}
1155
1156pub(crate) struct DisplayNode<'a, S> {
1158 node: &'a Node<S>,
1159 root: &'a PathBuf,
1160}
1161
1162impl<S: ParsedSource> fmt::Display for DisplayNode<'_, S> {
1163 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1164 let path = utils::source_name(&self.node.path, self.root);
1165 write!(f, "{}", path.display())?;
1166 if let Some(v) = self.node.data.version_req() {
1167 write!(f, " {v}")?;
1168 }
1169 Ok(())
1170 }
1171}
1172
1173#[derive(Debug, thiserror::Error)]
1175#[allow(dead_code)]
1176enum SourceVersionError {
1177 #[error("Failed to parse solidity version {0}: {1}")]
1178 InvalidVersion(String, SolcError),
1179 #[error("No solc version exists that matches the version requirement: {0}")]
1180 NoMatchingVersion(VersionReq),
1181 #[error("No solc version installed that matches the version requirement: {0}")]
1182 NoMatchingVersionOffline(VersionReq),
1183}
1184
1185#[cfg(test)]
1186mod tests {
1187 use super::*;
1188
1189 #[test]
1190 fn can_resolve_hardhat_dependency_graph() {
1191 let root = Path::new(env!("CARGO_MANIFEST_DIR")).join("../../test-data/hardhat-sample");
1192 let paths = ProjectPathsConfig::hardhat(&root).unwrap();
1193
1194 let graph = Graph::<SolParser>::resolve(&paths).unwrap();
1195
1196 assert_eq!(graph.edges.num_input_files, 1);
1197 assert_eq!(graph.files().len(), 2);
1198
1199 assert_eq!(
1200 graph.files().clone(),
1201 HashMap::from([
1202 (paths.sources.join("Greeter.sol"), 0),
1203 (paths.root.join("node_modules/hardhat/console.sol"), 1),
1204 ])
1205 );
1206 }
1207
1208 #[test]
1209 fn can_resolve_dapp_dependency_graph() {
1210 let root = Path::new(env!("CARGO_MANIFEST_DIR")).join("../../test-data/dapp-sample");
1211 let paths = ProjectPathsConfig::dapptools(&root).unwrap();
1212
1213 let graph = Graph::<SolParser>::resolve(&paths).unwrap();
1214
1215 assert_eq!(graph.edges.num_input_files, 2);
1216 assert_eq!(graph.files().len(), 3);
1217 assert_eq!(
1218 graph.files().clone(),
1219 HashMap::from([
1220 (paths.sources.join("Dapp.sol"), 0),
1221 (paths.sources.join("Dapp.t.sol"), 1),
1222 (paths.root.join("lib/ds-test/src/test.sol"), 2),
1223 ])
1224 );
1225
1226 let dapp_test = graph.node(1);
1227 assert_eq!(dapp_test.path, paths.sources.join("Dapp.t.sol"));
1228 assert_eq!(
1229 dapp_test.data.imports.iter().map(|i| i.data().path()).collect::<Vec<&Path>>(),
1230 vec![Path::new("ds-test/test.sol"), Path::new("./Dapp.sol")]
1231 );
1232 assert_eq!(graph.imported_nodes(1).to_vec(), vec![2, 0]);
1233 }
1234
1235 #[test]
1236 fn can_print_dapp_sample_graph() {
1237 let root = Path::new(env!("CARGO_MANIFEST_DIR")).join("../../test-data/dapp-sample");
1238 let paths = ProjectPathsConfig::dapptools(&root).unwrap();
1239 let graph = Graph::<SolParser>::resolve(&paths).unwrap();
1240 let mut out = Vec::<u8>::new();
1241 tree::print(&graph, &Default::default(), &mut out).unwrap();
1242
1243 if !cfg!(windows) {
1244 assert_eq!(
1245 "
1246src/Dapp.sol >=0.6.6
1247src/Dapp.t.sol >=0.6.6
1248├── lib/ds-test/src/test.sol >=0.4.23
1249└── src/Dapp.sol >=0.6.6
1250"
1251 .trim_start()
1252 .as_bytes()
1253 .to_vec(),
1254 out
1255 );
1256 }
1257
1258 graph.edges.parser().compiler.enter(|c| {
1259 assert_eq!(c.gcx().sources.len(), 3);
1260 });
1261 }
1262
1263 #[test]
1264 #[cfg(not(target_os = "windows"))]
1265 fn can_print_hardhat_sample_graph() {
1266 let root = Path::new(env!("CARGO_MANIFEST_DIR")).join("../../test-data/hardhat-sample");
1267 let paths = ProjectPathsConfig::hardhat(&root).unwrap();
1268 let graph = Graph::<SolParser>::resolve(&paths).unwrap();
1269 let mut out = Vec::<u8>::new();
1270 tree::print(&graph, &Default::default(), &mut out).unwrap();
1271 assert_eq!(
1272 "contracts/Greeter.sol >=0.6.0
1273└── node_modules/hardhat/console.sol >=0.4.22, <0.9.0
1274",
1275 String::from_utf8(out).unwrap()
1276 );
1277 }
1278
1279 #[test]
1280 #[cfg(feature = "svm-solc")]
1281 fn test_print_unresolved() {
1282 use crate::{solc::SolcCompiler, ProjectBuilder};
1283
1284 let root =
1285 Path::new(env!("CARGO_MANIFEST_DIR")).join("../../test-data/incompatible-pragmas");
1286 let paths = ProjectPathsConfig::dapptools(&root).unwrap();
1287 let graph = Graph::<SolParser>::resolve(&paths).unwrap();
1288 let Err(SolcError::Message(err)) = graph.get_input_node_versions(
1289 &ProjectBuilder::<SolcCompiler>::default()
1290 .paths(paths)
1291 .build(SolcCompiler::AutoDetect)
1292 .unwrap(),
1293 ) else {
1294 panic!("expected error");
1295 };
1296
1297 snapbox::assert_data_eq!(
1298 err,
1299 snapbox::str![[r#"
1300[37mFound incompatible versions:
1301[0m[31msrc/A.sol =0.8.25[0m imports:
1302 [37msrc/B.sol[0m
1303 [31msrc/C.sol =0.7.0[0m
1304"#]]
1305 );
1306 }
1307
1308 #[cfg(target_os = "linux")]
1309 #[test]
1310 fn can_read_different_case() {
1311 use crate::resolver::parse::SolData;
1312 use std::fs::{self, create_dir_all};
1313 use utils::tempdir;
1314
1315 let tmp_dir = tempdir("out").unwrap();
1316 let path = tmp_dir.path().join("forge-std");
1317 create_dir_all(&path).unwrap();
1318 let existing = path.join("Test.sol");
1319 let non_existing = path.join("test.sol");
1320 fs::write(
1321 existing,
1322 "
1323pragma solidity ^0.8.10;
1324contract A {}
1325 ",
1326 )
1327 .unwrap();
1328
1329 assert!(!non_existing.exists());
1330
1331 let found = crate::resolver::Node::<SolData>::read(&non_existing).unwrap_err();
1332 matches!(found, SolcError::ResolveCaseSensitiveFileName { .. });
1333 }
1334}