hearth-graph 0.1.1

Standalone tree-sitter symbol index and module graph with injectable languages
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
//! Rust module-tree resolution.
//!
//! v1 does not reproduce the module declaration tree, including multi-level
//! inline modules, `#[path]`, `cfg`, macros, and `mod` ambiguity. To keep the
//! label constitutively sound, every outcome is `Partial`. Making Rust
//! resolution exact requires Cargo target metadata plus a declaration-tree
//! model, which is future work.

use std::{
    cmp::Ordering,
    fs, io,
    path::{Path, PathBuf},
};

use compact_str::CompactString;

use super::{
    FailedKind, ResolutionCompleteness, ResolutionOutcome, Resolve, Resolved, UnresolvedReason,
};
use crate::imports::{ImportKind, RawImport};

const COMPLETENESS: ResolutionCompleteness = ResolutionCompleteness::Partial;

/// Configuration for Rust module-tree resolution.
#[derive(Debug, Clone)]
pub struct RustResolveOptions {
    /// Absolute paths to crate-root files such as `src/lib.rs` and `src/main.rs`.
    pub crate_roots: Vec<CompactString>,
}

/// Build a Rust resolver backed by the operating system filesystem.
pub fn rust_resolver(options: RustResolveOptions) -> Box<dyn Resolve> {
    Box::new(RustResolver { options })
}

struct RustResolver {
    options: RustResolveOptions,
}

impl Resolve for RustResolver {
    fn baseline_completeness(&self) -> ResolutionCompleteness {
        COMPLETENESS
    }

    fn resolve(&self, from_file: &str, import: &RawImport) -> ResolutionOutcome {
        let from_path = Path::new(from_file);
        debug_assert!(
            from_path.is_absolute(),
            "from_file must be absolute: {from_file}"
        );
        if !from_path.is_absolute() {
            return unresolved(
                failed(FailedKind::InvalidSpecifier, "from_file must be absolute"),
                Vec::new(),
            );
        }
        let Some(_) = from_path.parent() else {
            return unresolved(
                failed(
                    FailedKind::InvalidSpecifier,
                    "from_file must have a parent directory",
                ),
                Vec::new(),
            );
        };

        match import.kind {
            ImportKind::RustMod => self.resolve_mod(from_path, import.specifier.as_str()),
            ImportKind::RustUse => self.resolve_use(from_path, import.specifier.as_str()),
            _ => unresolved(UnresolvedReason::Unsupported, Vec::new()),
        }
    }

    fn clear_cache(&self) {}
}

impl RustResolver {
    fn resolve_use(&self, from_path: &Path, specifier: &str) -> ResolutionOutcome {
        let segments: Vec<&str> = specifier.split("::").collect();
        let Some(first) = segments.first().copied() else {
            return unresolved(UnresolvedReason::NotFound, Vec::new());
        };

        match first {
            "crate" => self.resolve_from_crate_root(from_path, &segments[1..]),
            "self" => {
                let Some(base) = children_directory(from_path, self.is_crate_root_file(from_path))
                else {
                    return invalid_from_file();
                };
                resolve_segments(base, &segments[1..], Some(from_path.to_path_buf()))
            }
            "super" => self.resolve_super(from_path, &segments),
            external => self.resolve_bare(from_path, external, &segments[1..]),
        }
    }

    fn resolve_mod(&self, from_file: &Path, specifier: &str) -> ResolutionOutcome {
        let Some(base) = children_directory(from_file, self.is_crate_root_file(from_file)) else {
            return invalid_from_file();
        };
        let mut dependencies = Vec::new();
        match probe_module(&base, specifier, &mut dependencies, false) {
            Ok(Some(found)) => resolved_path(found, dependencies),
            Ok(None) => unresolved(UnresolvedReason::NotFound, dependencies),
            Err(error) => io_failure(error, dependencies),
        }
    }

    fn resolve_from_crate_root(&self, from_file: &Path, segments: &[&str]) -> ResolutionOutcome {
        let Some(root) = self.select_crate_root(from_file) else {
            return unresolved(UnresolvedReason::NotFound, Vec::new());
        };
        let Some(root_dir) = root.parent() else {
            return invalid_from_file();
        };
        resolve_segments(root_dir.to_path_buf(), segments, Some(root))
    }

    fn resolve_bare(&self, from_file: &Path, first: &str, remaining: &[&str]) -> ResolutionOutcome {
        let Some(base) = children_directory(from_file, self.is_crate_root_file(from_file)) else {
            return invalid_from_file();
        };
        let mut dependencies = Vec::new();
        match probe_module(&base, first, &mut dependencies, true) {
            Ok(Some(found)) => resolve_segments_with_dependencies(
                base.join(first),
                remaining,
                Some(found),
                dependencies,
            ),
            Ok(None) => ResolutionOutcome {
                resolved: Resolved::External(first.into()),
                dependencies,
                notes: Vec::new(),
                completeness: COMPLETENESS,
            },
            Err(error) => io_failure(error, dependencies),
        }
    }

