Skip to main content

foundry_compilers/resolver/
mod.rs

1//! Resolution of the entire dependency graph for a project.
2//!
3//! This module implements the core logic in taking all contracts of a project and creating a
4//! resolved graph with applied remappings for all source contracts.
5//!
6//! Some constraints we're working with when resolving contracts
7//!
8//!   1. Each file can contain several source units and can have any number of imports/dependencies
9//!      (using the term interchangeably). Each dependency can declare a version range that it is
10//!      compatible with, solidity version pragma.
11//!   2. A dependency can be imported from any directory, see `Remappings`
12//!
13//! Finding all dependencies is fairly simple, we're simply doing a DFS, starting from the source
14//! contracts
15//!
16//! ## Solc version auto-detection
17//!
18//! Solving a constraint graph is an NP-hard problem. The algorithm for finding the "best" solution
19//! makes several assumptions and tries to find a version of "Solc" that is compatible with all
20//! source files.
21//!
22//! The algorithm employed here is fairly simple, we simply do a DFS over all the source files and
23//! find the set of Solc versions that the file and all its imports are compatible with, and then we
24//! try to find a single Solc version that is compatible with all the files. This is effectively the
25//! intersection of all version sets.
26//!
27//! We always try to activate the highest (installed) solc version first. Uninstalled solc is only
28//! used if this version is the only compatible version for a single file or in the intersection of
29//! all version sets.
30//!
31//! This leads to finding the optimal version, if there is one. If there is no single Solc version
32//! that is compatible with all sources and their imports, then suddenly this becomes a very
33//! difficult problem, because what would be the "best" solution. In this case, just choose the
34//! latest (installed) Solc version and try to minimize the number of Solc versions used.
35//!
36//! ## Performance
37//!
38//! Note that this is a relatively performance-critical portion of the ethers-solc preprocessing.
39//! The data that needs to be processed is proportional to the size of the dependency
40//! graph, which can, depending on the project, often be quite large.
41//!
42//! Note that, unlike the solidity compiler, we work with the filesystem, where we have to resolve
43//! remappings and follow relative paths. We're also limiting the nodes in the graph to solidity
44//! files, since we're only interested in their
45//! [version pragma](https://docs.soliditylang.org/en/develop/layout-of-source-files.html#version-pragma),
46//! which is defined on a per source file basis.
47
48use crate::{
49    ArtifactOutput, CompilerSettings, Project, ProjectPathsConfig, SourceParser,
50    compilers::{Compiler, CompilerVersion, ParsedSource},
51    project::VersionedSources,
52    resolver::parse::SolParser,
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::{Charset, TreeOptions, print};
73
74/// Container for result of version and profile resolution of sources contained in [`Graph`].
75#[derive(Debug)]
76pub struct ResolvedSources<'a, C: Compiler> {
77    /// Resolved set of sources.
78    ///
79    /// Mapping from language to a [`Vec`] of compiler inputs consisting of version, sources set
80    /// and settings.
81    pub sources: VersionedSources<'a, C::Language, C::Settings>,
82    /// A mapping from a source file path to the primary profile name selected for it.
83    ///
84    /// This is required because the same source file might be compiled with multiple different
85    /// profiles if it's present as a dependency for other sources. We want to keep a single name
86    /// of the profile which was chosen specifically for each source so that we can default to it.
87    /// Right now, this is used when generating artifact names, "primary" artifact will never have
88    /// a profile suffix.
89    pub primary_profiles: HashMap<PathBuf, &'a str>,
90    /// Graph edges.
91    pub edges: GraphEdges<C::Parser>,
92}
93
94/// The underlying edges of the graph which only contains the raw relationship data.
95///
96/// This is kept separate from the `Graph` as the `Node`s get consumed when the `Solc` to `Sources`
97/// set is determined.
98#[derive(Clone, Debug)]
99pub struct GraphEdges<P: SourceParser> {
100    /// The indices of `edges` correspond to the `nodes`. That is, `edges[0]`
101    /// is the set of outgoing edges for `nodes[0]`.
102    edges: Vec<Vec<usize>>,
103    /// Reverse of `edges`. That is, `rev_edges[0]` is the set of incoming edges for `nodes[0]`.
104    rev_edges: Vec<Vec<usize>>,
105    /// index maps for a solidity file to an index, for fast lookup.
106    indices: HashMap<PathBuf, usize>,
107    /// reverse of `indices` for reverse lookup
108    rev_indices: HashMap<usize, PathBuf>,
109    /// the identified version requirement of a file
110    versions: HashMap<usize, Option<VersionReq>>,
111    /// the extracted data from the source files
112    data: Vec<P::ParsedSource>,
113    /// The parser which parsed `data`.
114    parser: Option<P>,
115    /// with how many input files we started with, corresponds to `let input_files =
116    /// nodes[..num_input_files]`.
117    ///
118    /// Combined with the `indices` this way we can determine if a file was original added to the
119    /// graph as input or was added as resolved import, see [`Self::is_input_file()`]
120    num_input_files: usize,
121    /// tracks all imports that we failed to resolve for a file
122    unresolved_imports: HashSet<(PathBuf, PathBuf)>,
123    /// tracks additional include paths resolved by scanning all imports of the graph
124    ///
125    /// Absolute imports, like `import "src/Contract.sol"` are possible, but this does not play
126    /// nice with the standard-json import format, since the VFS won't be able to resolve
127    /// "src/Contract.sol" without help via `--include-path`
128    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    /// Returns the parser used to parse the sources.
150    pub const fn parser(&self) -> &P {
151        self.parser.as_ref().unwrap()
152    }
153
154    /// Returns the parser used to parse the sources.
155    pub const fn parser_mut(&mut self) -> &mut P {
156        self.parser.as_mut().unwrap()
157    }
158
159    /// How many files are source files
160    pub const fn num_source_files(&self) -> usize {
161        self.num_input_files
162    }
163
164    /// Returns an iterator over all file indices
165    pub fn files(&self) -> impl Iterator<Item = usize> + '_ {
166        0..self.edges.len()
167    }
168
169    /// Returns an iterator over all source file indices
170    pub fn source_files(&self) -> impl Iterator<Item = usize> + '_ {
171        0..self.num_input_files
172    }
173
174    /// Returns an iterator over all library files
175    pub fn library_files(&self) -> impl Iterator<Item = usize> + '_ {
176        self.files().skip(self.num_input_files)
177    }
178
179    /// Returns all additional `--include-paths`
180    pub const fn include_paths(&self) -> &BTreeSet<PathBuf> {
181        &self.resolved_solc_include_paths
182    }
183
184    /// Returns all imports that we failed to resolve
185    pub const fn unresolved_imports(&self) -> &HashSet<(PathBuf, PathBuf)> {
186        &self.unresolved_imports
187    }
188
189    /// Returns a list of nodes the given node index points to for the given kind.
190    pub fn imported_nodes(&self, from: usize) -> &[usize] {
191        &self.edges[from]
192    }
193
194    /// Returns an iterator that yields all imports of a node and all their imports
195    pub fn all_imported_nodes(&self, from: usize) -> impl Iterator<Item = usize> + '_ {
196        NodesIter::new(from, self).skip(1)
197    }
198
199    /// Returns all files imported by the given file
200    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    /// Returns all files that import the given file
209    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    /// Returns the id of the given file
218    pub fn node_id(&self, file: &Path) -> usize {
219        self.indices[file]
220    }
221
222    /// Returns the path of the given node
223    pub fn node_path(&self, id: usize) -> &Path {
224        &self.rev_indices[&id]
225    }
226
227    /// Returns true if the `file` was originally included when the graph was first created and not
228    /// added when all `imports` were resolved
229    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    /// Returns the `VersionReq` for the given file
238    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    /// Returns the parsed source data for the given file
243    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/// Represents a fully-resolved solidity dependency graph.
252///
253/// Each node in the graph is a file and edges represent dependencies between them.
254///
255/// See also <https://docs.soliditylang.org/en/latest/layout-of-source-files.html?highlight=import#importing-other-source-files>
256#[derive(Debug)]
257pub struct Graph<P: SourceParser = SolParser> {
258    /// all nodes in the project, a `Node` represents a single file
259    pub nodes: Vec<Node<P::ParsedSource>>,
260    /// relationship of the nodes
261    edges: GraphEdges<P>,
262    /// the root of the project this graph represents
263    root: PathBuf,
264}
265
266type L<P> = <<P as SourceParser>::ParsedSource as ParsedSource>::Language;
267
268impl<P: SourceParser> Graph<P> {
269    /// Returns the parser used to parse the sources.
270    pub const fn parser(&self) -> &P {
271        self.edges.parser()
272    }
273
274    /// Print the graph to `StdOut`
275    pub fn print(&self) {
276        self.print_with_options(Default::default())
277    }
278
279    /// Print the graph to `StdOut` using the provided `TreeOptions`
280    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    /// Returns a list of nodes the given node index points to for the given kind.
287    pub fn imported_nodes(&self, from: usize) -> &[usize] {
288        self.edges.imported_nodes(from)
289    }
290
291    /// Returns an iterator that yields all imports of a node and all their imports
292    pub fn all_imported_nodes(&self, from: usize) -> impl Iterator<Item = usize> + '_ {
293        self.edges.all_imported_nodes(from)
294    }
295
296    /// Returns `true` if the given node has any outgoing edges.
297    pub(crate) fn has_outgoing_edges(&self, index: usize) -> bool {
298        !self.edges.edges[index].is_empty()
299    }
300
301    /// Returns all the resolved files and their index in the graph.
302    pub const fn files(&self) -> &HashMap<PathBuf, usize> {
303        &self.edges.indices
304    }
305
306    /// Returns `true` if the graph is empty.
307    pub const fn is_empty(&self) -> bool {
308        self.nodes.is_empty()
309    }
310
311    /// Gets a node by index.
312    ///
313    /// # Panics
314    ///
315    /// if the `index` node id is not included in the graph
316    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    /// Returns an iterator that yields all nodes of the dependency tree that the given node id
325    /// spans, starting with the node itself.
326    ///
327    /// # Panics
328    ///
329    /// if the `start` node id is not included in the graph
330    pub fn node_ids(&self, start: usize) -> impl Iterator<Item = usize> + '_ {
331        NodesIter::new(start, &self.edges)
332    }
333
334    /// Same as `Self::node_ids` but returns the actual `Node`
335    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        // need to move the extracted data to the edges, essentially splitting the node so we have
342        // access to the data at a later stage in the compile pipeline
343        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    /// Consumes the `Graph`, effectively splitting the `nodes` and the `GraphEdges` off and
356    /// returning the `nodes` converted to `Sources`
357    pub fn into_sources(self) -> (Sources, GraphEdges<P>) {
358        let (sources, edges) = self.split();
359        (sources.into_iter().collect(), edges)
360    }
361
362    /// Returns an iterator that yields only those nodes that represent input files.
363    /// See `Self::resolve_sources`
364    /// This won't yield any resolved library nodes
365    pub fn input_nodes(&self) -> impl Iterator<Item = &Node<P::ParsedSource>> {
366        self.nodes.iter().take(self.edges.num_input_files)
367    }
368
369    /// Returns all files imported by the given file
370    pub fn imports(&self, path: &Path) -> HashSet<&Path> {
371        self.edges.imports(path)
372    }
373
374    /// Resolves a number of sources within the given config
375    #[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        /// checks if the given target path was already resolved, if so it adds its id to the list
381        /// of resolved imports. If it hasn't been resolved yet, it queues in the file for
382        /// processing
383        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                // imported file is not part of the input files
394                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        // The cache relies on the absolute paths relative to the project root as cache keys.
404        sources.make_absolute(&paths.root);
405
406        let mut parser = P::new(paths.with_language_ref());
407
408        // we start off by reading all input files, which includes all solidity files from the
409        // source and test folder
410        let mut unresolved: VecDeque<_> = parser.parse_sources(&mut sources)?.into();
411
412        // identifiers of all resolved files
413        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        // contains the files and their dependencies
419        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        // tracks additional paths that should be used with `--include-path`, these are libraries
424        // that use absolute imports like `import "src/Contract.sol"`
425        let mut resolved_solc_include_paths = BTreeSet::new();
426        resolved_solc_include_paths.insert(paths.root.clone());
427
428        // keep track of all unique paths that we failed to resolve to not spam the reporter with
429        // the same path
430        let mut unresolved_imports = HashSet::new();
431
432        // now we need to resolve all imports for the source file and those imported from other
433        // locations
434        while let Some((path, node)) = unresolved.pop_front() {
435            let mut resolved_imports = Vec::new();
436            // parent directory of the current file
437            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.clone(), 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            // Will be populated later
466            rev_edges.push(Vec::new());
467        }
468
469        // Build `rev_edges`
470        for (idx, edges) in edges.iter().enumerate() {
471            for &edge in edges {
472                rev_edges[edge].push(idx);
473            }
474        }
475
476        if !unresolved_imports.is_empty() {
477            // notify on all unresolved imports
478            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        parser.finalize_imports(&mut nodes, &resolved_solc_include_paths)?;
488
489        let edges = GraphEdges {
490            edges,
491            rev_edges,
492            rev_indices: index.iter().map(|(k, v)| (*v, k.clone())).collect(),
493            indices: index,
494            num_input_files,
495            versions: nodes
496                .iter()
497                .enumerate()
498                .map(|(idx, node)| (idx, node.data.version_req().cloned()))
499                .collect(),
500            data: Default::default(),
501            parser: Some(parser),
502            unresolved_imports,
503            resolved_solc_include_paths,
504        };
505        Ok(Self { nodes, edges, root: paths.root.clone() })
506    }
507
508    /// Resolves the dependencies of a project's source contracts
509    pub fn resolve(
510        paths: &ProjectPathsConfig<<P::ParsedSource as ParsedSource>::Language>,
511    ) -> Result<Self> {
512        Self::resolve_sources(paths, paths.read_input_files()?)
513    }
514
515    /// Consumes the nodes of the graph and returns all input files together with their appropriate
516    /// version and the edges of the graph
517    ///
518    /// First we determine the compatible version for each input file (from sources and test folder,
519    /// see `Self::resolve`) and then we add all resolved library imports.
520    pub fn into_sources_by_version<C, T>(
521        self,
522        project: &Project<C, T>,
523    ) -> Result<ResolvedSources<'_, C>>
524    where
525        T: ArtifactOutput<CompilerContract = C::CompilerContract>,
526        C: Compiler<Parser = P, Language = <P::ParsedSource as ParsedSource>::Language>,
527    {
528        /// insert the imports of the given node into the sources map
529        /// There can be following graph:
530        /// `A(<=0.8.10) imports C(>0.4.0)` and `B(0.8.11) imports C(>0.4.0)`
531        /// where `C` is a library import, in which case we assign `C` only to the first input file.
532        /// However, it's not required to include them in the solc `CompilerInput` as they would get
533        /// picked up by solc otherwise, but we add them, so we can create a corresponding
534        /// cache entry for them as well. This can be optimized however
535        fn insert_imports(
536            idx: usize,
537            all_nodes: &mut HashMap<usize, (PathBuf, Source)>,
538            sources: &mut Sources,
539            edges: &[Vec<usize>],
540            processed_sources: &mut HashSet<usize>,
541        ) {
542            // iterate over all dependencies not processed yet
543            for dep in edges[idx].iter().copied() {
544                // keep track of processed dependencies, if the dep was already in the set we have
545                // processed it already
546                if !processed_sources.insert(dep) {
547                    continue;
548                }
549
550                // library import
551                if let Some((path, source)) = all_nodes.get(&dep).cloned() {
552                    sources.insert(path, source);
553                    insert_imports(dep, all_nodes, sources, edges, processed_sources);
554                }
555            }
556        }
557
558        let versioned_nodes = self.get_input_node_versions(project)?;
559        let versioned_nodes = self.resolve_settings(project, versioned_nodes)?;
560        let (nodes, edges) = self.split();
561
562        let mut all_nodes = nodes.into_iter().enumerate().collect::<HashMap<_, _>>();
563
564        let mut resulted_sources = HashMap::new();
565        let mut default_profiles = HashMap::new();
566
567        let profiles = project.settings_profiles().collect::<Vec<_>>();
568
569        // determine the `Sources` set for each solc version
570        for (language, versioned_nodes) in versioned_nodes {
571            let mut versioned_sources = Vec::with_capacity(versioned_nodes.len());
572
573            for (version, profile_to_nodes) in versioned_nodes {
574                for (profile_idx, input_node_indexes) in profile_to_nodes {
575                    let mut sources = Sources::new();
576
577                    // all input nodes will be processed
578                    let mut processed_sources = input_node_indexes.iter().copied().collect();
579
580                    // we only process input nodes (from sources, tests for example)
581                    for idx in input_node_indexes {
582                        // insert the input node in the sources set and remove it from the available
583                        // set
584                        let (path, source) =
585                            all_nodes.get(&idx).cloned().expect("node is preset. qed");
586
587                        default_profiles.insert(path.clone(), profiles[profile_idx].0);
588                        sources.insert(path, source);
589                        insert_imports(
590                            idx,
591                            &mut all_nodes,
592                            &mut sources,
593                            &edges.edges,
594                            &mut processed_sources,
595                        );
596                    }
597                    versioned_sources.push((version.clone(), sources, profiles[profile_idx]));
598                }
599            }
600
601            resulted_sources.insert(language, versioned_sources);
602        }
603
604        Ok(ResolvedSources { sources: resulted_sources, primary_profiles: default_profiles, edges })
605    }
606
607    /// Writes the list of imported files into the given formatter:
608    ///
609    /// ```text
610    /// path/to/a.sol (<version>) imports:
611    ///     path/to/b.sol (<version>)
612    ///     path/to/c.sol (<version>)
613    ///     ...
614    /// ```
615    fn format_imports_list<
616        C: Compiler,
617        T: ArtifactOutput<CompilerContract = C::CompilerContract>,
618        W: std::fmt::Write,
619    >(
620        &self,
621        idx: usize,
622        incompatible: HashSet<usize>,
623        project: &Project<C, T>,
624        f: &mut W,
625    ) -> std::result::Result<(), std::fmt::Error> {
626        let format_node = |idx, f: &mut W| {
627            let node = self.node(idx);
628            let color = if incompatible.contains(&idx) { Color::Red } else { Color::White };
629
630            let mut line = utils::source_name(&node.path, &self.root).display().to_string();
631            if let Some(req) = self.version_requirement(idx, project) {
632                line.push_str(&format!(" {req}"));
633            }
634
635            write!(f, "{}", line.paint(color))
636        };
637        format_node(idx, f)?;
638        write!(f, " imports:")?;
639        for dep in self.node_ids(idx).skip(1) {
640            write!(f, "\n    ")?;
641            format_node(dep, f)?;
642        }
643
644        Ok(())
645    }
646
647    /// Combines version requirement parsed from file and from project restrictions.
648    fn version_requirement<
649        C: Compiler,
650        T: ArtifactOutput<CompilerContract = C::CompilerContract>,
651    >(
652        &self,
653        idx: usize,
654        project: &Project<C, T>,
655    ) -> Option<VersionReq> {
656        let node = self.node(idx);
657        let parsed_req = node.data.version_req();
658        let other_req = project.restrictions.get(&node.path).and_then(|r| r.version.as_ref());
659
660        match (parsed_req, other_req) {
661            (Some(parsed_req), Some(other_req)) => {
662                let mut req = parsed_req.clone();
663                req.comparators.extend(other_req.comparators.clone());
664                Some(req)
665            }
666            (Some(parsed_req), None) => Some(parsed_req.clone()),
667            (None, Some(other_req)) => Some(other_req.clone()),
668            _ => None,
669        }
670    }
671
672    /// Checks that the file's version is even available.
673    ///
674    /// This returns an error if the file's version is invalid semver, or is not available such as
675    /// 0.8.20, if the highest available version is `0.8.19`
676    fn check_available_version<
677        C: Compiler,
678        T: ArtifactOutput<CompilerContract = C::CompilerContract>,
679    >(
680        &self,
681        idx: usize,
682        all_versions: &[&CompilerVersion],
683        project: &Project<C, T>,
684    ) -> std::result::Result<(), SourceVersionError> {
685        let Some(req) = self.version_requirement(idx, project) else { return Ok(()) };
686
687        if !all_versions.iter().any(|v| req.matches(v.as_ref())) {
688            return if project.offline {
689                Err(SourceVersionError::NoMatchingVersionOffline(req))
690            } else {
691                Err(SourceVersionError::NoMatchingVersion(req))
692            };
693        }
694
695        Ok(())
696    }
697
698    /// Filters incompatible versions from the `candidates`. It iterates over node imports and in
699    /// case if there is no compatible version it returns the latest seen node id.
700    fn retain_compatible_versions<
701        C: Compiler,
702        T: ArtifactOutput<CompilerContract = C::CompilerContract>,
703    >(
704        &self,
705        idx: usize,
706        candidates: &mut Vec<&CompilerVersion>,
707        project: &Project<C, T>,
708    ) -> Result<(), String> {
709        let mut all_versions = candidates.clone();
710
711        let nodes: Vec<_> = self.node_ids(idx).collect();
712        let mut failed_node_idx = None;
713        for node in &nodes {
714            if let Some(req) = self.version_requirement(*node, project) {
715                candidates.retain(|v| req.matches(v.as_ref()));
716
717                if candidates.is_empty() {
718                    failed_node_idx = Some(*node);
719                    break;
720                }
721            }
722        }
723
724        let Some(failed_node_idx) = failed_node_idx else {
725            // everything is fine
726            return Ok(());
727        };
728
729        // This now keeps data for the node which were the last one before we had no candidates
730        // left. It means that there is a node directly conflicting with it in `nodes` coming
731        // before.
732        let failed_node = self.node(failed_node_idx);
733
734        if let Err(version_err) =
735            self.check_available_version(failed_node_idx, &all_versions, project)
736        {
737            // check if the version is even valid
738            let f = utils::source_name(&failed_node.path, &self.root).display();
739            return Err(format!("Encountered invalid solc version in {f}: {version_err}"));
740        }
741
742        // if the node requirement makes sense, it means that there is at least one node
743        // which requirement conflicts with it
744
745        // retain only versions compatible with the `failed_node`
746        if let Some(req) = self.version_requirement(failed_node_idx, project) {
747            all_versions.retain(|v| req.matches(v.as_ref()));
748        }
749
750        // iterate over all the nodes once again and find the one incompatible
751        for node in &nodes {
752            if self.check_available_version(*node, &all_versions, project).is_err() {
753                let mut msg = "Found incompatible versions:\n".white().to_string();
754
755                self.format_imports_list(idx, [*node, failed_node_idx].into(), project, &mut msg)
756                    .unwrap();
757                return Err(msg);
758            }
759        }
760
761        let mut msg = "Found incompatible versions:\n".white().to_string();
762        self.format_imports_list(idx, nodes.into_iter().collect(), project, &mut msg).unwrap();
763        Err(msg)
764    }
765
766    /// Filters profiles incompatible with the given node and its imports.
767    fn retain_compatible_profiles<
768        C: Compiler,
769        T: ArtifactOutput<CompilerContract = C::CompilerContract>,
770    >(
771        &self,
772        idx: usize,
773        project: &Project<C, T>,
774        candidates: &mut Vec<(usize, (&str, &C::Settings))>,
775    ) -> Result<(), String> {
776        let mut all_profiles = candidates.clone();
777
778        let nodes: Vec<_> = self.node_ids(idx).collect();
779        let mut failed_node_idx = None;
780        for node in &nodes {
781            if let Some(req) = project.restrictions.get(&self.node(*node).path) {
782                candidates.retain(|(_, (_, settings))| settings.satisfies_restrictions(&**req));
783                if candidates.is_empty() {
784                    failed_node_idx = Some(*node);
785                    break;
786                }
787            }
788        }
789
790        let Some(failed_node_idx) = failed_node_idx else {
791            // everything is fine
792            return Ok(());
793        };
794
795        let failed_node = self.node(failed_node_idx);
796
797        // retain only profiles compatible with the `failed_node`
798        if let Some(req) = project.restrictions.get(&failed_node.path) {
799            all_profiles.retain(|(_, (_, settings))| settings.satisfies_restrictions(&**req));
800        }
801
802        if all_profiles.is_empty() {
803            let f = utils::source_name(&failed_node.path, &self.root).display();
804            return Err(format!("Missing profile satisfying settings restrictions for {f}"));
805        }
806
807        // iterate over all the nodes once again and find the one incompatible
808        for node in &nodes {
809            if let Some(req) = project.restrictions.get(&self.node(*node).path)
810                && !all_profiles
811                    .iter()
812                    .any(|(_, (_, settings))| settings.satisfies_restrictions(&**req))
813            {
814                let mut msg = "Found incompatible settings restrictions:\n".white().to_string();
815
816                self.format_imports_list(idx, [*node, failed_node_idx].into(), project, &mut msg)
817                    .unwrap();
818                return Err(msg);
819            }
820        }
821
822        let mut msg = "Found incompatible settings restrictions:\n".white().to_string();
823        self.format_imports_list(idx, nodes.into_iter().collect(), project, &mut msg).unwrap();
824        Err(msg)
825    }
826
827    fn input_nodes_by_language(&self) -> HashMap<L<P>, Vec<usize>> {
828        let mut nodes = HashMap::new();
829
830        for idx in 0..self.edges.num_input_files {
831            nodes.entry(self.nodes[idx].data.language()).or_insert_with(Vec::new).push(idx);
832        }
833
834        nodes
835    }
836
837    /// Returns a map of versions together with the input nodes that are compatible with that
838    /// version.
839    ///
840    /// This will essentially do a DFS on all input sources and their transitive imports and
841    /// checking that all can compiled with the version stated in the input file.
842    ///
843    /// Returns an error message with __all__ input files that don't have compatible imports.
844    ///
845    /// This also attempts to prefer local installations over remote available.
846    /// If `offline` is set to `true` then only already installed.
847    #[allow(clippy::type_complexity)]
848    fn get_input_node_versions<
849        C: Compiler<Language = L<P>>,
850        T: ArtifactOutput<CompilerContract = C::CompilerContract>,
851    >(
852        &self,
853        project: &Project<C, T>,
854    ) -> Result<HashMap<L<P>, HashMap<Version, Vec<usize>>>> {
855        trace!("resolving input node versions");
856
857        let mut resulted_nodes = HashMap::new();
858
859        for (language, nodes) in self.input_nodes_by_language() {
860            // this is likely called by an application and will be eventually printed so we don't
861            // exit on first error, instead gather all the errors and return a bundled
862            // error message instead
863            let mut errors = Vec::new();
864
865            // the sorted list of all versions
866            let all_versions = if project.offline {
867                project
868                    .compiler
869                    .available_versions(&language)
870                    .into_iter()
871                    .filter(|v| v.is_installed())
872                    .collect()
873            } else {
874                project.compiler.available_versions(&language)
875            };
876
877            if all_versions.is_empty() && !nodes.is_empty() {
878                return Err(SolcError::msg(format!(
879                    "Found {language} sources, but no compiler versions are available for it"
880                )));
881            }
882
883            // stores all versions and their nodes that can be compiled
884            let mut versioned_nodes = HashMap::new();
885
886            // stores all files and the versions they're compatible with
887            let mut all_candidates = Vec::with_capacity(self.edges.num_input_files);
888            // walking through the node's dep tree and filtering the versions along the way
889            for idx in nodes {
890                let mut candidates = all_versions.iter().collect::<Vec<_>>();
891                // remove all incompatible versions from the candidates list by checking the node
892                // and all its imports
893                if let Err(err) = self.retain_compatible_versions(idx, &mut candidates, project) {
894                    errors.push(err);
895                } else {
896                    // found viable candidates, pick the most recent version that's already
897                    // installed
898                    let candidate =
899                        if let Some(pos) = candidates.iter().rposition(|v| v.is_installed()) {
900                            candidates[pos]
901                        } else {
902                            candidates.last().expect("not empty; qed.")
903                        }
904                        .clone();
905
906                    // also store all possible candidates to optimize the set
907                    all_candidates.push((idx, candidates.into_iter().collect::<HashSet<_>>()));
908
909                    versioned_nodes
910                        .entry(candidate)
911                        .or_insert_with(|| Vec::with_capacity(1))
912                        .push(idx);
913                }
914            }
915
916            // detected multiple versions but there might still exist a single version that
917            // satisfies all sources
918            if versioned_nodes.len() > 1 {
919                versioned_nodes = Self::resolve_multiple_versions(all_candidates);
920            }
921
922            if versioned_nodes.len() == 1 {
923                trace!(
924                    "found exact solc version for all sources  \"{}\"",
925                    versioned_nodes.keys().next().unwrap()
926                );
927            }
928
929            if errors.is_empty() {
930                trace!("resolved {} versions {:?}", versioned_nodes.len(), versioned_nodes.keys());
931                resulted_nodes.insert(
932                    language,
933                    versioned_nodes
934                        .into_iter()
935                        .map(|(v, nodes)| (Version::from(v), nodes))
936                        .collect(),
937                );
938            } else {
939                let s = errors.join("\n");
940                debug!("failed to resolve versions: {s}");
941                return Err(SolcError::msg(s));
942            }
943        }
944
945        Ok(resulted_nodes)
946    }
947
948    #[allow(clippy::complexity)]
949    fn resolve_settings<
950        C: Compiler<Language = L<P>>,
951        T: ArtifactOutput<CompilerContract = C::CompilerContract>,
952    >(
953        &self,
954        project: &Project<C, T>,
955        input_nodes_versions: HashMap<L<P>, HashMap<Version, Vec<usize>>>,
956    ) -> Result<HashMap<L<P>, HashMap<Version, HashMap<usize, Vec<usize>>>>> {
957        let mut resulted_sources = HashMap::new();
958        let mut errors = Vec::new();
959        for (language, versions) in input_nodes_versions {
960            let mut versioned_sources = HashMap::new();
961            for (version, nodes) in versions {
962                let mut profile_to_nodes = HashMap::new();
963                for idx in nodes {
964                    let mut profile_candidates =
965                        project.settings_profiles().enumerate().collect::<Vec<_>>();
966                    if let Err(err) =
967                        self.retain_compatible_profiles(idx, project, &mut profile_candidates)
968                    {
969                        errors.push(err);
970                    } else {
971                        let (profile_idx, _) = profile_candidates.first().expect("exists");
972                        profile_to_nodes.entry(*profile_idx).or_insert_with(Vec::new).push(idx);
973                    }
974                }
975                versioned_sources.insert(version, profile_to_nodes);
976            }
977            resulted_sources.insert(language, versioned_sources);
978        }
979
980        if errors.is_empty() {
981            Ok(resulted_sources)
982        } else {
983            let s = errors.join("\n");
984            debug!("failed to resolve settings: {s}");
985            Err(SolcError::msg(s))
986        }
987    }
988
989    /// Tries to find the "best" set of versions to nodes, See [Solc version
990    /// auto-detection](#solc-version-auto-detection)
991    ///
992    /// This is a bit inefficient but is fine, the max. number of versions is ~80 and there's
993    /// a high chance that the number of source files is <50, even for larger projects.
994    fn resolve_multiple_versions(
995        all_candidates: Vec<(usize, HashSet<&CompilerVersion>)>,
996    ) -> HashMap<CompilerVersion, Vec<usize>> {
997        // returns the intersection as sorted set of nodes
998        fn intersection<'a>(
999            mut sets: Vec<&HashSet<&'a CompilerVersion>>,
1000        ) -> Vec<&'a CompilerVersion> {
1001            if sets.is_empty() {
1002                return Vec::new();
1003            }
1004
1005            let mut result = sets.pop().cloned().expect("not empty; qed.");
1006            if !sets.is_empty() {
1007                result.retain(|item| sets.iter().all(|set| set.contains(item)));
1008            }
1009
1010            let mut v = result.into_iter().collect::<Vec<_>>();
1011            v.sort_unstable();
1012            v
1013        }
1014
1015        /// returns the highest version that is installed
1016        /// if the candidates set only contains uninstalled versions then this returns the highest
1017        /// uninstalled version
1018        fn remove_candidate(candidates: &mut Vec<&CompilerVersion>) -> CompilerVersion {
1019            debug_assert!(!candidates.is_empty());
1020
1021            if let Some(pos) = candidates.iter().rposition(|v| v.is_installed()) {
1022                candidates.remove(pos)
1023            } else {
1024                candidates.pop().expect("not empty; qed.")
1025            }
1026            .clone()
1027        }
1028
1029        let all_sets = all_candidates.iter().map(|(_, versions)| versions).collect();
1030
1031        // find all versions that satisfy all nodes
1032        let mut intersection = intersection(all_sets);
1033        if !intersection.is_empty() {
1034            let exact_version = remove_candidate(&mut intersection);
1035            let all_nodes = all_candidates.into_iter().map(|(node, _)| node).collect();
1036            trace!("resolved solc version compatible with all sources  \"{}\"", exact_version);
1037            return HashMap::from([(exact_version, all_nodes)]);
1038        }
1039
1040        // no version satisfies all nodes
1041        let mut versioned_nodes: HashMap<_, _> = HashMap::new();
1042
1043        // try to minimize the set of versions, this is guaranteed to lead to `versioned_nodes.len()
1044        // > 1` as no solc version exists that can satisfy all sources
1045        for (node, versions) in all_candidates {
1046            // need to sort them again
1047            let mut versions = versions.into_iter().collect::<Vec<_>>();
1048            versions.sort_unstable();
1049
1050            let candidate = if let Some(idx) =
1051                versions.iter().rposition(|v| versioned_nodes.contains_key(*v))
1052            {
1053                // use a version that's already in the set
1054                versions.remove(idx).clone()
1055            } else {
1056                // use the highest version otherwise
1057                remove_candidate(&mut versions)
1058            };
1059
1060            versioned_nodes.entry(candidate).or_insert_with(|| Vec::with_capacity(1)).push(node);
1061        }
1062
1063        trace!(
1064            "no solc version can satisfy all source files, resolved multiple versions  \"{:?}\"",
1065            versioned_nodes.keys()
1066        );
1067
1068        versioned_nodes
1069    }
1070}
1071
1072/// An iterator over a node and its dependencies
1073#[derive(Debug)]
1074pub struct NodesIter<'a, P: SourceParser> {
1075    /// stack of nodes
1076    stack: VecDeque<usize>,
1077    visited: HashSet<usize>,
1078    graph: &'a GraphEdges<P>,
1079}
1080
1081impl<'a, P: SourceParser> NodesIter<'a, P> {
1082    fn new(start: usize, graph: &'a GraphEdges<P>) -> Self {
1083        Self { stack: VecDeque::from([start]), visited: HashSet::new(), graph }
1084    }
1085}
1086
1087impl<P: SourceParser> Iterator for NodesIter<'_, P> {
1088    type Item = usize;
1089    fn next(&mut self) -> Option<Self::Item> {
1090        let node = self.stack.pop_front()?;
1091
1092        if self.visited.insert(node) {
1093            // push the node's direct dependencies to the stack if we haven't visited it already
1094            self.stack.extend(self.graph.imported_nodes(node).iter().copied());
1095        }
1096        Some(node)
1097    }
1098}
1099
1100#[derive(Debug)]
1101pub struct Node<S> {
1102    /// path of the solidity  file
1103    path: PathBuf,
1104    /// content of the solidity file
1105    source: Source,
1106    /// parsed data
1107    pub data: S,
1108}
1109
1110impl<S> Node<S> {
1111    pub const fn new(path: PathBuf, source: Source, data: S) -> Self {
1112        Self { path, source, data }
1113    }
1114
1115    pub fn map_data<T>(self, f: impl FnOnce(S) -> T) -> Node<T> {
1116        Node::new(self.path, self.source, f(self.data))
1117    }
1118}
1119
1120impl<S: ParsedSource> Node<S> {
1121    /// Reads the content of the file and returns a [Node] containing relevant information
1122    pub fn read(file: &Path) -> Result<Self> {
1123        let source = Source::read_(file)?;
1124        Self::parse(file, source)
1125    }
1126
1127    pub fn parse(file: &Path, source: Source) -> Result<Self> {
1128        let data = S::parse(source.as_ref(), file)?;
1129        Ok(Self::new(file.to_path_buf(), source, data))
1130    }
1131
1132    /// Returns the path of the file.
1133    pub fn path(&self) -> &Path {
1134        &self.path
1135    }
1136
1137    /// Returns the contents of the file.
1138    pub fn content(&self) -> &str {
1139        &self.source.content
1140    }
1141
1142    pub fn unpack(&self) -> (&Path, &Source) {
1143        (&self.path, &self.source)
1144    }
1145}
1146
1147/// Helper type for formatting a node
1148pub(crate) struct DisplayNode<'a, S> {
1149    node: &'a Node<S>,
1150    root: &'a PathBuf,
1151}
1152
1153impl<S: ParsedSource> fmt::Display for DisplayNode<'_, S> {
1154    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1155        let path = utils::source_name(&self.node.path, self.root);
1156        write!(f, "{}", path.display())?;
1157        if let Some(v) = self.node.data.version_req() {
1158            write!(f, " {v}")?;
1159        }
1160        Ok(())
1161    }
1162}
1163
1164/// Errors thrown when checking the solc version of a file
1165#[derive(Debug, thiserror::Error)]
1166#[allow(dead_code)]
1167enum SourceVersionError {
1168    #[error("Failed to parse solidity version {0}: {1}")]
1169    InvalidVersion(String, SolcError),
1170    #[error("No solc version exists that matches the version requirement: {0}")]
1171    NoMatchingVersion(VersionReq),
1172    #[error("No solc version installed that matches the version requirement: {0}")]
1173    NoMatchingVersionOffline(VersionReq),
1174}
1175
1176#[cfg(test)]
1177mod tests {
1178    use super::*;
1179
1180    #[test]
1181    fn can_resolve_hardhat_dependency_graph() {
1182        let root = Path::new(env!("CARGO_MANIFEST_DIR")).join("../../test-data/hardhat-sample");
1183        let paths = ProjectPathsConfig::hardhat(&root).unwrap();
1184
1185        let graph = Graph::<SolParser>::resolve(&paths).unwrap();
1186
1187        assert_eq!(graph.edges.num_input_files, 1);
1188        assert_eq!(graph.files().len(), 2);
1189
1190        assert_eq!(
1191            graph.files().clone(),
1192            HashMap::from([
1193                (paths.sources.join("Greeter.sol"), 0),
1194                (paths.root.join("node_modules/hardhat/console.sol"), 1),
1195            ])
1196        );
1197    }
1198
1199    #[test]
1200    fn can_resolve_dapp_dependency_graph() {
1201        let root = Path::new(env!("CARGO_MANIFEST_DIR")).join("../../test-data/dapp-sample");
1202        let paths = ProjectPathsConfig::dapptools(&root).unwrap();
1203
1204        let graph = Graph::<SolParser>::resolve(&paths).unwrap();
1205
1206        assert_eq!(graph.edges.num_input_files, 2);
1207        assert_eq!(graph.files().len(), 3);
1208        assert_eq!(
1209            graph.files().clone(),
1210            HashMap::from([
1211                (paths.sources.join("Dapp.sol"), 0),
1212                (paths.sources.join("Dapp.t.sol"), 1),
1213                (paths.root.join("lib/ds-test/src/test.sol"), 2),
1214            ])
1215        );
1216
1217        let dapp_test = graph.node(1);
1218        assert_eq!(dapp_test.path, paths.sources.join("Dapp.t.sol"));
1219        assert_eq!(
1220            dapp_test.data.imports.iter().map(|i| i.data().path()).collect::<Vec<&Path>>(),
1221            vec![Path::new("ds-test/test.sol"), Path::new("./Dapp.sol")]
1222        );
1223        assert_eq!(graph.imported_nodes(1).to_vec(), vec![2, 0]);
1224    }
1225
1226    #[test]
1227    fn can_print_dapp_sample_graph() {
1228        let root = Path::new(env!("CARGO_MANIFEST_DIR")).join("../../test-data/dapp-sample");
1229        let paths = ProjectPathsConfig::dapptools(&root).unwrap();
1230        let graph = Graph::<SolParser>::resolve(&paths).unwrap();
1231        let mut out = Vec::<u8>::new();
1232        tree::print(&graph, &Default::default(), &mut out).unwrap();
1233
1234        if !cfg!(windows) {
1235            assert_eq!(
1236                "
1237src/Dapp.sol >=0.6.6
1238src/Dapp.t.sol >=0.6.6
1239├── lib/ds-test/src/test.sol >=0.4.23
1240└── src/Dapp.sol >=0.6.6
1241"
1242                .trim_start()
1243                .as_bytes()
1244                .to_vec(),
1245                out
1246            );
1247        }
1248
1249        graph.edges.parser().compiler.enter(|c| {
1250            assert_eq!(c.gcx().sources.len(), 3);
1251        });
1252    }
1253
1254    #[test]
1255    #[cfg(not(target_os = "windows"))]
1256    fn can_print_hardhat_sample_graph() {
1257        let root = Path::new(env!("CARGO_MANIFEST_DIR")).join("../../test-data/hardhat-sample");
1258        let paths = ProjectPathsConfig::hardhat(&root).unwrap();
1259        let graph = Graph::<SolParser>::resolve(&paths).unwrap();
1260        let mut out = Vec::<u8>::new();
1261        tree::print(&graph, &Default::default(), &mut out).unwrap();
1262        assert_eq!(
1263            "contracts/Greeter.sol >=0.6.0
1264└── node_modules/hardhat/console.sol >=0.4.22, <0.9.0
1265",
1266            String::from_utf8(out).unwrap()
1267        );
1268    }
1269
1270    #[test]
1271    #[cfg(feature = "svm-solc")]
1272    fn test_print_unresolved() {
1273        use crate::{ProjectBuilder, solc::SolcCompiler};
1274
1275        let root =
1276            Path::new(env!("CARGO_MANIFEST_DIR")).join("../../test-data/incompatible-pragmas");
1277        let paths = ProjectPathsConfig::dapptools(&root).unwrap();
1278        let graph = Graph::<SolParser>::resolve(&paths).unwrap();
1279        let Err(SolcError::Message(err)) = graph.get_input_node_versions(
1280            &ProjectBuilder::<SolcCompiler>::default()
1281                .paths(paths)
1282                .build(SolcCompiler::AutoDetect)
1283                .unwrap(),
1284        ) else {
1285            panic!("expected error");
1286        };
1287
1288        snapbox::assert_data_eq!(
1289            err,
1290            snapbox::str![[r#"
1291Found incompatible versions:
1292src/A.sol =0.8.25 imports:
1293    src/B.sol
1294    src/C.sol =0.7.0
1295"#]]
1296        );
1297    }
1298
1299    #[cfg(target_os = "linux")]
1300    #[test]
1301    fn can_read_different_case() {
1302        use crate::resolver::parse::SolData;
1303        use std::fs::{self, create_dir_all};
1304        use utils::tempdir;
1305
1306        let tmp_dir = tempdir("out").unwrap();
1307        let path = tmp_dir.path().join("forge-std");
1308        create_dir_all(&path).unwrap();
1309        let existing = path.join("Test.sol");
1310        let non_existing = path.join("test.sol");
1311        fs::write(
1312            existing,
1313            "
1314pragma solidity ^0.8.10;
1315contract A {}
1316        ",
1317        )
1318        .unwrap();
1319
1320        assert!(!non_existing.exists());
1321
1322        let found = crate::resolver::Node::<SolData>::read(&non_existing).unwrap_err();
1323        matches!(found, SolcError::ResolveCaseSensitiveFileName { .. });
1324    }
1325}