Fix crate-name import resolution for bin+lib crates

Register the Cargo package name (normalized - -> _) as an alias for
src/lib.rs in the resolver, so 'use <crate>::{...}' in src/main.rs
resolves to src/lib.rs. Adds rustdemo/Cargo.toml fixture and test
coverage; updates blast-radius test for the new transitive edge.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
endergebnisandCopilot committed 2026-09-04 03:07:47 +02:00
1 parent 0bb09da454
commit 6875998ab6
4 files changed
+64 -5

No files matched your search

+28 -2
View File
@@ -18,6 +18,30 @@ pub struct IndexStats {
pub imports: i64,
}
/// Read the `[package] name` from `Cargo.toml`, normalized to the crate
/// identifier (`-` → `_`) used in `use <crate>::…`.
fn read_crate_name(project: &Path) -> Option<String> {
let toml = std::fs::read_to_string(project.join("Cargo.toml")).ok()?;
let mut in_package = false;
for line in toml.lines() {
let l = line.trim();
if l.starts_with('[') {
in_package = l == "[package]";
continue;
}
if in_package
&& let Some(v) = l.strip_prefix("name").map(str::trim)
&& let Some(v) = v.strip_prefix('=').map(str::trim)
{
let name = v.trim_matches('"');
if !name.is_empty() {
return Some(name.replace('-', "_"));
}
}
}
None
}
/// (Re)index a project. `recreate` drops any existing index first.
pub fn run(project: &Path, db_path: &Path, recreate: bool) -> Result<IndexStats> {
if recreate && db_path.exists() {
@@ -83,8 +107,10 @@ pub fn run(project: &Path, db_path: &Path, recreate: bool) -> Result<IndexStats>
}
}
let resolver =
imports::Resolver::build(file_ids.iter().map(|(k, v)| (k.clone(), *v)).collect());
let resolver = imports::Resolver::build(
file_ids.iter().map(|(k, v)| (k.clone(), *v)).collect(),
read_crate_name(project).as_deref(),
);
for f in &changed {
let fid = file_ids[&f.rel];