neo-devpack-solidity 0.22.0

Production-focused Solidity-to-NeoVM compilation system
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
//! Multi-source standard JSON compilation proptests + smoke tests.
//!
//! Closes the wave-#30 coverage hole: `examples_smoke_props::examples_compile_smoke`
//! skips every `.sol` file with a non-comment `import` directive (13 files at
//! the time of writing) because the in-process `compile_contracts` helper is
//! single-source. Real production Solidity always uses imports — relative
//! paths, transitive chains, and (rarely) cycles. This module exercises the
//! standard-JSON import resolver under those shapes.
//!
//! Why this matters: if `build_combined_source_with_import_validation`
//! mishandles relative paths, transitive deps, or import cycles, real
//! deployments break silently. See
//! `src/cli/standard_json/standard_json_process/imports.rs`.
//!
//! Strategy: invoke the `neo-solc --standard-json` binary as a subprocess
//! (the only public entry point that returns the full output JSON; the
//! in-process `process_standard_json_content` is `pub(crate)`). Each test
//! writes a tempdir input.json, runs the compiler, parses the output JSON,
//! and asserts on `contracts`/`errors`.
//!
//! Reference: `tests/standard_json_cli_tests.rs` (CLI subprocess pattern),
//! `src/cli/tests/standard_json/basic/multiple_sources.rs` (in-process
//! 2-file shape), `tests/fuzz_tests/examples_smoke_props.rs` (examples scan).

#![allow(unused_imports)]

use proptest::prelude::*;
use serde_json::{json, Value};
use std::path::{Path, PathBuf};
use std::process::Command;
use tempfile::tempdir;

// ---------- Helpers ----------

fn compiler_path() -> &'static str {
    env!("CARGO_BIN_EXE_neo-solc")
}

/// Run `neo-solc --standard-json` against an `input` JSON value. Returns the
/// parsed output JSON (whether the process exited 0 or non-0; the compiler
/// writes a JSON error envelope either way) and the process exit status.
fn run_standard_json(input: &Value) -> (Value, std::process::ExitStatus) {
    let dir = tempdir().expect("tempdir");
    let input_path = dir.path().join("input.json");
    let output_path = dir.path().join("out.json");
    std::fs::write(
        &input_path,
        serde_json::to_string_pretty(input).expect("serialise input"),
    )
    .expect("write input");
    let proc_out = Command::new(compiler_path())
        .arg("--standard-json")
        .arg("--input")
        .arg(&input_path)
        .arg("--output")
        .arg(&output_path)
        .output()
        .expect("spawn neo-solc");
    let body = std::fs::read_to_string(&output_path).unwrap_or_else(|_| {
        // Some error paths fail before writing output; surface stdout/stderr.
        format!(
            "{{\"errors\":[{{\"message\":\"no output written; stderr={}\"}}]}}",
            String::from_utf8_lossy(&proc_out.stderr).replace('"', "'")
        )
    });
    let parsed: Value = serde_json::from_str(&body).unwrap_or_else(|e| {
        json!({
            "errors": [{ "message": format!("parse output failed: {e}; raw={body}") }]
        })
    });
    (parsed, proc_out.status)
}

/// Did the output JSON contain at least one severity=error entry?
fn has_errors(output: &Value) -> bool {
    output
        .get("errors")
        .and_then(|e| e.as_array())
        .map(|arr| {
            arr.iter().any(|e| {
                e.get("severity")
                    .and_then(|v| v.as_str())
                    .map(|s| s == "error")
                    .unwrap_or(false)
            })
        })
        .unwrap_or(false)
}

/// Concatenate all error messages (and types) from the output JSON for
/// diagnostic snippets.
fn error_summary(output: &Value) -> String {
    output
        .get("errors")
        .and_then(|e| e.as_array())
        .map(|arr| {
            arr.iter()
                .map(|e| {
                    let typ = e.get("type").and_then(|v| v.as_str()).unwrap_or("?");
                    let msg = e.get("message").and_then(|v| v.as_str()).unwrap_or("");
                    format!("[{typ}] {msg}")
                })
                .collect::<Vec<_>>()
                .join(" | ")
        })
        .unwrap_or_default()
}

