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
/*
* rust-intelligence — Ultra-fast, zero-memory code intelligence for Rust
* Copyright (c) 2026 Alexander Panasenko
*
* Contact: alex@prod.codes
* Author: https://prod.codes/about/
* Project: https://github.com/alex09x/rust-intelligence
* SPDX-License-Identifier: MIT OR Apache-2.0
*/
//! Resilient cache lookup for external dependency index shards.
//!
//! When a workspace manifest references a dependency object path or unit key from an older
//! compiler driver run that no longer exists on disk, `CacheObjectResolver` discovers candidate
//! shards in `cache_dir/objects/` strictly matching the crate's name, version, and 64-bit generation digest.
use crate::reader::IndexReader;
use crate::workspace::manifest::DependencyShardInfo;
use std::collections::HashMap;
use std::fs;
use std::path::PathBuf;
#[derive(Debug, Clone)]
#[allow(dead_code)]
struct CachedObjectInfo {
version: String,
generation: String,
unit_key: String,
path: PathBuf,
}
/// Resolves dependency index shard files from primary paths or the global cache directory.
pub struct CacheObjectResolver {
cache_dir: PathBuf,
cache_map: Option<HashMap<String, Vec<CachedObjectInfo>>>,
}
impl CacheObjectResolver {
/// Creates a new resolver backed by the specified global cache directory.
pub fn new(cache_dir: PathBuf) -> Self {
Self { cache_dir, cache_map: None }
}
/// Resolves an existing `.ridx` shard file for the given dependency shard.
///
/// Resolution order:
/// 1. Exact `dep.object_path` if it exists on disk.
/// 2. `cache_object_path(cache_dir, dep.unit_key)` if it exists on disk.
/// 3. Resilient fallback: scans `cache_dir/objects/*/*.ridx` for shards matching `dep.name`,
/// strictly requiring exact version and generation match.
pub fn resolve(&mut self, dep: &DependencyShardInfo) -> Option<PathBuf> {
let obj_path = PathBuf::from(&dep.object_path);
if obj_path.is_file() {
return Some(obj_path);
}
let fallback = crate::format::cache_object_path(&self.cache_dir, &dep.unit_key);
if fallback.is_file() {
return Some(fallback);
}
self.resolve_resilient(dep)
}
fn resolve_resilient(&mut self, dep: &DependencyShardInfo) -> Option<PathBuf> {
let expected_gen = match dep.generation.as_deref() {
Some(g) if !g.is_empty() => g,
_ => return None,
};
if self.cache_map.is_none() {
self.populate_cache_map();
}
let map = self.cache_map.as_ref()?;
let candidates = map.get(&dep.name)?;
// Invariant & Design Decision:
// When exact object_path and unit_key fail to resolve (e.g. following a rebuild of
// ridx-driver or toolchain bump where driver hash shifts across unit_keys), resolve_resilient
// discovers candidate shards matching exact crate name, semantic version, and generation digest.
//
// While unit_key additionally hashes active Cargo features, compilation target, and driver
// version, `generation` is a 64-bit content digest over source file contents.
// In LSP and code navigation workflows, reusing a source-identical shard of the same version
// across targets or features is an accepted compromise to preserve cross-crate intelligence
// and avoid full dependency re-indexing when the driver or toolchain updates;
// steps 1 and 2 are tried first.
candidates.iter().find(|c| c.version == dep.version && c.generation == expected_gen).map(|c| c.path.clone())
}
fn populate_cache_map(&mut self) {
let mut map: HashMap<String, Vec<CachedObjectInfo>> = HashMap::new();
let objects_dir = self.cache_dir.join("objects");
if let Ok(entries) = fs::read_dir(&objects_dir) {
for entry in entries.flatten() {
let sub = entry.path();
if sub.is_dir() {
if let Ok(files) = fs::read_dir(&sub) {
for f in files.flatten() {
let p = f.path();
if p.extension().and_then(|s| s.to_str()) == Some("ridx") {
if let Ok(reader) = IndexReader::open(&p) {
let name = reader.crate_name().to_string();
if !name.is_empty() {
map.entry(name).or_default().push(CachedObjectInfo {
version: reader.crate_version().to_string(),
generation: reader.generation().to_string(),
unit_key: reader.unit_key().to_string(),
path: p,
});
}
}
}
}
}
}
}
}
self.cache_map = Some(map);
}
}