solidity-language-server 0.1.36

A fast Solidity language server powered by solc and Foundry
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
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
use crate::config::FoundryConfig;
use crate::config::ProjectIndexCacheMode;
use crate::goto::{CachedBuild, NodeInfo};
use crate::types::{AbsPath, NodeId, RelPath};
use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::collections::{BTreeMap, HashMap};
use std::fs;
use std::io::Write;
use std::path::{Path, PathBuf};
use std::time::Instant;
use tiny_keccak::{Hasher, Keccak};

const CACHE_SCHEMA_VERSION_V2: u32 = 3;
const CACHE_DIR: &str = ".solidity-language-server";
const CACHE_FILE_V2: &str = "solidity-lsp-schema-v2.json";
const CACHE_SHARDS_DIR_V2: &str = "reference-index-v2";
const CACHE_SOLC_INPUT_FILE: &str = "last-solc-input.json";
const CACHE_GITIGNORE_FILE: &str = ".gitignore";
const CACHE_GITIGNORE_CONTENTS: &str = "*\n";

#[derive(Debug, Clone, Serialize, Deserialize)]
struct PersistedNodeEntry {
    id: i64,
    info: NodeInfo,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
struct PersistedExternalRef {
    src: crate::types::SrcLocation,
    decl_id: i64,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
struct PersistedFileShardV2 {
    abs_path: String,
    entries: Vec<PersistedNodeEntry>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
struct PersistedReferenceCacheV2 {
    schema_version: u32,
    project_root: String,
    config_fingerprint: String,
    file_hashes: BTreeMap<String, String>,
    #[serde(default)]
    file_hash_history: BTreeMap<String, Vec<String>>,
    path_to_abs: HashMap<String, String>,
    id_to_path_map: HashMap<crate::types::SolcFileId, String>,
    external_refs: Vec<PersistedExternalRef>,
    // relative-path -> shard file name
    node_shards: BTreeMap<String, String>,
}

#[derive(Debug, Clone)]
pub struct CacheLoadReport {
    pub build: Option<CachedBuild>,
    pub hit: bool,
    pub miss_reason: Option<String>,
    pub file_count_hashed: usize,
    pub file_count_reused: usize,
    pub complete: bool,
    pub duration_ms: u128,
}

#[derive(Debug, Clone)]
pub struct CacheSaveReport {
    pub file_count_hashed: usize,
    pub duration_ms: u128,
}

/// Public helper — returns the root of the on-disk cache directory for
/// `project_root`. Used by the `solidity.clearCache` command handler.
pub fn cache_dir(root: &Path) -> PathBuf {
    root.join(CACHE_DIR)
}

fn cache_file_path_v2(root: &Path) -> PathBuf {
    root.join(CACHE_DIR).join(CACHE_FILE_V2)
}

fn cache_shards_dir_v2(root: &Path) -> PathBuf {
    root.join(CACHE_DIR).join(CACHE_SHARDS_DIR_V2)
}

fn cache_solc_input_path(root: &Path) -> PathBuf {
    root.join(CACHE_DIR).join(CACHE_SOLC_INPUT_FILE)
}

/// Persist the last solc standard-JSON input to the cache directory so it can
/// be inspected for debugging.
///
/// Source entries that use `"content"` (in-memory editor buffers with unsaved
/// changes) are rewritten to `"urls"` in the debug file to keep it small.
/// The actual solc invocation still receives the real content.
pub fn save_last_solc_input(root: &Path, input: &Value) -> Result<(), String> {
    let cache_root = root.join(CACHE_DIR);
    fs::create_dir_all(&cache_root)
        .map_err(|e| format!("failed to create cache dir {}: {e}", cache_root.display()))?;
    let path = cache_solc_input_path(root);

    // Replace "content" entries with "urls" so the debug file stays small.
    let mut sanitised = input.clone();
    if let Some(sources) = sanitised.get_mut("sources").and_then(|s| s.as_object_mut()) {
        for (rel_path, entry) in sources.iter_mut() {
            if entry.get("content").is_some() {
                *entry = serde_json::json!({ "urls": [rel_path.clone()] });
            }
        }
    }

    let bytes = serde_json::to_vec_pretty(&sanitised)
        .map_err(|e| format!("failed to serialize solc input: {e}"))?;
    let mut file =
        fs::File::create(&path).map_err(|e| format!("failed to create {}: {e}", path.display()))?;
    file.write_all(&bytes)
        .map_err(|e| format!("failed to write {}: {e}", path.display()))?;
    Ok(())
}

fn ensure_cache_dir_layout(root: &Path) -> Result<(PathBuf, PathBuf), String> {
    let cache_root = root.join(CACHE_DIR);
    fs::create_dir_all(&cache_root)
        .map_err(|e| format!("failed to create cache dir {}: {e}", cache_root.display()))?;

    // Ensure cache artifacts are ignored by Git in consumer projects.
    let gitignore_path = cache_root.join(CACHE_GITIGNORE_FILE);
    if !gitignore_path.exists() {
        fs::write(&gitignore_path, CACHE_GITIGNORE_CONTENTS).map_err(|e| {
            format!(
                "failed to write cache gitignore {}: {e}",
                gitignore_path.display()
            )
        })?;
    }

    let shards_dir = cache_shards_dir_v2(root);
    fs::create_dir_all(&shards_dir)
        .map_err(|e| format!("failed to create shards dir {}: {e}", shards_dir.display()))?;

    Ok((cache_root, shards_dir))
}

fn shard_file_name_for_rel_path(rel_path: &str) -> String {
    format!("{}.json", keccak_hex(rel_path.as_bytes()))
}

fn write_atomic_json(path: &Path, payload: &[u8]) -> Result<(), String> {
    let tmp_path = path.with_extension(format!(
        "{}.tmp",
        path.extension()
            .and_then(|s| s.to_str())
            .unwrap_or_default()
    ));
    {
        let mut file = fs::File::create(&tmp_path)
            .map_err(|e| format!("create tmp {}: {e}", tmp_path.display()))?;
        file.write_all(payload)
            .map_err(|e| format!("write tmp {}: {e}", tmp_path.display()))?;
        file.flush()
            .map_err(|e| format!("flush tmp {}: {e}", tmp_path.display()))?;
        file.sync_all()
            .map_err(|e| format!("sync tmp {}: {e}", tmp_path.display()))?;
    }
    fs::rename(&tmp_path, path).map_err(|e| {
        format!(
            "rename tmp {} -> {}: {e}",
            tmp_path.display(),
            path.display()
        )
    })
}

fn keccak_hex(bytes: &[u8]) -> String {
    let mut out = [0u8; 32];
    let mut hasher = Keccak::v256();
    hasher.update(bytes);
    hasher.finalize(&mut out);
    hex::encode(out)
}

fn file_hash(path: &Path) -> Option<String> {
    let bytes = fs::read(path).ok()?;
    Some(keccak_hex(&bytes))
}

fn relative_to_root(root: &Path, file: &Path) -> String {
    file.strip_prefix(root)
        .unwrap_or(file)
        .to_string_lossy()
        .replace('\\', "/")
}

fn current_file_hashes(
    config: &FoundryConfig,
    include_libs: bool,
) -> Result<BTreeMap<String, String>, String> {
    let source_files = if include_libs {
        crate::solc::discover_source_files_with_libs(config)
    } else {
        crate::solc::discover_source_files(config)
    };
    hash_file_list(config, &source_files)
}

/// Hash an explicit list of absolute paths (relative to config.root).
fn hash_file_list(
    config: &FoundryConfig,
    source_files: &[PathBuf],
) -> Result<BTreeMap<String, String>, String> {
    if source_files.is_empty() {
        return Ok(BTreeMap::new());
    }
    let mut hashes = BTreeMap::new();
    for path in source_files {
        let rel = relative_to_root(&config.root, path);
        let hash = file_hash(path)
            .ok_or_else(|| format!("failed to hash source file {}", path.display()))?;
        hashes.insert(rel, hash);
    }
    Ok(hashes)
}

fn config_fingerprint(config: &FoundryConfig) -> String {
    let payload = serde_json::json!({
        // Including the server version means any binary upgrade automatically
        // invalidates the on-disk cache and triggers a clean rebuild.
        "lsp_version": env!("CARGO_PKG_VERSION"),
        "solc_version": config.solc_version,
        "remappings": config.remappings,
        "evm_version": config.evm_version,
        "sources_dir": config.sources_dir,
        "libs": config.libs,
        // via_ir changes the Yul IR pipeline which can produce different AST
        // node IDs — toggling it must invalidate the cache.
        "via_ir": config.via_ir,
    });
    keccak_hex(payload.to_string().as_bytes())
}

fn push_hash_history(meta: &mut PersistedReferenceCacheV2, rel: &str, hash: &str) {
    const MAX_HISTORY: usize = 8;
    let history = meta.file_hash_history.entry(rel.to_string()).or_default();
    if history.last().is_some_and(|h| h == hash) {
        return;
    }
    history.push(hash.to_string());
    if history.len() > MAX_HISTORY {
        let drop_count = history.len() - MAX_HISTORY;
        history.drain(0..drop_count);
    }
}

pub fn save_reference_cache(config: &FoundryConfig, build: &CachedBuild) -> Result<(), String> {
    save_reference_cache_with_report(config, build, None).map(|_| ())
}

/// Incrementally upsert v2 cache shards for changed files, serializing
/// global metadata (`path_to_abs`, `id_to_path_map`, `external_refs`) from
/// the **merged in-memory `CachedBuild`** (root-key entry in `ast_cache`).
///
/// This ensures the disk cache always mirrors the authoritative in-memory
/// state, which has correct globally-remapped file IDs from
/// `merge_scoped_cached_build`.  Only file shards for `changed_abs_paths`
/// are rewritten (the incremental fast-path); all other shards are preserved.
///
/// The full-project reconcile (`save_reference_cache_with_report`) is still
/// the canonical full save; this function bridges the gap between saves so
/// that a restart can warm-start from a reasonably up-to-date cache.
pub fn upsert_reference_cache_v2_with_report(
    config: &FoundryConfig,
    build: &CachedBuild,
    changed_abs_paths: &[String],
) -> Result<CacheSaveReport, String> {
    let started = Instant::now();
    if !config.root.is_dir() {
        return Err(format!("invalid project root: {}", config.root.display()));
    }

    let (_cache_root, shards_dir) = ensure_cache_dir_layout(&config.root)?;

    // Load existing metadata (for file_hashes and node_shards of unchanged
    // files) or start fresh.
    let meta_path = cache_file_path_v2(&config.root);
    let mut meta = if let Ok(bytes) = fs::read(&meta_path) {
        serde_json::from_slice::<PersistedReferenceCacheV2>(&bytes).unwrap_or(
            PersistedReferenceCacheV2 {
                schema_version: CACHE_SCHEMA_VERSION_V2,
                project_root: config.root.to_string_lossy().to_string(),
                config_fingerprint: config_fingerprint(config),
                file_hashes: BTreeMap::new(),
                file_hash_history: BTreeMap::new(),
                path_to_abs: HashMap::new(),
                id_to_path_map: HashMap::new(),
                external_refs: Vec::new(),
                node_shards: BTreeMap::new(),
            },
        )
    } else {
        PersistedReferenceCacheV2 {
            schema_version: CACHE_SCHEMA_VERSION_V2,
            project_root: config.root.to_string_lossy().to_string(),
            config_fingerprint: config_fingerprint(config),
            file_hashes: BTreeMap::new(),
            file_hash_history: BTreeMap::new(),
            path_to_abs: HashMap::new(),
            id_to_path_map: HashMap::new(),
            external_refs: Vec::new(),
            node_shards: BTreeMap::new(),
        }
    };

    // Reset metadata when root/fingerprint changed.
    if meta.project_root != config.root.to_string_lossy()
        || meta.config_fingerprint != config_fingerprint(config)
    {
        meta = PersistedReferenceCacheV2 {
            schema_version: CACHE_SCHEMA_VERSION_V2,
            project_root: config.root.to_string_lossy().to_string(),
            config_fingerprint: config_fingerprint(config),
            file_hashes: BTreeMap::new(),
            file_hash_history: BTreeMap::new(),
            path_to_abs: HashMap::new(),
            id_to_path_map: HashMap::new(),
            external_refs: Vec::new(),
            node_shards: BTreeMap::new(),
        };
    }

    // Write shards only for the changed files.
    let changed_set: std::collections::HashSet<&str> =
        changed_abs_paths.iter().map(|s| s.as_str()).collect();
    let mut touched = 0usize;
    for (abs_path, file_nodes) in &build.nodes {
        if !changed_set.contains(abs_path.as_str()) {
            continue;
        }
        let abs = Path::new(abs_path.as_str());
        let rel = relative_to_root(&config.root, abs);
        let shard_name = shard_file_name_for_rel_path(&rel);
        let shard_path = shards_dir.join(&shard_name);

        let mut entries = Vec::with_capacity(file_nodes.len());
        for (id, info) in file_nodes {
            entries.push(PersistedNodeEntry {
                id: id.0,
                info: info.clone(),
            });
        }
        let shard = PersistedFileShardV2 {
            abs_path: abs_path.to_string(),
            entries,
        };
        let shard_payload =
            serde_json::to_vec(&shard).map_err(|e| format!("serialize shard {}: {e}", rel))?;
        write_atomic_json(&shard_path, &shard_payload)?;

        if let Some(hash) = file_hash(abs) {
            push_hash_history(&mut meta, &rel, &hash);
            meta.file_hashes.insert(rel.clone(), hash);
            meta.node_shards.insert(rel, shard_name);
            touched += 1;
        }
    }

    // Serialize global metadata from the authoritative merged build.
    // This replaces the buggy per-file merge that previously wrote
    // un-remapped file IDs and identity path_to_abs entries.
    meta.path_to_abs = build
        .path_to_abs
        .iter()
        .map(|(k, v)| (k.to_string(), v.to_string()))
        .collect();
    meta.id_to_path_map = build.id_to_path_map.clone();
    meta.external_refs = build
        .external_refs
        .iter()
        .map(|(src, id)| PersistedExternalRef {
            src: src.clone(),
            decl_id: id.0,
        })
        .collect();

    let payload_v2 = serde_json::to_vec(&meta).map_err(|e| format!("serialize v2 cache: {e}"))?;
    write_atomic_json(&meta_path, &payload_v2)?;

    Ok(CacheSaveReport {
        file_count_hashed: touched,
        duration_ms: started.elapsed().as_millis(),
    })
}

pub fn save_reference_cache_with_report(
    config: &FoundryConfig,
    build: &CachedBuild,
    source_files: Option<&[PathBuf]>,
) -> Result<CacheSaveReport, String> {
    let started = Instant::now();
    if !config.root.is_dir() {
        return Err(format!("invalid project root: {}", config.root.display()));
    }

    // When an explicit file list is given, hash only those.
    // Otherwise derive the list from the build's node keys (the files that
    // were actually compiled) — this avoids walking unrelated lib files.
    let file_hashes = if let Some(files) = source_files {
        hash_file_list(config, files)?
    } else {
        let build_paths: Vec<PathBuf> = build
            .nodes
            .keys()
            .map(|p| PathBuf::from(p.as_str()))
            .collect();
        if build_paths.is_empty() {
            current_file_hashes(config, true)?
        } else {
            hash_file_list(config, &build_paths)?
        }
    };
    let file_count_hashed = file_hashes.len();
    let external_refs = build
        .external_refs
        .iter()
        .map(|(src, id)| PersistedExternalRef {
            src: src.clone(),
            decl_id: id.0,
        })
        .collect::<Vec<_>>();

    let (_cache_root, shards_dir) = ensure_cache_dir_layout(&config.root)?;

    let mut node_shards: BTreeMap<String, String> = BTreeMap::new();
    let mut live_shards = std::collections::HashSet::new();
    for (abs_path, file_nodes) in &build.nodes {
        let abs = Path::new(abs_path.as_str());
        let rel = relative_to_root(&config.root, abs);
        let shard_name = shard_file_name_for_rel_path(&rel);
        let shard_path = shards_dir.join(&shard_name);

        let mut entries = Vec::with_capacity(file_nodes.len());
        for (id, info) in file_nodes {
            entries.push(PersistedNodeEntry {
                id: id.0,
                info: info.clone(),
            });
        }
        let shard = PersistedFileShardV2 {
            abs_path: abs_path.to_string(),
            entries,
        };
        let shard_payload =
            serde_json::to_vec(&shard).map_err(|e| format!("serialize shard {}: {e}", rel))?;
        write_atomic_json(&shard_path, &shard_payload)?;
        node_shards.insert(rel, shard_name.clone());
        live_shards.insert(shard_name);
    }

    // Best-effort cleanup of stale shard files.
    if let Ok(dir) = fs::read_dir(&shards_dir) {
        for entry in dir.flatten() {
            let file_name = entry.file_name().to_string_lossy().to_string();
            if !live_shards.contains(&file_name) {
                let _ = fs::remove_file(entry.path());
            }
        }
    }

    let persisted_v2 = PersistedReferenceCacheV2 {
        schema_version: CACHE_SCHEMA_VERSION_V2,
        project_root: config.root.to_string_lossy().to_string(),
        config_fingerprint: config_fingerprint(config),
        file_hashes: file_hashes.clone(),
        file_hash_history: {
            let mut h = BTreeMap::new();
            for (rel, hash) in &file_hashes {
                h.insert(rel.clone(), vec![hash.clone()]);
            }
            h
        },
        path_to_abs: build
            .path_to_abs
            .iter()
            .map(|(k, v)| (k.to_string(), v.to_string()))
            .collect(),
        external_refs: external_refs.clone(),
        id_to_path_map: build.id_to_path_map.clone(),
        node_shards,
    };
    let payload_v2 =
        serde_json::to_vec(&persisted_v2).map_err(|e| format!("serialize v2 cache: {e}"))?;
    write_atomic_json(&cache_file_path_v2(&config.root), &payload_v2)?;

    Ok(CacheSaveReport {
        file_count_hashed,
        duration_ms: started.elapsed().as_millis(),
    })
}

pub fn load_reference_cache(config: &FoundryConfig) -> Option<CachedBuild> {
    load_reference_cache_with_report(config, ProjectIndexCacheMode::Auto, false).build
}

/// Discover existing LSP caches in lib sub-projects.
///
/// Result of discovering lib sub-projects.
pub struct DiscoveredLibs {
    /// Sub-projects that already have a valid `.solidity-language-server/` cache.
    pub cached: Vec<PathBuf>,
    /// Sub-projects with `foundry.toml` but no existing cache.
    pub uncached: Vec<PathBuf>,
}

/// Walks the configured `libs` directories looking for `foundry.toml` files.
/// Returns sub-project roots partitioned into those with existing caches
/// and those without.
pub fn discover_lib_sub_projects(config: &FoundryConfig) -> DiscoveredLibs {
    let mut cached = Vec::new();
    let mut uncached = Vec::new();
    for lib_dir_name in &config.libs {
        let lib_dir = config.root.join(lib_dir_name);
        if !lib_dir.is_dir() {
            continue;
        }
        discover_lib_sub_projects_recursive(&lib_dir, &mut cached, &mut uncached);
    }
    DiscoveredLibs { cached, uncached }
}

/// Backwards-compatible wrapper: returns only sub-projects that have an
/// existing cache on disk.
pub fn discover_lib_caches(config: &FoundryConfig) -> Vec<PathBuf> {
    discover_lib_sub_projects(config).cached
}

fn discover_lib_sub_projects_recursive(
    dir: &Path,
    cached: &mut Vec<PathBuf>,
    uncached: &mut Vec<PathBuf>,
) {
    let entries = match fs::read_dir(dir) {
        Ok(e) => e,
        Err(_) => return,
    };
    for entry in entries.flatten() {
        let path = entry.path();
        if !path.is_dir() {
            continue;
        }
        let Some(name) = path.file_name().and_then(|n| n.to_str()) else {
            continue;
        };
        // Skip hidden dirs and build artifacts
        if name.starts_with('.')
            || matches!(name, "out" | "cache" | "artifacts" | "target" | "broadcast")
        {
            continue;
        }
        let has_config = path.join("foundry.toml").is_file();
        if has_config {
            let has_cache = path.join(CACHE_DIR).join(CACHE_FILE_V2).is_file();
            if has_cache {
                cached.push(path.clone());
            } else {
                uncached.push(path.clone());
            }
        }
        // Always recurse deeper — nested libs (e.g. lib/v4-periphery/lib/v4-core/)
        // can have their own caches too.
        discover_lib_sub_projects_recursive(&path, cached, uncached);
    }
}

/// Load a sub-project's cache as a [`CachedBuild`].
///
/// This is a simplified version of [`load_reference_cache_with_report`] that
/// does not validate config fingerprints or file hashes — we just load
/// whatever shards are on disk.  Sub-caches are used read-only for cross-file
/// reference lookup; staleness is acceptable.
pub fn load_lib_cache(sub_root: &Path) -> Option<CachedBuild> {
    let cache_path = sub_root.join(CACHE_DIR).join(CACHE_FILE_V2);
    let bytes = fs::read(&cache_path).ok()?;
    let persisted: PersistedReferenceCacheV2 = serde_json::from_slice(&bytes).ok()?;

    if persisted.schema_version != CACHE_SCHEMA_VERSION_V2 {
        return None;
    }

    let shards_dir = sub_root.join(CACHE_DIR).join(CACHE_SHARDS_DIR_V2);
    let mut nodes: HashMap<AbsPath, HashMap<NodeId, NodeInfo>> = HashMap::new();
    let mut reused_decl_ids = std::collections::HashSet::new();

    for shard_name in persisted.node_shards.values() {
        let shard_path = shards_dir.join(shard_name);
        let shard_bytes = match fs::read(&shard_path) {
            Ok(v) => v,
            Err(_) => continue,
        };
        let shard: PersistedFileShardV2 = match serde_json::from_slice(&shard_bytes) {
            Ok(v) => v,
            Err(_) => continue,
        };
        let mut file_nodes = HashMap::with_capacity(shard.entries.len());
        for entry in shard.entries {
            reused_decl_ids.insert(entry.id);
            file_nodes.insert(NodeId(entry.id), entry.info);
        }
        nodes.insert(AbsPath::new(shard.abs_path), file_nodes);
    }

    if nodes.is_empty() {
        return None;
    }

    let mut external_refs = HashMap::new();
    for item in persisted.external_refs {
        if reused_decl_ids.contains(&item.decl_id) {
            external_refs.insert(item.src, NodeId(item.decl_id));
        }
    }

    Some(CachedBuild::from_reference_index(
        nodes,
        persisted
            .path_to_abs
            .into_iter()
            .map(|(k, v)| (RelPath::new(k), AbsPath::new(v)))
            .collect(),
        external_refs,
        persisted.id_to_path_map,
        0,
        None,
    ))
}

/// Return absolute paths of source files whose current hash differs from v2
/// cache metadata (including newly-added files missing from metadata).
pub fn changed_files_since_v2_cache(
    config: &FoundryConfig,
    include_libs: bool,
) -> Result<Vec<PathBuf>, String> {
    if !config.root.is_dir() {
        return Err(format!("invalid project root: {}", config.root.display()));
    }

    let cache_path_v2 = cache_file_path_v2(&config.root);
    let bytes = fs::read(&cache_path_v2).map_err(|e| format!("cache file read failed: {e}"))?;
    let persisted: PersistedReferenceCacheV2 =
        serde_json::from_slice(&bytes).map_err(|e| format!("cache decode failed: {e}"))?;

    if persisted.schema_version != CACHE_SCHEMA_VERSION_V2 {
        return Err(format!(
            "schema mismatch: cache={}, expected={}",
            persisted.schema_version, CACHE_SCHEMA_VERSION_V2
        ));
    }
    if persisted.project_root != config.root.to_string_lossy() {
        return Err("project root mismatch".to_string());
    }
    if persisted.config_fingerprint != config_fingerprint(config) {
        return Err("config fingerprint mismatch".to_string());
    }

    // Hash cached files and compare to saved hashes.
    let saved_paths: Vec<PathBuf> = persisted
        .file_hashes
        .keys()
        .map(|rel| config.root.join(rel))
        .collect();
    let current_hashes = hash_file_list(config, &saved_paths)?;
    let mut changed = Vec::new();
    for (rel, current_hash) in &current_hashes {
        match persisted.file_hashes.get(rel) {
            Some(prev) if prev == current_hash => {}
            _ => changed.push(config.root.join(rel)),
        }
    }

    // Detect new files: walk the source dir to find .sol files that are not
    // in the cached file list.  This ensures newly-created files trigger a
    // scoped reindex instead of silently remaining invisible until a full
    // rebuild.  When include_libs is true (fullProjectScan), we also scan
    // library directories so that newly-added lib files are picked up.
    let saved_rels: std::collections::HashSet<&String> = persisted.file_hashes.keys().collect();
    let discovered = if include_libs {
        crate::solc::discover_source_files_with_libs(config)
    } else {
        crate::solc::discover_source_files(config)
    };
    for path in &discovered {
        let rel = relative_to_root(&config.root, path);
        if !saved_rels.contains(&rel) {
            changed.push(path.clone());
        }
    }

    Ok(changed)
}

pub fn load_reference_cache_with_report(
    config: &FoundryConfig,
    cache_mode: ProjectIndexCacheMode,
    _include_libs: bool,
) -> CacheLoadReport {
    let started = Instant::now();
    let miss = |reason: String, file_count_hashed: usize, duration_ms: u128| CacheLoadReport {
        build: None,
        hit: false,
        miss_reason: Some(reason),
        file_count_hashed,
        file_count_reused: 0,
        complete: false,
        duration_ms,
    };

    if !config.root.is_dir() {
        return miss(
            format!("invalid project root: {}", config.root.display()),
            0,
            started.elapsed().as_millis(),
        );
    }

    let should_try_v2 = matches!(
        cache_mode,
        ProjectIndexCacheMode::Auto | ProjectIndexCacheMode::V2
    );

    // Try v2 first (partial warm-start capable).
    let cache_path_v2 = cache_file_path_v2(&config.root);
    if should_try_v2
        && let Ok(bytes) = fs::read(&cache_path_v2)
        && let Ok(persisted) = serde_json::from_slice::<PersistedReferenceCacheV2>(&bytes)
    {
        if persisted.schema_version != CACHE_SCHEMA_VERSION_V2 {
            return miss(
                format!(
                    "schema mismatch: cache={}, expected={}",
                    persisted.schema_version, CACHE_SCHEMA_VERSION_V2
                ),
                0,
                started.elapsed().as_millis(),
            );
        }
        if persisted.project_root != config.root.to_string_lossy() {
            return miss(
                "project root mismatch".to_string(),
                0,
                started.elapsed().as_millis(),
            );
        }
        if persisted.config_fingerprint != config_fingerprint(config) {
            return miss(
                "config fingerprint mismatch".to_string(),
                0,
                started.elapsed().as_millis(),
            );
        }

        // Hash only the files that were saved — no rediscovery needed.
        // This means we compare exactly the closure that was compiled last time.
        let saved_paths: Vec<PathBuf> = persisted
            .file_hashes
            .keys()
            .map(|rel| config.root.join(rel))
            .collect();
        let current_hashes = match hash_file_list(config, &saved_paths) {
            Ok(h) => h,
            Err(e) => return miss(e, 0, started.elapsed().as_millis()),
        };
        let file_count_hashed = current_hashes.len();

        let shards_dir = cache_shards_dir_v2(&config.root);
        let mut nodes: HashMap<AbsPath, HashMap<NodeId, NodeInfo>> = HashMap::new();
        let mut file_count_reused = 0usize;
        let mut reused_decl_ids = std::collections::HashSet::new();

        for (rel_path, current_hash) in &current_hashes {
            let Some(cached_hash) = persisted.file_hashes.get(rel_path) else {
                continue;
            };
            if cached_hash != current_hash {
                continue;
            }
            let Some(shard_name) = persisted.node_shards.get(rel_path) else {
                continue;
            };
            let shard_path = shards_dir.join(shard_name);
            let shard_bytes = match fs::read(&shard_path) {
                Ok(v) => v,
                Err(_) => continue,
            };
            let shard: PersistedFileShardV2 = match serde_json::from_slice(&shard_bytes) {
                Ok(v) => v,
                Err(_) => continue,
            };
            let mut file_nodes = HashMap::with_capacity(shard.entries.len());
            for entry in shard.entries {
                reused_decl_ids.insert(entry.id);
                file_nodes.insert(NodeId(entry.id), entry.info);
            }
            nodes.insert(AbsPath::new(shard.abs_path), file_nodes);
            file_count_reused += 1;
        }

        if file_count_reused == 0 {
            return miss(
                "v2 cache: no reusable files".to_string(),
                file_count_hashed,
                started.elapsed().as_millis(),
            );
        }

        let mut external_refs = HashMap::new();
        for item in persisted.external_refs {
            if reused_decl_ids.contains(&item.decl_id) {
                external_refs.insert(item.src, NodeId(item.decl_id));
            }
        }

        // Complete = every saved file was reused with a matching hash AND
        // the saved set covers every project file (src/test/script). The
        // second clause is what catches a phase-1-only cache: when a previous
        // run saved only the src closure, every saved file still matches on
        // disk, but project test/script files were never added to
        // `file_hashes` — so without this coverage check we'd return
        // `complete=true` and the eager indexer would skip phase 2 forever.
        let project_covered = {
            let project_files = crate::solc::discover_source_files(config);
            let saved: std::collections::HashSet<&String> = persisted.file_hashes.keys().collect();
            project_files.iter().all(|abs| {
                pathdiff::diff_paths(abs, &config.root)
                    .and_then(|rel| rel.to_str().map(|s| s.to_string()))
                    .map(|rel| saved.contains(&rel))
                    .unwrap_or(false)
            })
        };
        let complete = file_count_reused == file_count_hashed
            && current_hashes == persisted.file_hashes
            && project_covered;

        return CacheLoadReport {
            build: Some(CachedBuild::from_reference_index(
                nodes,
                persisted
                    .path_to_abs
                    .into_iter()
                    .map(|(k, v)| (RelPath::new(k), AbsPath::new(v)))
                    .collect(),
                external_refs,
                persisted.id_to_path_map,
                0,
                None,
            )),
            hit: true,
            miss_reason: if complete {
                None
            } else {
                Some("v2 cache partial reuse".to_string())
            },
            file_count_hashed,
            file_count_reused,
            complete,
            duration_ms: started.elapsed().as_millis(),
        };
    }

    miss(
        "cache mode v2: no usable v2 cache".to_string(),
        0,
        started.elapsed().as_millis(),
    )
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_save_last_solc_input_writes_cache_file() {
        let dir = tempfile::tempdir().unwrap();
        let input = serde_json::json!({
            "language": "Solidity",
            "sources": {
                "src/Foo.sol": { "urls": ["src/Foo.sol"] }
            },
            "settings": { "outputSelection": { "*": { "": ["ast"] } } }
        });

        save_last_solc_input(dir.path(), &input).unwrap();

        let saved_path = cache_solc_input_path(dir.path());
        assert!(saved_path.is_file());

        let bytes = std::fs::read(saved_path).unwrap();
        let parsed: Value = serde_json::from_slice(&bytes).unwrap();
        assert_eq!(parsed, input);
    }
}