/// Look up `output.contracts[file][contract]`. Returns `None` if any layer
/// is missing.
fn get_contract<'a>(output: &'a Value, file: &str, contract: &str) -> Option<&'a Value> {
    output.get("contracts")?.get(file)?.get(contract)
}

// ==================== Test (a): simple 2-file import ====================

proptest! {
    #![proptest_config(ProptestConfig {
        // Each case spawns a subprocess; keep cases tight.
        cases: 4,
        ..ProptestConfig::default()
    })]

    /// 2-file Solidity package with a `./Counter.sol` import. Asserts both
    /// contracts appear in standard-JSON output and that `Main` carries a
    /// `c` getter (auto-generated from `Counter public c`).
    #[test]
    fn multi_source_simple_import(seed_n in 0u64..16) {
        let counter_src = format!(
            r#"// SPDX-License-Identifier: MIT
pragma solidity ^0.8.19;

contract Counter {{
    uint256 public n;
    function inc() external {{ n = n + {seed_n} + 1; }}
}}
"#);
        let main_src = r#"// SPDX-License-Identifier: MIT
pragma solidity ^0.8.19;

import "./Counter.sol";

contract Main {
    Counter public c;
    constructor() { c = new Counter(); }
}
"#;

        let input = json!({
            "language": "Solidity",
            "sources": {
                "Counter.sol": { "content": counter_src },
                "Main.sol":    { "content": main_src },
            },
            "settings": {}
        });

        let (output, status) = run_standard_json(&input);
        prop_assert!(
            status.success(),
            "compile exited non-zero: errors={}",
            error_summary(&output)
        );

        // Both contracts should be emitted.
        let main = get_contract(&output, "Main.sol", "Main");
        let counter = get_contract(&output, "Counter.sol", "Counter");
        prop_assert!(
            main.is_some(),
            "Main not present in output: errors={}; contracts={}",
            error_summary(&output),
            output.get("contracts").map(|v| v.to_string()).unwrap_or_default()
        );
        prop_assert!(
            counter.is_some(),
            "Counter not present in output: errors={}",
            error_summary(&output)
        );

        // Main should expose the `c()` getter (auto-generated for `Counter public c`).
        let method_ids = main
            .and_then(|m| m.get("evm"))
            .and_then(|m| m.get("methodIdentifiers"));
        let has_c_getter = method_ids
            .and_then(|m| m.as_object())
            .map(|m| m.keys().any(|k| k == "c()"))
            .unwrap_or(false);
        prop_assert!(
            has_c_getter,
            "expected Main to expose c() getter; methodIdentifiers={:?}",
            method_ids
        );
    }
}

// ==================== Test (b): A -> B -> C transitive ====================

proptest! {
    #![proptest_config(ProptestConfig { cases: 4, ..ProptestConfig::default() })]

    /// Transitive chain: `A.sol` imports `B.sol`, `B.sol` imports `C.sol`.
    /// Asserts all three are emitted and no resolution errors surface.
    #[test]
    fn multi_source_transitive_import(_seed in 0u32..16) {
        let c_src = r#"// SPDX-License-Identifier: MIT
pragma solidity ^0.8.19;

contract C {
    function leaf() external pure returns (uint256) { return 7; }
}
"#;
        let b_src = r#"// SPDX-License-Identifier: MIT
pragma solidity ^0.8.19;

import "./C.sol";

contract B {
    function mid() external pure returns (uint256) { return 5; }
}
"#;
        let a_src = r#"// SPDX-License-Identifier: MIT
pragma solidity ^0.8.19;

import "./B.sol";

contract A {
    function top() external pure returns (uint256) { return 3; }
}
"#;

        let input = json!({
            "language": "Solidity",
            "sources": {
                "A.sol": { "content": a_src },
                "B.sol": { "content": b_src },
                "C.sol": { "content": c_src },
            },
            "settings": {}
        });

        let (output, status) = run_standard_json(&input);
        prop_assert!(
            status.success(),
            "transitive compile exited non-zero: {}",
            error_summary(&output)
        );

        for (file, name) in [("A.sol", "A"), ("B.sol", "B"), ("C.sol", "C")] {
            prop_assert!(
                get_contract(&output, file, name).is_some(),
                "{name} not present in transitive output: errors={}",
                error_summary(&output)
            );
        }
    }
}

