Skip to main content

foundry_common/preprocessor/
mod.rs

1use crate::{errors::convert_solar_errors, fs::normalize_path};
2use foundry_compilers::{
3    ProjectPathsConfig, SourceParser, Updates, apply_updates,
4    artifacts::{EvmVersion, SolcLanguage},
5    error::Result,
6    multi::{MultiCompiler, MultiCompilerInput, MultiCompilerLanguage},
7    project::{
8        NativeDependencies, NativeDependencyState, Preprocessor, PreprocessorState,
9        merge_native_dependencies,
10    },
11    resolver::{
12        Graph,
13        parse::{SolData, SolParser},
14    },
15    solc::{SolcCompiler, SolcVersionedInput},
16};
17use solar::parse::{ast::Span, interface::SourceMap};
18use std::{
19    collections::{BTreeSet, HashSet},
20    ops::{ControlFlow, Range},
21    path::{Path, PathBuf},
22    sync::{
23        Arc, Mutex,
24        atomic::{AtomicBool, Ordering},
25    },
26};
27
28mod data;
29use data::{collect_preprocessor_data, create_deploy_helpers};
30
31pub use data::is_deploy_helper_path;
32
33mod deps;
34use deps::{ConstructorContext, PreprocessorDependencies, remove_bytecode_dependencies};
35
36/// Preprocessor that replaces static bytecode linking in tests and scripts (`new Contract`) with
37/// dynamic linkage through (`Vm.create*`).
38///
39/// This allows for more efficient caching when iterating on tests.
40///
41/// See <https://github.com/foundry-rs/foundry/pull/10010>.
42#[derive(Clone, Debug, Default)]
43pub struct DynamicTestLinkingPreprocessor {
44    /// Set when preprocessing changes any compiler input. Shared by clones.
45    rewrote_sources: Arc<AtomicBool>,
46    /// One warning per compiler job that disabled DTL; printed after a successful compile.
47    /// Skipping the print when solc fails avoids duplicating solc's own diagnostics
48    /// (see <https://github.com/foundry-rs/foundry/issues/17913>).
49    pending_disable_warnings: Arc<Mutex<Vec<String>>>,
50}
51
52impl DynamicTestLinkingPreprocessor {
53    /// Returns whether preprocessing changed any compiler input.
54    pub fn rewrote_sources(&self) -> bool {
55        self.rewrote_sources.load(Ordering::Relaxed)
56    }
57
58    /// Takes the pending "dynamic test linking disabled" warnings.
59    pub fn take_disable_warnings(&self) -> Vec<String> {
60        self.pending_disable_warnings
61            .lock()
62            .map(|mut g| std::mem::take(&mut *g))
63            .unwrap_or_default()
64    }
65}
66
67impl Preprocessor<SolcCompiler> for DynamicTestLinkingPreprocessor {
68    fn cache_version(&self) -> u64 {
69        14
70    }
71
72    fn preprocess(
73        &self,
74        solc: &SolcCompiler,
75        input: &mut SolcVersionedInput,
76        paths: &ProjectPathsConfig<SolcLanguage>,
77        mocks: &mut HashSet<PathBuf>,
78    ) -> Result<()> {
79        let source_units = input.input.sources.keys().cloned().collect::<Vec<_>>();
80        self.preprocess_with_dependencies(
81            solc,
82            input,
83            paths,
84            mocks,
85            &mut PreprocessorState::untracked(),
86            &source_units,
87        )
88    }
89
90    #[instrument(name = "DynamicTestLinkingPreprocessor::preprocess", skip_all)]
91    fn preprocess_with_dependencies(
92        &self,
93        solc: &SolcCompiler,
94        input: &mut SolcVersionedInput,
95        paths: &ProjectPathsConfig<SolcLanguage>,
96        mocks: &mut HashSet<PathBuf>,
97        preprocessor_state: &mut PreprocessorState,
98        source_units: &[PathBuf],
99    ) -> Result<()> {
100        // Skip if we are not preprocessing any tests or scripts. Avoids unnecessary AST parsing.
101        if !input.input.sources.iter().any(|(path, _)| paths.is_test_or_script(path)) {
102            trace!("no tests or scripts to preprocess");
103            return Ok(());
104        }
105
106        let constructor_context = ConstructorContext {
107            abi_coder_v2: input.version >= semver::Version::new(0, 8, 0),
108            // Without an explicit target, preserve native validation rather than guessing the
109            // selected compiler's default EVM version.
110            supports_create2: input
111                .input
112                .settings
113                .evm_version
114                .is_some_and(|version| version >= EvmVersion::Constantinople),
115        };
116        // Avoid the via-IR address-return stack error with uint160, retaining canonical ABI checks.
117        // Keep this compatibility workaround scoped to the verified compiler releases.
118        // Specific compiler selection normalizes away prerelease metadata in the input version.
119        let use_uint160_return = input.input.settings.via_ir == Some(true)
120            && input.version.pre.is_empty()
121            && !matches!(solc, SolcCompiler::Specific(solc) if !solc.version.pre.is_empty())
122            && matches!(
123                (input.version.major, input.version.minor, input.version.patch),
124                (0, 8, 15..=25)
125            );
126        let original_sources = input.input.sources.clone();
127        let mut updates = Updates::default();
128        let mut parser_paths = paths.clone();
129        parser_paths.include_paths.extend(input.cli_settings.include_paths.iter().cloned());
130        let mut compiler =
131            foundry_compilers::resolver::parse::SolParser::new(parser_paths.with_language_ref())
132                .into_compiler();
133        // Solc validates the input version; Solar only analyzes deployment dependencies.
134        compiler.dcx().set_allowed_diagnostic_codes(["5333".into()]);
135        let result = compiler.enter_mut(|compiler| -> solar::interface::Result {
136            let mut pcx = compiler.parse();
137
138            // Add the sources into the context.
139            // Include all sources in the source map so as to not re-load them from disk, but only
140            // parse and preprocess tests and scripts.
141            let mut preprocessed_paths = vec![];
142            let mut script_paths = HashSet::new();
143            let sources = &mut input.input.sources;
144            for (path, source) in sources.iter() {
145                if let Ok(src_file) = compiler
146                    .sess()
147                    .source_map()
148                    .new_source_file(path.clone(), source.content.as_str())
149                    && paths.is_test_or_script(path)
150                {
151                    pcx.add_file(src_file);
152                    if paths.is_script(path) {
153                        script_paths.insert(path.clone());
154                    }
155                    preprocessed_paths.push(path.clone());
156                }
157            }
158
159            // Parse and preprocess.
160            pcx.parse();
161            let ControlFlow::Continue(()) = compiler.lower_asts()? else { return Ok(()) };
162            compiler.dcx().has_errors()?;
163            let gcx = compiler.gcx();
164            // Collect tests and scripts dependencies and identify mock contracts.
165            // Script paths are passed separately so salted new-expressions are left untouched
166            // (Foundry's broadcast redirects native CREATE2 through the deterministic factory,
167            // but vm.deployCode runs at a deeper depth and bypasses that redirect).
168            let deps = PreprocessorDependencies::new(
169                gcx,
170                constructor_context,
171                &preprocessed_paths,
172                &script_paths,
173                paths,
174                source_units,
175                mocks,
176                preprocessor_state,
177            );
178            // Collect data of source contracts referenced in tests and scripts.
179            let data = collect_preprocessor_data(
180                gcx,
181                &deps.referenced_contracts,
182                &paths.root,
183                source_units,
184            );
185
186            // Extend existing sources with preprocessor deploy helper sources.
187            sources.extend(create_deploy_helpers(gcx, constructor_context, &data));
188
189            // Generate and apply preprocessor source updates.
190            updates =
191                remove_bytecode_dependencies(gcx, &deps, &data, &paths.root, use_uint160_return);
192            apply_updates(sources, updates.clone());
193
194            Ok(())
195        });
196
197        let diagnostics = convert_solar_errors(compiler.dcx());
198        if result.is_err() || diagnostics.is_err() {
199            let reason = diagnostics.err().map(|err| err.to_string());
200            if let Some(err) = &reason {
201                warn!(%err, "dynamic test linking analysis failed; using native bytecode");
202            } else {
203                warn!("dynamic test linking analysis failed; using native bytecode");
204            }
205            input.input.sources = original_sources;
206            let dependencies = native_import_dependencies(&parser_paths, &input.input.sources);
207            let affected = dependencies.len();
208            for (path, state) in dependencies {
209                if preprocessor_state.update(path.clone(), Some(state)) {
210                    mocks.remove(&path);
211                }
212            }
213            if affected > 0 {
214                let reason = reason
215                    .as_deref()
216                    .and_then(solar_error_summary)
217                    .unwrap_or_else(|| "Solar analysis failed".to_string());
218                // Defer printing until after solc succeeds so a source error is not
219                // reported twice (Solar wording here, then solc with a location).
220                if let Ok(mut warnings) = self.pending_disable_warnings.lock() {
221                    warnings.push(format!(
222                        "dynamic test linking disabled for {affected} files: {reason}"
223                    ));
224                }
225            }
226        } else {
227            // Lets source locations reported by the compiler be mapped back to the files on disk.
228            preprocessor_state.record_source_updates(&updates);
229            if input.input.sources != original_sources {
230                self.rewrote_sources.store(true, Ordering::Relaxed);
231            }
232        }
233
234        Ok(())
235    }
236
237    fn is_source_context_sensitive(
238        &self,
239        source: &Path,
240        paths: &ProjectPathsConfig<SolcLanguage>,
241    ) -> bool {
242        // Context changes can alter rewriting eligibility even for previously native deployments.
243        paths.is_test_or_script(source)
244    }
245
246    fn is_source_context_change_relevant(
247        &self,
248        previous: &BTreeSet<PathBuf>,
249        current: &BTreeSet<PathBuf>,
250    ) -> bool {
251        if !current.is_subset(previous) {
252            return true;
253        }
254        // Rewriting reads source units only to require a unique artifact path suffix and to avoid
255        // deploy helper path collisions. A removal affects neither unless the removed unit is a
256        // helper path or ends with another unit's path.
257        previous.difference(current).any(|removed| {
258            let mut suffix = removed.components();
259            is_deploy_helper_path(removed)
260                || std::iter::from_fn(|| suffix.next().map(|_| suffix.as_path()))
261                    .any(|suffix| !suffix.as_os_str().is_empty() && previous.contains(suffix))
262        })
263    }
264}
265
266impl Preprocessor<MultiCompiler> for DynamicTestLinkingPreprocessor {
267    fn cache_version(&self) -> u64 {
268        <Self as Preprocessor<SolcCompiler>>::cache_version(self)
269    }
270
271    fn preprocess(
272        &self,
273        compiler: &MultiCompiler,
274        input: &mut MultiCompilerInput,
275        paths: &ProjectPathsConfig<MultiCompilerLanguage>,
276        mocks: &mut HashSet<PathBuf>,
277    ) -> Result<()> {
278        let source_units = match input {
279            MultiCompilerInput::Solc(input) => {
280                input.input.sources.keys().cloned().collect::<Vec<_>>()
281            }
282            _ => Vec::new(),
283        };
284        self.preprocess_with_dependencies(
285            compiler,
286            input,
287            paths,
288            mocks,
289            &mut PreprocessorState::untracked(),
290            &source_units,
291        )
292    }
293
294    fn preprocess_with_dependencies(
295        &self,
296        compiler: &MultiCompiler,
297        input: &mut MultiCompilerInput,
298        paths: &ProjectPathsConfig<MultiCompilerLanguage>,
299        mocks: &mut HashSet<PathBuf>,
300        preprocessor_state: &mut PreprocessorState,
301        source_units: &[PathBuf],
302    ) -> Result<()> {
303        // Preprocess only Solc compilers.
304        let MultiCompilerInput::Solc(input) = input else { return Ok(()) };
305
306        let Some(solc) = &compiler.solc else { return Ok(()) };
307
308        let paths = paths.clone().with_language::<SolcLanguage>();
309        <Self as Preprocessor<SolcCompiler>>::preprocess_with_dependencies(
310            self,
311            solc,
312            input,
313            &paths,
314            mocks,
315            preprocessor_state,
316            source_units,
317        )
318    }
319
320    fn is_source_context_sensitive(
321        &self,
322        source: &Path,
323        paths: &ProjectPathsConfig<MultiCompilerLanguage>,
324    ) -> bool {
325        <Self as Preprocessor<SolcCompiler>>::is_source_context_sensitive(
326            self,
327            source,
328            paths.with_language_ref(),
329        )
330    }
331
332    fn is_source_context_change_relevant(
333        &self,
334        previous: &BTreeSet<PathBuf>,
335        current: &BTreeSet<PathBuf>,
336    ) -> bool {
337        <Self as Preprocessor<SolcCompiler>>::is_source_context_change_relevant(
338            self, previous, current,
339        )
340    }
341}
342
343/// Tracks transitive imports of unmodified tests and scripts after analysis fails.
344fn native_import_dependencies(
345    paths: &ProjectPathsConfig<SolcLanguage>,
346    sources: &foundry_compilers::artifacts::Sources,
347) -> NativeDependencies {
348    let Ok(graph) = Graph::<SolParser>::resolve_sources(paths, sources.clone()) else {
349        return sources
350            .keys()
351            .filter(|path| paths.is_test_or_script(path))
352            .map(|path| {
353                (normalize_path(&paths.root.join(path)), NativeDependencyState::Conservative)
354            })
355            .collect();
356    };
357
358    // Keep conservative invalidation only where import extraction itself is incomplete.
359    let mut incomplete = HashSet::new();
360    if graph.nodes.iter().any(|node| node.data.parse_result.is_err()) {
361        incomplete.extend(
362            graph
363                .nodes
364                .iter()
365                .filter(|node| SolData::parse(node.content(), node.path()).parse_result.is_err())
366                .map(|node| normalize_path(node.path())),
367        );
368    }
369    let graph_paths = graph.files().keys().cloned().collect::<HashSet<_>>();
370    let (_, edges) = graph.into_sources();
371    incomplete.extend(edges.unresolved_imports().iter().map(|(_, path)| normalize_path(path)));
372
373    let mut dependencies = NativeDependencies::new();
374    for path in sources.keys().filter(|path| paths.is_test_or_script(path)) {
375        let graph_path = paths.root.join(path);
376        let path = normalize_path(&graph_path);
377        let state = if graph_paths.contains(&graph_path) {
378            let imports =
379                edges.imports(&graph_path).into_iter().map(normalize_path).collect::<BTreeSet<_>>();
380            if incomplete.contains(&path) || incomplete.iter().any(|path| imports.contains(path)) {
381                NativeDependencyState::Conservative
382            } else {
383                NativeDependencyState::Known(imports)
384            }
385        } else {
386            NativeDependencyState::Conservative
387        };
388        merge_native_dependencies(&mut dependencies, NativeDependencies::from([(path, state)]));
389    }
390    dependencies
391}
392
393/// Extracts the first rendered Solar error header without ANSI codes.
394fn solar_error_summary(reason: &str) -> Option<String> {
395    anstream::adapter::strip_str(reason)
396        .to_string()
397        .lines()
398        .map(str::trim)
399        .find(|line| line.starts_with("error:") || line.starts_with("error["))
400        .map(str::to_string)
401}
402
403/// Returns the range of the given span in the source map.
404#[track_caller]
405fn span_to_range(source_map: &SourceMap, span: Span) -> Range<usize> {
406    source_map.span_to_range(span).unwrap()
407}
408
409#[cfg(test)]
410mod tests {
411    use super::*;
412    use alloy_primitives::keccak256;
413    use foundry_compilers::{
414        Compiler, CompilerInput,
415        artifacts::Source,
416        solc::{Solc, SolcSettings},
417    };
418    use semver::Version;
419
420    fn input() -> (tempfile::TempDir, ProjectPathsConfig<SolcLanguage>, SolcVersionedInput) {
421        let root = tempfile::tempdir().unwrap();
422        let paths = ProjectPathsConfig::builder().root(root.path()).build().unwrap();
423        let sources = [
424            ("src/Dep.sol", "contract Dep {}"),
425            ("test/Mock.sol", "import '../src/Dep.sol'; contract Mock is Dep {}"),
426            (
427                "test/Deploy.sol",
428                "import '../src/Dep.sol'; contract Deploy { bytes constant CODE = type(Dep).creationCode; bytes32 constant CODE_HASH = keccak256(type(Dep).creationCode); bytes32 immutable codeHash = keccak256(type(Dep).creationCode); function deploy() public returns (Dep) { return new Dep(); } function native() public pure returns (bytes memory) { return type(Dep).creationCode; } }",
429            ),
430        ]
431        .into_iter()
432        .map(|(path, content)| (PathBuf::from(path), Source::new(content)))
433        .collect();
434        let input = SolcVersionedInput::build(
435            sources,
436            SolcSettings::default(),
437            SolcLanguage::Solidity,
438            Version::new(0, 8, 30),
439        );
440        (root, paths, input)
441    }
442
443    fn assert_preprocessed(
444        paths: &ProjectPathsConfig<SolcLanguage>,
445        input: &SolcVersionedInput,
446        mocks: &HashSet<PathBuf>,
447    ) {
448        let source = &input.input.sources[&PathBuf::from("test/Deploy.sol")].content;
449        assert!(!source.contains("return new Dep();"), "eligible deployment was not rewritten");
450        assert!(source.contains("type(Dep).creationCode"), "native dependency was lost");
451        assert!(
452            source.contains("bytes constant CODE = type(Dep).creationCode"),
453            "constant creation code was rewritten"
454        );
455        assert!(
456            source.contains("bytes32 constant CODE_HASH = keccak256(type(Dep).creationCode)"),
457            "constant creation code hash was rewritten"
458        );
459        assert!(
460            !source.contains("immutable codeHash = keccak256(type(Dep).creationCode)"),
461            "constant preservation leaked into the immutable initializer"
462        );
463        assert!(
464            source.contains("immutable codeHash = keccak256(VmContractHelper"),
465            "immutable creation code was not rewritten"
466        );
467        assert!(mocks.contains(&paths.root.join("test/Mock.sol")));
468    }
469
470    #[test]
471    fn helper_sources_are_stable_across_compiler_jobs() {
472        let (_root, paths, mut input) = input();
473        input.input.sources = [
474            ("src/Dep.sol", "contract Dep { constructor(uint256 value) {} }".to_string()),
475            ("test/Deploy.sol", "import '../src/Dep.sol'; contract Deploy { function deploy() public { new Dep(7); } }".to_string()),
476        ].into_iter().map(|(path, source)| (PathBuf::from(path), Source::new(source))).collect();
477        let mut wider = input.clone();
478        // These declarations would also affect collision avoidance if it inspected unrelated files.
479        let dep_id = format!("{:x}", keccak256("src/Dep.sol:Dep"));
480        let test_id = format!("{:x}", keccak256("test/Deploy.sol:Deploy"));
481        wider.input.sources.insert(PathBuf::from("test/AAA.sol"), Source::new(format!(
482            "contract Extra {{}} interface VmContractHelper{test_id} {{}} contract DeployHelper{dep_id} {{}} function encodeArgs{dep_id}() pure {{}}"
483        )));
484        for input in [&mut input, &mut wider] {
485            <DynamicTestLinkingPreprocessor as Preprocessor<SolcCompiler>>::preprocess(
486                &DynamicTestLinkingPreprocessor::default(),
487                &SolcCompiler::default(),
488                input,
489                &paths,
490                &mut HashSet::new(),
491            )
492            .unwrap();
493        }
494        for (path, source) in &input.input.sources {
495            assert_eq!(source.content, wider.input.sources[path].content, "{path:?}");
496        }
497    }
498
499    #[test]
500    fn direct_solc_preprocess_tracks_mocks_without_cache_context() {
501        let (_root, paths, mut input) = input();
502        let mut mocks = HashSet::new();
503        <DynamicTestLinkingPreprocessor as Preprocessor<SolcCompiler>>::preprocess(
504            &DynamicTestLinkingPreprocessor::default(),
505            &SolcCompiler::default(),
506            &mut input,
507            &paths,
508            &mut mocks,
509        )
510        .unwrap();
511        assert_preprocessed(&paths, &input, &mocks);
512    }
513
514    #[test]
515    fn direct_multi_preprocess_tracks_mocks_without_cache_context() {
516        let (_root, paths, input) = input();
517        let mut input = MultiCompilerInput::Solc(Box::new(input));
518        let mut mocks = HashSet::new();
519        <DynamicTestLinkingPreprocessor as Preprocessor<MultiCompiler>>::preprocess(
520            &DynamicTestLinkingPreprocessor::default(),
521            &MultiCompiler { solc: Some(SolcCompiler::default()), vyper: None },
522            &mut input,
523            paths.with_language_ref(),
524            &mut mocks,
525        )
526        .unwrap();
527        let MultiCompilerInput::Solc(input) = input else { unreachable!() };
528        assert_preprocessed(&paths, &input, &mocks);
529    }
530
531    #[test]
532    fn constructor_abi_context_preserves_source_pragmas_and_version_defaults() {
533        for (minor, pragma, native) in [
534            (7, "", true),
535            (8, "", false),
536            (8, "pragma abicoder /* comment */ v1;", true),
537            (7, "pragma abicoder v2;", false),
538            (7, "pragma experimental ABIEncoderV2;", false),
539        ] {
540            let (_root, paths, mut input) = input();
541            input.version = Version::new(0, minor, 6);
542            input.input.sources = [
543                ("src/Dep.sol", "contract Dep { constructor(uint256 n) {} }".to_string()),
544                ("test/Deploy.sol", format!("{pragma} import '../src/Dep.sol'; contract Deploy {{ function deploy() public {{ new Dep(7); }} }}")),
545                // A pragma in another source must not change this source's ABI context.
546                ("test/Other.sol", "pragma abicoder v2; import '../src/Dep.sol'; contract Other { function deploy() public { new Dep(7); } }".to_string()),
547            ].into_iter().map(|(path, source)| (PathBuf::from(path), Source::new(source))).collect();
548            <DynamicTestLinkingPreprocessor as Preprocessor<SolcCompiler>>::preprocess(
549                &DynamicTestLinkingPreprocessor::default(),
550                &SolcCompiler::default(),
551                &mut input,
552                &paths,
553                &mut HashSet::new(),
554            )
555            .unwrap();
556            let source = &input.input.sources[&PathBuf::from("test/Deploy.sol")].content;
557            assert_eq!(source.contains("new Dep(7)"), native, "0.{minor}.6 {pragma}");
558            let other = &input.input.sources[&PathBuf::from("test/Other.sol")].content;
559            assert!(!other.contains("new Dep(7)"), "unrelated v2 source was not rewritten");
560        }
561    }
562
563    #[test]
564    fn constructor_salt_uses_compiler_evm_target() {
565        for (evm_version, native) in [
566            (None, true),
567            (Some(EvmVersion::Byzantium), true),
568            (Some(EvmVersion::Constantinople), false),
569            (Some(EvmVersion::Prague), false),
570        ] {
571            let (_root, paths, mut input) = input();
572            input.input.settings.evm_version = evm_version;
573            input.input.sources.insert(
574                PathBuf::from("test/Deploy.sol"),
575                Source::new("pragma abicoder v1; import '../src/Dep.sol'; contract Deploy { function deploy() public { new Dep{salt: bytes32(0)}(); } function plain() public { new Dep(); } }"),
576            );
577            <DynamicTestLinkingPreprocessor as Preprocessor<SolcCompiler>>::preprocess(
578                &DynamicTestLinkingPreprocessor::default(),
579                &SolcCompiler::default(),
580                &mut input,
581                &paths,
582                &mut HashSet::new(),
583            )
584            .unwrap();
585            let source = &input.input.sources[&PathBuf::from("test/Deploy.sol")].content;
586            assert_eq!(source.contains("new Dep{salt:"), native, "{evm_version:?}");
587            assert!(
588                !source.contains("new Dep();"),
589                "ordinary parameterless CREATE was not rewritten"
590            );
591        }
592    }
593
594    #[test]
595    fn constructor_call_options_are_preserved() {
596        let (_root, paths, mut input) = input();
597        input.input.sources.insert(
598            PathBuf::from("src/Dep.sol"),
599            Source::new("contract Dep { constructor() payable {} }"),
600        );
601        input.input.sources.insert(
602            PathBuf::from("test/Deploy.sol"),
603            Source::new("import '../src/Dep.sol'; contract Deploy { function deploy() public { new Dep{value: 1, salt: bytes32(7)}(); } }"),
604        );
605        <DynamicTestLinkingPreprocessor as Preprocessor<SolcCompiler>>::preprocess(
606            &DynamicTestLinkingPreprocessor::default(),
607            &SolcCompiler::default(),
608            &mut input,
609            &paths,
610            &mut HashSet::new(),
611        )
612        .unwrap();
613        let source = &input.input.sources[&PathBuf::from("test/Deploy.sol")].content;
614        assert!(!source.contains("new Dep{value:"), "deployment was not rewritten");
615        assert!(source.contains("_value: 1"), "value option was not preserved");
616        assert!(source.contains("_salt: bytes32(7)"), "salt option was not preserved");
617    }
618
619    #[test]
620    fn return_data_fallback_follows_helpers_without_disabling_unrelated_contracts() {
621        for (helper, declarations, expression) in [
622            (
623                "function size(uint256) view returns (uint256 n) { assembly { n := returndatasize() } }",
624                "import * as R from '../src/Read.sol';",
625                "R.size(0)",
626            ),
627            (
628                "function size(uint256) view returns (uint256 n) { assembly { n := returndatasize() } }",
629                "import {size} from '../src/Read.sol'; using {size} for uint256;",
630                "uint256(0).size()",
631            ),
632            (
633                "function size(uint256) view returns (uint256 n) { assembly { n := returndatasize() } }",
634                "import {size as readSize} from '../src/Read.sol'; using {readSize} for uint256;",
635                "uint256(0).readSize()",
636            ),
637            (
638                "library R { function size(uint256) internal view returns (uint256 n) { assembly { n := returndatasize() } } }",
639                "import {R} from '../src/Read.sol'; using R for uint256;",
640                "uint256(0).size()",
641            ),
642            (
643                "library R { function size(uint256) internal view returns (uint256 n) { assembly { n := returndatasize() } } }",
644                "import {R} from '../src/Read.sol';",
645                "R.size(0)",
646            ),
647            (
648                "library R { function size(uint256) internal pure returns (uint256 n) { assembly { n := returndatasize() } } }",
649                "import {R} from '../src/Read.sol';",
650                "(R).size(0)",
651            ),
652            (
653                "type Word is uint256; using {size as -} for Word global; function size(Word) pure returns (Word) { uint256 n; assembly { n := returndatasize() } return Word.wrap(n); }",
654                "import {Word} from '../src/Read.sol';",
655                "Word.unwrap(-Word.wrap(0))",
656            ),
657            (
658                "type Word is uint256; using {size as +} for Word global; function size(Word, Word) pure returns (Word) { uint256 n; assembly { n := returndatasize() } return Word.wrap(n); }",
659                "import {Word} from '../src/Read.sol';",
660                "Word.unwrap(Word.wrap(0) + Word.wrap(0))",
661            ),
662        ] {
663            let (_root, paths, mut input) = input();
664            input.input.sources.insert(PathBuf::from("src/Read.sol"), Source::new(helper));
665            let observer = format!(
666                "{declarations} import '../src/Dep.sol'; contract Observer {{ function observe() public {{ new Dep(); require({expression} == 0); }} }}"
667            );
668            let safe = "import '../src/Read.sol'; import '../src/Dep.sol'; contract Safe { function deploy() public { new Dep(); } }";
669            input.input.sources.insert(PathBuf::from("test/Observer.sol"), Source::new(&observer));
670            input.input.sources.insert(PathBuf::from("test/Safe.sol"), Source::new(safe));
671            <DynamicTestLinkingPreprocessor as Preprocessor<SolcCompiler>>::preprocess(
672                &DynamicTestLinkingPreprocessor::default(),
673                &SolcCompiler::default(),
674                &mut input,
675                &paths,
676                &mut HashSet::new(),
677            )
678            .unwrap();
679            assert_eq!(
680                input.input.sources[&PathBuf::from("test/Observer.sol")].content.as_str(),
681                observer
682            );
683            assert_ne!(input.input.sources[&PathBuf::from("test/Safe.sol")].content.as_str(), safe);
684        }
685    }
686
687    #[test]
688    fn return_data_fallback_includes_inherited_observers() {
689        assert_return_data_scope_native(
690            "abstract contract Base { function make() internal virtual; function observe() public { make(); uint256 n; assembly { n := returndatasize() } require(n == 0); } } contract Derived is Base { function make() internal override { new Dep(); } }",
691        );
692    }
693
694    #[test]
695    fn return_data_fallback_collects_using_before_initializers() {
696        assert_return_data_scope_native(
697            "library Reader { function size(uint256) internal view returns (uint256 n) { assembly { n := returndatasize() } } } contract Initializer { using Reader for uint256; Dep target = new Dep(); uint256 observed = uint256(0).size(); function observe() public view { require(observed == 0); } }",
698        );
699    }
700
701    fn assert_return_data_scope_native(declarations: &str) {
702        let (_root, paths, mut input) = input();
703        let observer = format!("import '../src/Dep.sol'; {declarations}");
704        let safe =
705            "import '../src/Dep.sol'; contract Safe { function deploy() public { new Dep(); } }";
706        input.input.sources.insert(PathBuf::from("test/Observer.sol"), Source::new(&observer));
707        input.input.sources.insert(PathBuf::from("test/Safe.sol"), Source::new(safe));
708        <DynamicTestLinkingPreprocessor as Preprocessor<SolcCompiler>>::preprocess(
709            &DynamicTestLinkingPreprocessor::default(),
710            &SolcCompiler::default(),
711            &mut input,
712            &paths,
713            &mut HashSet::new(),
714        )
715        .unwrap();
716        assert_eq!(
717            input.input.sources[&PathBuf::from("test/Observer.sol")].content.as_str(),
718            observer
719        );
720        assert_ne!(input.input.sources[&PathBuf::from("test/Safe.sol")].content.as_str(), safe);
721    }
722
723    #[test]
724    fn return_data_namespace_resolution_selects_exports() {
725        for (namespace, expression, native) in [
726            ("Read", "R.identity(7)", false),
727            ("Read", "R.size(0)", true),
728            ("Export", "R.identity(7)", false),
729            ("Export", "R.observe(0)", true),
730            ("Nested", "R.Inner.identity(7)", false),
731            ("Nested", "R.Inner.observe(0)", true),
732            ("Read", "R.Reader.identity(7)", false),
733            ("Read", "R.Reader.size(0)", true),
734            ("Export", "R.Renamed.identity(7)", false),
735            ("Export", "R.Renamed.size(0)", true),
736        ] {
737            let (_root, paths, mut input) = input();
738            for (path, source) in [
739                (
740                    "src/Read.sol",
741                    "function identity(uint256 n) pure returns (uint256) { return n; } function size(uint256) view returns (uint256 n) { assembly { n := returndatasize() } } library Reader { function identity(uint256 n) internal pure returns (uint256) { return n; } function size(uint256) internal view returns (uint256 n) { assembly { n := returndatasize() } } }",
742                ),
743                (
744                    "src/Export.sol",
745                    "import {size as observe, identity, Reader as Renamed} from './Read.sol';",
746                ),
747                ("src/Nested.sol", "import * as Inner from './Export.sol';"),
748            ] {
749                input.input.sources.insert(PathBuf::from(path), Source::new(source));
750            }
751            let source = format!(
752                "import '../src/Dep.sol'; import * as R from '../src/{namespace}.sol'; contract Case {{ function run() public {{ new Dep(); {expression}; }} }}"
753            );
754            input.input.sources.insert(PathBuf::from("test/Case.sol"), Source::new(&source));
755            <DynamicTestLinkingPreprocessor as Preprocessor<SolcCompiler>>::preprocess(
756                &DynamicTestLinkingPreprocessor::default(),
757                &SolcCompiler::default(),
758                &mut input,
759                &paths,
760                &mut HashSet::new(),
761            )
762            .unwrap();
763            let actual = &input.input.sources[&PathBuf::from("test/Case.sol")].content;
764            assert_eq!(actual.contains("new Dep();"), native, "{namespace}: {expression}");
765        }
766    }
767
768    #[test]
769    fn analysis_fallback_tracks_transitive_imports() {
770        let (_root, mut paths, mut input) = input();
771        paths.remappings.push("dep/=lib/dep/".parse().unwrap());
772        input.input.sources = [
773            ("lib/dep/Leaf.sol", "contract Leaf {}"),
774            ("src/Middle.sol", "import 'dep/Leaf.sol'; contract Middle {}"),
775            ("src/Unrelated.sol", "contract Unrelated {}"),
776            ("test/Fallback.sol", "import '../src/Middle.sol'; contract Fallback is Missing {}"),
777            ("test/Independent.sol", "contract Independent {}"),
778            ("script/Deploy.sol", "import '../src/Middle.sol'; contract Deploy {}"),
779        ]
780        .into_iter()
781        .map(|(path, source)| (PathBuf::from(path), Source::new(source)))
782        .collect();
783        for (path, source) in &input.input.sources {
784            let path = paths.root.join(path);
785            std::fs::create_dir_all(path.parent().unwrap()).unwrap();
786            std::fs::write(path, source.content.as_str()).unwrap();
787        }
788        let original = input.input.sources.clone();
789        let mut mocks = HashSet::from([paths.root.join("test/Fallback.sol")]);
790        <DynamicTestLinkingPreprocessor as Preprocessor<SolcCompiler>>::preprocess(
791            &DynamicTestLinkingPreprocessor::default(),
792            &SolcCompiler::default(),
793            &mut input,
794            &paths,
795            &mut mocks,
796        )
797        .unwrap();
798        assert_eq!(input.input.sources, original);
799        assert!(mocks.is_empty());
800        let dependencies = native_import_dependencies(&paths, &input.input.sources);
801        let imports = BTreeSet::from([
802            paths.root.join("src/Middle.sol"),
803            paths.root.join("lib/dep/Leaf.sol"),
804        ]);
805        assert_eq!(
806            dependencies,
807            NativeDependencies::from([
808                (
809                    paths.root.join("test/Fallback.sol"),
810                    NativeDependencyState::Known(imports.clone())
811                ),
812                (paths.root.join("script/Deploy.sol"), NativeDependencyState::Known(imports)),
813                (
814                    paths.root.join("test/Independent.sol"),
815                    NativeDependencyState::Known(BTreeSet::new())
816                ),
817            ])
818        );
819    }
820
821    #[test]
822    fn analysis_fallback_limits_incomplete_imports_to_consumers() {
823        let (_root, paths, mut input) = input();
824        input.input.sources = [
825            ("src/AHealthy.sol", "contract AHealthy {}"),
826            (
827                "test/Fallback.sol",
828                "import /* Solar's regex fallback cannot recover this */ '../src/AHealthy.sol'; contract Fallback { function fail() public { throw; } }",
829            ),
830            (
831                "test/Healthy.sol",
832                "import '../src/AHealthy.sol'; contract Healthy is AHealthy {}",
833            ),
834            ("test/Independent.sol", "contract Independent {}"),
835        ]
836        .into_iter()
837        .map(|(path, source)| (PathBuf::from(path), Source::new(source)))
838        .collect();
839        for (path, source) in &input.input.sources {
840            let path = paths.root.join(path);
841            std::fs::create_dir_all(path.parent().unwrap()).unwrap();
842            std::fs::write(path, source.content.as_str()).unwrap();
843        }
844        let dependencies = native_import_dependencies(&paths, &input.input.sources);
845        assert_eq!(
846            dependencies,
847            NativeDependencies::from([
848                (paths.root.join("test/Fallback.sol"), NativeDependencyState::Conservative),
849                (
850                    paths.root.join("test/Healthy.sol"),
851                    NativeDependencyState::Known(BTreeSet::from([paths
852                        .root
853                        .join("src/AHealthy.sol")]))
854                ),
855                (
856                    paths.root.join("test/Independent.sol"),
857                    NativeDependencyState::Known(BTreeSet::new())
858                ),
859            ])
860        );
861    }
862
863    #[test]
864    fn analysis_fallback_merges_normalized_graph_paths() {
865        let (_root, mut paths, mut input) = input();
866        paths.remappings.push("test/z/:src/=lib/alternate/".parse().unwrap());
867        input.input.sources = [
868            ("src/Dep.sol", "contract Dep {}"),
869            ("lib/alternate/Dep.sol", "contract Dep {}"),
870            ("test/A.sol", "import '../src/Dep.sol'; contract A is Dep {}"),
871            ("test/z/../A.sol", "import '../src/Dep.sol'; contract A is Dep {}"),
872        ]
873        .into_iter()
874        .map(|(path, source)| (PathBuf::from(path), Source::new(source)))
875        .collect();
876        for (path, source) in &input.input.sources {
877            let path = paths.root.join(path);
878            std::fs::create_dir_all(path.parent().unwrap()).unwrap();
879            std::fs::write(path, source.content.as_str()).unwrap();
880        }
881        let dependencies = native_import_dependencies(&paths, &input.input.sources);
882        assert_eq!(
883            dependencies,
884            NativeDependencies::from([(
885                paths.root.join("test/A.sol"),
886                NativeDependencyState::Known(BTreeSet::from([
887                    paths.root.join("src/Dep.sol"),
888                    paths.root.join("lib/alternate/Dep.sol")
889                ]))
890            )])
891        );
892    }
893
894    #[test]
895    fn solar_error_summary_accepts_coded_and_colored_errors() {
896        assert_eq!(
897            solar_error_summary(
898                "solar reported errors:\n\n\u{1b}[1;91merror\u{1b}[0m\u{1b}[1m: boom\u{1b}[0m"
899            ),
900            Some("error: boom".to_string())
901        );
902        assert_eq!(
903            solar_error_summary("solar reported errors:\n\n\u{1b}[31merror[1234]: boom\u{1b}[0m"),
904            Some("error[1234]: boom".to_string())
905        );
906        assert_eq!(
907            solar_error_summary("solar reported errors:\n\nerror: boom"),
908            Some("error: boom".to_string())
909        );
910        assert_eq!(solar_error_summary("solar reported 1 error"), None);
911    }
912
913    #[test]
914    fn deploy_helper_preserves_target_abi_coder() {
915        for (minor, patch, pragma, needs_v2) in [
916            (7, 6, "", false),
917            (7, 6, "pragma abicoder v2;", true),
918            (7, 6, "pragma experimental ABIEncoderV2;", true),
919            (8, 0, "", false),
920            (8, 37, "", false),
921            (8, 37, "pragma abicoder v2;", false),
922            (8, 37, "pragma experimental ABIEncoderV2;", false),
923            (8, 37, "pragma abicoder v1;", false),
924        ] {
925            let (_root, paths, mut input) = input();
926            input.version = Version::new(0, minor, patch);
927            input.input.sources = [
928                ("src/Dep.sol", format!("{pragma} contract Dep {{ constructor(uint256 n) {{}} }}")),
929                ("test/Deploy.sol", "pragma experimental ABIEncoderV2; import '../src/Dep.sol'; contract Deploy { function deploy() public { new Dep(7); } }".to_string()),
930            ].into_iter().map(|(path, source)| (PathBuf::from(path), Source::new(source))).collect();
931            <DynamicTestLinkingPreprocessor as Preprocessor<SolcCompiler>>::preprocess(
932                &DynamicTestLinkingPreprocessor::default(),
933                &SolcCompiler::default(),
934                &mut input,
935                &paths,
936                &mut HashSet::new(),
937            )
938            .unwrap();
939            let helpers = input
940                .input
941                .sources
942                .iter()
943                .filter(|(path, _)| is_deploy_helper_path(path))
944                .collect::<Vec<_>>();
945            assert_eq!(helpers.len(), 1);
946            assert_eq!(
947                helpers[0].1.content.matches("pragma experimental ABIEncoderV2;").count(),
948                usize::from(needs_v2),
949                "0.{minor}.{patch} {pragma}"
950            );
951            let source = &input.input.sources[&PathBuf::from("test/Deploy.sol")].content;
952            assert_eq!(source.matches("new Dep(7)").count(), 0);
953            assert_eq!(source.matches(".deployCode({").count(), 1);
954        }
955    }
956
957    #[test]
958    fn preprocessor_cache_versions_match() {
959        assert_eq!(
960            <DynamicTestLinkingPreprocessor as Preprocessor<SolcCompiler>>::cache_version(
961                &DynamicTestLinkingPreprocessor::default()
962            ),
963            <DynamicTestLinkingPreprocessor as Preprocessor<MultiCompiler>>::cache_version(
964                &DynamicTestLinkingPreprocessor::default()
965            )
966        );
967    }
968
969    #[test]
970    fn uint160_deployment_returns_follow_compiler_settings() {
971        for (version, via_ir, specific, workaround) in [
972            ("0.8.14", Some(true), false, false),
973            ("0.8.15", Some(true), false, true),
974            ("0.8.24+commit.e11b9ed9", Some(true), false, true),
975            ("0.8.25", Some(true), false, true),
976            ("0.8.26", Some(true), false, false),
977            ("0.8.24", Some(false), false, false),
978            ("0.8.24", None, false, false),
979            ("0.8.24-dev", Some(true), false, false),
980            ("0.8.24-dev", Some(true), true, false),
981            ("0.8.24+commit.e11b9ed9", Some(true), true, true),
982        ] {
983            let (_root, paths, mut input) = input();
984            input.version = version.parse().unwrap();
985            input.input.settings.via_ir = via_ir;
986            let compiler = if specific {
987                let compiler = SolcCompiler::Specific(Solc::new_with_version(
988                    paths.root.join("absent-solc"),
989                    input.version.clone(),
990                ));
991                // Compiler selection strips metadata before constructing the versioned input.
992                let selected = compiler.available_versions(&SolcLanguage::Solidity).pop().unwrap();
993                assert!(selected.as_ref().pre.is_empty());
994                let mut settings = SolcSettings::default();
995                settings.settings.via_ir = via_ir;
996                input = SolcVersionedInput::build(
997                    input.input.sources,
998                    settings,
999                    SolcLanguage::Solidity,
1000                    selected.as_ref().clone(),
1001                );
1002                compiler
1003            } else {
1004                SolcCompiler::default()
1005            };
1006            <DynamicTestLinkingPreprocessor as Preprocessor<SolcCompiler>>::preprocess(
1007                &DynamicTestLinkingPreprocessor::default(),
1008                &compiler,
1009                &mut input,
1010                &paths,
1011                &mut HashSet::new(),
1012            )
1013            .unwrap();
1014            let source = &input.input.sources[&PathBuf::from("test/Deploy.sol")].content;
1015            assert_eq!(
1016                source.matches("external returns (uint160);").count(),
1017                if workaround { 8 } else { 0 },
1018                "{version}, via_ir={via_ir:?}, specific={specific}"
1019            );
1020            assert_eq!(
1021                source.matches("external returns (address);").count(),
1022                if workaround { 0 } else { 8 }
1023            );
1024            assert_eq!(source.matches("payable(address(").count(), usize::from(workaround));
1025        }
1026    }
1027
1028    #[test]
1029    fn source_context_sensitivity_uses_configured_directories() {
1030        let root = tempfile::tempdir().unwrap();
1031        let mut paths = ProjectPathsConfig::builder().root(root.path()).build().unwrap();
1032        paths.tests = paths.root.join("src/spec");
1033        paths.scripts = paths.root.join("ops");
1034        let multi_paths = paths.clone().with_language::<MultiCompilerLanguage>();
1035        let preprocessor = DynamicTestLinkingPreprocessor::default();
1036        for (source, expected) in [
1037            ("src/spec/Helper.sol", true),
1038            ("ops/Utility.sol", true),
1039            ("test/Default.t.sol", false),
1040            ("src/LooksLikeTest.t.sol", false),
1041            ("lib/Spec.t.sol", false),
1042        ] {
1043            let source = paths.root.join(source);
1044            assert_eq!(
1045                <DynamicTestLinkingPreprocessor as Preprocessor<SolcCompiler>>::is_source_context_sensitive(
1046                    &preprocessor, &source, &paths,
1047                ),
1048                expected,
1049                "{}", source.display(),
1050            );
1051            assert_eq!(
1052                <DynamicTestLinkingPreprocessor as Preprocessor<MultiCompiler>>::is_source_context_sensitive(
1053                    &preprocessor, &source, &multi_paths,
1054                ),
1055                expected,
1056                "{}", source.display(),
1057            );
1058        }
1059    }
1060}