    fn resolve_super(&self, from_file: &Path, segments: &[&str]) -> ResolutionOutcome {
        if self.is_crate_root_file(from_file) {
            return unresolved(UnresolvedReason::NotFound, Vec::new());
        }

        let crate_root = self.select_crate_root(from_file);
        let root_directory = crate_root
            .as_deref()
            .and_then(Path::parent)
            .map(Path::to_path_buf);
        let Some(mut logical_directory) = children_directory(from_file, false) else {
            return invalid_from_file();
        };
        let super_count = segments
            .iter()
            .take_while(|segment| **segment == "super")
            .count();
        let mut dependencies = Vec::new();
        let mut seed = None;

        for _ in 0..super_count {
            if root_directory
                .as_deref()
                .is_some_and(|root| logical_directory == root)
                || is_standard_source_root(&logical_directory)
            {
                return unresolved(UnresolvedReason::NotFound, dependencies);
            }
            let Some(ancestor_directory) = logical_directory.parent().map(Path::to_path_buf) else {
                return unresolved(UnresolvedReason::NotFound, dependencies);
            };
            if root_directory
                .as_deref()
                .is_some_and(|root| !ancestor_directory.starts_with(root))
            {
                return unresolved(UnresolvedReason::NotFound, dependencies);
            }

            seed = match ancestor_module_file(
                &ancestor_directory,
                crate_root.as_deref(),
                &mut dependencies,
            ) {
                Ok(seed) => seed,
                Err(error) => {
                    return io_failure(error, dependencies);
                }
            };
            logical_directory = ancestor_directory;
        }

        resolve_segments_with_dependencies(
            logical_directory,
            &segments[super_count..],
            seed,
            dependencies,
        )
    }

    fn select_crate_root(&self, from_file: &Path) -> Option<PathBuf> {
        if is_implicit_crate_root(from_file) {
            return Some(from_file.to_path_buf());
        }
        let from_dir = from_file.parent()?;
        let mut roots: Vec<PathBuf> = self
            .options
            .crate_roots
            .iter()
            .map(|root| PathBuf::from(root.as_str()))
            .filter(|root| {
                root.parent()
                    .is_some_and(|root_dir| from_dir.starts_with(root_dir))
            })
            .collect();
        roots.sort_unstable_by(|left, right| compare_crate_roots(left, right));
        roots.dedup();
        roots.into_iter().next()
    }

    fn is_crate_root_file(&self, path: &Path) -> bool {
        is_implicit_crate_root(path)
            || matches!(
                path.file_name().and_then(|name| name.to_str()),
                Some("lib.rs" | "main.rs")
            )
            || self
                .options
                .crate_roots
                .iter()
                .any(|root| Path::new(root.as_str()) == path)
    }
}

fn resolve_segments(base: PathBuf, segments: &[&str], seed: Option<PathBuf>) -> ResolutionOutcome {
    resolve_segments_with_dependencies(base, segments, seed, Vec::new())
}

fn resolve_segments_with_dependencies(
    base: PathBuf,
    segments: &[&str],
    seed: Option<PathBuf>,
    mut dependencies: Vec<CompactString>,
) -> ResolutionOutcome {
    match walk_segments(base, segments, seed, &mut dependencies) {
        Ok(Some(found)) => resolved_path(found, dependencies),
        Ok(None) => unresolved(UnresolvedReason::NotFound, dependencies),
        Err(error) => io_failure(error, dependencies),
    }
}

fn walk_segments(
    mut directory: PathBuf,
    segments: &[&str],
    seed: Option<PathBuf>,
    dependencies: &mut Vec<CompactString>,
) -> Result<Option<PathBuf>, CandidateIoError> {
    let mut last_found = seed;
    for segment in segments {
        let file_candidate = directory.join(format!("{segment}.rs"));
        dependencies.push(compact_path(&file_candidate));
        if candidate_is_file(&file_candidate)? {
            last_found = Some(file_candidate);
            directory.push(segment);
            continue;
        }

        let module_candidate = directory.join(segment).join("mod.rs");
        dependencies.push(compact_path(&module_candidate));
        if candidate_is_file(&module_candidate)? {
            last_found = Some(module_candidate);
            directory.push(segment);
            continue;
        }
        break;
    }
    Ok(last_found)
}

fn probe_module(
    directory: &Path,
    segment: &str,
    dependencies: &mut Vec<CompactString>,
    track_both_candidates: bool,
) -> Result<Option<PathBuf>, CandidateIoError> {
    let file_candidate = directory.join(format!("{segment}.rs"));
    let module_candidate = directory.join(segment).join("mod.rs");
    dependencies.push(compact_path(&file_candidate));
    if track_both_candidates {
        dependencies.push(compact_path(&module_candidate));
    }
    if candidate_is_file(&file_candidate)? {
        return Ok(Some(file_candidate));
    }
    if !track_both_candidates {
        dependencies.push(compact_path(&module_candidate));
    }
    candidate_is_file(&module_candidate).map(|found| found.then_some(module_candidate))
}