// ==================== Test (c): circular import is handled gracefully ====================

proptest! {
    #![proptest_config(ProptestConfig { cases: 2, ..ProptestConfig::default() })]

    /// A imports B, B imports A. The import resolver MUST handle this
    /// gracefully — no infinite loop, no stack overflow, no host panic.
    /// The current resolver intentionally tolerates cycles (back-edges are
    /// dropped during DFS rather than reported, see imports.rs:218); this
    /// test pins that behavior. If a future change starts rejecting cycles
    /// with a typed error, the test will still pass as long as the process
    /// terminates and the output contains a reasonable diagnostic.
    #[test]
    fn multi_source_circular_import_rejected(_seed in 0u32..4) {
        let a_src = r#"// SPDX-License-Identifier: MIT
pragma solidity ^0.8.19;

import "./B.sol";

contract A {
    function fromA() external pure returns (uint256) { return 1; }
}
"#;
        let b_src = r#"// SPDX-License-Identifier: MIT
pragma solidity ^0.8.19;

import "./A.sol";

contract B {
    function fromB() external pure returns (uint256) { return 2; }
}
"#;

        let input = json!({
            "language": "Solidity",
            "sources": {
                "A.sol": { "content": a_src },
                "B.sol": { "content": b_src },
            },
            "settings": {}
        });

        // Subprocess invocation: a hang or crash here would surface as a
        // test timeout / non-zero exit with no output JSON. That alone is
        // sufficient to detect the regression class the wave-#30 finding
        // anticipated.
        let (output, status) = run_standard_json(&input);

        // Two acceptable shapes:
        //   1. Compile succeeds — both A and B emitted (current resolver
        //      tolerates cycles by dropping back-edges).
        //   2. Compile fails cleanly with an error mentioning "circular",
        //      "cycle", or similar (a future, stricter resolver).
        // What's NOT acceptable: the process hangs or crashes (caught by
        // `Command::output` returning an Err / non-terminating wait).
        if status.success() {
            // Tolerated cycle: both contracts must still be emitted.
            let a = get_contract(&output, "A.sol", "A");
            let b = get_contract(&output, "B.sol", "B");
            prop_assert!(
                a.is_some() && b.is_some(),
                "circular-import compile succeeded but emitted incomplete output: \
                 A={}, B={}, errors={}",
                a.is_some(), b.is_some(), error_summary(&output)
            );
        } else {
            // Strict-rejection path: error message should mention the cycle.
            let summary = error_summary(&output).to_lowercase();
            prop_assert!(
                summary.contains("circular")
                    || summary.contains("cycle")
                    || summary.contains("recursive")
                    || summary.contains("import"),
                "circular-import compile failed but error did not mention cycles: {}",
                summary
            );
        }
    }
}

// ==================== Test (d): library used via `using ... for` ====================

