Skip to main content

code_atlas/
analyze.rs

1use std::{
2    collections::{HashMap, HashSet},
3    env, fs,
4    io::{BufRead, BufReader, Read, Write},
5    path::{Path, PathBuf},
6    process::{Command, Stdio},
7    sync::OnceLock,
8    thread,
9};
10
11use anyhow::Result;
12use regex::Regex;
13use syn::{Expr, spanned::Spanned, visit::Visit};
14
15use serde::{Deserialize, Serialize};
16
17use crate::{
18    model::{AnalyzerReport, Atlas, Callsite, LanguageCoverage, NodeId},
19    scip::{ScipDocument, ScipIndex, ScipRange, decode_index},
20};
21
22const CACHE_VERSION: u32 = 10;
23const FILE_MARKER: &str = "__CODE_ATLAS_FILE__\t";
24const END_MARKER: &str = "__CODE_ATLAS_END__";
25const ERROR_MARKER: &str = "__CODE_ATLAS_ERROR__\t";
26const SCRIPT_CALL_MARKER: &str = "__CODE_ATLAS_SCRIPT_CALL__\t";
27const BEAM_CALL_MARKER: &str = "__CODE_ATLAS_BEAM_CALL__\t";
28const XREF_BATCH_EXPRESSION: &str = r#"
29for path <- IO.stream(:stdio, :line) do
30  path = String.trim(path)
31  IO.puts("__CODE_ATLAS_FILE__\t" <> path)
32  Mix.Task.reenable("xref")
33  try do
34    Mix.Tasks.Xref.run(["trace", path, "--no-compile", "--no-deps-check"])
35  rescue
36    error -> IO.puts("__CODE_ATLAS_ERROR__\t" <> Exception.message(error))
37  catch
38    kind, reason -> IO.puts("__CODE_ATLAS_ERROR__\t" <> Exception.format(kind, reason, __STACKTRACE__))
39  end
40  IO.puts("__CODE_ATLAS_END__")
41end
42"#;
43const ELIXIR_SCRIPT_EXPRESSION: &str = r#"
44for raw_path <- IO.stream(:stdio, :line) do
45  path = String.trim(raw_path)
46  IO.puts("__CODE_ATLAS_FILE__\t" <> path)
47  case File.read(path) do
48    {:ok, contents} ->
49      parsed =
50        if String.ends_with?(path, ".eex") do
51          try do
52            {:ok, EEx.compile_string(contents, file: path, line: 1)}
53          rescue
54            error -> {:error, error}
55          end
56        else
57          Code.string_to_quoted(contents, columns: true)
58        end
59      case parsed do
60        {:ok, ast} ->
61          {_ast, aliases} = Macro.prewalk(ast, %{}, fn
62            {:alias, _, [{:__aliases__, _, parts} | options]} = node, aliases ->
63              full = Enum.map_join(parts, ".", &Atom.to_string/1)
64              short = case Keyword.get(List.first(options) || [], :as) do
65                {:__aliases__, _, as_parts} -> as_parts |> List.last() |> Atom.to_string()
66                _ -> parts |> List.last() |> Atom.to_string()
67              end
68              {node, Map.put(aliases, short, full)}
69            node, aliases -> {node, aliases}
70          end)
71          Macro.prewalk(ast, fn
72            {{:., _, [{:__aliases__, _, parts}, function]}, meta, arguments} = node
73                when is_atom(function) and is_list(arguments) ->
74              names = Enum.map(parts, &Atom.to_string/1)
75              module = case names do
76                [first | rest] ->
77                  case Map.get(aliases, first) do
78                    nil -> Enum.join(names, ".")
79                    resolved -> Enum.join([resolved | rest], ".")
80                  end
81                [] -> ""
82              end
83              IO.puts("__CODE_ATLAS_SCRIPT_CALL__\t#{meta[:line] || 1}\t#{module}\t#{function}\t#{length(arguments)}")
84              node
85            node -> node
86          end)
87        {:error, error} ->
88          message = error |> inspect(limit: 10) |> String.replace("\n", " ") |> String.replace("\r", " ") |> String.replace("\t", " ")
89          IO.puts("__CODE_ATLAS_ERROR__\t" <> message)
90      end
91    {:error, error} -> IO.puts("__CODE_ATLAS_ERROR__\t" <> inspect(error))
92  end
93  IO.puts("__CODE_ATLAS_END__")
94end
95"#;
96const ELIXIR_BEAM_EXPRESSION: &str = r#"
97defmodule CodeAtlas.BeamCalls do
98  def scan(forms, project_modules) do
99    forms
100    |> calls(project_modules, [])
101    |> Enum.reverse()
102  end
103
104  defp calls({:call, annotation, {:remote, _, {:atom, _, module}, {:atom, _, function}}, arguments}, project_modules, calls) do
105    calls = Enum.reduce(arguments, calls, &calls(&1, project_modules, &2))
106    if MapSet.member?(project_modules, module) do
107      [{line(annotation), module, function, length(arguments)} | calls]
108    else
109      calls
110    end
111  end
112
113  defp calls(tuple, project_modules, calls) when is_tuple(tuple) do
114    tuple
115    |> Tuple.to_list()
116    |> Enum.reduce(calls, &calls(&1, project_modules, &2))
117  end
118
119  defp calls(list, project_modules, calls) when is_list(list) do
120    Enum.reduce(list, calls, &calls(&1, project_modules, &2))
121  end
122
123  defp calls(_, _, calls), do: calls
124
125  defp line(annotation) do
126    case :erl_anno.line(annotation) do
127      line when is_integer(line) -> max(line, 1)
128      _ -> 1
129    end
130  rescue
131    _ -> 1
132  end
133end
134
135requested =
136  IO.stream(:stdio, :line)
137  |> Stream.map(&String.trim/1)
138  |> Stream.reject(&(&1 == ""))
139  |> MapSet.new()
140
141project_modules =
142  Mix.Project.config()[:app]
143  |> Application.spec(:modules)
144  |> List.wrap()
145  |> MapSet.new()
146
147Mix.Project.compile_path()
148|> Path.join("*.beam")
149|> Path.wildcard()
150|> Enum.sort()
151|> Task.async_stream(
152  fn beam ->
153    case :beam_lib.chunks(String.to_charlist(beam), [:abstract_code, :compile_info]) do
154      {:ok, {_, chunks}} ->
155        with {:raw_abstract_v1, forms} <- chunks[:abstract_code],
156             source when not is_nil(source) <- chunks[:compile_info][:source] do
157          source = source |> to_string() |> Path.expand() |> Path.relative_to_cwd()
158          if MapSet.member?(requested, source) do
159            {:ok, source, CodeAtlas.BeamCalls.scan(forms, project_modules)}
160          else
161            :skip
162          end
163        else
164          _ -> :skip
165        end
166
167      _ ->
168        :skip
169    end
170  end,
171  ordered: true,
172  max_concurrency: System.schedulers_online(),
173  timeout: :infinity
174)
175|> Enum.each(fn
176  {:ok, {:ok, source, calls}} ->
177    IO.puts("__CODE_ATLAS_FILE__\t" <> source)
178    Enum.each(calls, fn {line, module, function, arity} ->
179      IO.puts("__CODE_ATLAS_BEAM_CALL__\t#{line}\t#{inspect(module)}\t#{function}\t#{arity}")
180    end)
181    IO.puts("__CODE_ATLAS_END__")
182
183  _ ->
184    :ok
185end)
186"#;
187
188#[derive(Debug, Clone, PartialEq, Eq)]
189pub struct XrefCall {
190    pub source_line: u32,
191    pub module: String,
192    pub function: String,
193    pub arity: u32,
194    pub kind: String,
195}
196
197#[derive(Debug, Clone)]
198struct RustlerTarget {
199    file: NodeId,
200    line: Option<u32>,
201}
202
203#[derive(Debug)]
204struct BeamCall {
205    source: NodeId,
206    source_line: u32,
207    module: String,
208    function: String,
209    arity: u32,
210}
211
212#[derive(Debug, Default)]
213struct ElixirSourceIndex {
214    module_files: HashMap<String, NodeId>,
215    definition_lines: HashMap<NodeId, HashMap<String, u32>>,
216}
217
218#[derive(Debug, Default)]
219struct ErlangSourceIndex {
220    module_files: HashMap<String, NodeId>,
221    definition_lines: HashMap<NodeId, HashMap<String, u32>>,
222}
223
224fn mix_project_roots(atlas: &Atlas) -> Vec<String> {
225    let mut roots: Vec<_> = atlas
226        .nodes
227        .iter()
228        .filter(|node| {
229            node.is_file() && (node.path == "mix.exs" || node.path.ends_with("/mix.exs"))
230        })
231        .filter_map(|node| {
232            Path::new(&node.path)
233                .parent()
234                .map(|parent| parent.to_string_lossy().trim_end_matches('/').to_owned())
235        })
236        .collect();
237    roots.sort_by_key(|root| (root.matches('/').count(), root.clone()));
238    roots.dedup();
239    roots
240}
241
242fn elixir_files_for_project(
243    atlas: &Atlas,
244    project_root: &str,
245    project_roots: &[String],
246) -> Vec<(String, NodeId)> {
247    atlas
248        .nodes
249        .iter()
250        .filter(|node| node.is_file() && node.path.ends_with(".ex"))
251        .filter(|node| nearest_project_root(&node.path, project_roots) == Some(project_root))
252        .map(|node| {
253            let input_path = if project_root.is_empty() {
254                node.path.clone()
255            } else {
256                node.path
257                    .strip_prefix(project_root)
258                    .and_then(|path| path.strip_prefix('/'))
259                    .unwrap_or(&node.path)
260                    .to_owned()
261            };
262            (input_path, node.id)
263        })
264        .collect()
265}
266
267fn nearest_project_root<'a>(path: &str, project_roots: &'a [String]) -> Option<&'a str> {
268    project_roots
269        .iter()
270        .filter(|root| {
271            root.is_empty()
272                || path == root.as_str()
273                || path
274                    .strip_prefix(root.as_str())
275                    .is_some_and(|suffix| suffix.starts_with('/'))
276        })
277        .max_by_key(|root| root.len())
278        .map(String::as_str)
279}
280
281fn display_project_root(root: &str) -> &str {
282    if root.is_empty() { "." } else { root }
283}
284
285pub fn analyze_calls(atlas: &mut Atlas) -> Result<()> {
286    if load_cached_analysis(atlas)? {
287        exclude_same_file_calls(atlas);
288        eprintln!(
289            "Loaded {} cross-file callsites from the repository cache ({} same-file callsites excluded).",
290            atlas.calls.len(),
291            atlas.report.same_file_calls_excluded
292        );
293        return Ok(());
294    }
295
296    initialize_language_coverage(atlas);
297
298    let elixir_index = elixir_source_index(atlas)?;
299    let rustler_targets = rustler_targets(atlas, &elixir_index.module_files)?;
300    let mut next_id = atlas.calls.len() as u64;
301
302    let project_roots = mix_project_roots(atlas);
303    if !project_roots.is_empty() {
304        for project_root in &project_roots {
305            let files = elixir_files_for_project(atlas, project_root, &project_roots);
306            analyze_elixir_calls(
307                atlas,
308                &elixir_index,
309                &rustler_targets,
310                &mut next_id,
311                project_root,
312                files,
313            );
314        }
315    } else if atlas
316        .nodes
317        .iter()
318        .any(|node| node.is_file() && node.language == "elixir")
319    {
320        atlas
321            .report
322            .warnings
323            .push("no mix.exs found; Elixir xref analyzer was skipped".to_owned());
324    }
325    analyze_elixir_scripts(atlas, &elixir_index, &mut next_id)?;
326    analyze_erlang_calls(atlas, &elixir_index, &mut next_id)?;
327
328    analyze_typescript_calls(atlas, &mut next_id)?;
329    analyze_gleam_calls(atlas, &mut next_id)?;
330    analyze_rust_calls(atlas, &mut next_id)?;
331
332    save_cached_analysis(atlas)?;
333
334    exclude_same_file_calls(atlas);
335
336    Ok(())
337}
338
339fn initialize_language_coverage(atlas: &mut Atlas) {
340    for language in [
341        "elixir",
342        "erlang",
343        "rust",
344        "javascript",
345        "typescript",
346        "gleam",
347    ] {
348        let files_discovered = atlas
349            .nodes
350            .iter()
351            .filter(|node| node.is_file() && node.language == language)
352            .count();
353        atlas.report.language_coverage.insert(
354            language.to_owned(),
355            LanguageCoverage {
356                provider: "none".to_owned(),
357                fidelity: "not applicable".to_owned(),
358                files_discovered,
359                ..LanguageCoverage::default()
360            },
361        );
362    }
363    if coverage_mut(atlas, "elixir").files_discovered > 0 {
364        let elixir = coverage_mut(atlas, "elixir");
365        elixir.provider = "mix-xref".to_owned();
366        elixir.fidelity = "compiler-backed project files".to_owned();
367    }
368    if coverage_mut(atlas, "erlang").files_discovered > 0 {
369        let erlang = coverage_mut(atlas, "erlang");
370        erlang.provider = "erlang-remote-static".to_owned();
371        erlang.fidelity =
372            "static in-repository remote calls, function references, and imports".to_owned();
373    }
374    if coverage_mut(atlas, "gleam").files_discovered > 0 {
375        let gleam = coverage_mut(atlas, "gleam");
376        gleam.provider = "gleam-import-static".to_owned();
377        gleam.fidelity = "conservative explicit-import resolution".to_owned();
378    }
379}
380
381fn coverage_mut<'a>(atlas: &'a mut Atlas, language: &str) -> &'a mut LanguageCoverage {
382    atlas
383        .report
384        .language_coverage
385        .entry(language.to_owned())
386        .or_default()
387}
388
389fn exclude_same_file_calls(atlas: &mut Atlas) {
390    let before = atlas.calls.len();
391    atlas.calls.retain(|call| call.source != call.target);
392    atlas.report.same_file_calls_excluded = before - atlas.calls.len();
393}
394
395fn analyze_elixir_calls(
396    atlas: &mut Atlas,
397    elixir_index: &ElixirSourceIndex,
398    rustler_targets: &HashMap<(String, String), RustlerTarget>,
399    next_id: &mut u64,
400    project_root: &str,
401    files: Vec<(String, NodeId)>,
402) -> bool {
403    // Compile once so the fast path can read exact calls from BEAM debug data.
404    let compile = Command::new("mix")
405        .arg("compile")
406        .env("MIX_OS_CONCURRENCY_LOCK", "0")
407        .current_dir(atlas.root_path.join(project_root))
408        .output();
409    match compile {
410        Ok(output) if output.status.success() => {}
411        Ok(output) => {
412            atlas.report.warnings.push(format!(
413                "mix compile failed in {} before xref analysis; prepare the checkout with its documented toolchain and dependencies: {}",
414                display_project_root(project_root),
415                String::from_utf8_lossy(&output.stderr).trim()
416            ));
417            return false;
418        }
419        Err(error) => {
420            atlas.report.warnings.push(format!(
421                "could not run mix compile in {} before xref analysis: {error}",
422                display_project_root(project_root)
423            ));
424            return false;
425        }
426    }
427
428    if files.is_empty() {
429        return true;
430    }
431
432    let analyzed = match analyze_elixir_beam_calls(
433        atlas,
434        elixir_index,
435        rustler_targets,
436        next_id,
437        project_root,
438        &files,
439    ) {
440        Ok(analyzed) => analyzed,
441        Err(error) => {
442            atlas.report.warnings.push(format!(
443                "could not read compiled Elixir calls in {}: {error:#}; falling back to mix xref",
444                display_project_root(project_root)
445            ));
446            HashSet::new()
447        }
448    };
449    let missing: Vec<_> = files
450        .into_iter()
451        .filter(|(_, id)| !analyzed.contains(id))
452        .collect();
453    if missing.is_empty() {
454        return true;
455    }
456    if !analyzed.is_empty() {
457        let coverage = coverage_mut(atlas, "elixir");
458        coverage.provider = "compiled-beam + xref-fallback".to_owned();
459        coverage.fidelity =
460            "expanded compiled calls with exact lines; compiler-traced fallback files".to_owned();
461    }
462    eprintln!(
463        "Falling back to mix xref for {} Elixir files without compiled debug data in {}…",
464        missing.len(),
465        display_project_root(project_root)
466    );
467    analyze_elixir_xref_calls(
468        atlas,
469        elixir_index,
470        rustler_targets,
471        next_id,
472        project_root,
473        missing,
474    )
475}
476
477fn analyze_elixir_beam_calls(
478    atlas: &mut Atlas,
479    elixir_index: &ElixirSourceIndex,
480    rustler_targets: &HashMap<(String, String), RustlerTarget>,
481    next_id: &mut u64,
482    project_root: &str,
483    files: &[(String, NodeId)],
484) -> Result<HashSet<NodeId>> {
485    eprintln!(
486        "Reading compiled call data for {} Elixir files in {}…",
487        files.len(),
488        display_project_root(project_root)
489    );
490    let source_ids: HashMap<_, _> = files.iter().cloned().collect();
491    let mut child = Command::new("mix")
492        .args([
493            "run",
494            "--no-compile",
495            "--no-deps-check",
496            "--no-start",
497            "-e",
498            ELIXIR_BEAM_EXPRESSION,
499        ])
500        .env("MIX_OS_CONCURRENCY_LOCK", "0")
501        .current_dir(atlas.root_path.join(project_root))
502        .stdin(Stdio::piped())
503        .stdout(Stdio::piped())
504        .stderr(Stdio::piped())
505        .spawn()?;
506    let mut stdin = child.stdin.take().expect("piped stdin");
507    let input_paths: Vec<_> = files.iter().map(|(path, _)| path.clone()).collect();
508    let writer = thread::spawn(move || {
509        for path in input_paths {
510            writeln!(stdin, "{path}")?;
511        }
512        Ok::<_, std::io::Error>(())
513    });
514    let mut stderr = child.stderr.take().expect("piped stderr");
515    let stderr_reader = thread::spawn(move || {
516        let mut contents = String::new();
517        stderr.read_to_string(&mut contents).map(|_| contents)
518    });
519    let stdout = child.stdout.take().expect("piped stdout");
520    let mut current = None;
521    let mut analyzed = HashSet::new();
522    let mut calls = Vec::new();
523    for line in BufReader::new(stdout).lines() {
524        let line = line?;
525        if let Some(path) = line.strip_prefix(FILE_MARKER) {
526            current = source_ids.get(path).copied();
527            continue;
528        }
529        if line == END_MARKER {
530            if let Some(source) = current {
531                if analyzed.insert(source)
532                    && (analyzed.len().is_multiple_of(1_000) || analyzed.len() == files.len())
533                {
534                    eprintln!(
535                        "  read {}/{} compiled Elixir files in {}",
536                        analyzed.len(),
537                        files.len(),
538                        display_project_root(project_root)
539                    );
540                }
541            }
542            current = None;
543            continue;
544        }
545        let Some(payload) = line.strip_prefix(BEAM_CALL_MARKER) else {
546            continue;
547        };
548        let Some(source) = current else {
549            continue;
550        };
551        let mut fields = payload.splitn(4, '\t');
552        let Some(source_line) = fields.next().and_then(|value| value.parse::<u32>().ok()) else {
553            continue;
554        };
555        let Some(module) = fields.next() else {
556            continue;
557        };
558        let Some(function) = fields.next() else {
559            continue;
560        };
561        let Some(arity) = fields.next().and_then(|value| value.parse::<u32>().ok()) else {
562            continue;
563        };
564        calls.push(BeamCall {
565            source,
566            source_line,
567            module: module.to_owned(),
568            function: function.to_owned(),
569            arity,
570        });
571    }
572    writer
573        .join()
574        .expect("BEAM input writer panicked")
575        .map_err(|error| anyhow::anyhow!("could not send BEAM input: {error}"))?;
576    let status = child.wait()?;
577    let stderr = stderr_reader
578        .join()
579        .expect("BEAM stderr reader panicked")
580        .unwrap_or_default();
581    if !status.success() {
582        anyhow::bail!(
583            "compiled BEAM scanner exited with {status}: {}",
584            stderr.trim()
585        );
586    }
587
588    atlas.report.elixir_files_traced += analyzed.len();
589    coverage_mut(atlas, "elixir").files_analyzed += analyzed.len();
590    for call in calls {
591        let key = (call.module.clone(), call.function.clone());
592        let (target, target_line, analyzer, confidence) =
593            if let Some(target) = rustler_targets.get(&key) {
594                atlas.report.rustler_calls += 1;
595                (target.file, target.line, "elixir-beam+rustler", 0.98)
596            } else if let Some(target) = elixir_index.module_files.get(&call.module) {
597                (
598                    *target,
599                    definition_line(elixir_index, *target, &call.function),
600                    "elixir-beam",
601                    0.95,
602                )
603            } else {
604                atlas.report.unresolved_calls += 1;
605                coverage_mut(atlas, "elixir").callsites_unresolved += 1;
606                continue;
607            };
608        atlas.calls.push(Callsite {
609            id: *next_id,
610            source: call.source,
611            source_line: call.source_line,
612            target,
613            target_line,
614            callee: format!("{}.{}/{}", call.module, call.function, call.arity),
615            kind: "runtime".to_owned(),
616            analyzer: analyzer.to_owned(),
617            confidence,
618        });
619        *next_id += 1;
620        atlas.report.resolved_calls += 1;
621        coverage_mut(atlas, "elixir").callsites_resolved += 1;
622    }
623    let coverage = coverage_mut(atlas, "elixir");
624    coverage.provider = "compiled-beam".to_owned();
625    coverage.fidelity =
626        "expanded remote calls from compiled debug data with exact source lines".to_owned();
627    Ok(analyzed)
628}
629
630fn analyze_elixir_xref_calls(
631    atlas: &mut Atlas,
632    elixir_index: &ElixirSourceIndex,
633    rustler_targets: &HashMap<(String, String), RustlerTarget>,
634    next_id: &mut u64,
635    project_root: &str,
636    files: Vec<(String, NodeId)>,
637) -> bool {
638    eprintln!(
639        "Tracing {} fallback Elixir files in {} with one Mix runtime…",
640        files.len(),
641        display_project_root(project_root)
642    );
643    let source_ids: HashMap<_, _> = files
644        .iter()
645        .map(|(input_path, id)| (input_path.clone(), *id))
646        .collect();
647    let mut child = match Command::new("mix")
648        .args([
649            "run",
650            "--no-compile",
651            "--no-deps-check",
652            "--no-start",
653            "-e",
654            XREF_BATCH_EXPRESSION,
655        ])
656        .env("MIX_OS_CONCURRENCY_LOCK", "0")
657        .current_dir(atlas.root_path.join(project_root))
658        .stdin(Stdio::piped())
659        .stdout(Stdio::piped())
660        .stderr(Stdio::piped())
661        .spawn()
662    {
663        Ok(child) => child,
664        Err(error) => {
665            atlas
666                .report
667                .warnings
668                .push(format!("could not start batched Mix xref: {error}"));
669            return false;
670        }
671    };
672    let mut stdin = child.stdin.take().expect("piped stdin");
673    let input_paths: Vec<_> = files
674        .iter()
675        .map(|(input_path, _)| input_path.clone())
676        .collect();
677    let writer = thread::spawn(move || {
678        for path in input_paths {
679            writeln!(stdin, "{path}")?;
680        }
681        Ok::<_, std::io::Error>(())
682    });
683    let mut stderr = child.stderr.take().expect("piped stderr");
684    let stderr_reader = thread::spawn(move || {
685        let mut contents = String::new();
686        stderr.read_to_string(&mut contents).map(|_| contents)
687    });
688    let stdout = child.stdout.take().expect("piped stdout");
689    let mut current: Option<(String, NodeId)> = None;
690    let mut current_failed = false;
691    let mut project_completed = 0_usize;
692    for line in BufReader::new(stdout).lines() {
693        let line = match line {
694            Ok(line) => line,
695            Err(error) => {
696                atlas
697                    .report
698                    .warnings
699                    .push(format!("could not read batched xref output: {error}"));
700                break;
701            }
702        };
703        if let Some(path) = line.strip_prefix(FILE_MARKER) {
704            current = source_ids
705                .get(path)
706                .copied()
707                .map(|id| (path.to_owned(), id));
708            current_failed = false;
709            continue;
710        }
711        if let Some(error) = line.strip_prefix(ERROR_MARKER) {
712            let path = current
713                .as_ref()
714                .map(|(path, _)| path.as_str())
715                .unwrap_or("unknown file");
716            atlas
717                .report
718                .warnings
719                .push(format!("mix xref trace failed for {path}: {error}"));
720            current_failed = true;
721            continue;
722        }
723        if line == END_MARKER {
724            if current.is_some() && !current_failed {
725                atlas.report.elixir_files_traced += 1;
726                coverage_mut(atlas, "elixir").files_analyzed += 1;
727                project_completed += 1;
728                if project_completed.is_multiple_of(100) || project_completed == files.len() {
729                    eprintln!(
730                        "  traced {project_completed}/{} Elixir files in {}",
731                        files.len(),
732                        display_project_root(project_root)
733                    );
734                }
735            }
736            current = None;
737            continue;
738        }
739        if let Some((_, source_id)) = current.as_ref() {
740            let Some(call) = parse_xref_line(&line) else {
741                continue;
742            };
743            let key = (call.module.clone(), call.function.clone());
744            let (target, target_line, analyzer) = if let Some(target) = rustler_targets.get(&key) {
745                atlas.report.rustler_calls += 1;
746                (target.file, target.line, "elixir-xref+rustler")
747            } else if let Some(target) = elixir_index.module_files.get(&call.module) {
748                (
749                    *target,
750                    definition_line(elixir_index, *target, &call.function),
751                    "elixir-xref",
752                )
753            } else {
754                atlas.report.unresolved_calls += 1;
755                coverage_mut(atlas, "elixir").callsites_unresolved += 1;
756                continue;
757            };
758
759            atlas.calls.push(Callsite {
760                id: *next_id,
761                source: *source_id,
762                source_line: call.source_line,
763                target,
764                target_line,
765                callee: format!("{}.{}/{}", call.module, call.function, call.arity),
766                kind: call.kind,
767                analyzer: analyzer.to_owned(),
768                confidence: if analyzer.contains("rustler") {
769                    0.98
770                } else {
771                    1.0
772                },
773            });
774            *next_id += 1;
775            atlas.report.resolved_calls += 1;
776            coverage_mut(atlas, "elixir").callsites_resolved += 1;
777        }
778    }
779    let writer_succeeded = match writer.join().expect("xref input writer panicked") {
780        Ok(()) => true,
781        Err(error) => {
782            atlas
783                .report
784                .warnings
785                .push(format!("could not send xref input: {error}"));
786            false
787        }
788    };
789    let status = child.wait();
790    let stderr = stderr_reader
791        .join()
792        .expect("xref stderr reader panicked")
793        .unwrap_or_default();
794    let batch_succeeded = match status {
795        Ok(status) if status.success() => true,
796        Ok(status) => {
797            atlas.report.warnings.push(format!(
798                "batched Mix xref exited with {status}: {}",
799                stderr.trim()
800            ));
801            false
802        }
803        Err(error) => {
804            atlas
805                .report
806                .warnings
807                .push(format!("could not wait for batched Mix xref: {error}"));
808            false
809        }
810    };
811    writer_succeeded && batch_succeeded
812}
813
814#[derive(Debug, Serialize, Deserialize)]
815struct CachedAnalysis {
816    version: u32,
817    calls: Vec<CachedCallsite>,
818    report: AnalyzerReport,
819}
820
821#[derive(Debug, Serialize, Deserialize)]
822struct CachedCallsite {
823    source_path: String,
824    source_line: u32,
825    target_path: String,
826    target_line: Option<u32>,
827    callee: String,
828    kind: String,
829    analyzer: String,
830    confidence: f32,
831}
832
833fn load_cached_analysis(atlas: &mut Atlas) -> Result<bool> {
834    let Some(path) = analysis_cache_path(atlas) else {
835        return Ok(false);
836    };
837    let bytes = match fs::read(&path) {
838        Ok(bytes) => bytes,
839        Err(error) if error.kind() == std::io::ErrorKind::NotFound => return Ok(false),
840        Err(error) => {
841            atlas
842                .report
843                .warnings
844                .push(format!("could not read analysis cache: {error}"));
845            return Ok(false);
846        }
847    };
848    let cached: CachedAnalysis = match serde_json::from_slice(&bytes) {
849        Ok(cached) => cached,
850        Err(error) => {
851            atlas
852                .report
853                .warnings
854                .push(format!("ignored invalid analysis cache: {error}"));
855            return Ok(false);
856        }
857    };
858    if cached.version != CACHE_VERSION {
859        return Ok(false);
860    }
861    let mut calls = Vec::with_capacity(cached.calls.len());
862    for (id, call) in cached.calls.into_iter().enumerate() {
863        let (Some(source), Some(target)) = (
864            atlas.path_to_id.get(&call.source_path).copied(),
865            atlas.path_to_id.get(&call.target_path).copied(),
866        ) else {
867            return Ok(false);
868        };
869        calls.push(Callsite {
870            id: id as u64,
871            source,
872            source_line: call.source_line,
873            target,
874            target_line: call.target_line,
875            callee: call.callee,
876            kind: call.kind,
877            analyzer: call.analyzer,
878            confidence: call.confidence,
879        });
880    }
881    atlas.calls = calls;
882    atlas.report = cached.report;
883    atlas.report.cache_hit = true;
884    Ok(true)
885}
886
887fn save_cached_analysis(atlas: &mut Atlas) -> Result<()> {
888    let Some(path) = analysis_cache_path(atlas) else {
889        return Ok(());
890    };
891    let calls = atlas
892        .calls
893        .iter()
894        .map(|call| CachedCallsite {
895            source_path: atlas.nodes[call.source].path.clone(),
896            source_line: call.source_line,
897            target_path: atlas.nodes[call.target].path.clone(),
898            target_line: call.target_line,
899            callee: call.callee.clone(),
900            kind: call.kind.clone(),
901            analyzer: call.analyzer.clone(),
902            confidence: call.confidence,
903        })
904        .collect();
905    let mut report = atlas.report.clone();
906    report.cache_hit = false;
907    let payload = CachedAnalysis {
908        version: CACHE_VERSION,
909        calls,
910        report,
911    };
912    if let Some(parent) = path.parent() {
913        if let Err(error) = fs::create_dir_all(parent) {
914            atlas.report.warnings.push(format!(
915                "could not create analysis cache directory: {error}"
916            ));
917            return Ok(());
918        }
919    }
920    if let Err(error) = fs::write(&path, serde_json::to_vec(&payload)?) {
921        atlas
922            .report
923            .warnings
924            .push(format!("could not write analysis cache: {error}"));
925    }
926    Ok(())
927}
928
929fn analysis_cache_path(atlas: &Atlas) -> Option<PathBuf> {
930    let output = Command::new("git")
931        .args(["rev-parse", "--git-dir"])
932        .current_dir(&atlas.root_path)
933        .output()
934        .ok()?;
935    if !output.status.success() {
936        return None;
937    }
938    let git_dir = PathBuf::from(String::from_utf8_lossy(&output.stdout).trim());
939    let git_dir = if git_dir.is_absolute() {
940        git_dir
941    } else {
942        atlas.root_path.join(git_dir)
943    };
944    let mut hash = 0xcbf2_9ce4_8422_2325_u64;
945    let tracked_diff = Command::new("git")
946        .args(["diff", "--binary", "HEAD", "--"])
947        .current_dir(&atlas.root_path)
948        .output()
949        .ok()
950        .filter(|output| output.status.success())
951        .map(|output| output.stdout)
952        .unwrap_or_default();
953    for byte in std::iter::once(u8::from(atlas.dirty))
954        .chain(
955            atlas
956                .revision
957                .as_bytes()
958                .iter()
959                .copied()
960                .chain(atlas.files().flat_map(|node| {
961                    node.path
962                        .as_bytes()
963                        .iter()
964                        .copied()
965                        .chain(std::iter::once(0))
966                })),
967        )
968        .chain(tracked_diff)
969    {
970        hash ^= u64::from(byte);
971        hash = hash.wrapping_mul(0x0000_0100_0000_01b3);
972    }
973    for tool in [
974        "elixir".to_owned(),
975        "erl".to_owned(),
976        rust_analyzer_binary(),
977        scip_typescript_binary(atlas),
978    ] {
979        for byte in command_version(&tool).bytes().chain(std::iter::once(0)) {
980            hash ^= u64::from(byte);
981            hash = hash.wrapping_mul(0x0000_0100_0000_01b3);
982        }
983    }
984    Some(
985        git_dir
986            .join("code-atlas")
987            .join(format!("analysis-v{CACHE_VERSION}-{hash:016x}.json")),
988    )
989}
990
991fn command_version(binary: &str) -> String {
992    let output = Command::new(binary).arg("--version").output();
993    match output {
994        Ok(output) if output.status.success() => format!(
995            "{}{}",
996            String::from_utf8_lossy(&output.stdout),
997            String::from_utf8_lossy(&output.stderr)
998        ),
999        _ => "unavailable".to_owned(),
1000    }
1001}
1002
1003fn analyze_elixir_scripts(
1004    atlas: &mut Atlas,
1005    elixir_index: &ElixirSourceIndex,
1006    next_id: &mut u64,
1007) -> Result<()> {
1008    if coverage_mut(atlas, "elixir").files_discovered == 0 {
1009        return Ok(());
1010    }
1011    match analyze_elixir_scripts_ast(atlas, elixir_index, next_id) {
1012        Ok(true) => {
1013            let coverage = coverage_mut(atlas, "elixir");
1014            coverage.provider = format!("{} + script-ast", coverage.provider);
1015            coverage.fidelity = format!("{}; compiler-parsed scripts", coverage.fidelity);
1016            Ok(())
1017        }
1018        Ok(false) => {
1019            let coverage = coverage_mut(atlas, "elixir");
1020            coverage.provider = format!("{} + script-static", coverage.provider);
1021            coverage.fidelity = format!("{}; conservatively parsed scripts", coverage.fidelity);
1022            analyze_elixir_scripts_fallback(atlas, elixir_index, next_id)
1023        }
1024        Err(error) => {
1025            atlas.report.warnings.push(format!(
1026                "Elixir AST script analysis failed; scripts used the regex fallback: {error:#}"
1027            ));
1028            let coverage = coverage_mut(atlas, "elixir");
1029            coverage.provider = format!("{} + script-static", coverage.provider);
1030            coverage.fidelity = format!("{}; conservatively parsed scripts", coverage.fidelity);
1031            analyze_elixir_scripts_fallback(atlas, elixir_index, next_id)
1032        }
1033    }
1034}
1035
1036fn analyze_elixir_scripts_ast(
1037    atlas: &mut Atlas,
1038    elixir_index: &ElixirSourceIndex,
1039    next_id: &mut u64,
1040) -> Result<bool> {
1041    if !command_exists("elixir") {
1042        return Ok(false);
1043    }
1044    let scripts: Vec<_> = atlas
1045        .nodes
1046        .iter()
1047        .filter(|node| {
1048            node.is_file() && (node.path.ends_with(".exs") || node.path.ends_with(".eex"))
1049        })
1050        .map(|node| (node.id, node.path.clone()))
1051        .collect();
1052    if scripts.is_empty() {
1053        return Ok(true);
1054    }
1055    let source_ids: HashMap<_, _> = scripts
1056        .iter()
1057        .map(|(id, path)| (path.clone(), *id))
1058        .collect();
1059    let mut child = Command::new("elixir")
1060        .args(["-e", ELIXIR_SCRIPT_EXPRESSION])
1061        .current_dir(&atlas.root_path)
1062        .stdin(Stdio::piped())
1063        .stdout(Stdio::piped())
1064        .stderr(Stdio::piped())
1065        .spawn()?;
1066    {
1067        let mut stdin = child.stdin.take().expect("piped stdin");
1068        for (_, path) in &scripts {
1069            writeln!(stdin, "{path}")?;
1070        }
1071    }
1072    let output = child.wait_with_output()?;
1073    if !output.status.success() {
1074        anyhow::bail!("{}", String::from_utf8_lossy(&output.stderr).trim());
1075    }
1076    let mut current = None;
1077    let mut failed = false;
1078    for line in String::from_utf8_lossy(&output.stdout).lines() {
1079        if let Some(path) = line.strip_prefix(FILE_MARKER) {
1080            current = source_ids.get(path).copied();
1081            failed = false;
1082            continue;
1083        }
1084        if let Some(error) = line.strip_prefix(ERROR_MARKER) {
1085            atlas
1086                .report
1087                .warnings
1088                .push(format!("could not parse Elixir script: {error}"));
1089            failed = true;
1090            continue;
1091        }
1092        if line == END_MARKER {
1093            if current.is_some() && !failed {
1094                atlas.report.elixir_script_files_scanned += 1;
1095                coverage_mut(atlas, "elixir").files_analyzed += 1;
1096            }
1097            current = None;
1098            continue;
1099        }
1100        let Some(payload) = line.strip_prefix(SCRIPT_CALL_MARKER) else {
1101            continue;
1102        };
1103        let Some(source) = current else {
1104            continue;
1105        };
1106        let mut fields = payload.splitn(4, '\t');
1107        let Some(source_line) = fields.next().and_then(|value| value.parse::<u32>().ok()) else {
1108            continue;
1109        };
1110        let Some(module) = fields.next() else {
1111            continue;
1112        };
1113        let Some(function) = fields.next() else {
1114            continue;
1115        };
1116        let Some(arity) = fields.next().and_then(|value| value.parse::<u32>().ok()) else {
1117            continue;
1118        };
1119        let Some(target) = elixir_index.module_files.get(module).copied() else {
1120            atlas.report.unresolved_calls += 1;
1121            coverage_mut(atlas, "elixir").callsites_unresolved += 1;
1122            continue;
1123        };
1124        atlas.calls.push(Callsite {
1125            id: *next_id,
1126            source,
1127            source_line,
1128            target,
1129            target_line: definition_line(elixir_index, target, function),
1130            callee: format!("{module}.{function}/{arity}"),
1131            kind: "syntax".to_owned(),
1132            analyzer: "elixir-script-ast".to_owned(),
1133            confidence: 0.75,
1134        });
1135        *next_id += 1;
1136        atlas.report.resolved_calls += 1;
1137        coverage_mut(atlas, "elixir").callsites_resolved += 1;
1138    }
1139    Ok(true)
1140}
1141
1142fn analyze_elixir_scripts_fallback(
1143    atlas: &mut Atlas,
1144    elixir_index: &ElixirSourceIndex,
1145    next_id: &mut u64,
1146) -> Result<()> {
1147    let alias_pattern =
1148        Regex::new(r"^\s*alias\s+([A-Z][A-Za-z0-9_.]*)(?:\s*,\s*as:\s*([A-Z][A-Za-z0-9_]*))?")?;
1149    let call_pattern = Regex::new(r"\b([A-Z][A-Za-z0-9_.]*)\.([a-z_][A-Za-z0-9_!?]*)\s*\(")?;
1150    let scripts: Vec<_> = atlas
1151        .nodes
1152        .iter()
1153        .filter(|node| {
1154            node.is_file() && (node.path.ends_with(".exs") || node.path.ends_with(".eex"))
1155        })
1156        .map(|node| (node.id, node.path.clone()))
1157        .collect();
1158    for (source, path) in scripts {
1159        let contents = fs::read_to_string(atlas.root_path.join(&path)).unwrap_or_default();
1160        let mut aliases = HashMap::new();
1161        for line in contents.lines() {
1162            if let Some(captures) = alias_pattern.captures(line) {
1163                let full = captures[1].to_owned();
1164                let short = captures
1165                    .get(2)
1166                    .map(|value| value.as_str())
1167                    .or_else(|| full.rsplit('.').next())
1168                    .unwrap_or(&full)
1169                    .to_owned();
1170                aliases.insert(short, full);
1171            }
1172        }
1173        atlas.report.elixir_script_files_scanned += 1;
1174        coverage_mut(atlas, "elixir").files_analyzed += 1;
1175        for (index, line) in contents.lines().enumerate() {
1176            let code = line.split('#').next().unwrap_or("");
1177            for captures in call_pattern.captures_iter(code) {
1178                let written_module = &captures[1];
1179                let module = elixir_index
1180                    .module_files
1181                    .contains_key(written_module)
1182                    .then(|| written_module.to_owned())
1183                    .or_else(|| aliases.get(written_module).cloned());
1184                let Some(module) = module else {
1185                    continue;
1186                };
1187                let Some(target) = elixir_index.module_files.get(&module).copied() else {
1188                    continue;
1189                };
1190                let function = captures[2].to_owned();
1191                atlas.calls.push(Callsite {
1192                    id: *next_id,
1193                    source,
1194                    source_line: index as u32 + 1,
1195                    target,
1196                    target_line: definition_line(elixir_index, target, &function),
1197                    callee: format!("{module}.{function}/?"),
1198                    kind: "syntax".to_owned(),
1199                    analyzer: "elixir-script-static".to_owned(),
1200                    confidence: 0.55,
1201                });
1202                *next_id += 1;
1203                atlas.report.resolved_calls += 1;
1204                coverage_mut(atlas, "elixir").callsites_resolved += 1;
1205            }
1206        }
1207    }
1208    Ok(())
1209}
1210
1211fn analyze_erlang_calls(
1212    atlas: &mut Atlas,
1213    elixir_index: &ElixirSourceIndex,
1214    next_id: &mut u64,
1215) -> Result<()> {
1216    let atom = r"(?:[a-z][A-Za-z0-9_@]*|'(?:\\.|[^'\\])*')";
1217    let module_pattern = Regex::new(&format!(r"(?m)^\s*-module\s*\(\s*({atom})\s*\)\s*\."))?;
1218    let definition_pattern = Regex::new(&format!(r"(?m)^({atom})\s*\("))?;
1219    let remote_call_pattern = Regex::new(&format!(r"({atom})\s*:\s*({atom})\s*\("))?;
1220    let remote_fun_pattern = Regex::new(&format!(r"\bfun\s+({atom})\s*:\s*({atom})\s*/\s*[0-9]+"))?;
1221    let import_pattern = Regex::new(&format!(
1222        r"(?s)-import\s*\(\s*({atom})\s*,\s*\[(.*?)\]\s*\)\s*\."
1223    ))?;
1224    let imported_function_pattern = Regex::new(&format!(r"({atom})\s*/\s*[0-9]+"))?;
1225
1226    let files: Vec<_> = atlas
1227        .nodes
1228        .iter()
1229        .filter(|node| node.is_file() && node.language == "erlang")
1230        .map(|node| {
1231            let contents = fs::read_to_string(atlas.root_path.join(&node.path)).unwrap_or_default();
1232            (node.id, contents)
1233        })
1234        .collect();
1235    if files.is_empty() {
1236        return Ok(());
1237    }
1238
1239    let mut index = ErlangSourceIndex::default();
1240    let mut sanitized_files = Vec::with_capacity(files.len());
1241    for (id, contents) in files {
1242        let sanitized = sanitize_erlang_source(&contents);
1243        if let Some(captures) = module_pattern.captures(&sanitized) {
1244            index
1245                .module_files
1246                .insert(normalize_erlang_atom(&captures[1]), id);
1247        }
1248        let mut definitions = HashMap::new();
1249        for captures in definition_pattern.captures_iter(&sanitized) {
1250            let function = normalize_erlang_atom(&captures[1]);
1251            let line = erlang_line_number(&sanitized, captures.get(1).unwrap().start());
1252            definitions.entry(function).or_insert(line);
1253        }
1254        index.definition_lines.insert(id, definitions);
1255        sanitized_files.push((id, sanitized));
1256    }
1257
1258    for (source, sanitized) in sanitized_files {
1259        atlas.report.erlang_files_scanned += 1;
1260        coverage_mut(atlas, "erlang").files_analyzed += 1;
1261
1262        for captures in remote_call_pattern.captures_iter(&sanitized) {
1263            let matched = captures.get(0).unwrap();
1264            if erlang_line_is_attribute(&sanitized, matched.start()) {
1265                continue;
1266            }
1267            record_erlang_call(
1268                atlas,
1269                elixir_index,
1270                &index,
1271                next_id,
1272                source,
1273                erlang_line_number(&sanitized, matched.start()),
1274                &normalize_erlang_atom(&captures[1]),
1275                &normalize_erlang_atom(&captures[2]),
1276                "runtime",
1277            );
1278        }
1279
1280        for captures in remote_fun_pattern.captures_iter(&sanitized) {
1281            let matched = captures.get(0).unwrap();
1282            if erlang_line_is_attribute(&sanitized, matched.start()) {
1283                continue;
1284            }
1285            record_erlang_call(
1286                atlas,
1287                elixir_index,
1288                &index,
1289                next_id,
1290                source,
1291                erlang_line_number(&sanitized, matched.start()),
1292                &normalize_erlang_atom(&captures[1]),
1293                &normalize_erlang_atom(&captures[2]),
1294                "reference",
1295            );
1296        }
1297
1298        let mut imports = HashMap::new();
1299        for captures in import_pattern.captures_iter(&sanitized) {
1300            let module = normalize_erlang_atom(&captures[1]);
1301            for function in imported_function_pattern.captures_iter(&captures[2]) {
1302                imports.insert(normalize_erlang_atom(&function[1]), module.clone());
1303            }
1304        }
1305        for (function, module) in imports {
1306            let call_pattern = Regex::new(&format!(r"\b{}\s*\(", regex::escape(&function)))?;
1307            for matched in call_pattern.find_iter(&sanitized) {
1308                if erlang_line_is_attribute(&sanitized, matched.start())
1309                    || erlang_call_is_qualified(&sanitized, matched.start())
1310                {
1311                    continue;
1312                }
1313                record_erlang_call(
1314                    atlas,
1315                    elixir_index,
1316                    &index,
1317                    next_id,
1318                    source,
1319                    erlang_line_number(&sanitized, matched.start()),
1320                    &module,
1321                    &function,
1322                    "runtime",
1323                );
1324            }
1325        }
1326    }
1327    Ok(())
1328}
1329
1330#[allow(clippy::too_many_arguments)]
1331fn record_erlang_call(
1332    atlas: &mut Atlas,
1333    elixir_index: &ElixirSourceIndex,
1334    erlang_index: &ErlangSourceIndex,
1335    next_id: &mut u64,
1336    source: NodeId,
1337    source_line: u32,
1338    module: &str,
1339    function: &str,
1340    kind: &str,
1341) {
1342    let erlang_target = erlang_index.module_files.get(module).copied();
1343    let elixir_target = module
1344        .strip_prefix("Elixir.")
1345        .and_then(|name| elixir_index.module_files.get(name).copied());
1346    let Some(target) = erlang_target.or(elixir_target) else {
1347        return;
1348    };
1349    let target_line = erlang_index
1350        .definition_lines
1351        .get(&target)
1352        .and_then(|definitions| definitions.get(function).copied())
1353        .or_else(|| definition_line(elixir_index, target, function));
1354    atlas.calls.push(Callsite {
1355        id: *next_id,
1356        source,
1357        source_line,
1358        target,
1359        target_line,
1360        callee: format!("{module}:{function}"),
1361        kind: kind.to_owned(),
1362        analyzer: "erlang-remote-static".to_owned(),
1363        confidence: 0.8,
1364    });
1365    *next_id += 1;
1366    atlas.report.resolved_calls += 1;
1367    atlas.report.erlang_calls += 1;
1368    coverage_mut(atlas, "erlang").callsites_resolved += 1;
1369}
1370
1371fn sanitize_erlang_source(contents: &str) -> String {
1372    let mut output = String::with_capacity(contents.len());
1373    let mut in_comment = false;
1374    let mut in_string = false;
1375    let mut in_quoted_atom = false;
1376    let mut escaped = false;
1377    for character in contents.chars() {
1378        if in_comment {
1379            if character == '\n' {
1380                in_comment = false;
1381                output.push('\n');
1382            } else {
1383                output.push(' ');
1384            }
1385            continue;
1386        }
1387        if in_string {
1388            if character == '\n' {
1389                output.push('\n');
1390            } else {
1391                output.push(' ');
1392            }
1393            if escaped {
1394                escaped = false;
1395            } else if character == '\\' {
1396                escaped = true;
1397            } else if character == '"' {
1398                in_string = false;
1399            }
1400            continue;
1401        }
1402        if in_quoted_atom {
1403            output.push(character);
1404            if escaped {
1405                escaped = false;
1406            } else if character == '\\' {
1407                escaped = true;
1408            } else if character == '\'' {
1409                in_quoted_atom = false;
1410            }
1411            continue;
1412        }
1413        match character {
1414            '%' => {
1415                in_comment = true;
1416                output.push(' ');
1417            }
1418            '"' => {
1419                in_string = true;
1420                output.push(' ');
1421            }
1422            '\'' => {
1423                in_quoted_atom = true;
1424                output.push(character);
1425            }
1426            _ => output.push(character),
1427        }
1428    }
1429    output
1430}
1431
1432fn normalize_erlang_atom(atom: &str) -> String {
1433    atom.strip_prefix('\'')
1434        .and_then(|value| value.strip_suffix('\''))
1435        .unwrap_or(atom)
1436        .replace("\\'", "'")
1437        .replace("\\\\", "\\")
1438}
1439
1440fn erlang_line_number(source: &str, byte_offset: usize) -> u32 {
1441    source[..byte_offset]
1442        .bytes()
1443        .filter(|byte| *byte == b'\n')
1444        .count() as u32
1445        + 1
1446}
1447
1448fn erlang_line_is_attribute(source: &str, byte_offset: usize) -> bool {
1449    let line_start = source[..byte_offset]
1450        .rfind('\n')
1451        .map_or(0, |position| position + 1);
1452    source[line_start..byte_offset]
1453        .trim_start()
1454        .starts_with('-')
1455}
1456
1457fn erlang_call_is_qualified(source: &str, byte_offset: usize) -> bool {
1458    source[..byte_offset]
1459        .chars()
1460        .rev()
1461        .find(|character| !character.is_whitespace())
1462        == Some(':')
1463}
1464
1465fn analyze_typescript_calls(atlas: &mut Atlas, next_id: &mut u64) -> Result<bool> {
1466    let file_count = atlas
1467        .nodes
1468        .iter()
1469        .filter(|node| matches!(node.language.as_str(), "javascript" | "typescript"))
1470        .count();
1471    if file_count == 0 {
1472        return Ok(true);
1473    }
1474    let cacheable = match generate_typescript_scip(atlas) {
1475        Ok(Some(indexes)) => {
1476            let stats = ingest_scip_calls(atlas, &indexes, &["javascript", "typescript"], next_id)?;
1477            for language in ["javascript", "typescript"] {
1478                let coverage = coverage_mut(atlas, language);
1479                coverage.provider = "scip-typescript".to_owned();
1480                coverage.fidelity = "compiler-backed semantic symbols".to_owned();
1481                coverage.files_analyzed =
1482                    stats.files_by_language.get(language).copied().unwrap_or(0);
1483                coverage.callsites_resolved = stats
1484                    .resolved_by_language
1485                    .get(language)
1486                    .copied()
1487                    .unwrap_or(0);
1488                coverage.callsites_unresolved = stats
1489                    .unresolved_by_language
1490                    .get(language)
1491                    .copied()
1492                    .unwrap_or(0);
1493            }
1494            atlas.report.typescript_files_scanned =
1495                stats.files_by_language.values().copied().sum::<usize>();
1496            atlas.report.typescript_calls =
1497                stats.resolved_by_language.values().copied().sum::<usize>();
1498            atlas.report.resolved_calls += atlas.report.typescript_calls;
1499            atlas.report.unresolved_calls += stats
1500                .unresolved_by_language
1501                .values()
1502                .copied()
1503                .sum::<usize>();
1504            return Ok(true);
1505        }
1506        Ok(None) => {
1507            atlas.report.warnings.push(
1508                "scip-typescript was not found; JavaScript/TypeScript used the relative-import fallback"
1509                    .to_owned(),
1510            );
1511            true
1512        }
1513        Err(error) => {
1514            atlas.report.warnings.push(format!(
1515                "scip-typescript indexing failed; JavaScript/TypeScript used the relative-import fallback: {error:#}"
1516            ));
1517            false
1518        }
1519    };
1520    for language in ["javascript", "typescript"] {
1521        let coverage = coverage_mut(atlas, language);
1522        coverage.provider = "relative-import-static".to_owned();
1523        coverage.fidelity = "conservative syntax fallback".to_owned();
1524    }
1525    analyze_typescript_calls_fallback(atlas, next_id)?;
1526    Ok(cacheable)
1527}
1528
1529fn analyze_typescript_calls_fallback(atlas: &mut Atlas, next_id: &mut u64) -> Result<()> {
1530    let import_pattern = Regex::new(r#"^\s*import\s+(.+?)\s+from\s+["']([^"']+)["']\s*;?\s*$"#)?;
1531    let call_pattern =
1532        Regex::new(r"\b([A-Za-z_$][A-Za-z0-9_$]*)(?:\.([A-Za-z_$][A-Za-z0-9_$]*))?\s*\(")?;
1533    let files: Vec<_> = atlas
1534        .nodes
1535        .iter()
1536        .filter(|node| {
1537            node.is_file()
1538                && matches!(
1539                    Path::new(&node.path)
1540                        .extension()
1541                        .and_then(|value| value.to_str()),
1542                    Some("ts" | "tsx" | "mts" | "cts" | "js" | "jsx" | "mjs" | "cjs")
1543                )
1544        })
1545        .map(|node| (node.id, node.path.clone()))
1546        .collect();
1547    for (source, path) in files {
1548        atlas.report.typescript_files_scanned += 1;
1549        let language = atlas.nodes[source].language.clone();
1550        coverage_mut(atlas, &language).files_analyzed += 1;
1551        let contents = fs::read_to_string(atlas.root_path.join(&path)).unwrap_or_default();
1552        let mut bindings: HashMap<String, (NodeId, bool)> = HashMap::new();
1553        for line in contents.lines() {
1554            let Some(captures) = import_pattern.captures(line) else {
1555                continue;
1556            };
1557            let Some(target) = resolve_relative_module(atlas, &path, &captures[2]) else {
1558                continue;
1559            };
1560            let clause = captures[1].trim();
1561            if let Some(namespace) = clause.strip_prefix("* as ") {
1562                bindings.insert(namespace.trim().to_owned(), (target, true));
1563                continue;
1564            }
1565            let mut remainder = clause;
1566            if !remainder.starts_with('{') {
1567                let default = remainder.split([',', ' ']).next().unwrap_or("").trim();
1568                if !default.is_empty() {
1569                    bindings.insert(default.to_owned(), (target, false));
1570                }
1571                remainder = remainder
1572                    .split_once(',')
1573                    .map(|(_, rest)| rest)
1574                    .unwrap_or("");
1575            }
1576            if let Some(named) = remainder
1577                .trim()
1578                .strip_prefix('{')
1579                .and_then(|value| value.strip_suffix('}'))
1580            {
1581                for item in named.split(',') {
1582                    let mut parts = item.split_whitespace();
1583                    let original = parts.next().unwrap_or("");
1584                    let alias = match (parts.next(), parts.next()) {
1585                        (Some("as"), Some(alias)) => alias,
1586                        _ => original,
1587                    };
1588                    if !alias.is_empty() {
1589                        bindings.insert(alias.to_owned(), (target, false));
1590                    }
1591                }
1592            }
1593        }
1594        for (index, line) in contents.lines().enumerate() {
1595            let trimmed = line.trim_start();
1596            if trimmed.starts_with("import ") || trimmed.starts_with("//") {
1597                continue;
1598            }
1599            let code = line.split("//").next().unwrap_or("");
1600            for captures in call_pattern.captures_iter(code) {
1601                let binding = &captures[1];
1602                let Some((target, namespace)) = bindings.get(binding).copied() else {
1603                    continue;
1604                };
1605                if namespace && captures.get(2).is_none() {
1606                    continue;
1607                }
1608                let function = captures
1609                    .get(2)
1610                    .map(|value| value.as_str())
1611                    .unwrap_or(binding);
1612                atlas.calls.push(Callsite {
1613                    id: *next_id,
1614                    source,
1615                    source_line: index as u32 + 1,
1616                    target,
1617                    target_line: None,
1618                    callee: format!("{}.{}", atlas.nodes[target].path, function),
1619                    kind: "runtime".to_owned(),
1620                    analyzer: "typescript-import-static".to_owned(),
1621                    confidence: 0.65,
1622                });
1623                *next_id += 1;
1624                atlas.report.resolved_calls += 1;
1625                atlas.report.typescript_calls += 1;
1626                coverage_mut(atlas, &language).callsites_resolved += 1;
1627            }
1628        }
1629    }
1630    Ok(())
1631}
1632
1633fn resolve_relative_module(atlas: &Atlas, source_path: &str, import: &str) -> Option<NodeId> {
1634    if !import.starts_with('.') {
1635        return None;
1636    }
1637    let source_parent = Path::new(source_path)
1638        .parent()
1639        .unwrap_or_else(|| Path::new(""));
1640    let joined = source_parent.join(import);
1641    let mut components = Vec::new();
1642    for component in joined.components() {
1643        use std::path::Component;
1644        match component {
1645            Component::Normal(value) => components.push(value.to_string_lossy().to_string()),
1646            Component::ParentDir => {
1647                components.pop()?;
1648            }
1649            Component::CurDir => {}
1650            _ => return None,
1651        }
1652    }
1653    let base = components.join("/");
1654    let mut candidates = vec![base.clone()];
1655    for extension in ["ts", "tsx", "mts", "cts", "js", "jsx", "mjs", "cjs"] {
1656        candidates.push(format!("{base}.{extension}"));
1657        candidates.push(format!("{base}/index.{extension}"));
1658    }
1659    candidates
1660        .into_iter()
1661        .find_map(|candidate| atlas.path_to_id.get(&candidate).copied())
1662}
1663
1664struct RootedScipIndex {
1665    root: String,
1666    index: ScipIndex,
1667}
1668
1669#[derive(Default)]
1670struct ScipIngestStats {
1671    files_by_language: HashMap<String, usize>,
1672    resolved_by_language: HashMap<String, usize>,
1673    unresolved_by_language: HashMap<String, usize>,
1674}
1675
1676fn generate_rust_scip(atlas: &Atlas) -> Result<Option<Vec<RootedScipIndex>>> {
1677    let binary = rust_analyzer_binary();
1678    if !command_exists(&binary) {
1679        return Ok(None);
1680    }
1681    let roots = outermost_project_roots(atlas, &["Cargo.toml"]);
1682    if roots.is_empty() {
1683        return Ok(None);
1684    }
1685    let temporary = tempfile::tempdir()?;
1686    let mut indexes = Vec::new();
1687    for (number, root) in roots.iter().enumerate() {
1688        let output_path = temporary.path().join(format!("rust-{number}.scip"));
1689        let project_path = if root.is_empty() {
1690            atlas.root_path.clone()
1691        } else {
1692            atlas.root_path.join(root)
1693        };
1694        let output = Command::new(&binary)
1695            .arg("scip")
1696            .arg(&project_path)
1697            .arg("--output")
1698            .arg(&output_path)
1699            .arg("--exclude-vendored-libraries")
1700            .current_dir(&atlas.root_path)
1701            .output()?;
1702        if !output.status.success() {
1703            anyhow::bail!("{}", String::from_utf8_lossy(&output.stderr).trim());
1704        }
1705        indexes.push(RootedScipIndex {
1706            root: root.clone(),
1707            index: decode_index(&fs::read(&output_path)?)?,
1708        });
1709    }
1710    Ok(Some(indexes))
1711}
1712
1713fn generate_typescript_scip(atlas: &Atlas) -> Result<Option<Vec<RootedScipIndex>>> {
1714    let binary = scip_typescript_binary(atlas);
1715    if !command_exists(&binary) {
1716        return Ok(None);
1717    }
1718    let mut roots = outermost_project_roots(atlas, &["tsconfig.json"]);
1719    let infer_tsconfig = roots.is_empty();
1720    if roots.is_empty() {
1721        roots = outermost_project_roots(atlas, &["package.json"]);
1722    }
1723    if roots.is_empty() {
1724        return Ok(None);
1725    }
1726    let temporary = tempfile::tempdir()?;
1727    let mut indexes = Vec::new();
1728    for (number, root) in roots.iter().enumerate() {
1729        let output_path = temporary.path().join(format!("typescript-{number}.scip"));
1730        let project_path = if root.is_empty() {
1731            atlas.root_path.clone()
1732        } else {
1733            atlas.root_path.join(root)
1734        };
1735        let mut command = Command::new(&binary);
1736        command
1737            .arg("index")
1738            .arg("--output")
1739            .arg(&output_path)
1740            .current_dir(&project_path);
1741        if infer_tsconfig {
1742            command.arg("--infer-tsconfig");
1743        }
1744        let output = command.output()?;
1745        if !output.status.success() {
1746            anyhow::bail!("{}", String::from_utf8_lossy(&output.stderr).trim());
1747        }
1748        indexes.push(RootedScipIndex {
1749            root: root.clone(),
1750            index: decode_index(&fs::read(&output_path)?)?,
1751        });
1752    }
1753    Ok(Some(indexes))
1754}
1755
1756fn rust_analyzer_binary() -> String {
1757    env::var("CODE_ATLAS_RUST_ANALYZER").unwrap_or_else(|_| "rust-analyzer".to_owned())
1758}
1759
1760fn scip_typescript_binary(atlas: &Atlas) -> String {
1761    env::var("CODE_ATLAS_SCIP_TYPESCRIPT")
1762        .ok()
1763        .or_else(|| {
1764            let local = atlas.root_path.join("node_modules/.bin/scip-typescript");
1765            local.is_file().then(|| local.display().to_string())
1766        })
1767        .unwrap_or_else(|| "scip-typescript".to_owned())
1768}
1769
1770fn command_exists(binary: &str) -> bool {
1771    Command::new(binary)
1772        .arg("--version")
1773        .stdout(Stdio::null())
1774        .stderr(Stdio::null())
1775        .status()
1776        .is_ok_and(|status| status.success())
1777}
1778
1779fn outermost_project_roots(atlas: &Atlas, manifests: &[&str]) -> Vec<String> {
1780    let mut roots: Vec<_> = atlas
1781        .nodes
1782        .iter()
1783        .filter(|node| node.is_file())
1784        .filter(|node| manifests.iter().any(|manifest| node.name == *manifest))
1785        .map(|node| {
1786            Path::new(&node.path)
1787                .parent()
1788                .map(|parent| parent.to_string_lossy().trim_end_matches('/').to_owned())
1789                .unwrap_or_default()
1790        })
1791        .collect();
1792    roots.sort_by_key(|root| (root.matches('/').count(), root.clone()));
1793    let mut outermost = Vec::<String>::new();
1794    for root in roots {
1795        if outermost.iter().any(|parent| {
1796            parent.is_empty()
1797                || root == *parent
1798                || root
1799                    .strip_prefix(parent)
1800                    .is_some_and(|suffix| suffix.starts_with('/'))
1801        }) {
1802            continue;
1803        }
1804        outermost.push(root);
1805    }
1806    outermost
1807}
1808
1809fn ingest_scip_calls(
1810    atlas: &mut Atlas,
1811    indexes: &[RootedScipIndex],
1812    languages: &[&str],
1813    next_id: &mut u64,
1814) -> Result<ScipIngestStats> {
1815    let mut documents = Vec::new();
1816    let mut analyzed_files: HashMap<String, HashSet<NodeId>> = HashMap::new();
1817    for rooted in indexes {
1818        for document in &rooted.index.documents {
1819            let Some(file) = scip_document_file(atlas, &rooted.root, document) else {
1820                continue;
1821            };
1822            let language = atlas.nodes[file].language.clone();
1823            if !languages.contains(&language.as_str()) {
1824                continue;
1825            }
1826            analyzed_files.entry(language).or_default().insert(file);
1827            documents.push((file, document));
1828        }
1829    }
1830
1831    let mut definitions: HashMap<&str, (NodeId, u32)> = HashMap::new();
1832    for (file, document) in &documents {
1833        for occurrence in &document.occurrences {
1834            if occurrence.symbol.is_empty() || occurrence.symbol_roles & 1 == 0 {
1835                continue;
1836            }
1837            if let Some(range) = occurrence.range {
1838                definitions
1839                    .entry(&occurrence.symbol)
1840                    .or_insert((*file, range.start_line + 1));
1841            }
1842        }
1843    }
1844
1845    let mut stats = ScipIngestStats::default();
1846    for (language, files) in analyzed_files {
1847        stats.files_by_language.insert(language, files.len());
1848    }
1849    let mut seen = HashSet::new();
1850    let mut source_cache: HashMap<NodeId, String> = HashMap::new();
1851    for (source, document) in documents {
1852        let language = atlas.nodes[source].language.clone();
1853        let contents = source_cache.entry(source).or_insert_with(|| {
1854            fs::read_to_string(atlas.root_path.join(&atlas.nodes[source].path)).unwrap_or_default()
1855        });
1856        for occurrence in &document.occurrences {
1857            if occurrence.symbol.is_empty() || occurrence.symbol_roles & (1 | 2 | 16 | 32) != 0 {
1858                continue;
1859            }
1860            let Some(range) = occurrence.range else {
1861                continue;
1862            };
1863            let Some(callee) = occurrence_call_text(contents, range, document.position_encoding)
1864            else {
1865                continue;
1866            };
1867            let Some((target, target_line)) = definitions.get(occurrence.symbol.as_str()).copied()
1868            else {
1869                *stats
1870                    .unresolved_by_language
1871                    .entry(language.clone())
1872                    .or_default() += 1;
1873                continue;
1874            };
1875            let identity = (
1876                source,
1877                range.start_line,
1878                range.start_character,
1879                target,
1880                target_line,
1881                occurrence.symbol.clone(),
1882            );
1883            if !seen.insert(identity) {
1884                continue;
1885            }
1886            atlas.calls.push(Callsite {
1887                id: *next_id,
1888                source,
1889                source_line: range.start_line + 1,
1890                target,
1891                target_line: Some(target_line),
1892                callee,
1893                kind: "semantic".to_owned(),
1894                analyzer: if language == "rust" {
1895                    "rust-analyzer-scip".to_owned()
1896                } else {
1897                    "scip-typescript".to_owned()
1898                },
1899                confidence: 0.98,
1900            });
1901            *next_id += 1;
1902            *stats
1903                .resolved_by_language
1904                .entry(language.clone())
1905                .or_default() += 1;
1906        }
1907    }
1908    Ok(stats)
1909}
1910
1911fn scip_document_file(atlas: &Atlas, root: &str, document: &ScipDocument) -> Option<NodeId> {
1912    let relative = document
1913        .relative_path
1914        .replace('\\', "/")
1915        .trim_start_matches("./")
1916        .to_owned();
1917    let rooted = if root.is_empty() {
1918        relative.clone()
1919    } else {
1920        format!("{root}/{relative}")
1921    };
1922    atlas
1923        .path_to_id
1924        .get(&rooted)
1925        .or_else(|| atlas.path_to_id.get(&relative))
1926        .copied()
1927}
1928
1929fn occurrence_call_text(contents: &str, range: ScipRange, encoding: i32) -> Option<String> {
1930    if range.start_line != range.end_line {
1931        return None;
1932    }
1933    let line = contents.lines().nth(range.start_line as usize)?;
1934    let start = encoded_column_to_byte(line, range.start_character, encoding)?;
1935    let end = encoded_column_to_byte(line, range.end_character, encoding)?;
1936    let callee = line.get(start..end)?.trim();
1937    if callee.is_empty() || !suffix_is_call(line.get(end..)?) {
1938        return None;
1939    }
1940    Some(callee.to_owned())
1941}
1942
1943fn encoded_column_to_byte(line: &str, column: u32, encoding: i32) -> Option<usize> {
1944    if encoding != 2 {
1945        let byte = column as usize;
1946        return (byte <= line.len() && line.is_char_boundary(byte)).then_some(byte);
1947    }
1948    let mut utf16 = 0_u32;
1949    for (byte, character) in line.char_indices() {
1950        if utf16 == column {
1951            return Some(byte);
1952        }
1953        utf16 += character.len_utf16() as u32;
1954        if utf16 > column {
1955            return None;
1956        }
1957    }
1958    (utf16 == column).then_some(line.len())
1959}
1960
1961fn suffix_is_call(suffix: &str) -> bool {
1962    let mut rest = suffix.trim_start();
1963    if let Some(optional) = rest.strip_prefix("?.") {
1964        rest = optional.trim_start();
1965    }
1966    if let Some(macro_call) = rest.strip_prefix('!') {
1967        rest = macro_call.trim_start();
1968    }
1969    if let Some(generic) = rest
1970        .strip_prefix("::")
1971        .and_then(|value| value.strip_prefix('<'))
1972    {
1973        let Some(after_generics) = skip_balanced_angles(generic) else {
1974            return false;
1975        };
1976        rest = after_generics;
1977    } else if let Some(generic) = rest.strip_prefix('<') {
1978        let Some(after_generics) = skip_balanced_angles(generic) else {
1979            return false;
1980        };
1981        rest = after_generics;
1982    }
1983    rest.trim_start().starts_with('(')
1984}
1985
1986fn skip_balanced_angles(value: &str) -> Option<&str> {
1987    let mut depth = 1_usize;
1988    for (offset, character) in value.char_indices() {
1989        match character {
1990            '<' => depth += 1,
1991            '>' => {
1992                depth -= 1;
1993                if depth == 0 {
1994                    return value.get(offset + character.len_utf8()..);
1995                }
1996            }
1997            _ => {}
1998        }
1999    }
2000    None
2001}
2002
2003fn analyze_gleam_calls(atlas: &mut Atlas, next_id: &mut u64) -> Result<()> {
2004    let import_pattern = Regex::new(
2005        r"^\s*import\s+([a-z][A-Za-z0-9_/]*)(?:\s+as\s+([a-z][A-Za-z0-9_]*))?(?:\.\{([^}]*)\})?",
2006    )?;
2007    let qualified_call = Regex::new(r"\b([a-z][A-Za-z0-9_]*)\.([a-z][A-Za-z0-9_]*)\s*\(")?;
2008    let unqualified_call = Regex::new(r"\b([a-z][A-Za-z0-9_]*)\s*\(")?;
2009    let gleam_files: Vec<_> = atlas
2010        .nodes
2011        .iter()
2012        .filter(|node| node.is_file() && node.path.ends_with(".gleam"))
2013        .map(|node| (node.id, node.path.clone()))
2014        .collect();
2015    let modules: HashMap<_, _> = gleam_files
2016        .iter()
2017        .filter_map(|(id, path)| gleam_module_name(path).map(|name| (name, *id)))
2018        .collect();
2019    for (source, path) in gleam_files {
2020        atlas.report.gleam_files_scanned += 1;
2021        coverage_mut(atlas, "gleam").files_analyzed += 1;
2022        let contents = fs::read_to_string(atlas.root_path.join(&path)).unwrap_or_default();
2023        let mut module_aliases = HashMap::new();
2024        let mut functions = HashMap::new();
2025        for line in contents.lines() {
2026            let Some(captures) = import_pattern.captures(line) else {
2027                continue;
2028            };
2029            let module = captures[1].to_owned();
2030            let Some(target) = modules.get(&module).copied() else {
2031                continue;
2032            };
2033            let alias = captures
2034                .get(2)
2035                .map(|value| value.as_str())
2036                .or_else(|| module.rsplit('/').next())
2037                .unwrap_or(&module);
2038            module_aliases.insert(alias.to_owned(), target);
2039            if let Some(selected) = captures.get(3) {
2040                for item in selected.as_str().split(',') {
2041                    let mut parts = item.split_whitespace();
2042                    let original = parts.next().unwrap_or("");
2043                    let alias = match (parts.next(), parts.next()) {
2044                        (Some("as"), Some(alias)) => alias,
2045                        _ => original,
2046                    };
2047                    if !alias.is_empty() {
2048                        functions.insert(alias.to_owned(), target);
2049                    }
2050                }
2051            }
2052        }
2053        for (index, line) in contents.lines().enumerate() {
2054            let code = line.split("//").next().unwrap_or("");
2055            for captures in qualified_call.captures_iter(code) {
2056                let Some(target) = module_aliases.get(&captures[1]).copied() else {
2057                    continue;
2058                };
2059                push_gleam_call(atlas, next_id, source, index, target, &captures[2]);
2060            }
2061            for captures in unqualified_call.captures_iter(code) {
2062                let Some(target) = functions.get(&captures[1]).copied() else {
2063                    continue;
2064                };
2065                push_gleam_call(atlas, next_id, source, index, target, &captures[1]);
2066            }
2067        }
2068    }
2069    Ok(())
2070}
2071
2072fn gleam_module_name(path: &str) -> Option<String> {
2073    let without_extension = path.strip_suffix(".gleam")?;
2074    without_extension
2075        .split_once("/src/")
2076        .map(|(_, module)| module.to_owned())
2077        .or_else(|| without_extension.strip_prefix("src/").map(str::to_owned))
2078}
2079
2080fn push_gleam_call(
2081    atlas: &mut Atlas,
2082    next_id: &mut u64,
2083    source: NodeId,
2084    line_index: usize,
2085    target: NodeId,
2086    function: &str,
2087) {
2088    atlas.calls.push(Callsite {
2089        id: *next_id,
2090        source,
2091        source_line: line_index as u32 + 1,
2092        target,
2093        target_line: None,
2094        callee: format!("{}.{}", atlas.nodes[target].path, function),
2095        kind: "runtime".to_owned(),
2096        analyzer: "gleam-import-static".to_owned(),
2097        confidence: 0.70,
2098    });
2099    *next_id += 1;
2100    atlas.report.resolved_calls += 1;
2101    atlas.report.gleam_calls += 1;
2102    coverage_mut(atlas, "gleam").callsites_resolved += 1;
2103}
2104
2105#[derive(Debug, Clone, Copy)]
2106struct RustDefinition {
2107    file: NodeId,
2108    line: u32,
2109}
2110
2111#[derive(Default)]
2112struct RustDefinitionVisitor {
2113    definitions: Vec<(String, u32)>,
2114}
2115
2116impl<'ast> Visit<'ast> for RustDefinitionVisitor {
2117    fn visit_item_fn(&mut self, function: &'ast syn::ItemFn) {
2118        self.definitions.push((
2119            function.sig.ident.to_string(),
2120            span_line(function.sig.ident.span()),
2121        ));
2122        syn::visit::visit_item_fn(self, function);
2123    }
2124
2125    fn visit_impl_item_fn(&mut self, function: &'ast syn::ImplItemFn) {
2126        self.definitions.push((
2127            function.sig.ident.to_string(),
2128            span_line(function.sig.ident.span()),
2129        ));
2130        syn::visit::visit_impl_item_fn(self, function);
2131    }
2132}
2133
2134#[derive(Debug)]
2135struct RustCallCandidate {
2136    name: String,
2137    line: u32,
2138    qualified: bool,
2139}
2140
2141#[derive(Default)]
2142struct RustCallVisitor {
2143    calls: Vec<RustCallCandidate>,
2144}
2145
2146impl<'ast> Visit<'ast> for RustCallVisitor {
2147    fn visit_expr_call(&mut self, call: &'ast syn::ExprCall) {
2148        if let Expr::Path(path) = call.func.as_ref() {
2149            if let Some(segment) = path.path.segments.last() {
2150                self.calls.push(RustCallCandidate {
2151                    name: segment.ident.to_string(),
2152                    line: span_line(call.span()),
2153                    qualified: path.path.segments.len() > 1,
2154                });
2155            }
2156        }
2157        syn::visit::visit_expr_call(self, call);
2158    }
2159
2160    fn visit_expr_method_call(&mut self, call: &'ast syn::ExprMethodCall) {
2161        self.calls.push(RustCallCandidate {
2162            name: call.method.to_string(),
2163            line: span_line(call.span()),
2164            qualified: false,
2165        });
2166        syn::visit::visit_expr_method_call(self, call);
2167    }
2168}
2169
2170fn analyze_rust_calls(atlas: &mut Atlas, next_id: &mut u64) -> Result<bool> {
2171    let file_count = atlas
2172        .nodes
2173        .iter()
2174        .filter(|node| node.is_file() && node.language == "rust")
2175        .count();
2176    if file_count == 0 {
2177        return Ok(true);
2178    }
2179    let cacheable = match generate_rust_scip(atlas) {
2180        Ok(Some(indexes)) => {
2181            let stats = ingest_scip_calls(atlas, &indexes, &["rust"], next_id)?;
2182            let files_analyzed = stats.files_by_language.get("rust").copied().unwrap_or(0);
2183            let callsites_resolved = stats.resolved_by_language.get("rust").copied().unwrap_or(0);
2184            let callsites_unresolved = stats
2185                .unresolved_by_language
2186                .get("rust")
2187                .copied()
2188                .unwrap_or(0);
2189            {
2190                let coverage = coverage_mut(atlas, "rust");
2191                coverage.provider = "rust-analyzer-scip".to_owned();
2192                coverage.fidelity = "compiler-backed semantic symbols".to_owned();
2193                coverage.files_analyzed = files_analyzed;
2194                coverage.callsites_resolved = callsites_resolved;
2195                coverage.callsites_unresolved = callsites_unresolved;
2196            }
2197            atlas.report.rust_files_scanned = files_analyzed;
2198            atlas.report.rust_calls = callsites_resolved;
2199            atlas.report.resolved_calls += callsites_resolved;
2200            atlas.report.unresolved_calls += callsites_unresolved;
2201            return Ok(true);
2202        }
2203        Ok(None) => {
2204            atlas.report.warnings.push(
2205                "rust-analyzer was not found; Rust used the globally-unique syntax fallback"
2206                    .to_owned(),
2207            );
2208            true
2209        }
2210        Err(error) => {
2211            atlas.report.warnings.push(format!(
2212                "rust-analyzer SCIP indexing failed; Rust used the globally-unique syntax fallback: {error:#}"
2213            ));
2214            false
2215        }
2216    };
2217    let coverage = coverage_mut(atlas, "rust");
2218    coverage.provider = "rust-syntax-unique".to_owned();
2219    coverage.fidelity = "conservative syntax fallback".to_owned();
2220    analyze_rust_calls_fallback(atlas, next_id)?;
2221    Ok(cacheable)
2222}
2223
2224fn analyze_rust_calls_fallback(atlas: &mut Atlas, next_id: &mut u64) -> Result<()> {
2225    let rust_files: Vec<(NodeId, String)> = atlas
2226        .nodes
2227        .iter()
2228        .filter(|node| node.is_file() && node.path.ends_with(".rs"))
2229        .map(|node| (node.id, node.path.clone()))
2230        .collect();
2231    if rust_files.is_empty() {
2232        return Ok(());
2233    }
2234
2235    let mut syntax_trees = Vec::new();
2236    let mut unique_definitions: HashMap<String, Option<RustDefinition>> = HashMap::new();
2237    for (file, path) in &rust_files {
2238        let source = fs::read_to_string(atlas.root_path.join(path)).unwrap_or_default();
2239        let syntax = match syn::parse_file(&source) {
2240            Ok(syntax) => syntax,
2241            Err(error) => {
2242                atlas
2243                    .report
2244                    .warnings
2245                    .push(format!("could not parse Rust file {path}: {error}"));
2246                continue;
2247            }
2248        };
2249        atlas.report.rust_files_scanned += 1;
2250        coverage_mut(atlas, "rust").files_analyzed += 1;
2251        let mut visitor = RustDefinitionVisitor::default();
2252        visitor.visit_file(&syntax);
2253        for (name, line) in visitor.definitions {
2254            unique_definitions
2255                .entry(name)
2256                .and_modify(|definition| *definition = None)
2257                .or_insert(Some(RustDefinition { file: *file, line }));
2258        }
2259        syntax_trees.push((*file, syntax));
2260    }
2261
2262    for (source, syntax) in syntax_trees {
2263        let mut visitor = RustCallVisitor::default();
2264        visitor.visit_file(&syntax);
2265        for candidate in visitor.calls {
2266            if !candidate.qualified {
2267                atlas.report.unresolved_calls += 1;
2268                coverage_mut(atlas, "rust").callsites_unresolved += 1;
2269                continue;
2270            }
2271            let Some(Some(target)) = unique_definitions.get(&candidate.name) else {
2272                atlas.report.unresolved_calls += 1;
2273                coverage_mut(atlas, "rust").callsites_unresolved += 1;
2274                continue;
2275            };
2276            atlas.calls.push(Callsite {
2277                id: *next_id,
2278                source,
2279                source_line: candidate.line,
2280                target: target.file,
2281                target_line: Some(target.line),
2282                callee: candidate.name,
2283                kind: "syntax".to_owned(),
2284                analyzer: "rust-syntax-unique".to_owned(),
2285                confidence: 0.70,
2286            });
2287            *next_id += 1;
2288            atlas.report.resolved_calls += 1;
2289            atlas.report.rust_calls += 1;
2290            coverage_mut(atlas, "rust").callsites_resolved += 1;
2291        }
2292    }
2293    Ok(())
2294}
2295
2296fn span_line(span: proc_macro2::Span) -> u32 {
2297    span.start().line.max(1) as u32
2298}
2299
2300pub fn parse_xref_line(line: &str) -> Option<XrefCall> {
2301    static PATTERN: OnceLock<Regex> = OnceLock::new();
2302    let pattern = PATTERN.get_or_init(|| {
2303        Regex::new(
2304            r"^.+?:(\d+): (?:import )?call ([A-Z][A-Za-z0-9_.]*)\.([a-zA-Z0-9_!?]+)/([0-9]+) \(([^)]+)\)$",
2305        )
2306        .expect("valid xref regex")
2307    });
2308    let captures = pattern.captures(line)?;
2309    Some(XrefCall {
2310        source_line: captures.get(1)?.as_str().parse().ok()?,
2311        module: captures.get(2)?.as_str().to_owned(),
2312        function: captures.get(3)?.as_str().to_owned(),
2313        arity: captures.get(4)?.as_str().parse().ok()?,
2314        kind: captures.get(5)?.as_str().to_owned(),
2315    })
2316}
2317
2318fn elixir_source_index(atlas: &Atlas) -> Result<ElixirSourceIndex> {
2319    let module_pattern = Regex::new(r"(?m)^\s*defmodule\s+([A-Z][A-Za-z0-9_.]*)\s+do\b")?;
2320    let mut index = ElixirSourceIndex::default();
2321    for node in atlas.nodes.iter().filter(|node| {
2322        node.is_file() && (node.path.ends_with(".ex") || node.path.ends_with(".exs"))
2323    }) {
2324        let contents = fs::read_to_string(atlas.root_path.join(&node.path)).unwrap_or_default();
2325        for captures in module_pattern.captures_iter(&contents) {
2326            index.module_files.insert(captures[1].to_owned(), node.id);
2327        }
2328        index
2329            .definition_lines
2330            .insert(node.id, index_elixir_definition_lines(&contents));
2331    }
2332    Ok(index)
2333}
2334
2335fn index_elixir_definition_lines(contents: &str) -> HashMap<String, u32> {
2336    static PATTERN: OnceLock<Regex> = OnceLock::new();
2337    let pattern = PATTERN.get_or_init(|| {
2338        Regex::new(
2339            r"^\s*(?:defp?|defmacrop?|defguardp?|defdelegate)\s+([a-zA-Z_][A-Za-z0-9_!?]*)(?:\s*\(|\s*,|\s+do|\s+when)",
2340        )
2341        .expect("valid Elixir definition regex")
2342    });
2343    let mut definitions = HashMap::new();
2344    for (line, source) in contents.lines().enumerate() {
2345        let Some(captures) = pattern.captures(source) else {
2346            continue;
2347        };
2348        definitions
2349            .entry(captures[1].to_owned())
2350            .or_insert(line as u32 + 1);
2351    }
2352    definitions
2353}
2354
2355fn rustler_targets(
2356    atlas: &Atlas,
2357    modules: &HashMap<String, NodeId>,
2358) -> Result<HashMap<(String, String), RustlerTarget>> {
2359    let init_pattern = Regex::new(r#"rustler::init!\(\s*"Elixir\.([^"]+)""#)?;
2360    let nif_pattern =
2361        Regex::new(r#"(?s)#\[rustler::nif(?:\(([^]]*)\))?\]\s*(?:pub\s+)?fn\s+([a-zA-Z0-9_]+)"#)?;
2362    let name_pattern = Regex::new(r#"name\s*=\s*"([^"]+)""#)?;
2363
2364    let cargo_roots = project_roots_for_manifest(atlas, "Cargo.toml");
2365    let mut nif_modules = HashMap::new();
2366    for node in atlas
2367        .nodes
2368        .iter()
2369        .filter(|node| node.is_file() && node.path.ends_with(".rs"))
2370    {
2371        let contents = fs::read_to_string(atlas.root_path.join(&node.path)).unwrap_or_default();
2372        if let Some(captures) = init_pattern.captures(&contents) {
2373            let root = nearest_project_root(&node.path, &cargo_roots)
2374                .unwrap_or("")
2375                .to_owned();
2376            nif_modules.insert(root, captures[1].to_owned());
2377        }
2378    }
2379
2380    if nif_modules.is_empty() {
2381        return Ok(HashMap::new());
2382    }
2383
2384    let mut targets = HashMap::new();
2385    for node in atlas
2386        .nodes
2387        .iter()
2388        .filter(|node| node.is_file() && node.path.ends_with(".rs"))
2389    {
2390        let Some(nif_module) =
2391            nearest_project_root(&node.path, &cargo_roots).and_then(|root| nif_modules.get(root))
2392        else {
2393            continue;
2394        };
2395        if !modules.contains_key(nif_module) {
2396            continue;
2397        }
2398        let contents = fs::read_to_string(atlas.root_path.join(&node.path)).unwrap_or_default();
2399        for captures in nif_pattern.captures_iter(&contents) {
2400            let rust_name = captures.get(2).expect("function capture").as_str();
2401            let args = captures.get(1).map(|value| value.as_str()).unwrap_or("");
2402            let exported_name = name_pattern
2403                .captures(args)
2404                .and_then(|captures| captures.get(1))
2405                .map(|value| value.as_str())
2406                .unwrap_or(rust_name);
2407            let line = contents[..captures.get(0).expect("whole capture").start()]
2408                .lines()
2409                .count() as u32
2410                + 1;
2411            targets.insert(
2412                (nif_module.clone(), exported_name.to_owned()),
2413                RustlerTarget {
2414                    file: node.id,
2415                    line: Some(line),
2416                },
2417            );
2418        }
2419    }
2420    Ok(targets)
2421}
2422
2423fn project_roots_for_manifest(atlas: &Atlas, manifest: &str) -> Vec<String> {
2424    let mut roots: Vec<_> = atlas
2425        .nodes
2426        .iter()
2427        .filter(|node| node.is_file() && node.name == manifest)
2428        .map(|node| {
2429            Path::new(&node.path)
2430                .parent()
2431                .map(|parent| parent.to_string_lossy().trim_end_matches('/').to_owned())
2432                .unwrap_or_default()
2433        })
2434        .collect();
2435    roots.sort_by_key(|root| (root.matches('/').count(), root.clone()));
2436    roots.dedup();
2437    roots
2438}
2439
2440fn definition_line(index: &ElixirSourceIndex, file: NodeId, function: &str) -> Option<u32> {
2441    index.definition_lines.get(&file)?.get(function).copied()
2442}
2443
2444#[cfg(test)]
2445mod tests {
2446    use super::*;
2447
2448    #[test]
2449    fn assigns_files_to_the_nearest_mix_project_root() {
2450        let roots = vec![
2451            String::new(),
2452            "apps/alpha".to_owned(),
2453            "apps/alpha/vendor/nested".to_owned(),
2454            "apps/beta".to_owned(),
2455        ];
2456
2457        assert_eq!(nearest_project_root("lib/root.ex", &roots), Some(""));
2458        assert_eq!(
2459            nearest_project_root("apps/alpha/lib/alpha.ex", &roots),
2460            Some("apps/alpha")
2461        );
2462        assert_eq!(
2463            nearest_project_root("apps/alpha/vendor/nested/lib/nested.ex", &roots),
2464            Some("apps/alpha/vendor/nested")
2465        );
2466        assert_eq!(
2467            nearest_project_root("apps/beta/lib/beta.ex", &roots),
2468            Some("apps/beta")
2469        );
2470    }
2471    use std::collections::HashMap;
2472
2473    use crate::model::{AnalyzerReport, BuildTimings, Node, NodeKind, Rect};
2474
2475    #[test]
2476    fn parses_individual_xref_callsite() {
2477        assert_eq!(
2478            parse_xref_line("lib/wasmex.ex:252: call Wasmex.Module.compile/2 (runtime)"),
2479            Some(XrefCall {
2480                source_line: 252,
2481                module: "Wasmex.Module".to_owned(),
2482                function: "compile".to_owned(),
2483                arity: 2,
2484                kind: "runtime".to_owned(),
2485            })
2486        );
2487    }
2488
2489    #[test]
2490    fn resolves_erlang_remote_imported_and_function_reference_calls() {
2491        let directory = tempfile::tempdir().unwrap();
2492        fs::write(
2493            directory.path().join("a.erl"),
2494            "-module(a).\n-import(b, [imported/0]).\nrun() ->\n  b:remote(),\n  imported(),\n  fun b:callback/1,\n  Text = \"b:fake()\",\n  Text. % b:comment()\n",
2495        )
2496        .unwrap();
2497        fs::write(
2498            directory.path().join("b.erl"),
2499            "-module(b).\nremote() -> ok.\nimported() -> ok.\ncallback(_) -> ok.\n",
2500        )
2501        .unwrap();
2502        let mut atlas = Atlas {
2503            root_path: directory.path().to_path_buf(),
2504            revision: "test".to_owned(),
2505            dirty: true,
2506            excluded_test_files: 0,
2507            excluded_hidden_files: 0,
2508            excluded_custom_files: 0,
2509            excluded_paths: Vec::new(),
2510            nodes: vec![
2511                Node {
2512                    id: 0,
2513                    parent: None,
2514                    children: vec![1, 2],
2515                    kind: NodeKind::Directory,
2516                    name: "fixture".to_owned(),
2517                    path: String::new(),
2518                    depth: 0,
2519                    bytes: 2,
2520                    loc: 2,
2521                    commits: 2,
2522                    weight: 2.0,
2523                    language: "directory".to_owned(),
2524                    rect: Rect::default(),
2525                },
2526                Node {
2527                    id: 1,
2528                    parent: Some(0),
2529                    children: Vec::new(),
2530                    kind: NodeKind::File,
2531                    name: "a.erl".to_owned(),
2532                    path: "a.erl".to_owned(),
2533                    depth: 1,
2534                    bytes: 1,
2535                    loc: 8,
2536                    commits: 1,
2537                    weight: 1.0,
2538                    language: "erlang".to_owned(),
2539                    rect: Rect::default(),
2540                },
2541                Node {
2542                    id: 2,
2543                    parent: Some(0),
2544                    children: Vec::new(),
2545                    kind: NodeKind::File,
2546                    name: "b.erl".to_owned(),
2547                    path: "b.erl".to_owned(),
2548                    depth: 1,
2549                    bytes: 1,
2550                    loc: 4,
2551                    commits: 1,
2552                    weight: 1.0,
2553                    language: "erlang".to_owned(),
2554                    rect: Rect::default(),
2555                },
2556            ],
2557            calls: Vec::new(),
2558            path_to_id: HashMap::from([("a.erl".to_owned(), 1), ("b.erl".to_owned(), 2)]),
2559            report: AnalyzerReport::default(),
2560            timings: BuildTimings::default(),
2561        };
2562        let mut next_id = 0;
2563        initialize_language_coverage(&mut atlas);
2564        analyze_erlang_calls(&mut atlas, &ElixirSourceIndex::default(), &mut next_id).unwrap();
2565
2566        assert_eq!(atlas.report.erlang_files_scanned, 2);
2567        assert_eq!(atlas.report.erlang_calls, 3);
2568        assert_eq!(atlas.calls.len(), 3);
2569        assert!(atlas.calls.iter().all(|call| call.source == 1));
2570        assert!(atlas.calls.iter().all(|call| call.target == 2));
2571        assert_eq!(atlas.report.language_coverage["erlang"].files_analyzed, 2);
2572        assert_eq!(
2573            atlas.report.language_coverage["erlang"].callsites_resolved,
2574            3
2575        );
2576        assert!(atlas.calls.iter().any(|call| call.callee == "b:remote"));
2577        assert!(atlas.calls.iter().any(|call| call.callee == "b:imported"));
2578        assert!(atlas.calls.iter().any(|call| call.callee == "b:callback"));
2579    }
2580
2581    #[test]
2582    fn finds_elixir_function_macro_guard_and_delegate_targets() {
2583        let definitions = index_elixir_definition_lines(
2584            "def run(), do: :ok\ndefmacro build(value), do: value\ndefguard valid(value) when is_atom(value)\ndefdelegate fetch(value), to: Other\n",
2585        );
2586
2587        assert_eq!(definitions.get("run"), Some(&1));
2588        assert_eq!(definitions.get("build"), Some(&2));
2589        assert_eq!(definitions.get("valid"), Some(&3));
2590        assert_eq!(definitions.get("fetch"), Some(&4));
2591    }
2592
2593    #[test]
2594    fn eex_templates_are_scanned_for_module_calls() {
2595        let directory = tempfile::tempdir().unwrap();
2596        fs::create_dir_all(directory.path().join("templates")).unwrap();
2597        fs::write(
2598            directory.path().join("templates/show.eex"),
2599            "<%= Demo.run() %>\n",
2600        )
2601        .unwrap();
2602        fs::write(
2603            directory.path().join("demo.ex"),
2604            "defmodule Demo do\n  def run(), do: :ok\nend\n",
2605        )
2606        .unwrap();
2607        let mut template = rust_node(1, "templates/show.eex");
2608        template.language = "elixir".to_owned();
2609        let mut target = rust_node(2, "demo.ex");
2610        target.language = "elixir".to_owned();
2611        let mut atlas = Atlas {
2612            root_path: directory.path().to_path_buf(),
2613            revision: "test".to_owned(),
2614            dirty: true,
2615            excluded_test_files: 0,
2616            excluded_hidden_files: 0,
2617            excluded_custom_files: 0,
2618            excluded_paths: Vec::new(),
2619            nodes: vec![
2620                Node {
2621                    id: 0,
2622                    parent: None,
2623                    children: vec![1, 2],
2624                    kind: NodeKind::Directory,
2625                    name: "fixture".to_owned(),
2626                    path: String::new(),
2627                    depth: 0,
2628                    bytes: 2,
2629                    loc: 2,
2630                    commits: 2,
2631                    weight: 2.0,
2632                    language: "directory".to_owned(),
2633                    rect: Rect::default(),
2634                },
2635                template,
2636                target,
2637            ],
2638            calls: Vec::new(),
2639            path_to_id: HashMap::new(),
2640            report: AnalyzerReport::default(),
2641            timings: BuildTimings::default(),
2642        };
2643        let index = ElixirSourceIndex {
2644            module_files: HashMap::from([("Demo".to_owned(), 2)]),
2645            definition_lines: HashMap::from([(2, HashMap::from([("run".to_owned(), 2)]))]),
2646        };
2647        let mut next_id = 0;
2648        initialize_language_coverage(&mut atlas);
2649
2650        analyze_elixir_scripts_fallback(&mut atlas, &index, &mut next_id).unwrap();
2651
2652        assert_eq!(atlas.report.elixir_script_files_scanned, 1);
2653        assert_eq!(atlas.calls.len(), 1);
2654        assert_eq!(atlas.calls[0].source, 1);
2655        assert_eq!(atlas.calls[0].target, 2);
2656        assert_eq!(atlas.calls[0].target_line, Some(2));
2657    }
2658
2659    #[test]
2660    fn ignores_non_call_xref_records() {
2661        assert!(parse_xref_line("lib/a.ex:2: alias A (runtime)").is_none());
2662        assert!(parse_xref_line("lib/a.ex:3: struct A (export)").is_none());
2663    }
2664
2665    #[test]
2666    fn excludes_same_file_calls_without_aggregating_cross_file_calls() {
2667        let directory = tempfile::tempdir().unwrap();
2668        let mut atlas = Atlas {
2669            root_path: directory.path().to_path_buf(),
2670            revision: "test".to_owned(),
2671            dirty: true,
2672            excluded_test_files: 0,
2673            excluded_hidden_files: 0,
2674            excluded_custom_files: 0,
2675            excluded_paths: Vec::new(),
2676            nodes: vec![
2677                Node {
2678                    id: 0,
2679                    parent: None,
2680                    children: vec![1, 2],
2681                    kind: NodeKind::Directory,
2682                    name: "fixture".to_owned(),
2683                    path: String::new(),
2684                    depth: 0,
2685                    bytes: 2,
2686                    loc: 2,
2687                    commits: 2,
2688                    weight: 2.0,
2689                    language: "directory".to_owned(),
2690                    rect: Rect::default(),
2691                },
2692                rust_node(1, "a.rs"),
2693                rust_node(2, "b.rs"),
2694            ],
2695            calls: vec![
2696                Callsite {
2697                    id: 11,
2698                    source: 1,
2699                    source_line: 3,
2700                    target: 1,
2701                    target_line: Some(8),
2702                    callee: "a::local".to_owned(),
2703                    kind: "syntax".to_owned(),
2704                    analyzer: "fixture".to_owned(),
2705                    confidence: 1.0,
2706                },
2707                Callsite {
2708                    id: 12,
2709                    source: 1,
2710                    source_line: 4,
2711                    target: 2,
2712                    target_line: Some(9),
2713                    callee: "b::remote".to_owned(),
2714                    kind: "syntax".to_owned(),
2715                    analyzer: "fixture".to_owned(),
2716                    confidence: 1.0,
2717                },
2718            ],
2719            path_to_id: HashMap::new(),
2720            report: AnalyzerReport::default(),
2721            timings: BuildTimings::default(),
2722        };
2723
2724        exclude_same_file_calls(&mut atlas);
2725
2726        assert_eq!(atlas.calls.len(), 1);
2727        assert_eq!(atlas.calls[0].id, 12);
2728        assert_eq!(atlas.report.same_file_calls_excluded, 1);
2729    }
2730
2731    #[test]
2732    fn resolves_unique_cross_file_rust_call_without_aggregation() {
2733        let directory = tempfile::tempdir().unwrap();
2734        fs::write(
2735            directory.path().join("a.rs"),
2736            "pub fn alpha() { b::beta(); }\n",
2737        )
2738        .unwrap();
2739        fs::write(directory.path().join("b.rs"), "pub fn beta() {}\n").unwrap();
2740        let mut atlas = Atlas {
2741            root_path: directory.path().to_path_buf(),
2742            revision: "test".to_owned(),
2743            dirty: false,
2744            excluded_test_files: 0,
2745            excluded_hidden_files: 0,
2746            excluded_custom_files: 0,
2747            excluded_paths: Vec::new(),
2748            nodes: vec![
2749                Node {
2750                    id: 0,
2751                    parent: None,
2752                    children: vec![1, 2],
2753                    kind: NodeKind::Directory,
2754                    name: "fixture".to_owned(),
2755                    path: String::new(),
2756                    depth: 0,
2757                    bytes: 0,
2758                    loc: 0,
2759                    commits: 0,
2760                    weight: 2.0,
2761                    language: "directory".to_owned(),
2762                    rect: Rect::default(),
2763                },
2764                rust_node(1, "a.rs"),
2765                rust_node(2, "b.rs"),
2766            ],
2767            calls: Vec::new(),
2768            path_to_id: HashMap::new(),
2769            report: AnalyzerReport::default(),
2770            timings: BuildTimings::default(),
2771        };
2772        let mut next_id = 0;
2773
2774        initialize_language_coverage(&mut atlas);
2775        analyze_rust_calls_fallback(&mut atlas, &mut next_id).unwrap();
2776
2777        assert_eq!(atlas.report.rust_files_scanned, 2);
2778        assert_eq!(atlas.report.rust_calls, 1);
2779        assert_eq!(atlas.calls.len(), 1);
2780        assert_eq!(atlas.calls[0].source, 1);
2781        assert_eq!(atlas.calls[0].target, 2);
2782    }
2783
2784    #[test]
2785    fn dirty_worktree_cache_round_trips_calls_by_path() {
2786        let directory = tempfile::tempdir().unwrap();
2787        assert!(
2788            Command::new("git")
2789                .args(["init", "--quiet"])
2790                .arg(directory.path())
2791                .status()
2792                .unwrap()
2793                .success()
2794        );
2795        let nodes = vec![
2796            Node {
2797                id: 0,
2798                parent: None,
2799                children: vec![1, 2],
2800                kind: NodeKind::Directory,
2801                name: "fixture".to_owned(),
2802                path: String::new(),
2803                depth: 0,
2804                bytes: 2,
2805                loc: 2,
2806                commits: 2,
2807                weight: 2.0,
2808                language: "directory".to_owned(),
2809                rect: Rect::default(),
2810            },
2811            rust_node(1, "a.rs"),
2812            rust_node(2, "b.rs"),
2813        ];
2814        let mut path_to_id = HashMap::new();
2815        path_to_id.insert("a.rs".to_owned(), 1);
2816        path_to_id.insert("b.rs".to_owned(), 2);
2817        let mut atlas = Atlas {
2818            root_path: directory.path().to_path_buf(),
2819            revision: "abc123".to_owned(),
2820            dirty: true,
2821            excluded_test_files: 0,
2822            excluded_hidden_files: 0,
2823            excluded_custom_files: 0,
2824            excluded_paths: Vec::new(),
2825            nodes: nodes.clone(),
2826            calls: vec![Callsite {
2827                id: 7,
2828                source: 1,
2829                source_line: 3,
2830                target: 2,
2831                target_line: Some(8),
2832                callee: "b::run".to_owned(),
2833                kind: "syntax".to_owned(),
2834                analyzer: "fixture".to_owned(),
2835                confidence: 0.9,
2836            }],
2837            path_to_id: path_to_id.clone(),
2838            report: AnalyzerReport {
2839                resolved_calls: 1,
2840                ..AnalyzerReport::default()
2841            },
2842            timings: BuildTimings::default(),
2843        };
2844        save_cached_analysis(&mut atlas).unwrap();
2845
2846        atlas.calls.clear();
2847        atlas.report = AnalyzerReport::default();
2848        atlas.nodes = nodes;
2849        atlas.path_to_id = path_to_id;
2850        assert!(load_cached_analysis(&mut atlas).unwrap());
2851        assert_eq!(atlas.calls.len(), 1);
2852        assert_eq!(atlas.calls[0].source, 1);
2853        assert_eq!(atlas.calls[0].target, 2);
2854        assert!(atlas.report.cache_hit);
2855    }
2856
2857    #[test]
2858    fn resolves_typescript_and_gleam_imported_calls_without_aggregation() {
2859        let directory = tempfile::tempdir().unwrap();
2860        fs::create_dir_all(directory.path().join("src")).unwrap();
2861        fs::create_dir_all(directory.path().join("gleam/src")).unwrap();
2862        fs::write(
2863            directory.path().join("src/a.ts"),
2864            "import { run as execute } from './b';\nexecute();\nexecute();\n",
2865        )
2866        .unwrap();
2867        fs::write(
2868            directory.path().join("src/b.ts"),
2869            "export function run() {}\n",
2870        )
2871        .unwrap();
2872        fs::write(
2873            directory.path().join("gleam/src/a.gleam"),
2874            "import b\npub fn start() { b.run() }\n",
2875        )
2876        .unwrap();
2877        fs::write(
2878            directory.path().join("gleam/src/b.gleam"),
2879            "pub fn run() { Nil }\n",
2880        )
2881        .unwrap();
2882        let paths = [
2883            "src/a.ts",
2884            "src/b.ts",
2885            "gleam/src/a.gleam",
2886            "gleam/src/b.gleam",
2887        ];
2888        let mut nodes = vec![Node {
2889            id: 0,
2890            parent: None,
2891            children: (1..=paths.len()).collect(),
2892            kind: NodeKind::Directory,
2893            name: "fixture".to_owned(),
2894            path: String::new(),
2895            depth: 0,
2896            bytes: 4,
2897            loc: 4,
2898            commits: 4,
2899            weight: 4.0,
2900            language: "directory".to_owned(),
2901            rect: Rect::default(),
2902        }];
2903        let mut path_to_id = HashMap::new();
2904        for (index, path) in paths.iter().enumerate() {
2905            let id = index + 1;
2906            nodes.push(Node {
2907                id,
2908                parent: Some(0),
2909                children: Vec::new(),
2910                kind: NodeKind::File,
2911                name: path.rsplit('/').next().unwrap().to_owned(),
2912                path: (*path).to_owned(),
2913                depth: 1,
2914                bytes: 1,
2915                loc: 1,
2916                commits: 1,
2917                weight: 1.0,
2918                language: if path.ends_with(".gleam") {
2919                    "gleam".to_owned()
2920                } else {
2921                    "typescript".to_owned()
2922                },
2923                rect: Rect::default(),
2924            });
2925            path_to_id.insert((*path).to_owned(), id);
2926        }
2927        let mut atlas = Atlas {
2928            root_path: directory.path().to_path_buf(),
2929            revision: "test".to_owned(),
2930            dirty: true,
2931            excluded_test_files: 0,
2932            excluded_hidden_files: 0,
2933            excluded_custom_files: 0,
2934            excluded_paths: Vec::new(),
2935            nodes,
2936            calls: Vec::new(),
2937            path_to_id,
2938            report: AnalyzerReport::default(),
2939            timings: BuildTimings::default(),
2940        };
2941        let mut next_id = 0;
2942        initialize_language_coverage(&mut atlas);
2943        analyze_typescript_calls_fallback(&mut atlas, &mut next_id).unwrap();
2944        analyze_gleam_calls(&mut atlas, &mut next_id).unwrap();
2945
2946        assert_eq!(atlas.report.typescript_files_scanned, 2);
2947        assert_eq!(atlas.report.typescript_calls, 2);
2948        assert_eq!(atlas.report.gleam_files_scanned, 2);
2949        assert_eq!(atlas.report.gleam_calls, 1);
2950        assert_eq!(atlas.report.language_coverage["gleam"].files_discovered, 2);
2951        assert_eq!(atlas.report.language_coverage["gleam"].files_analyzed, 2);
2952        assert_eq!(
2953            atlas.report.language_coverage["gleam"].callsites_resolved,
2954            1
2955        );
2956        assert_eq!(atlas.calls.len(), 3);
2957        assert_eq!(atlas.calls[0].source, 1);
2958        assert_eq!(atlas.calls[0].target, 2);
2959        assert_eq!(atlas.calls[1].source, 1);
2960        assert_eq!(atlas.calls[1].target, 2);
2961        assert_eq!(atlas.calls[2].source, 3);
2962        assert_eq!(atlas.calls[2].target, 4);
2963    }
2964
2965    #[test]
2966    fn semantic_scip_ingestion_keeps_individual_calls_and_exact_targets() {
2967        let directory = tempfile::tempdir().unwrap();
2968        fs::create_dir_all(directory.path().join("src")).unwrap();
2969        fs::write(
2970            directory.path().join("src/a.ts"),
2971            "execute();\nexecute();\n",
2972        )
2973        .unwrap();
2974        fs::write(
2975            directory.path().join("src/b.ts"),
2976            "export function execute() {}\n",
2977        )
2978        .unwrap();
2979        let paths = ["src/a.ts", "src/b.ts"];
2980        let mut path_to_id = HashMap::new();
2981        let mut nodes = vec![Node {
2982            id: 0,
2983            parent: None,
2984            children: vec![1, 2],
2985            kind: NodeKind::Directory,
2986            name: "fixture".to_owned(),
2987            path: String::new(),
2988            depth: 0,
2989            bytes: 2,
2990            loc: 2,
2991            commits: 2,
2992            weight: 2.0,
2993            language: "directory".to_owned(),
2994            rect: Rect::default(),
2995        }];
2996        for (index, path) in paths.iter().enumerate() {
2997            let id = index + 1;
2998            nodes.push(Node {
2999                id,
3000                parent: Some(0),
3001                children: Vec::new(),
3002                kind: NodeKind::File,
3003                name: path.rsplit('/').next().unwrap().to_owned(),
3004                path: (*path).to_owned(),
3005                depth: 1,
3006                bytes: 1,
3007                loc: 1,
3008                commits: 1,
3009                weight: 1.0,
3010                language: "typescript".to_owned(),
3011                rect: Rect::default(),
3012            });
3013            path_to_id.insert((*path).to_owned(), id);
3014        }
3015        let symbol = "scip-typescript npm fixture 1.0 src/b.ts/execute().";
3016        let reference = |line| crate::scip::ScipOccurrence {
3017            range: Some(ScipRange {
3018                start_line: line,
3019                start_character: 0,
3020                end_line: line,
3021                end_character: 7,
3022            }),
3023            symbol: symbol.to_owned(),
3024            symbol_roles: 0,
3025        };
3026        let indexes = vec![RootedScipIndex {
3027            root: String::new(),
3028            index: ScipIndex {
3029                documents: vec![
3030                    ScipDocument {
3031                        relative_path: "src/a.ts".to_owned(),
3032                        language: "typescript".to_owned(),
3033                        position_encoding: 2,
3034                        occurrences: vec![reference(0), reference(1)],
3035                    },
3036                    ScipDocument {
3037                        relative_path: "src/b.ts".to_owned(),
3038                        language: "typescript".to_owned(),
3039                        position_encoding: 2,
3040                        occurrences: vec![crate::scip::ScipOccurrence {
3041                            range: Some(ScipRange {
3042                                start_line: 0,
3043                                start_character: 16,
3044                                end_line: 0,
3045                                end_character: 23,
3046                            }),
3047                            symbol: symbol.to_owned(),
3048                            symbol_roles: 1,
3049                        }],
3050                    },
3051                ],
3052            },
3053        }];
3054        let mut atlas = Atlas {
3055            root_path: directory.path().to_path_buf(),
3056            revision: "test".to_owned(),
3057            dirty: true,
3058            excluded_test_files: 0,
3059            excluded_hidden_files: 0,
3060            excluded_custom_files: 0,
3061            excluded_paths: Vec::new(),
3062            nodes,
3063            calls: Vec::new(),
3064            path_to_id,
3065            report: AnalyzerReport::default(),
3066            timings: BuildTimings::default(),
3067        };
3068        let mut next_id = 0;
3069
3070        let stats = ingest_scip_calls(&mut atlas, &indexes, &["typescript"], &mut next_id).unwrap();
3071
3072        assert_eq!(stats.files_by_language["typescript"], 2);
3073        assert_eq!(stats.resolved_by_language["typescript"], 2);
3074        assert_eq!(atlas.calls.len(), 2);
3075        assert_eq!(atlas.calls[0].source_line, 1);
3076        assert_eq!(atlas.calls[1].source_line, 2);
3077        assert_eq!(atlas.calls[0].target, 2);
3078        assert_eq!(atlas.calls[0].target_line, Some(1));
3079        assert_eq!(atlas.calls[0].analyzer, "scip-typescript");
3080    }
3081
3082    #[test]
3083    fn semantic_scip_ingestion_supports_javascript_documents() {
3084        let directory = tempfile::tempdir().unwrap();
3085        fs::create_dir_all(directory.path().join("src")).unwrap();
3086        fs::write(directory.path().join("src/a.js"), "execute();\n").unwrap();
3087        fs::write(
3088            directory.path().join("src/b.js"),
3089            "export function execute() {}\n",
3090        )
3091        .unwrap();
3092        let paths = ["src/a.js", "src/b.js"];
3093        let mut path_to_id = HashMap::new();
3094        let mut nodes = vec![Node {
3095            id: 0,
3096            parent: None,
3097            children: vec![1, 2],
3098            kind: NodeKind::Directory,
3099            name: "fixture".to_owned(),
3100            path: String::new(),
3101            depth: 0,
3102            bytes: 2,
3103            loc: 2,
3104            commits: 2,
3105            weight: 2.0,
3106            language: "directory".to_owned(),
3107            rect: Rect::default(),
3108        }];
3109        for (index, path) in paths.iter().enumerate() {
3110            let id = index + 1;
3111            nodes.push(Node {
3112                id,
3113                parent: Some(0),
3114                children: Vec::new(),
3115                kind: NodeKind::File,
3116                name: path.rsplit('/').next().unwrap().to_owned(),
3117                path: (*path).to_owned(),
3118                depth: 1,
3119                bytes: 1,
3120                loc: 1,
3121                commits: 1,
3122                weight: 1.0,
3123                language: "javascript".to_owned(),
3124                rect: Rect::default(),
3125            });
3126            path_to_id.insert((*path).to_owned(), id);
3127        }
3128        let symbol = "scip-typescript npm fixture 1.0 src/b.js/execute().";
3129        let indexes = vec![RootedScipIndex {
3130            root: String::new(),
3131            index: ScipIndex {
3132                documents: vec![
3133                    ScipDocument {
3134                        relative_path: "src/a.js".to_owned(),
3135                        language: "javascript".to_owned(),
3136                        position_encoding: 2,
3137                        occurrences: vec![crate::scip::ScipOccurrence {
3138                            range: Some(ScipRange {
3139                                start_line: 0,
3140                                start_character: 0,
3141                                end_line: 0,
3142                                end_character: 7,
3143                            }),
3144                            symbol: symbol.to_owned(),
3145                            symbol_roles: 0,
3146                        }],
3147                    },
3148                    ScipDocument {
3149                        relative_path: "src/b.js".to_owned(),
3150                        language: "javascript".to_owned(),
3151                        position_encoding: 2,
3152                        occurrences: vec![crate::scip::ScipOccurrence {
3153                            range: Some(ScipRange {
3154                                start_line: 0,
3155                                start_character: 16,
3156                                end_line: 0,
3157                                end_character: 23,
3158                            }),
3159                            symbol: symbol.to_owned(),
3160                            symbol_roles: 1,
3161                        }],
3162                    },
3163                ],
3164            },
3165        }];
3166        let mut atlas = Atlas {
3167            root_path: directory.path().to_path_buf(),
3168            revision: "test".to_owned(),
3169            dirty: true,
3170            excluded_test_files: 0,
3171            excluded_hidden_files: 0,
3172            excluded_custom_files: 0,
3173            excluded_paths: Vec::new(),
3174            nodes,
3175            calls: Vec::new(),
3176            path_to_id,
3177            report: AnalyzerReport::default(),
3178            timings: BuildTimings::default(),
3179        };
3180        let mut next_id = 0;
3181
3182        let stats = ingest_scip_calls(&mut atlas, &indexes, &["javascript"], &mut next_id).unwrap();
3183
3184        assert_eq!(stats.files_by_language["javascript"], 2);
3185        assert_eq!(stats.resolved_by_language["javascript"], 1);
3186        assert_eq!(atlas.calls.len(), 1);
3187        assert_eq!(atlas.calls[0].source_line, 1);
3188        assert_eq!(atlas.calls[0].target_line, Some(1));
3189        assert_eq!(atlas.calls[0].analyzer, "scip-typescript");
3190    }
3191
3192    #[test]
3193    fn call_suffix_classifier_handles_methods_generics_macros_and_optional_calls() {
3194        assert!(suffix_is_call("()"));
3195        assert!(suffix_is_call("::<Value>()"));
3196        assert!(suffix_is_call("<Value>()"));
3197        assert!(suffix_is_call("!()"));
3198        assert!(suffix_is_call("?.()"));
3199        assert!(!suffix_is_call(";"));
3200        assert!(!suffix_is_call("::<Value>"));
3201    }
3202
3203    fn rust_node(id: usize, path: &str) -> Node {
3204        Node {
3205            id,
3206            parent: Some(0),
3207            children: Vec::new(),
3208            kind: NodeKind::File,
3209            name: path.to_owned(),
3210            path: path.to_owned(),
3211            depth: 1,
3212            bytes: 1,
3213            loc: 1,
3214            commits: 1,
3215            weight: 1.0,
3216            language: "rust".to_owned(),
3217            rect: Rect::default(),
3218        }
3219    }
3220}