fn ancestor_module_file(
    directory: &Path,
    crate_root: Option<&Path>,
    dependencies: &mut Vec<CompactString>,
) -> Result<Option<PathBuf>, CandidateIoError> {
    if let Some(root) = crate_root
        && root.parent() == Some(directory)
    {
        return Ok(Some(root.to_path_buf()));
    }

    if is_standard_source_root(directory) {
        for name in ["lib.rs", "main.rs"] {
            let candidate = directory.join(name);
            dependencies.push(compact_path(&candidate));
            if candidate_is_file(&candidate)? {
                return Ok(Some(candidate));
            }
        }
        return Ok(None);
    }

    let file_candidate = directory.with_extension("rs");
    dependencies.push(compact_path(&file_candidate));
    if candidate_is_file(&file_candidate)? {
        return Ok(Some(file_candidate));
    }
    let module_candidate = directory.join("mod.rs");
    dependencies.push(compact_path(&module_candidate));
    candidate_is_file(&module_candidate).map(|found| found.then_some(module_candidate))
}

fn children_directory(from_file: &Path, crate_root: bool) -> Option<PathBuf> {
    let parent = from_file.parent()?;
    if crate_root
        || matches!(
            from_file.file_name().and_then(|name| name.to_str()),
            Some("mod.rs" | "lib.rs" | "main.rs")
        )
    {
        Some(parent.to_path_buf())
    } else {
        Some(parent.join(from_file.file_stem()?))
    }
}

fn is_implicit_crate_root(path: &Path) -> bool {
    if path.extension().and_then(|extension| extension.to_str()) != Some("rs") {
        return false;
    }
    let Some(parent) = path.parent() else {
        return false;
    };
    match parent.file_name().and_then(|name| name.to_str()) {
        Some("examples" | "tests") => true,
        Some("bin") => parent
            .parent()
            .and_then(Path::file_name)
            .is_some_and(|name| name == "src"),
        _ => false,
    }
}

fn is_standard_source_root(path: &Path) -> bool {
    path.file_name().is_some_and(|name| name == "src")
}

fn compare_crate_roots(left: &Path, right: &Path) -> Ordering {
    let left_dir = left.parent().expect("filtered crate root has a parent");
    let right_dir = right.parent().expect("filtered crate root has a parent");
    right_dir
        .components()
        .count()
        .cmp(&left_dir.components().count())
        .then_with(|| crate_root_priority(left).cmp(&crate_root_priority(right)))
        .then_with(|| left.cmp(right))
}

fn crate_root_priority(path: &Path) -> u8 {
    match path.file_name().and_then(|name| name.to_str()) {
        Some("lib.rs") => 0,
        Some("main.rs") => 1,
        _ => 2,
    }
}

fn is_file(path: &Path) -> io::Result<bool> {
    match fs::metadata(path) {
        Ok(metadata) => Ok(metadata.is_file()),
        Err(error) if error.kind() == io::ErrorKind::NotFound => Ok(false),
        Err(error) => Err(error),
    }
}

fn candidate_is_file(path: &Path) -> Result<bool, CandidateIoError> {
    is_file(path).map_err(|source| CandidateIoError {
        path: path.to_path_buf(),
        source,
    })
}

fn resolved_path(path: PathBuf, dependencies: Vec<CompactString>) -> ResolutionOutcome {
    ResolutionOutcome {
        resolved: Resolved::Path(compact_path(&path)),
        dependencies,
        notes: Vec::new(),
        completeness: COMPLETENESS,
    }
}

fn unresolved(reason: UnresolvedReason, dependencies: Vec<CompactString>) -> ResolutionOutcome {
    ResolutionOutcome {
        resolved: Resolved::Unresolved(reason),
        dependencies,
        notes: Vec::new(),
        completeness: COMPLETENESS,
    }
}

fn invalid_from_file() -> ResolutionOutcome {
    unresolved(
        failed(
            FailedKind::InvalidSpecifier,
            "from_file must have a file name and parent directory",
        ),
        Vec::new(),
    )
}

fn io_failure(error: CandidateIoError, dependencies: Vec<CompactString>) -> ResolutionOutcome {
    unresolved(
        failed(
            FailedKind::Io,
            format!(
                "could not inspect {}: {}",
                error.path.display(),
                error.source
            ),
        ),
        dependencies,
    )
}

fn failed(kind: FailedKind, detail: impl Into<CompactString>) -> UnresolvedReason {
    UnresolvedReason::Failed {
        kind,
        detail: detail.into(),
    }
}

fn compact_path(path: &Path) -> CompactString {
    CompactString::from(path.to_string_lossy().as_ref())
}

struct CandidateIoError {
    path: PathBuf,
    source: io::Error,
}