proptest! {
    #![proptest_config(ProptestConfig { cases: 4, ..ProptestConfig::default() })]

    /// Library + consumer in separate files, wired via `using L for uint`.
    /// Asserts the compile succeeds and the consumer carries the expected
    /// public method. Runtime execution is NOT exercised here because
    /// running compiled code requires a parsed manifest, and the standard-
    /// JSON output's manifest object is the same one produced by
    /// `compile_contracts` (already covered by `examples_call_smoke`). The
    /// load-bearing assertion for THIS test is that the import resolver
    /// successfully wires a library consumer to its definition file.
    #[test]
    fn multi_source_imported_library_used(_seed in 0u32..16) {
        let lib_src = r#"// SPDX-License-Identifier: MIT
pragma solidity ^0.8.19;

library L {
    function add(uint a, uint b) internal pure returns (uint) { return a + b; }
}
"#;
        let consumer_src = r#"// SPDX-License-Identifier: MIT
pragma solidity ^0.8.19;

import "./L.sol";

contract C {
    using L for uint;
    function f() external pure returns (uint) {
        uint x = 1;
        return x.add(2);
    }
}
"#;

        let input = json!({
            "language": "Solidity",
            "sources": {
                "L.sol": { "content": lib_src },
                "C.sol": { "content": consumer_src },
            },
            "settings": {}
        });

        let (output, status) = run_standard_json(&input);
        prop_assert!(
            status.success(),
            "library compile exited non-zero: {}",
            error_summary(&output)
        );

        let c = get_contract(&output, "C.sol", "C");
        prop_assert!(
            c.is_some(),
            "consumer C not in output: errors={}",
            error_summary(&output)
        );

        // The consumer must expose `f()` as a public method.
        let has_f = c
            .and_then(|v| v.get("evm"))
            .and_then(|v| v.get("methodIdentifiers"))
            .and_then(|v| v.as_object())
            .map(|m| m.contains_key("f()"))
            .unwrap_or(false);
        prop_assert!(
            has_f,
            "expected C.f() in methodIdentifiers; output={}",
            c.map(|v| v.to_string()).unwrap_or_default()
        );
    }
}

// ==================== Test (e): the 13 wave-#30 skipped example contracts ====================

/// File detection: scan `examples/`, `devpack/contracts/`, `devpack/examples/`
/// for `.sol` files with a non-comment `import` directive — exactly the set
/// the wave-#30 smoke test skips.
fn collect_imported_sol_files(workspace_root: &Path) -> Vec<PathBuf> {
    let scan_roots = ["examples", "devpack/contracts", "devpack/examples"];
    let mut out: Vec<PathBuf> = Vec::new();
    for root in &scan_roots {
        let dir = workspace_root.join(root);
        if !dir.is_dir() {
            continue;
        }
        let mut stack: Vec<PathBuf> = vec![dir];
        while let Some(d) = stack.pop() {
            let entries = match std::fs::read_dir(&d) {
                Ok(e) => e,
                Err(_) => continue,
            };
            for entry in entries.flatten() {
                let p = entry.path();
                if p.is_dir() {
                    stack.push(p);
                } else if p.extension().and_then(|e| e.to_str()) == Some("sol") {
                    if let Ok(src) = std::fs::read_to_string(&p) {
                        if has_non_comment_import(&src) {
                            out.push(p);
                        }
                    }
                }
            }
        }
    }
    out.sort();
    out
}

fn has_non_comment_import(src: &str) -> bool {
    src.lines().any(|line| {
        let trimmed = line.trim_start();
        trimmed.starts_with("import ")
            || trimmed.starts_with("import\"")
            || trimmed.starts_with("import{")
            || trimmed.starts_with("import (")
    })
}

/// Extract the filename strings from non-comment `import "..."` directives
/// in `src`. Pure-string scan — no parser needed for this surface (all
/// imports in the example tree use the `import "path"` shape, see
/// devpack/contracts/*.sol).
fn extract_imports(src: &str) -> Vec<String> {
    let mut imports = Vec::new();
    for line in src.lines() {
        let trimmed = line.trim_start();
        if !(trimmed.starts_with("import ")
            || trimmed.starts_with("import\"")
            || trimmed.starts_with("import{")
            || trimmed.starts_with("import ("))
        {
            continue;
        }
        // Find the first quoted segment.
        let mut chars = trimmed.chars().peekable();
        let mut in_quote = false;
        let mut buf = String::new();
        for c in chars.by_ref() {
            match (in_quote, c) {
                (false, '"') => in_quote = true,
                (true, '"') => break,
                (true, _) => buf.push(c),
                (false, _) => {}
            }
        }
        if !buf.is_empty() {
            imports.push(buf);
        }
    }
    imports
}

/// Normalize a virtual path the same way the resolver does: drop `.`,
/// pop on `..`, keep `Normal` segments. Mirrors
/// `imports.rs::normalize_virtual_path` so closure keys collide with the
/// resolver's lookup logic exactly.
fn normalize_virtual(path: &Path) -> String {
    use std::path::Component;
    let mut components: Vec<String> = Vec::new();
    for comp in path.components() {
        match comp {
            Component::CurDir => {}
            Component::ParentDir => {
                components.pop();
            }
            Component::Normal(part) => {
                components.push(part.to_string_lossy().to_string());
            }
            Component::Prefix(_) | Component::RootDir => {}
        }
    }
    components.join("/")
}

/// Build the import closure for `entry_path`: every transitively-reachable
/// `.sol` file, indexed by a workspace-relative virtual path. The keys
/// match the resolver's view: importing file at `devpack/examples/X.sol`
/// importing `../contracts/Syscalls.sol` resolves to
/// `devpack/contracts/Syscalls.sol` — which we include under that exact key.
///
/// Returns `Vec<(virtual_key, content)>`. Caller wraps this into a
/// standard-JSON `sources` object; the resolver will then pair the entry
/// file's `import "../contracts/X.sol"` against the matching virtual key.
fn build_closure(
    workspace_root: &Path,
    entry_path: &Path,
) -> Result<Vec<(String, String)>, String> {
    use std::collections::{HashMap, HashSet, VecDeque};

    let entry_canon = entry_path
        .canonicalize()
        .map_err(|e| format!("canonicalize {}: {}", entry_path.display(), e))?;
    let workspace_canon = workspace_root
        .canonicalize()
        .map_err(|e| format!("canonicalize {}: {}", workspace_root.display(), e))?;

    fn workspace_relative(workspace: &Path, abs: &Path) -> Option<String> {
        let rel = abs.strip_prefix(workspace).ok()?;
        Some(normalize_virtual(rel))
    }

    let entry_key = workspace_relative(&workspace_canon, &entry_canon)
        .ok_or_else(|| format!("entry not under workspace: {}", entry_canon.display()))?;
    let entry_content = std::fs::read_to_string(&entry_canon)
        .map_err(|e| format!("read {}: {}", entry_canon.display(), e))?;

    // (virtual_key) -> (abs_path, content)
    let mut sources: HashMap<String, (PathBuf, String)> = HashMap::new();
    sources.insert(entry_key.clone(), (entry_canon.clone(), entry_content));

    // BFS. Each queued item is the virtual_key whose imports we still need
    // to walk.
    let mut queue: VecDeque<String> = VecDeque::new();
    queue.push_back(entry_key.clone());
    let mut seen_abs: HashSet<PathBuf> = HashSet::new();
    seen_abs.insert(entry_canon.clone());

    while let Some(cur_key) = queue.pop_front() {
        let (cur_abs, cur_content) = match sources.get(&cur_key) {
            Some((a, c)) => (a.clone(), c.clone()),
            None => continue,
        };
        for imp in extract_imports(&cur_content) {
            // Resolve `imp` against the CURRENT file's directory on disk.
            let cur_dir = match cur_abs.parent() {
                Some(d) => d,
                None => continue,
            };
            let abs_path = cur_dir.join(&imp);
            let abs_canon = match abs_path.canonicalize() {
                Ok(p) => p,
                Err(_) => continue, // External / missing dep — let resolver report.
            };
            if !seen_abs.insert(abs_canon.clone()) {
                continue;
            }
            // Compute the WORKSPACE-relative virtual key for this file.
            // That's what the resolver will land on once it joins
            // `cur_dir_virtual / imp` and normalizes.
            let virt_key = match workspace_relative(&workspace_canon, &abs_canon) {
                Some(k) => k,
                None => continue, // Outside workspace — can't ship its content.
            };
            let content = match std::fs::read_to_string(&abs_canon) {
                Ok(c) => c,
                Err(_) => continue,
            };
            sources.insert(virt_key.clone(), (abs_canon, content));
            queue.push_back(virt_key);
        }
    }

    Ok(sources.into_iter().map(|(k, (_, c))| (k, c)).collect())
}

/// Iterate the wave-#30-skipped example contracts (those with non-comment
/// `import` directives). For each, build a standard-JSON input that
/// bundles the entry file plus the transitive closure of its imports
/// (resolved off-disk), invoke the compiler, and tally outcomes.
///
/// Pass criterion: at least 80% of surveyed files compile cleanly — some
/// are expected to fail because they hit advanced features (interfaces
/// that aren't fully supported, native syscall shapes that need richer
/// linking) but the bulk should resolve and compile.
#[test]
fn multi_source_run_skipped_examples() {
    let workspace_root = std::env::var("CARGO_MANIFEST_DIR")
        .map(PathBuf::from)
        .unwrap_or_else(|_| PathBuf::from("."));

    let surveyed = collect_imported_sol_files(&workspace_root);
    assert!(
        !surveyed.is_empty(),
        "no .sol files with imports found under examples/ devpack/contracts/ devpack/examples/ — \
         scan-roots changed?"
    );

    let mut compiled = 0usize;
    let mut compile_failed: Vec<(PathBuf, String)> = Vec::new();
    let mut closure_failed: Vec<(PathBuf, String)> = Vec::new();

    for entry in &surveyed {
        let closure = match build_closure(&workspace_root, entry) {
            Ok(c) => c,
            Err(e) => {
                closure_failed.push((entry.clone(), e));
                continue;
            }
        };

        let mut sources_obj = serde_json::Map::new();
        for (key, content) in closure {
            sources_obj.insert(key, json!({ "content": content }));
        }

        let input = json!({
            "language": "Solidity",
            "sources": Value::Object(sources_obj),
            "settings": {}
        });

        let (output, status) = run_standard_json(&input);
        if status.success() && !has_errors(&output) {
            compiled += 1;
        } else {
            // Truncate the error summary so the panic message stays scannable.
            let summary: String = error_summary(&output).chars().take(300).collect();
            compile_failed.push((entry.clone(), summary));
        }
    }

    let total = surveyed.len();
    eprintln!(
        "multi_source_run_skipped_examples: surveyed {} files, compiled {}, \
         compile-failed {}, closure-failed {}",
        total,
        compiled,
        compile_failed.len(),
        closure_failed.len()
    );
    if !compile_failed.is_empty() {
        eprintln!("compile failures:");
        for (p, e) in &compile_failed {
            eprintln!("  - {}: {}", p.display(), e);
        }
    }
    if !closure_failed.is_empty() {
        eprintln!("closure-build failures:");
        for (p, e) in &closure_failed {
            eprintln!("  - {}: {}", p.display(), e);
        }
    }

    // Pass threshold: at least 80% of surveyed files compile cleanly via
    // the bundled closure. The remainder may trip on features that need
    // richer linking we don't model in this lightweight harness.
    let threshold = (total * 80) / 100;
    assert!(
        compiled >= threshold,
        "multi_source_run_skipped_examples: only {}/{} ({}%) compiled cleanly via \
         standard-JSON multi-source — wanted at least {} ({}%). \
         This is a regression in the import resolver or in one of the example contracts.",
        compiled,
        total,
        (compiled * 100) / total.max(1),
        threshold,
        80
    );
}