1use std::{
2 collections::{HashMap, HashSet},
3 env, fs,
4 io::{BufRead, BufReader, Read, Write},
5 path::{Path, PathBuf},
6 process::{Command, Stdio},
7 thread,
8};
9
10use anyhow::Result;
11use regex::Regex;
12use syn::{Expr, spanned::Spanned, visit::Visit};
13
14use serde::{Deserialize, Serialize};
15
16use crate::{
17 model::{AnalyzerReport, Atlas, Callsite, LanguageCoverage, NodeId},
18 scip::{ScipDocument, ScipIndex, ScipRange, decode_index},
19};
20
21const CACHE_VERSION: u32 = 7;
22const FILE_MARKER: &str = "__CODE_ATLAS_FILE__\t";
23const END_MARKER: &str = "__CODE_ATLAS_END__";
24const ERROR_MARKER: &str = "__CODE_ATLAS_ERROR__\t";
25const SCRIPT_CALL_MARKER: &str = "__CODE_ATLAS_SCRIPT_CALL__\t";
26const XREF_BATCH_EXPRESSION: &str = r#"
27for path <- IO.stream(:stdio, :line) do
28 path = String.trim(path)
29 IO.puts("__CODE_ATLAS_FILE__\t" <> path)
30 Mix.Task.reenable("xref")
31 try do
32 Mix.Tasks.Xref.run(["trace", path, "--no-compile", "--no-deps-check"])
33 rescue
34 error -> IO.puts("__CODE_ATLAS_ERROR__\t" <> Exception.message(error))
35 catch
36 kind, reason -> IO.puts("__CODE_ATLAS_ERROR__\t" <> Exception.format(kind, reason, __STACKTRACE__))
37 end
38 IO.puts("__CODE_ATLAS_END__")
39end
40"#;
41const ELIXIR_SCRIPT_EXPRESSION: &str = r#"
42for raw_path <- IO.stream(:stdio, :line) do
43 path = String.trim(raw_path)
44 IO.puts("__CODE_ATLAS_FILE__\t" <> path)
45 case File.read(path) do
46 {:ok, contents} ->
47 case Code.string_to_quoted(contents, columns: true) do
48 {:ok, ast} ->
49 {_ast, aliases} = Macro.prewalk(ast, %{}, fn
50 {:alias, _, [{:__aliases__, _, parts} | options]} = node, aliases ->
51 full = Enum.map_join(parts, ".", &Atom.to_string/1)
52 short = case Keyword.get(List.first(options) || [], :as) do
53 {:__aliases__, _, as_parts} -> as_parts |> List.last() |> Atom.to_string()
54 _ -> parts |> List.last() |> Atom.to_string()
55 end
56 {node, Map.put(aliases, short, full)}
57 node, aliases -> {node, aliases}
58 end)
59 Macro.prewalk(ast, fn
60 {{:., _, [{:__aliases__, _, parts}, function]}, meta, arguments} = node
61 when is_atom(function) and is_list(arguments) ->
62 names = Enum.map(parts, &Atom.to_string/1)
63 module = case names do
64 [first | rest] ->
65 case Map.get(aliases, first) do
66 nil -> Enum.join(names, ".")
67 resolved -> Enum.join([resolved | rest], ".")
68 end
69 [] -> ""
70 end
71 IO.puts("__CODE_ATLAS_SCRIPT_CALL__\t#{meta[:line] || 1}\t#{module}\t#{function}\t#{length(arguments)}")
72 node
73 node -> node
74 end)
75 {:error, error} ->
76 message = error |> inspect(limit: 10) |> String.replace("\n", " ") |> String.replace("\r", " ") |> String.replace("\t", " ")
77 IO.puts("__CODE_ATLAS_ERROR__\t" <> message)
78 end
79 {:error, error} -> IO.puts("__CODE_ATLAS_ERROR__\t" <> inspect(error))
80 end
81 IO.puts("__CODE_ATLAS_END__")
82end
83"#;
84
85#[derive(Debug, Clone, PartialEq, Eq)]
86pub struct XrefCall {
87 pub source_line: u32,
88 pub module: String,
89 pub function: String,
90 pub arity: u32,
91 pub kind: String,
92}
93
94#[derive(Debug, Clone)]
95struct RustlerTarget {
96 file: NodeId,
97 line: Option<u32>,
98}
99
100fn mix_project_roots(atlas: &Atlas) -> Vec<String> {
101 let mut roots: Vec<_> = atlas
102 .nodes
103 .iter()
104 .filter(|node| {
105 node.is_file() && (node.path == "mix.exs" || node.path.ends_with("/mix.exs"))
106 })
107 .filter_map(|node| {
108 Path::new(&node.path)
109 .parent()
110 .map(|parent| parent.to_string_lossy().trim_end_matches('/').to_owned())
111 })
112 .collect();
113 roots.sort_by_key(|root| (root.matches('/').count(), root.clone()));
114 roots.dedup();
115 roots
116}
117
118fn elixir_files_for_project(
119 atlas: &Atlas,
120 project_root: &str,
121 project_roots: &[String],
122) -> Vec<(String, NodeId)> {
123 atlas
124 .nodes
125 .iter()
126 .filter(|node| node.is_file() && node.path.ends_with(".ex"))
127 .filter(|node| nearest_project_root(&node.path, project_roots) == Some(project_root))
128 .map(|node| {
129 let input_path = if project_root.is_empty() {
130 node.path.clone()
131 } else {
132 node.path
133 .strip_prefix(project_root)
134 .and_then(|path| path.strip_prefix('/'))
135 .unwrap_or(&node.path)
136 .to_owned()
137 };
138 (input_path, node.id)
139 })
140 .collect()
141}
142
143fn nearest_project_root<'a>(path: &str, project_roots: &'a [String]) -> Option<&'a str> {
144 project_roots
145 .iter()
146 .filter(|root| {
147 root.is_empty()
148 || path == root.as_str()
149 || path
150 .strip_prefix(root.as_str())
151 .is_some_and(|suffix| suffix.starts_with('/'))
152 })
153 .max_by_key(|root| root.len())
154 .map(String::as_str)
155}
156
157fn display_project_root(root: &str) -> &str {
158 if root.is_empty() { "." } else { root }
159}
160
161pub fn analyze_calls(atlas: &mut Atlas) -> Result<()> {
162 if load_cached_analysis(atlas)? {
163 exclude_same_file_calls(atlas);
164 eprintln!(
165 "Loaded {} cross-file callsites from the repository cache ({} same-file callsites excluded).",
166 atlas.calls.len(),
167 atlas.report.same_file_calls_excluded
168 );
169 return Ok(());
170 }
171
172 initialize_language_coverage(atlas);
173
174 let module_files = elixir_module_files(atlas)?;
175 let rustler_targets = rustler_targets(atlas, &module_files)?;
176 let mut next_id = atlas.calls.len() as u64;
177
178 let project_roots = mix_project_roots(atlas);
179 let mut cacheable = true;
180 if !project_roots.is_empty() {
181 for project_root in &project_roots {
182 let files = elixir_files_for_project(atlas, project_root, &project_roots);
183 cacheable &= analyze_elixir_calls(
184 atlas,
185 &module_files,
186 &rustler_targets,
187 &mut next_id,
188 project_root,
189 files,
190 );
191 }
192 } else if atlas
193 .nodes
194 .iter()
195 .any(|node| node.is_file() && node.language == "elixir")
196 {
197 atlas
198 .report
199 .warnings
200 .push("no mix.exs found; Elixir xref analyzer was skipped".to_owned());
201 }
202 analyze_elixir_scripts(atlas, &module_files, &mut next_id)?;
203
204 cacheable &= analyze_typescript_calls(atlas, &mut next_id)?;
205 analyze_gleam_calls(atlas, &mut next_id)?;
206 cacheable &= analyze_rust_calls(atlas, &mut next_id)?;
207
208 if cacheable {
209 save_cached_analysis(atlas)?;
210 }
211
212 exclude_same_file_calls(atlas);
213
214 Ok(())
215}
216
217fn initialize_language_coverage(atlas: &mut Atlas) {
218 for language in ["elixir", "rust", "javascript", "typescript", "gleam"] {
219 let files_discovered = atlas
220 .nodes
221 .iter()
222 .filter(|node| node.is_file() && node.language == language)
223 .count();
224 atlas.report.language_coverage.insert(
225 language.to_owned(),
226 LanguageCoverage {
227 provider: "none".to_owned(),
228 fidelity: "not applicable".to_owned(),
229 files_discovered,
230 ..LanguageCoverage::default()
231 },
232 );
233 }
234 if coverage_mut(atlas, "elixir").files_discovered > 0 {
235 let elixir = coverage_mut(atlas, "elixir");
236 elixir.provider = "mix-xref + script-static".to_owned();
237 elixir.fidelity = "compiler-backed project files; conservative scripts".to_owned();
238 }
239 if coverage_mut(atlas, "gleam").files_discovered > 0 {
240 let gleam = coverage_mut(atlas, "gleam");
241 gleam.provider = "gleam-import-static".to_owned();
242 gleam.fidelity = "conservative explicit-import resolution".to_owned();
243 }
244}
245
246fn coverage_mut<'a>(atlas: &'a mut Atlas, language: &str) -> &'a mut LanguageCoverage {
247 atlas
248 .report
249 .language_coverage
250 .entry(language.to_owned())
251 .or_default()
252}
253
254fn exclude_same_file_calls(atlas: &mut Atlas) {
255 let before = atlas.calls.len();
256 atlas.calls.retain(|call| call.source != call.target);
257 atlas.report.same_file_calls_excluded = before - atlas.calls.len();
258}
259
260fn analyze_elixir_calls(
261 atlas: &mut Atlas,
262 module_files: &HashMap<String, NodeId>,
263 rustler_targets: &HashMap<(String, String), RustlerTarget>,
264 next_id: &mut u64,
265 project_root: &str,
266 files: Vec<(String, NodeId)>,
267) -> bool {
268 let compile = Command::new("mix")
271 .arg("compile")
272 .env("MIX_OS_CONCURRENCY_LOCK", "0")
273 .current_dir(atlas.root_path.join(project_root))
274 .output();
275 match compile {
276 Ok(output) if output.status.success() => {}
277 Ok(output) => {
278 atlas.report.warnings.push(format!(
279 "mix compile failed in {} before xref analysis; prepare the checkout with its documented toolchain and dependencies: {}",
280 display_project_root(project_root),
281 String::from_utf8_lossy(&output.stderr).trim()
282 ));
283 return false;
284 }
285 Err(error) => {
286 atlas.report.warnings.push(format!(
287 "could not run mix compile in {} before xref analysis: {error}",
288 display_project_root(project_root)
289 ));
290 return false;
291 }
292 }
293
294 if files.is_empty() {
295 return true;
296 }
297 eprintln!(
298 "Tracing {} Elixir files in {} with one Mix runtimeā¦",
299 files.len(),
300 display_project_root(project_root)
301 );
302 let source_ids: HashMap<_, _> = files
303 .iter()
304 .map(|(input_path, id)| (input_path.clone(), *id))
305 .collect();
306 let mut child = match Command::new("mix")
307 .args([
308 "run",
309 "--no-compile",
310 "--no-deps-check",
311 "--no-start",
312 "-e",
313 XREF_BATCH_EXPRESSION,
314 ])
315 .env("MIX_OS_CONCURRENCY_LOCK", "0")
316 .current_dir(atlas.root_path.join(project_root))
317 .stdin(Stdio::piped())
318 .stdout(Stdio::piped())
319 .stderr(Stdio::piped())
320 .spawn()
321 {
322 Ok(child) => child,
323 Err(error) => {
324 atlas
325 .report
326 .warnings
327 .push(format!("could not start batched Mix xref: {error}"));
328 return false;
329 }
330 };
331 let mut stdin = child.stdin.take().expect("piped stdin");
332 let input_paths: Vec<_> = files
333 .iter()
334 .map(|(input_path, _)| input_path.clone())
335 .collect();
336 let writer = thread::spawn(move || {
337 for path in input_paths {
338 writeln!(stdin, "{path}")?;
339 }
340 Ok::<_, std::io::Error>(())
341 });
342 let mut stderr = child.stderr.take().expect("piped stderr");
343 let stderr_reader = thread::spawn(move || {
344 let mut contents = String::new();
345 stderr.read_to_string(&mut contents).map(|_| contents)
346 });
347 let stdout = child.stdout.take().expect("piped stdout");
348 let mut current: Option<(String, NodeId)> = None;
349 let mut current_failed = false;
350 let mut project_completed = 0_usize;
351 for line in BufReader::new(stdout).lines() {
352 let line = match line {
353 Ok(line) => line,
354 Err(error) => {
355 atlas
356 .report
357 .warnings
358 .push(format!("could not read batched xref output: {error}"));
359 break;
360 }
361 };
362 if let Some(path) = line.strip_prefix(FILE_MARKER) {
363 current = source_ids
364 .get(path)
365 .copied()
366 .map(|id| (path.to_owned(), id));
367 current_failed = false;
368 continue;
369 }
370 if let Some(error) = line.strip_prefix(ERROR_MARKER) {
371 let path = current
372 .as_ref()
373 .map(|(path, _)| path.as_str())
374 .unwrap_or("unknown file");
375 atlas
376 .report
377 .warnings
378 .push(format!("mix xref trace failed for {path}: {error}"));
379 current_failed = true;
380 continue;
381 }
382 if line == END_MARKER {
383 if current.is_some() && !current_failed {
384 atlas.report.elixir_files_traced += 1;
385 coverage_mut(atlas, "elixir").files_analyzed += 1;
386 project_completed += 1;
387 if project_completed.is_multiple_of(100) || project_completed == files.len() {
388 eprintln!(
389 " traced {project_completed}/{} Elixir files in {}",
390 files.len(),
391 display_project_root(project_root)
392 );
393 }
394 }
395 current = None;
396 continue;
397 }
398 if let Some((_, source_id)) = current.as_ref() {
399 let Some(call) = parse_xref_line(&line) else {
400 continue;
401 };
402 let key = (call.module.clone(), call.function.clone());
403 let (target, target_line, analyzer) = if let Some(target) = rustler_targets.get(&key) {
404 atlas.report.rustler_calls += 1;
405 (target.file, target.line, "elixir-xref+rustler")
406 } else if let Some(target) = module_files.get(&call.module) {
407 (
408 *target,
409 definition_line(
410 &atlas.root_path.join(&atlas.nodes[*target].path),
411 &call.function,
412 ),
413 "elixir-xref",
414 )
415 } else {
416 atlas.report.unresolved_calls += 1;
417 coverage_mut(atlas, "elixir").callsites_unresolved += 1;
418 continue;
419 };
420
421 atlas.calls.push(Callsite {
422 id: *next_id,
423 source: *source_id,
424 source_line: call.source_line,
425 target,
426 target_line,
427 callee: format!("{}.{}/{}", call.module, call.function, call.arity),
428 kind: call.kind,
429 analyzer: analyzer.to_owned(),
430 confidence: if analyzer.contains("rustler") {
431 0.98
432 } else {
433 1.0
434 },
435 });
436 *next_id += 1;
437 atlas.report.resolved_calls += 1;
438 coverage_mut(atlas, "elixir").callsites_resolved += 1;
439 }
440 }
441 let writer_succeeded = match writer.join().expect("xref input writer panicked") {
442 Ok(()) => true,
443 Err(error) => {
444 atlas
445 .report
446 .warnings
447 .push(format!("could not send xref input: {error}"));
448 false
449 }
450 };
451 let status = child.wait();
452 let stderr = stderr_reader
453 .join()
454 .expect("xref stderr reader panicked")
455 .unwrap_or_default();
456 let batch_succeeded = match status {
457 Ok(status) if status.success() => true,
458 Ok(status) => {
459 atlas.report.warnings.push(format!(
460 "batched Mix xref exited with {status}: {}",
461 stderr.trim()
462 ));
463 false
464 }
465 Err(error) => {
466 atlas
467 .report
468 .warnings
469 .push(format!("could not wait for batched Mix xref: {error}"));
470 false
471 }
472 };
473 writer_succeeded && batch_succeeded
474}
475
476#[derive(Debug, Serialize, Deserialize)]
477struct CachedAnalysis {
478 version: u32,
479 calls: Vec<CachedCallsite>,
480 report: AnalyzerReport,
481}
482
483#[derive(Debug, Serialize, Deserialize)]
484struct CachedCallsite {
485 source_path: String,
486 source_line: u32,
487 target_path: String,
488 target_line: Option<u32>,
489 callee: String,
490 kind: String,
491 analyzer: String,
492 confidence: f32,
493}
494
495fn load_cached_analysis(atlas: &mut Atlas) -> Result<bool> {
496 let Some(path) = analysis_cache_path(atlas) else {
497 return Ok(false);
498 };
499 let bytes = match fs::read(&path) {
500 Ok(bytes) => bytes,
501 Err(error) if error.kind() == std::io::ErrorKind::NotFound => return Ok(false),
502 Err(error) => {
503 atlas
504 .report
505 .warnings
506 .push(format!("could not read analysis cache: {error}"));
507 return Ok(false);
508 }
509 };
510 let cached: CachedAnalysis = match serde_json::from_slice(&bytes) {
511 Ok(cached) => cached,
512 Err(error) => {
513 atlas
514 .report
515 .warnings
516 .push(format!("ignored invalid analysis cache: {error}"));
517 return Ok(false);
518 }
519 };
520 if cached.version != CACHE_VERSION {
521 return Ok(false);
522 }
523 let mut calls = Vec::with_capacity(cached.calls.len());
524 for (id, call) in cached.calls.into_iter().enumerate() {
525 let (Some(source), Some(target)) = (
526 atlas.path_to_id.get(&call.source_path).copied(),
527 atlas.path_to_id.get(&call.target_path).copied(),
528 ) else {
529 return Ok(false);
530 };
531 calls.push(Callsite {
532 id: id as u64,
533 source,
534 source_line: call.source_line,
535 target,
536 target_line: call.target_line,
537 callee: call.callee,
538 kind: call.kind,
539 analyzer: call.analyzer,
540 confidence: call.confidence,
541 });
542 }
543 atlas.calls = calls;
544 atlas.report = cached.report;
545 atlas.report.cache_hit = true;
546 Ok(true)
547}
548
549fn save_cached_analysis(atlas: &mut Atlas) -> Result<()> {
550 let Some(path) = analysis_cache_path(atlas) else {
551 return Ok(());
552 };
553 let calls = atlas
554 .calls
555 .iter()
556 .map(|call| CachedCallsite {
557 source_path: atlas.nodes[call.source].path.clone(),
558 source_line: call.source_line,
559 target_path: atlas.nodes[call.target].path.clone(),
560 target_line: call.target_line,
561 callee: call.callee.clone(),
562 kind: call.kind.clone(),
563 analyzer: call.analyzer.clone(),
564 confidence: call.confidence,
565 })
566 .collect();
567 let mut report = atlas.report.clone();
568 report.cache_hit = false;
569 let payload = CachedAnalysis {
570 version: CACHE_VERSION,
571 calls,
572 report,
573 };
574 if let Some(parent) = path.parent() {
575 if let Err(error) = fs::create_dir_all(parent) {
576 atlas.report.warnings.push(format!(
577 "could not create analysis cache directory: {error}"
578 ));
579 return Ok(());
580 }
581 }
582 if let Err(error) = fs::write(&path, serde_json::to_vec(&payload)?) {
583 atlas
584 .report
585 .warnings
586 .push(format!("could not write analysis cache: {error}"));
587 }
588 Ok(())
589}
590
591fn analysis_cache_path(atlas: &Atlas) -> Option<PathBuf> {
592 if atlas.dirty {
593 return None;
594 }
595 let output = Command::new("git")
596 .args(["rev-parse", "--git-dir"])
597 .current_dir(&atlas.root_path)
598 .output()
599 .ok()?;
600 if !output.status.success() {
601 return None;
602 }
603 let git_dir = PathBuf::from(String::from_utf8_lossy(&output.stdout).trim());
604 let git_dir = if git_dir.is_absolute() {
605 git_dir
606 } else {
607 atlas.root_path.join(git_dir)
608 };
609 let mut hash = 0xcbf2_9ce4_8422_2325_u64;
610 for byte in atlas
611 .revision
612 .as_bytes()
613 .iter()
614 .copied()
615 .chain(atlas.files().flat_map(|node| {
616 node.path
617 .as_bytes()
618 .iter()
619 .copied()
620 .chain(std::iter::once(0))
621 }))
622 {
623 hash ^= u64::from(byte);
624 hash = hash.wrapping_mul(0x0000_0100_0000_01b3);
625 }
626 for tool in [
627 "elixir".to_owned(),
628 rust_analyzer_binary(),
629 scip_typescript_binary(atlas),
630 ] {
631 for byte in command_version(&tool).bytes().chain(std::iter::once(0)) {
632 hash ^= u64::from(byte);
633 hash = hash.wrapping_mul(0x0000_0100_0000_01b3);
634 }
635 }
636 Some(
637 git_dir
638 .join("code-atlas")
639 .join(format!("analysis-v{CACHE_VERSION}-{hash:016x}.json")),
640 )
641}
642
643fn command_version(binary: &str) -> String {
644 let output = Command::new(binary).arg("--version").output();
645 match output {
646 Ok(output) if output.status.success() => format!(
647 "{}{}",
648 String::from_utf8_lossy(&output.stdout),
649 String::from_utf8_lossy(&output.stderr)
650 ),
651 _ => "unavailable".to_owned(),
652 }
653}
654
655fn analyze_elixir_scripts(
656 atlas: &mut Atlas,
657 module_files: &HashMap<String, NodeId>,
658 next_id: &mut u64,
659) -> Result<()> {
660 if coverage_mut(atlas, "elixir").files_discovered == 0 {
661 return Ok(());
662 }
663 match analyze_elixir_scripts_ast(atlas, module_files, next_id) {
664 Ok(true) => {
665 let coverage = coverage_mut(atlas, "elixir");
666 coverage.provider = "mix-xref + script-ast".to_owned();
667 coverage.fidelity = "compiler-backed project files; compiler-parsed scripts".to_owned();
668 Ok(())
669 }
670 Ok(false) => {
671 let coverage = coverage_mut(atlas, "elixir");
672 coverage.provider = "mix-xref + script-static".to_owned();
673 analyze_elixir_scripts_fallback(atlas, module_files, next_id)
674 }
675 Err(error) => {
676 atlas.report.warnings.push(format!(
677 "Elixir AST script analysis failed; scripts used the regex fallback: {error:#}"
678 ));
679 let coverage = coverage_mut(atlas, "elixir");
680 coverage.provider = "mix-xref + script-static".to_owned();
681 analyze_elixir_scripts_fallback(atlas, module_files, next_id)
682 }
683 }
684}
685
686fn analyze_elixir_scripts_ast(
687 atlas: &mut Atlas,
688 module_files: &HashMap<String, NodeId>,
689 next_id: &mut u64,
690) -> Result<bool> {
691 if !command_exists("elixir") {
692 return Ok(false);
693 }
694 let scripts: Vec<_> = atlas
695 .nodes
696 .iter()
697 .filter(|node| node.is_file() && node.path.ends_with(".exs"))
698 .map(|node| (node.id, node.path.clone()))
699 .collect();
700 if scripts.is_empty() {
701 return Ok(true);
702 }
703 let source_ids: HashMap<_, _> = scripts
704 .iter()
705 .map(|(id, path)| (path.clone(), *id))
706 .collect();
707 let mut child = Command::new("elixir")
708 .args(["-e", ELIXIR_SCRIPT_EXPRESSION])
709 .current_dir(&atlas.root_path)
710 .stdin(Stdio::piped())
711 .stdout(Stdio::piped())
712 .stderr(Stdio::piped())
713 .spawn()?;
714 {
715 let mut stdin = child.stdin.take().expect("piped stdin");
716 for (_, path) in &scripts {
717 writeln!(stdin, "{path}")?;
718 }
719 }
720 let output = child.wait_with_output()?;
721 if !output.status.success() {
722 anyhow::bail!("{}", String::from_utf8_lossy(&output.stderr).trim());
723 }
724 let mut current = None;
725 let mut failed = false;
726 for line in String::from_utf8_lossy(&output.stdout).lines() {
727 if let Some(path) = line.strip_prefix(FILE_MARKER) {
728 current = source_ids.get(path).copied();
729 failed = false;
730 continue;
731 }
732 if let Some(error) = line.strip_prefix(ERROR_MARKER) {
733 atlas
734 .report
735 .warnings
736 .push(format!("could not parse Elixir script: {error}"));
737 failed = true;
738 continue;
739 }
740 if line == END_MARKER {
741 if current.is_some() && !failed {
742 atlas.report.elixir_script_files_scanned += 1;
743 coverage_mut(atlas, "elixir").files_analyzed += 1;
744 }
745 current = None;
746 continue;
747 }
748 let Some(payload) = line.strip_prefix(SCRIPT_CALL_MARKER) else {
749 continue;
750 };
751 let Some(source) = current else {
752 continue;
753 };
754 let mut fields = payload.splitn(4, '\t');
755 let Some(source_line) = fields.next().and_then(|value| value.parse::<u32>().ok()) else {
756 continue;
757 };
758 let Some(module) = fields.next() else {
759 continue;
760 };
761 let Some(function) = fields.next() else {
762 continue;
763 };
764 let Some(arity) = fields.next().and_then(|value| value.parse::<u32>().ok()) else {
765 continue;
766 };
767 let Some(target) = module_files.get(module).copied() else {
768 atlas.report.unresolved_calls += 1;
769 coverage_mut(atlas, "elixir").callsites_unresolved += 1;
770 continue;
771 };
772 atlas.calls.push(Callsite {
773 id: *next_id,
774 source,
775 source_line,
776 target,
777 target_line: definition_line(
778 &atlas.root_path.join(&atlas.nodes[target].path),
779 function,
780 ),
781 callee: format!("{module}.{function}/{arity}"),
782 kind: "syntax".to_owned(),
783 analyzer: "elixir-script-ast".to_owned(),
784 confidence: 0.75,
785 });
786 *next_id += 1;
787 atlas.report.resolved_calls += 1;
788 coverage_mut(atlas, "elixir").callsites_resolved += 1;
789 }
790 Ok(true)
791}
792
793fn analyze_elixir_scripts_fallback(
794 atlas: &mut Atlas,
795 module_files: &HashMap<String, NodeId>,
796 next_id: &mut u64,
797) -> Result<()> {
798 let alias_pattern =
799 Regex::new(r"^\s*alias\s+([A-Z][A-Za-z0-9_.]*)(?:\s*,\s*as:\s*([A-Z][A-Za-z0-9_]*))?")?;
800 let call_pattern = Regex::new(r"\b([A-Z][A-Za-z0-9_.]*)\.([a-z_][A-Za-z0-9_!?]*)\s*\(")?;
801 let scripts: Vec<_> = atlas
802 .nodes
803 .iter()
804 .filter(|node| node.is_file() && node.path.ends_with(".exs"))
805 .map(|node| (node.id, node.path.clone()))
806 .collect();
807 for (source, path) in scripts {
808 let contents = fs::read_to_string(atlas.root_path.join(&path)).unwrap_or_default();
809 let mut aliases = HashMap::new();
810 for line in contents.lines() {
811 if let Some(captures) = alias_pattern.captures(line) {
812 let full = captures[1].to_owned();
813 let short = captures
814 .get(2)
815 .map(|value| value.as_str())
816 .or_else(|| full.rsplit('.').next())
817 .unwrap_or(&full)
818 .to_owned();
819 aliases.insert(short, full);
820 }
821 }
822 atlas.report.elixir_script_files_scanned += 1;
823 coverage_mut(atlas, "elixir").files_analyzed += 1;
824 for (index, line) in contents.lines().enumerate() {
825 let code = line.split('#').next().unwrap_or("");
826 for captures in call_pattern.captures_iter(code) {
827 let written_module = &captures[1];
828 let module = module_files
829 .contains_key(written_module)
830 .then(|| written_module.to_owned())
831 .or_else(|| aliases.get(written_module).cloned());
832 let Some(module) = module else {
833 continue;
834 };
835 let Some(target) = module_files.get(&module).copied() else {
836 continue;
837 };
838 let function = captures[2].to_owned();
839 atlas.calls.push(Callsite {
840 id: *next_id,
841 source,
842 source_line: index as u32 + 1,
843 target,
844 target_line: definition_line(
845 &atlas.root_path.join(&atlas.nodes[target].path),
846 &function,
847 ),
848 callee: format!("{module}.{function}/?"),
849 kind: "syntax".to_owned(),
850 analyzer: "elixir-script-static".to_owned(),
851 confidence: 0.55,
852 });
853 *next_id += 1;
854 atlas.report.resolved_calls += 1;
855 coverage_mut(atlas, "elixir").callsites_resolved += 1;
856 }
857 }
858 }
859 Ok(())
860}
861
862fn analyze_typescript_calls(atlas: &mut Atlas, next_id: &mut u64) -> Result<bool> {
863 let file_count = atlas
864 .nodes
865 .iter()
866 .filter(|node| matches!(node.language.as_str(), "javascript" | "typescript"))
867 .count();
868 if file_count == 0 {
869 return Ok(true);
870 }
871 let cacheable = match generate_typescript_scip(atlas) {
872 Ok(Some(indexes)) => {
873 let stats = ingest_scip_calls(atlas, &indexes, &["javascript", "typescript"], next_id)?;
874 for language in ["javascript", "typescript"] {
875 let coverage = coverage_mut(atlas, language);
876 coverage.provider = "scip-typescript".to_owned();
877 coverage.fidelity = "compiler-backed semantic symbols".to_owned();
878 coverage.files_analyzed =
879 stats.files_by_language.get(language).copied().unwrap_or(0);
880 coverage.callsites_resolved = stats
881 .resolved_by_language
882 .get(language)
883 .copied()
884 .unwrap_or(0);
885 coverage.callsites_unresolved = stats
886 .unresolved_by_language
887 .get(language)
888 .copied()
889 .unwrap_or(0);
890 }
891 atlas.report.typescript_files_scanned =
892 stats.files_by_language.values().copied().sum::<usize>();
893 atlas.report.typescript_calls =
894 stats.resolved_by_language.values().copied().sum::<usize>();
895 atlas.report.resolved_calls += atlas.report.typescript_calls;
896 atlas.report.unresolved_calls += stats
897 .unresolved_by_language
898 .values()
899 .copied()
900 .sum::<usize>();
901 return Ok(true);
902 }
903 Ok(None) => {
904 atlas.report.warnings.push(
905 "scip-typescript was not found; JavaScript/TypeScript used the relative-import fallback"
906 .to_owned(),
907 );
908 true
909 }
910 Err(error) => {
911 atlas.report.warnings.push(format!(
912 "scip-typescript indexing failed; JavaScript/TypeScript used the relative-import fallback: {error:#}"
913 ));
914 false
915 }
916 };
917 for language in ["javascript", "typescript"] {
918 let coverage = coverage_mut(atlas, language);
919 coverage.provider = "relative-import-static".to_owned();
920 coverage.fidelity = "conservative syntax fallback".to_owned();
921 }
922 analyze_typescript_calls_fallback(atlas, next_id)?;
923 Ok(cacheable)
924}
925
926fn analyze_typescript_calls_fallback(atlas: &mut Atlas, next_id: &mut u64) -> Result<()> {
927 let import_pattern = Regex::new(r#"^\s*import\s+(.+?)\s+from\s+["']([^"']+)["']\s*;?\s*$"#)?;
928 let call_pattern =
929 Regex::new(r"\b([A-Za-z_$][A-Za-z0-9_$]*)(?:\.([A-Za-z_$][A-Za-z0-9_$]*))?\s*\(")?;
930 let files: Vec<_> = atlas
931 .nodes
932 .iter()
933 .filter(|node| {
934 node.is_file()
935 && matches!(
936 Path::new(&node.path)
937 .extension()
938 .and_then(|value| value.to_str()),
939 Some("ts" | "tsx" | "mts" | "cts" | "js" | "jsx" | "mjs" | "cjs")
940 )
941 })
942 .map(|node| (node.id, node.path.clone()))
943 .collect();
944 for (source, path) in files {
945 atlas.report.typescript_files_scanned += 1;
946 let language = atlas.nodes[source].language.clone();
947 coverage_mut(atlas, &language).files_analyzed += 1;
948 let contents = fs::read_to_string(atlas.root_path.join(&path)).unwrap_or_default();
949 let mut bindings: HashMap<String, (NodeId, bool)> = HashMap::new();
950 for line in contents.lines() {
951 let Some(captures) = import_pattern.captures(line) else {
952 continue;
953 };
954 let Some(target) = resolve_relative_module(atlas, &path, &captures[2]) else {
955 continue;
956 };
957 let clause = captures[1].trim();
958 if let Some(namespace) = clause.strip_prefix("* as ") {
959 bindings.insert(namespace.trim().to_owned(), (target, true));
960 continue;
961 }
962 let mut remainder = clause;
963 if !remainder.starts_with('{') {
964 let default = remainder.split([',', ' ']).next().unwrap_or("").trim();
965 if !default.is_empty() {
966 bindings.insert(default.to_owned(), (target, false));
967 }
968 remainder = remainder
969 .split_once(',')
970 .map(|(_, rest)| rest)
971 .unwrap_or("");
972 }
973 if let Some(named) = remainder
974 .trim()
975 .strip_prefix('{')
976 .and_then(|value| value.strip_suffix('}'))
977 {
978 for item in named.split(',') {
979 let mut parts = item.split_whitespace();
980 let original = parts.next().unwrap_or("");
981 let alias = match (parts.next(), parts.next()) {
982 (Some("as"), Some(alias)) => alias,
983 _ => original,
984 };
985 if !alias.is_empty() {
986 bindings.insert(alias.to_owned(), (target, false));
987 }
988 }
989 }
990 }
991 for (index, line) in contents.lines().enumerate() {
992 let trimmed = line.trim_start();
993 if trimmed.starts_with("import ") || trimmed.starts_with("//") {
994 continue;
995 }
996 let code = line.split("//").next().unwrap_or("");
997 for captures in call_pattern.captures_iter(code) {
998 let binding = &captures[1];
999 let Some((target, namespace)) = bindings.get(binding).copied() else {
1000 continue;
1001 };
1002 if namespace && captures.get(2).is_none() {
1003 continue;
1004 }
1005 let function = captures
1006 .get(2)
1007 .map(|value| value.as_str())
1008 .unwrap_or(binding);
1009 atlas.calls.push(Callsite {
1010 id: *next_id,
1011 source,
1012 source_line: index as u32 + 1,
1013 target,
1014 target_line: None,
1015 callee: format!("{}.{}", atlas.nodes[target].path, function),
1016 kind: "runtime".to_owned(),
1017 analyzer: "typescript-import-static".to_owned(),
1018 confidence: 0.65,
1019 });
1020 *next_id += 1;
1021 atlas.report.resolved_calls += 1;
1022 atlas.report.typescript_calls += 1;
1023 coverage_mut(atlas, &language).callsites_resolved += 1;
1024 }
1025 }
1026 }
1027 Ok(())
1028}
1029
1030fn resolve_relative_module(atlas: &Atlas, source_path: &str, import: &str) -> Option<NodeId> {
1031 if !import.starts_with('.') {
1032 return None;
1033 }
1034 let source_parent = Path::new(source_path)
1035 .parent()
1036 .unwrap_or_else(|| Path::new(""));
1037 let joined = source_parent.join(import);
1038 let mut components = Vec::new();
1039 for component in joined.components() {
1040 use std::path::Component;
1041 match component {
1042 Component::Normal(value) => components.push(value.to_string_lossy().to_string()),
1043 Component::ParentDir => {
1044 components.pop()?;
1045 }
1046 Component::CurDir => {}
1047 _ => return None,
1048 }
1049 }
1050 let base = components.join("/");
1051 let mut candidates = vec![base.clone()];
1052 for extension in ["ts", "tsx", "mts", "cts", "js", "jsx", "mjs", "cjs"] {
1053 candidates.push(format!("{base}.{extension}"));
1054 candidates.push(format!("{base}/index.{extension}"));
1055 }
1056 candidates
1057 .into_iter()
1058 .find_map(|candidate| atlas.path_to_id.get(&candidate).copied())
1059}
1060
1061struct RootedScipIndex {
1062 root: String,
1063 index: ScipIndex,
1064}
1065
1066#[derive(Default)]
1067struct ScipIngestStats {
1068 files_by_language: HashMap<String, usize>,
1069 resolved_by_language: HashMap<String, usize>,
1070 unresolved_by_language: HashMap<String, usize>,
1071}
1072
1073fn generate_rust_scip(atlas: &Atlas) -> Result<Option<Vec<RootedScipIndex>>> {
1074 let binary = rust_analyzer_binary();
1075 if !command_exists(&binary) {
1076 return Ok(None);
1077 }
1078 let roots = outermost_project_roots(atlas, &["Cargo.toml"]);
1079 if roots.is_empty() {
1080 return Ok(None);
1081 }
1082 let temporary = tempfile::tempdir()?;
1083 let mut indexes = Vec::new();
1084 for (number, root) in roots.iter().enumerate() {
1085 let output_path = temporary.path().join(format!("rust-{number}.scip"));
1086 let project_path = if root.is_empty() {
1087 atlas.root_path.clone()
1088 } else {
1089 atlas.root_path.join(root)
1090 };
1091 let output = Command::new(&binary)
1092 .arg("scip")
1093 .arg(&project_path)
1094 .arg("--output")
1095 .arg(&output_path)
1096 .arg("--exclude-vendored-libraries")
1097 .current_dir(&atlas.root_path)
1098 .output()?;
1099 if !output.status.success() {
1100 anyhow::bail!("{}", String::from_utf8_lossy(&output.stderr).trim());
1101 }
1102 indexes.push(RootedScipIndex {
1103 root: root.clone(),
1104 index: decode_index(&fs::read(&output_path)?)?,
1105 });
1106 }
1107 Ok(Some(indexes))
1108}
1109
1110fn generate_typescript_scip(atlas: &Atlas) -> Result<Option<Vec<RootedScipIndex>>> {
1111 let binary = scip_typescript_binary(atlas);
1112 if !command_exists(&binary) {
1113 return Ok(None);
1114 }
1115 let mut roots = outermost_project_roots(atlas, &["tsconfig.json"]);
1116 let infer_tsconfig = roots.is_empty();
1117 if roots.is_empty() {
1118 roots = outermost_project_roots(atlas, &["package.json"]);
1119 }
1120 if roots.is_empty() {
1121 return Ok(None);
1122 }
1123 let temporary = tempfile::tempdir()?;
1124 let mut indexes = Vec::new();
1125 for (number, root) in roots.iter().enumerate() {
1126 let output_path = temporary.path().join(format!("typescript-{number}.scip"));
1127 let project_path = if root.is_empty() {
1128 atlas.root_path.clone()
1129 } else {
1130 atlas.root_path.join(root)
1131 };
1132 let mut command = Command::new(&binary);
1133 command
1134 .arg("index")
1135 .arg("--output")
1136 .arg(&output_path)
1137 .current_dir(&project_path);
1138 if infer_tsconfig {
1139 command.arg("--infer-tsconfig");
1140 }
1141 let output = command.output()?;
1142 if !output.status.success() {
1143 anyhow::bail!("{}", String::from_utf8_lossy(&output.stderr).trim());
1144 }
1145 indexes.push(RootedScipIndex {
1146 root: root.clone(),
1147 index: decode_index(&fs::read(&output_path)?)?,
1148 });
1149 }
1150 Ok(Some(indexes))
1151}
1152
1153fn rust_analyzer_binary() -> String {
1154 env::var("CODE_ATLAS_RUST_ANALYZER").unwrap_or_else(|_| "rust-analyzer".to_owned())
1155}
1156
1157fn scip_typescript_binary(atlas: &Atlas) -> String {
1158 env::var("CODE_ATLAS_SCIP_TYPESCRIPT")
1159 .ok()
1160 .or_else(|| {
1161 let local = atlas.root_path.join("node_modules/.bin/scip-typescript");
1162 local.is_file().then(|| local.display().to_string())
1163 })
1164 .unwrap_or_else(|| "scip-typescript".to_owned())
1165}
1166
1167fn command_exists(binary: &str) -> bool {
1168 Command::new(binary)
1169 .arg("--version")
1170 .stdout(Stdio::null())
1171 .stderr(Stdio::null())
1172 .status()
1173 .is_ok_and(|status| status.success())
1174}
1175
1176fn outermost_project_roots(atlas: &Atlas, manifests: &[&str]) -> Vec<String> {
1177 let mut roots: Vec<_> = atlas
1178 .nodes
1179 .iter()
1180 .filter(|node| node.is_file())
1181 .filter(|node| manifests.iter().any(|manifest| node.name == *manifest))
1182 .map(|node| {
1183 Path::new(&node.path)
1184 .parent()
1185 .map(|parent| parent.to_string_lossy().trim_end_matches('/').to_owned())
1186 .unwrap_or_default()
1187 })
1188 .collect();
1189 roots.sort_by_key(|root| (root.matches('/').count(), root.clone()));
1190 let mut outermost = Vec::<String>::new();
1191 for root in roots {
1192 if outermost.iter().any(|parent| {
1193 parent.is_empty()
1194 || root == *parent
1195 || root
1196 .strip_prefix(parent)
1197 .is_some_and(|suffix| suffix.starts_with('/'))
1198 }) {
1199 continue;
1200 }
1201 outermost.push(root);
1202 }
1203 outermost
1204}
1205
1206fn ingest_scip_calls(
1207 atlas: &mut Atlas,
1208 indexes: &[RootedScipIndex],
1209 languages: &[&str],
1210 next_id: &mut u64,
1211) -> Result<ScipIngestStats> {
1212 let mut documents = Vec::new();
1213 let mut analyzed_files: HashMap<String, HashSet<NodeId>> = HashMap::new();
1214 for rooted in indexes {
1215 for document in &rooted.index.documents {
1216 let Some(file) = scip_document_file(atlas, &rooted.root, document) else {
1217 continue;
1218 };
1219 let language = atlas.nodes[file].language.clone();
1220 if !languages.contains(&language.as_str()) {
1221 continue;
1222 }
1223 analyzed_files.entry(language).or_default().insert(file);
1224 documents.push((file, document));
1225 }
1226 }
1227
1228 let mut definitions: HashMap<&str, (NodeId, u32)> = HashMap::new();
1229 for (file, document) in &documents {
1230 for occurrence in &document.occurrences {
1231 if occurrence.symbol.is_empty() || occurrence.symbol_roles & 1 == 0 {
1232 continue;
1233 }
1234 if let Some(range) = occurrence.range {
1235 definitions
1236 .entry(&occurrence.symbol)
1237 .or_insert((*file, range.start_line + 1));
1238 }
1239 }
1240 }
1241
1242 let mut stats = ScipIngestStats::default();
1243 for (language, files) in analyzed_files {
1244 stats.files_by_language.insert(language, files.len());
1245 }
1246 let mut seen = HashSet::new();
1247 let mut source_cache: HashMap<NodeId, String> = HashMap::new();
1248 for (source, document) in documents {
1249 let language = atlas.nodes[source].language.clone();
1250 let contents = source_cache.entry(source).or_insert_with(|| {
1251 fs::read_to_string(atlas.root_path.join(&atlas.nodes[source].path)).unwrap_or_default()
1252 });
1253 for occurrence in &document.occurrences {
1254 if occurrence.symbol.is_empty() || occurrence.symbol_roles & (1 | 2 | 16 | 32) != 0 {
1255 continue;
1256 }
1257 let Some(range) = occurrence.range else {
1258 continue;
1259 };
1260 let Some(callee) = occurrence_call_text(contents, range, document.position_encoding)
1261 else {
1262 continue;
1263 };
1264 let Some((target, target_line)) = definitions.get(occurrence.symbol.as_str()).copied()
1265 else {
1266 *stats
1267 .unresolved_by_language
1268 .entry(language.clone())
1269 .or_default() += 1;
1270 continue;
1271 };
1272 let identity = (
1273 source,
1274 range.start_line,
1275 range.start_character,
1276 target,
1277 target_line,
1278 occurrence.symbol.clone(),
1279 );
1280 if !seen.insert(identity) {
1281 continue;
1282 }
1283 atlas.calls.push(Callsite {
1284 id: *next_id,
1285 source,
1286 source_line: range.start_line + 1,
1287 target,
1288 target_line: Some(target_line),
1289 callee,
1290 kind: "semantic".to_owned(),
1291 analyzer: if language == "rust" {
1292 "rust-analyzer-scip".to_owned()
1293 } else {
1294 "scip-typescript".to_owned()
1295 },
1296 confidence: 0.98,
1297 });
1298 *next_id += 1;
1299 *stats
1300 .resolved_by_language
1301 .entry(language.clone())
1302 .or_default() += 1;
1303 }
1304 }
1305 Ok(stats)
1306}
1307
1308fn scip_document_file(atlas: &Atlas, root: &str, document: &ScipDocument) -> Option<NodeId> {
1309 let relative = document
1310 .relative_path
1311 .replace('\\', "/")
1312 .trim_start_matches("./")
1313 .to_owned();
1314 let rooted = if root.is_empty() {
1315 relative.clone()
1316 } else {
1317 format!("{root}/{relative}")
1318 };
1319 atlas
1320 .path_to_id
1321 .get(&rooted)
1322 .or_else(|| atlas.path_to_id.get(&relative))
1323 .copied()
1324}
1325
1326fn occurrence_call_text(contents: &str, range: ScipRange, encoding: i32) -> Option<String> {
1327 if range.start_line != range.end_line {
1328 return None;
1329 }
1330 let line = contents.lines().nth(range.start_line as usize)?;
1331 let start = encoded_column_to_byte(line, range.start_character, encoding)?;
1332 let end = encoded_column_to_byte(line, range.end_character, encoding)?;
1333 let callee = line.get(start..end)?.trim();
1334 if callee.is_empty() || !suffix_is_call(line.get(end..)?) {
1335 return None;
1336 }
1337 Some(callee.to_owned())
1338}
1339
1340fn encoded_column_to_byte(line: &str, column: u32, encoding: i32) -> Option<usize> {
1341 if encoding != 2 {
1342 let byte = column as usize;
1343 return (byte <= line.len() && line.is_char_boundary(byte)).then_some(byte);
1344 }
1345 let mut utf16 = 0_u32;
1346 for (byte, character) in line.char_indices() {
1347 if utf16 == column {
1348 return Some(byte);
1349 }
1350 utf16 += character.len_utf16() as u32;
1351 if utf16 > column {
1352 return None;
1353 }
1354 }
1355 (utf16 == column).then_some(line.len())
1356}
1357
1358fn suffix_is_call(suffix: &str) -> bool {
1359 let mut rest = suffix.trim_start();
1360 if let Some(optional) = rest.strip_prefix("?.") {
1361 rest = optional.trim_start();
1362 }
1363 if let Some(macro_call) = rest.strip_prefix('!') {
1364 rest = macro_call.trim_start();
1365 }
1366 if let Some(generic) = rest
1367 .strip_prefix("::")
1368 .and_then(|value| value.strip_prefix('<'))
1369 {
1370 let Some(after_generics) = skip_balanced_angles(generic) else {
1371 return false;
1372 };
1373 rest = after_generics;
1374 } else if let Some(generic) = rest.strip_prefix('<') {
1375 let Some(after_generics) = skip_balanced_angles(generic) else {
1376 return false;
1377 };
1378 rest = after_generics;
1379 }
1380 rest.trim_start().starts_with('(')
1381}
1382
1383fn skip_balanced_angles(value: &str) -> Option<&str> {
1384 let mut depth = 1_usize;
1385 for (offset, character) in value.char_indices() {
1386 match character {
1387 '<' => depth += 1,
1388 '>' => {
1389 depth -= 1;
1390 if depth == 0 {
1391 return value.get(offset + character.len_utf8()..);
1392 }
1393 }
1394 _ => {}
1395 }
1396 }
1397 None
1398}
1399
1400fn analyze_gleam_calls(atlas: &mut Atlas, next_id: &mut u64) -> Result<()> {
1401 let import_pattern = Regex::new(
1402 r"^\s*import\s+([a-z][A-Za-z0-9_/]*)(?:\s+as\s+([a-z][A-Za-z0-9_]*))?(?:\.\{([^}]*)\})?",
1403 )?;
1404 let qualified_call = Regex::new(r"\b([a-z][A-Za-z0-9_]*)\.([a-z][A-Za-z0-9_]*)\s*\(")?;
1405 let unqualified_call = Regex::new(r"\b([a-z][A-Za-z0-9_]*)\s*\(")?;
1406 let gleam_files: Vec<_> = atlas
1407 .nodes
1408 .iter()
1409 .filter(|node| node.is_file() && node.path.ends_with(".gleam"))
1410 .map(|node| (node.id, node.path.clone()))
1411 .collect();
1412 let modules: HashMap<_, _> = gleam_files
1413 .iter()
1414 .filter_map(|(id, path)| gleam_module_name(path).map(|name| (name, *id)))
1415 .collect();
1416 for (source, path) in gleam_files {
1417 atlas.report.gleam_files_scanned += 1;
1418 coverage_mut(atlas, "gleam").files_analyzed += 1;
1419 let contents = fs::read_to_string(atlas.root_path.join(&path)).unwrap_or_default();
1420 let mut module_aliases = HashMap::new();
1421 let mut functions = HashMap::new();
1422 for line in contents.lines() {
1423 let Some(captures) = import_pattern.captures(line) else {
1424 continue;
1425 };
1426 let module = captures[1].to_owned();
1427 let Some(target) = modules.get(&module).copied() else {
1428 continue;
1429 };
1430 let alias = captures
1431 .get(2)
1432 .map(|value| value.as_str())
1433 .or_else(|| module.rsplit('/').next())
1434 .unwrap_or(&module);
1435 module_aliases.insert(alias.to_owned(), target);
1436 if let Some(selected) = captures.get(3) {
1437 for item in selected.as_str().split(',') {
1438 let mut parts = item.split_whitespace();
1439 let original = parts.next().unwrap_or("");
1440 let alias = match (parts.next(), parts.next()) {
1441 (Some("as"), Some(alias)) => alias,
1442 _ => original,
1443 };
1444 if !alias.is_empty() {
1445 functions.insert(alias.to_owned(), target);
1446 }
1447 }
1448 }
1449 }
1450 for (index, line) in contents.lines().enumerate() {
1451 let code = line.split("//").next().unwrap_or("");
1452 for captures in qualified_call.captures_iter(code) {
1453 let Some(target) = module_aliases.get(&captures[1]).copied() else {
1454 continue;
1455 };
1456 push_gleam_call(atlas, next_id, source, index, target, &captures[2]);
1457 }
1458 for captures in unqualified_call.captures_iter(code) {
1459 let Some(target) = functions.get(&captures[1]).copied() else {
1460 continue;
1461 };
1462 push_gleam_call(atlas, next_id, source, index, target, &captures[1]);
1463 }
1464 }
1465 }
1466 Ok(())
1467}
1468
1469fn gleam_module_name(path: &str) -> Option<String> {
1470 let without_extension = path.strip_suffix(".gleam")?;
1471 without_extension
1472 .split_once("/src/")
1473 .map(|(_, module)| module.to_owned())
1474 .or_else(|| without_extension.strip_prefix("src/").map(str::to_owned))
1475}
1476
1477fn push_gleam_call(
1478 atlas: &mut Atlas,
1479 next_id: &mut u64,
1480 source: NodeId,
1481 line_index: usize,
1482 target: NodeId,
1483 function: &str,
1484) {
1485 atlas.calls.push(Callsite {
1486 id: *next_id,
1487 source,
1488 source_line: line_index as u32 + 1,
1489 target,
1490 target_line: None,
1491 callee: format!("{}.{}", atlas.nodes[target].path, function),
1492 kind: "runtime".to_owned(),
1493 analyzer: "gleam-import-static".to_owned(),
1494 confidence: 0.70,
1495 });
1496 *next_id += 1;
1497 atlas.report.resolved_calls += 1;
1498 atlas.report.gleam_calls += 1;
1499 coverage_mut(atlas, "gleam").callsites_resolved += 1;
1500}
1501
1502#[derive(Debug, Clone, Copy)]
1503struct RustDefinition {
1504 file: NodeId,
1505 line: u32,
1506}
1507
1508#[derive(Default)]
1509struct RustDefinitionVisitor {
1510 definitions: Vec<(String, u32)>,
1511}
1512
1513impl<'ast> Visit<'ast> for RustDefinitionVisitor {
1514 fn visit_item_fn(&mut self, function: &'ast syn::ItemFn) {
1515 self.definitions.push((
1516 function.sig.ident.to_string(),
1517 span_line(function.sig.ident.span()),
1518 ));
1519 syn::visit::visit_item_fn(self, function);
1520 }
1521
1522 fn visit_impl_item_fn(&mut self, function: &'ast syn::ImplItemFn) {
1523 self.definitions.push((
1524 function.sig.ident.to_string(),
1525 span_line(function.sig.ident.span()),
1526 ));
1527 syn::visit::visit_impl_item_fn(self, function);
1528 }
1529}
1530
1531#[derive(Debug)]
1532struct RustCallCandidate {
1533 name: String,
1534 line: u32,
1535 qualified: bool,
1536}
1537
1538#[derive(Default)]
1539struct RustCallVisitor {
1540 calls: Vec<RustCallCandidate>,
1541}
1542
1543impl<'ast> Visit<'ast> for RustCallVisitor {
1544 fn visit_expr_call(&mut self, call: &'ast syn::ExprCall) {
1545 if let Expr::Path(path) = call.func.as_ref() {
1546 if let Some(segment) = path.path.segments.last() {
1547 self.calls.push(RustCallCandidate {
1548 name: segment.ident.to_string(),
1549 line: span_line(call.span()),
1550 qualified: path.path.segments.len() > 1,
1551 });
1552 }
1553 }
1554 syn::visit::visit_expr_call(self, call);
1555 }
1556
1557 fn visit_expr_method_call(&mut self, call: &'ast syn::ExprMethodCall) {
1558 self.calls.push(RustCallCandidate {
1559 name: call.method.to_string(),
1560 line: span_line(call.span()),
1561 qualified: false,
1562 });
1563 syn::visit::visit_expr_method_call(self, call);
1564 }
1565}
1566
1567fn analyze_rust_calls(atlas: &mut Atlas, next_id: &mut u64) -> Result<bool> {
1568 let file_count = atlas
1569 .nodes
1570 .iter()
1571 .filter(|node| node.is_file() && node.language == "rust")
1572 .count();
1573 if file_count == 0 {
1574 return Ok(true);
1575 }
1576 let cacheable = match generate_rust_scip(atlas) {
1577 Ok(Some(indexes)) => {
1578 let stats = ingest_scip_calls(atlas, &indexes, &["rust"], next_id)?;
1579 let files_analyzed = stats.files_by_language.get("rust").copied().unwrap_or(0);
1580 let callsites_resolved = stats.resolved_by_language.get("rust").copied().unwrap_or(0);
1581 let callsites_unresolved = stats
1582 .unresolved_by_language
1583 .get("rust")
1584 .copied()
1585 .unwrap_or(0);
1586 {
1587 let coverage = coverage_mut(atlas, "rust");
1588 coverage.provider = "rust-analyzer-scip".to_owned();
1589 coverage.fidelity = "compiler-backed semantic symbols".to_owned();
1590 coverage.files_analyzed = files_analyzed;
1591 coverage.callsites_resolved = callsites_resolved;
1592 coverage.callsites_unresolved = callsites_unresolved;
1593 }
1594 atlas.report.rust_files_scanned = files_analyzed;
1595 atlas.report.rust_calls = callsites_resolved;
1596 atlas.report.resolved_calls += callsites_resolved;
1597 atlas.report.unresolved_calls += callsites_unresolved;
1598 return Ok(true);
1599 }
1600 Ok(None) => {
1601 atlas.report.warnings.push(
1602 "rust-analyzer was not found; Rust used the globally-unique syntax fallback"
1603 .to_owned(),
1604 );
1605 true
1606 }
1607 Err(error) => {
1608 atlas.report.warnings.push(format!(
1609 "rust-analyzer SCIP indexing failed; Rust used the globally-unique syntax fallback: {error:#}"
1610 ));
1611 false
1612 }
1613 };
1614 let coverage = coverage_mut(atlas, "rust");
1615 coverage.provider = "rust-syntax-unique".to_owned();
1616 coverage.fidelity = "conservative syntax fallback".to_owned();
1617 analyze_rust_calls_fallback(atlas, next_id)?;
1618 Ok(cacheable)
1619}
1620
1621fn analyze_rust_calls_fallback(atlas: &mut Atlas, next_id: &mut u64) -> Result<()> {
1622 let rust_files: Vec<(NodeId, String)> = atlas
1623 .nodes
1624 .iter()
1625 .filter(|node| node.is_file() && node.path.ends_with(".rs"))
1626 .map(|node| (node.id, node.path.clone()))
1627 .collect();
1628 if rust_files.is_empty() {
1629 return Ok(());
1630 }
1631
1632 let mut syntax_trees = Vec::new();
1633 let mut unique_definitions: HashMap<String, Option<RustDefinition>> = HashMap::new();
1634 for (file, path) in &rust_files {
1635 let source = fs::read_to_string(atlas.root_path.join(path)).unwrap_or_default();
1636 let syntax = match syn::parse_file(&source) {
1637 Ok(syntax) => syntax,
1638 Err(error) => {
1639 atlas
1640 .report
1641 .warnings
1642 .push(format!("could not parse Rust file {path}: {error}"));
1643 continue;
1644 }
1645 };
1646 atlas.report.rust_files_scanned += 1;
1647 coverage_mut(atlas, "rust").files_analyzed += 1;
1648 let mut visitor = RustDefinitionVisitor::default();
1649 visitor.visit_file(&syntax);
1650 for (name, line) in visitor.definitions {
1651 unique_definitions
1652 .entry(name)
1653 .and_modify(|definition| *definition = None)
1654 .or_insert(Some(RustDefinition { file: *file, line }));
1655 }
1656 syntax_trees.push((*file, syntax));
1657 }
1658
1659 for (source, syntax) in syntax_trees {
1660 let mut visitor = RustCallVisitor::default();
1661 visitor.visit_file(&syntax);
1662 for candidate in visitor.calls {
1663 if !candidate.qualified {
1664 atlas.report.unresolved_calls += 1;
1665 coverage_mut(atlas, "rust").callsites_unresolved += 1;
1666 continue;
1667 }
1668 let Some(Some(target)) = unique_definitions.get(&candidate.name) else {
1669 atlas.report.unresolved_calls += 1;
1670 coverage_mut(atlas, "rust").callsites_unresolved += 1;
1671 continue;
1672 };
1673 atlas.calls.push(Callsite {
1674 id: *next_id,
1675 source,
1676 source_line: candidate.line,
1677 target: target.file,
1678 target_line: Some(target.line),
1679 callee: candidate.name,
1680 kind: "syntax".to_owned(),
1681 analyzer: "rust-syntax-unique".to_owned(),
1682 confidence: 0.70,
1683 });
1684 *next_id += 1;
1685 atlas.report.resolved_calls += 1;
1686 atlas.report.rust_calls += 1;
1687 coverage_mut(atlas, "rust").callsites_resolved += 1;
1688 }
1689 }
1690 Ok(())
1691}
1692
1693fn span_line(span: proc_macro2::Span) -> u32 {
1694 span.start().line.max(1) as u32
1695}
1696
1697pub fn parse_xref_line(line: &str) -> Option<XrefCall> {
1698 let pattern = Regex::new(
1699 r"^.+?:(\d+): (?:import )?call ([A-Z][A-Za-z0-9_.]*)\.([a-zA-Z0-9_!?]+)/([0-9]+) \(([^)]+)\)$",
1700 )
1701 .expect("valid xref regex");
1702 let captures = pattern.captures(line)?;
1703 Some(XrefCall {
1704 source_line: captures.get(1)?.as_str().parse().ok()?,
1705 module: captures.get(2)?.as_str().to_owned(),
1706 function: captures.get(3)?.as_str().to_owned(),
1707 arity: captures.get(4)?.as_str().parse().ok()?,
1708 kind: captures.get(5)?.as_str().to_owned(),
1709 })
1710}
1711
1712fn elixir_module_files(atlas: &Atlas) -> Result<HashMap<String, NodeId>> {
1713 let pattern = Regex::new(r"(?m)^\s*defmodule\s+([A-Z][A-Za-z0-9_.]*)\s+do\b")?;
1714 let mut modules = HashMap::new();
1715 for node in atlas.nodes.iter().filter(|node| {
1716 node.is_file() && (node.path.ends_with(".ex") || node.path.ends_with(".exs"))
1717 }) {
1718 let contents = fs::read_to_string(atlas.root_path.join(&node.path)).unwrap_or_default();
1719 for captures in pattern.captures_iter(&contents) {
1720 modules.insert(captures[1].to_owned(), node.id);
1721 }
1722 }
1723 Ok(modules)
1724}
1725
1726fn rustler_targets(
1727 atlas: &Atlas,
1728 modules: &HashMap<String, NodeId>,
1729) -> Result<HashMap<(String, String), RustlerTarget>> {
1730 let init_pattern = Regex::new(r#"rustler::init!\(\s*"Elixir\.([^"]+)""#)?;
1731 let nif_pattern =
1732 Regex::new(r#"(?s)#\[rustler::nif(?:\(([^]]*)\))?\]\s*(?:pub\s+)?fn\s+([a-zA-Z0-9_]+)"#)?;
1733 let name_pattern = Regex::new(r#"name\s*=\s*"([^"]+)""#)?;
1734
1735 let cargo_roots = project_roots_for_manifest(atlas, "Cargo.toml");
1736 let mut nif_modules = HashMap::new();
1737 for node in atlas
1738 .nodes
1739 .iter()
1740 .filter(|node| node.is_file() && node.path.ends_with(".rs"))
1741 {
1742 let contents = fs::read_to_string(atlas.root_path.join(&node.path)).unwrap_or_default();
1743 if let Some(captures) = init_pattern.captures(&contents) {
1744 let root = nearest_project_root(&node.path, &cargo_roots)
1745 .unwrap_or("")
1746 .to_owned();
1747 nif_modules.insert(root, captures[1].to_owned());
1748 }
1749 }
1750
1751 if nif_modules.is_empty() {
1752 return Ok(HashMap::new());
1753 }
1754
1755 let mut targets = HashMap::new();
1756 for node in atlas
1757 .nodes
1758 .iter()
1759 .filter(|node| node.is_file() && node.path.ends_with(".rs"))
1760 {
1761 let Some(nif_module) =
1762 nearest_project_root(&node.path, &cargo_roots).and_then(|root| nif_modules.get(root))
1763 else {
1764 continue;
1765 };
1766 if !modules.contains_key(nif_module) {
1767 continue;
1768 }
1769 let contents = fs::read_to_string(atlas.root_path.join(&node.path)).unwrap_or_default();
1770 for captures in nif_pattern.captures_iter(&contents) {
1771 let rust_name = captures.get(2).expect("function capture").as_str();
1772 let args = captures.get(1).map(|value| value.as_str()).unwrap_or("");
1773 let exported_name = name_pattern
1774 .captures(args)
1775 .and_then(|captures| captures.get(1))
1776 .map(|value| value.as_str())
1777 .unwrap_or(rust_name);
1778 let line = contents[..captures.get(0).expect("whole capture").start()]
1779 .lines()
1780 .count() as u32
1781 + 1;
1782 targets.insert(
1783 (nif_module.clone(), exported_name.to_owned()),
1784 RustlerTarget {
1785 file: node.id,
1786 line: Some(line),
1787 },
1788 );
1789 }
1790 }
1791 Ok(targets)
1792}
1793
1794fn project_roots_for_manifest(atlas: &Atlas, manifest: &str) -> Vec<String> {
1795 let mut roots: Vec<_> = atlas
1796 .nodes
1797 .iter()
1798 .filter(|node| node.is_file() && node.name == manifest)
1799 .map(|node| {
1800 Path::new(&node.path)
1801 .parent()
1802 .map(|parent| parent.to_string_lossy().trim_end_matches('/').to_owned())
1803 .unwrap_or_default()
1804 })
1805 .collect();
1806 roots.sort_by_key(|root| (root.matches('/').count(), root.clone()));
1807 roots.dedup();
1808 roots
1809}
1810
1811fn definition_line(path: &Path, function: &str) -> Option<u32> {
1812 let contents = fs::read_to_string(path).ok()?;
1813 let escaped = regex::escape(function);
1814 let pattern = Regex::new(&format!(
1815 r"(?m)^\s*(?:defp?|defmacrop?|defguardp?|defdelegate)\s+{escaped}(?:\s*\(|\s*,|\s+do|\s+when)"
1816 ))
1817 .ok()?;
1818 let found = pattern.find(&contents)?;
1819 Some(contents[..found.start()].lines().count() as u32 + 1)
1820}
1821
1822#[cfg(test)]
1823mod tests {
1824 use super::*;
1825
1826 #[test]
1827 fn assigns_files_to_the_nearest_mix_project_root() {
1828 let roots = vec![
1829 String::new(),
1830 "apps/alpha".to_owned(),
1831 "apps/alpha/vendor/nested".to_owned(),
1832 "apps/beta".to_owned(),
1833 ];
1834
1835 assert_eq!(nearest_project_root("lib/root.ex", &roots), Some(""));
1836 assert_eq!(
1837 nearest_project_root("apps/alpha/lib/alpha.ex", &roots),
1838 Some("apps/alpha")
1839 );
1840 assert_eq!(
1841 nearest_project_root("apps/alpha/vendor/nested/lib/nested.ex", &roots),
1842 Some("apps/alpha/vendor/nested")
1843 );
1844 assert_eq!(
1845 nearest_project_root("apps/beta/lib/beta.ex", &roots),
1846 Some("apps/beta")
1847 );
1848 }
1849 use std::collections::HashMap;
1850
1851 use crate::model::{AnalyzerReport, BuildTimings, Node, NodeKind, Rect};
1852
1853 #[test]
1854 fn parses_individual_xref_callsite() {
1855 assert_eq!(
1856 parse_xref_line("lib/wasmex.ex:252: call Wasmex.Module.compile/2 (runtime)"),
1857 Some(XrefCall {
1858 source_line: 252,
1859 module: "Wasmex.Module".to_owned(),
1860 function: "compile".to_owned(),
1861 arity: 2,
1862 kind: "runtime".to_owned(),
1863 })
1864 );
1865 }
1866
1867 #[test]
1868 fn finds_elixir_function_macro_guard_and_delegate_targets() {
1869 let directory = tempfile::tempdir().unwrap();
1870 let source = directory.path().join("targets.ex");
1871 fs::write(
1872 &source,
1873 "def run(), do: :ok\ndefmacro build(value), do: value\ndefguard valid(value) when is_atom(value)\ndefdelegate fetch(value), to: Other\n",
1874 )
1875 .unwrap();
1876
1877 assert_eq!(definition_line(&source, "run"), Some(1));
1878 assert_eq!(definition_line(&source, "build"), Some(2));
1879 assert_eq!(definition_line(&source, "valid"), Some(3));
1880 assert_eq!(definition_line(&source, "fetch"), Some(4));
1881 }
1882
1883 #[test]
1884 fn ignores_non_call_xref_records() {
1885 assert!(parse_xref_line("lib/a.ex:2: alias A (runtime)").is_none());
1886 assert!(parse_xref_line("lib/a.ex:3: struct A (export)").is_none());
1887 }
1888
1889 #[test]
1890 fn excludes_same_file_calls_without_aggregating_cross_file_calls() {
1891 let directory = tempfile::tempdir().unwrap();
1892 let mut atlas = Atlas {
1893 root_path: directory.path().to_path_buf(),
1894 revision: "test".to_owned(),
1895 dirty: true,
1896 excluded_test_files: 0,
1897 excluded_hidden_files: 0,
1898 excluded_custom_files: 0,
1899 excluded_paths: Vec::new(),
1900 nodes: vec![
1901 Node {
1902 id: 0,
1903 parent: None,
1904 children: vec![1, 2],
1905 kind: NodeKind::Directory,
1906 name: "fixture".to_owned(),
1907 path: String::new(),
1908 depth: 0,
1909 bytes: 2,
1910 loc: 2,
1911 commits: 2,
1912 weight: 2.0,
1913 language: "directory".to_owned(),
1914 rect: Rect::default(),
1915 },
1916 rust_node(1, "a.rs"),
1917 rust_node(2, "b.rs"),
1918 ],
1919 calls: vec![
1920 Callsite {
1921 id: 11,
1922 source: 1,
1923 source_line: 3,
1924 target: 1,
1925 target_line: Some(8),
1926 callee: "a::local".to_owned(),
1927 kind: "syntax".to_owned(),
1928 analyzer: "fixture".to_owned(),
1929 confidence: 1.0,
1930 },
1931 Callsite {
1932 id: 12,
1933 source: 1,
1934 source_line: 4,
1935 target: 2,
1936 target_line: Some(9),
1937 callee: "b::remote".to_owned(),
1938 kind: "syntax".to_owned(),
1939 analyzer: "fixture".to_owned(),
1940 confidence: 1.0,
1941 },
1942 ],
1943 path_to_id: HashMap::new(),
1944 report: AnalyzerReport::default(),
1945 timings: BuildTimings::default(),
1946 };
1947
1948 exclude_same_file_calls(&mut atlas);
1949
1950 assert_eq!(atlas.calls.len(), 1);
1951 assert_eq!(atlas.calls[0].id, 12);
1952 assert_eq!(atlas.report.same_file_calls_excluded, 1);
1953 }
1954
1955 #[test]
1956 fn resolves_unique_cross_file_rust_call_without_aggregation() {
1957 let directory = tempfile::tempdir().unwrap();
1958 fs::write(
1959 directory.path().join("a.rs"),
1960 "pub fn alpha() { b::beta(); }\n",
1961 )
1962 .unwrap();
1963 fs::write(directory.path().join("b.rs"), "pub fn beta() {}\n").unwrap();
1964 let mut atlas = Atlas {
1965 root_path: directory.path().to_path_buf(),
1966 revision: "test".to_owned(),
1967 dirty: false,
1968 excluded_test_files: 0,
1969 excluded_hidden_files: 0,
1970 excluded_custom_files: 0,
1971 excluded_paths: Vec::new(),
1972 nodes: vec![
1973 Node {
1974 id: 0,
1975 parent: None,
1976 children: vec![1, 2],
1977 kind: NodeKind::Directory,
1978 name: "fixture".to_owned(),
1979 path: String::new(),
1980 depth: 0,
1981 bytes: 0,
1982 loc: 0,
1983 commits: 0,
1984 weight: 2.0,
1985 language: "directory".to_owned(),
1986 rect: Rect::default(),
1987 },
1988 rust_node(1, "a.rs"),
1989 rust_node(2, "b.rs"),
1990 ],
1991 calls: Vec::new(),
1992 path_to_id: HashMap::new(),
1993 report: AnalyzerReport::default(),
1994 timings: BuildTimings::default(),
1995 };
1996 let mut next_id = 0;
1997
1998 initialize_language_coverage(&mut atlas);
1999 analyze_rust_calls_fallback(&mut atlas, &mut next_id).unwrap();
2000
2001 assert_eq!(atlas.report.rust_files_scanned, 2);
2002 assert_eq!(atlas.report.rust_calls, 1);
2003 assert_eq!(atlas.calls.len(), 1);
2004 assert_eq!(atlas.calls[0].source, 1);
2005 assert_eq!(atlas.calls[0].target, 2);
2006 }
2007
2008 #[test]
2009 fn clean_revision_cache_round_trips_calls_by_path() {
2010 let directory = tempfile::tempdir().unwrap();
2011 assert!(
2012 Command::new("git")
2013 .args(["init", "--quiet"])
2014 .arg(directory.path())
2015 .status()
2016 .unwrap()
2017 .success()
2018 );
2019 let nodes = vec![
2020 Node {
2021 id: 0,
2022 parent: None,
2023 children: vec![1, 2],
2024 kind: NodeKind::Directory,
2025 name: "fixture".to_owned(),
2026 path: String::new(),
2027 depth: 0,
2028 bytes: 2,
2029 loc: 2,
2030 commits: 2,
2031 weight: 2.0,
2032 language: "directory".to_owned(),
2033 rect: Rect::default(),
2034 },
2035 rust_node(1, "a.rs"),
2036 rust_node(2, "b.rs"),
2037 ];
2038 let mut path_to_id = HashMap::new();
2039 path_to_id.insert("a.rs".to_owned(), 1);
2040 path_to_id.insert("b.rs".to_owned(), 2);
2041 let mut atlas = Atlas {
2042 root_path: directory.path().to_path_buf(),
2043 revision: "abc123".to_owned(),
2044 dirty: false,
2045 excluded_test_files: 0,
2046 excluded_hidden_files: 0,
2047 excluded_custom_files: 0,
2048 excluded_paths: Vec::new(),
2049 nodes: nodes.clone(),
2050 calls: vec![Callsite {
2051 id: 7,
2052 source: 1,
2053 source_line: 3,
2054 target: 2,
2055 target_line: Some(8),
2056 callee: "b::run".to_owned(),
2057 kind: "syntax".to_owned(),
2058 analyzer: "fixture".to_owned(),
2059 confidence: 0.9,
2060 }],
2061 path_to_id: path_to_id.clone(),
2062 report: AnalyzerReport {
2063 resolved_calls: 1,
2064 ..AnalyzerReport::default()
2065 },
2066 timings: BuildTimings::default(),
2067 };
2068 save_cached_analysis(&mut atlas).unwrap();
2069
2070 atlas.calls.clear();
2071 atlas.report = AnalyzerReport::default();
2072 atlas.nodes = nodes;
2073 atlas.path_to_id = path_to_id;
2074 assert!(load_cached_analysis(&mut atlas).unwrap());
2075 assert_eq!(atlas.calls.len(), 1);
2076 assert_eq!(atlas.calls[0].source, 1);
2077 assert_eq!(atlas.calls[0].target, 2);
2078 assert!(atlas.report.cache_hit);
2079 }
2080
2081 #[test]
2082 fn resolves_typescript_and_gleam_imported_calls_without_aggregation() {
2083 let directory = tempfile::tempdir().unwrap();
2084 fs::create_dir_all(directory.path().join("src")).unwrap();
2085 fs::create_dir_all(directory.path().join("gleam/src")).unwrap();
2086 fs::write(
2087 directory.path().join("src/a.ts"),
2088 "import { run as execute } from './b';\nexecute();\nexecute();\n",
2089 )
2090 .unwrap();
2091 fs::write(
2092 directory.path().join("src/b.ts"),
2093 "export function run() {}\n",
2094 )
2095 .unwrap();
2096 fs::write(
2097 directory.path().join("gleam/src/a.gleam"),
2098 "import b\npub fn start() { b.run() }\n",
2099 )
2100 .unwrap();
2101 fs::write(
2102 directory.path().join("gleam/src/b.gleam"),
2103 "pub fn run() { Nil }\n",
2104 )
2105 .unwrap();
2106 let paths = [
2107 "src/a.ts",
2108 "src/b.ts",
2109 "gleam/src/a.gleam",
2110 "gleam/src/b.gleam",
2111 ];
2112 let mut nodes = vec![Node {
2113 id: 0,
2114 parent: None,
2115 children: (1..=paths.len()).collect(),
2116 kind: NodeKind::Directory,
2117 name: "fixture".to_owned(),
2118 path: String::new(),
2119 depth: 0,
2120 bytes: 4,
2121 loc: 4,
2122 commits: 4,
2123 weight: 4.0,
2124 language: "directory".to_owned(),
2125 rect: Rect::default(),
2126 }];
2127 let mut path_to_id = HashMap::new();
2128 for (index, path) in paths.iter().enumerate() {
2129 let id = index + 1;
2130 nodes.push(Node {
2131 id,
2132 parent: Some(0),
2133 children: Vec::new(),
2134 kind: NodeKind::File,
2135 name: path.rsplit('/').next().unwrap().to_owned(),
2136 path: (*path).to_owned(),
2137 depth: 1,
2138 bytes: 1,
2139 loc: 1,
2140 commits: 1,
2141 weight: 1.0,
2142 language: if path.ends_with(".gleam") {
2143 "gleam".to_owned()
2144 } else {
2145 "typescript".to_owned()
2146 },
2147 rect: Rect::default(),
2148 });
2149 path_to_id.insert((*path).to_owned(), id);
2150 }
2151 let mut atlas = Atlas {
2152 root_path: directory.path().to_path_buf(),
2153 revision: "test".to_owned(),
2154 dirty: true,
2155 excluded_test_files: 0,
2156 excluded_hidden_files: 0,
2157 excluded_custom_files: 0,
2158 excluded_paths: Vec::new(),
2159 nodes,
2160 calls: Vec::new(),
2161 path_to_id,
2162 report: AnalyzerReport::default(),
2163 timings: BuildTimings::default(),
2164 };
2165 let mut next_id = 0;
2166 initialize_language_coverage(&mut atlas);
2167 analyze_typescript_calls_fallback(&mut atlas, &mut next_id).unwrap();
2168 analyze_gleam_calls(&mut atlas, &mut next_id).unwrap();
2169
2170 assert_eq!(atlas.report.typescript_files_scanned, 2);
2171 assert_eq!(atlas.report.typescript_calls, 2);
2172 assert_eq!(atlas.report.gleam_files_scanned, 2);
2173 assert_eq!(atlas.report.gleam_calls, 1);
2174 assert_eq!(atlas.report.language_coverage["gleam"].files_discovered, 2);
2175 assert_eq!(atlas.report.language_coverage["gleam"].files_analyzed, 2);
2176 assert_eq!(
2177 atlas.report.language_coverage["gleam"].callsites_resolved,
2178 1
2179 );
2180 assert_eq!(atlas.calls.len(), 3);
2181 assert_eq!(atlas.calls[0].source, 1);
2182 assert_eq!(atlas.calls[0].target, 2);
2183 assert_eq!(atlas.calls[1].source, 1);
2184 assert_eq!(atlas.calls[1].target, 2);
2185 assert_eq!(atlas.calls[2].source, 3);
2186 assert_eq!(atlas.calls[2].target, 4);
2187 }
2188
2189 #[test]
2190 fn semantic_scip_ingestion_keeps_individual_calls_and_exact_targets() {
2191 let directory = tempfile::tempdir().unwrap();
2192 fs::create_dir_all(directory.path().join("src")).unwrap();
2193 fs::write(
2194 directory.path().join("src/a.ts"),
2195 "execute();\nexecute();\n",
2196 )
2197 .unwrap();
2198 fs::write(
2199 directory.path().join("src/b.ts"),
2200 "export function execute() {}\n",
2201 )
2202 .unwrap();
2203 let paths = ["src/a.ts", "src/b.ts"];
2204 let mut path_to_id = HashMap::new();
2205 let mut nodes = vec![Node {
2206 id: 0,
2207 parent: None,
2208 children: vec![1, 2],
2209 kind: NodeKind::Directory,
2210 name: "fixture".to_owned(),
2211 path: String::new(),
2212 depth: 0,
2213 bytes: 2,
2214 loc: 2,
2215 commits: 2,
2216 weight: 2.0,
2217 language: "directory".to_owned(),
2218 rect: Rect::default(),
2219 }];
2220 for (index, path) in paths.iter().enumerate() {
2221 let id = index + 1;
2222 nodes.push(Node {
2223 id,
2224 parent: Some(0),
2225 children: Vec::new(),
2226 kind: NodeKind::File,
2227 name: path.rsplit('/').next().unwrap().to_owned(),
2228 path: (*path).to_owned(),
2229 depth: 1,
2230 bytes: 1,
2231 loc: 1,
2232 commits: 1,
2233 weight: 1.0,
2234 language: "typescript".to_owned(),
2235 rect: Rect::default(),
2236 });
2237 path_to_id.insert((*path).to_owned(), id);
2238 }
2239 let symbol = "scip-typescript npm fixture 1.0 src/b.ts/execute().";
2240 let reference = |line| crate::scip::ScipOccurrence {
2241 range: Some(ScipRange {
2242 start_line: line,
2243 start_character: 0,
2244 end_line: line,
2245 end_character: 7,
2246 }),
2247 symbol: symbol.to_owned(),
2248 symbol_roles: 0,
2249 };
2250 let indexes = vec![RootedScipIndex {
2251 root: String::new(),
2252 index: ScipIndex {
2253 documents: vec![
2254 ScipDocument {
2255 relative_path: "src/a.ts".to_owned(),
2256 language: "typescript".to_owned(),
2257 position_encoding: 2,
2258 occurrences: vec![reference(0), reference(1)],
2259 },
2260 ScipDocument {
2261 relative_path: "src/b.ts".to_owned(),
2262 language: "typescript".to_owned(),
2263 position_encoding: 2,
2264 occurrences: vec![crate::scip::ScipOccurrence {
2265 range: Some(ScipRange {
2266 start_line: 0,
2267 start_character: 16,
2268 end_line: 0,
2269 end_character: 23,
2270 }),
2271 symbol: symbol.to_owned(),
2272 symbol_roles: 1,
2273 }],
2274 },
2275 ],
2276 },
2277 }];
2278 let mut atlas = Atlas {
2279 root_path: directory.path().to_path_buf(),
2280 revision: "test".to_owned(),
2281 dirty: true,
2282 excluded_test_files: 0,
2283 excluded_hidden_files: 0,
2284 excluded_custom_files: 0,
2285 excluded_paths: Vec::new(),
2286 nodes,
2287 calls: Vec::new(),
2288 path_to_id,
2289 report: AnalyzerReport::default(),
2290 timings: BuildTimings::default(),
2291 };
2292 let mut next_id = 0;
2293
2294 let stats = ingest_scip_calls(&mut atlas, &indexes, &["typescript"], &mut next_id).unwrap();
2295
2296 assert_eq!(stats.files_by_language["typescript"], 2);
2297 assert_eq!(stats.resolved_by_language["typescript"], 2);
2298 assert_eq!(atlas.calls.len(), 2);
2299 assert_eq!(atlas.calls[0].source_line, 1);
2300 assert_eq!(atlas.calls[1].source_line, 2);
2301 assert_eq!(atlas.calls[0].target, 2);
2302 assert_eq!(atlas.calls[0].target_line, Some(1));
2303 assert_eq!(atlas.calls[0].analyzer, "scip-typescript");
2304 }
2305
2306 #[test]
2307 fn semantic_scip_ingestion_supports_javascript_documents() {
2308 let directory = tempfile::tempdir().unwrap();
2309 fs::create_dir_all(directory.path().join("src")).unwrap();
2310 fs::write(directory.path().join("src/a.js"), "execute();\n").unwrap();
2311 fs::write(
2312 directory.path().join("src/b.js"),
2313 "export function execute() {}\n",
2314 )
2315 .unwrap();
2316 let paths = ["src/a.js", "src/b.js"];
2317 let mut path_to_id = HashMap::new();
2318 let mut nodes = vec![Node {
2319 id: 0,
2320 parent: None,
2321 children: vec![1, 2],
2322 kind: NodeKind::Directory,
2323 name: "fixture".to_owned(),
2324 path: String::new(),
2325 depth: 0,
2326 bytes: 2,
2327 loc: 2,
2328 commits: 2,
2329 weight: 2.0,
2330 language: "directory".to_owned(),
2331 rect: Rect::default(),
2332 }];
2333 for (index, path) in paths.iter().enumerate() {
2334 let id = index + 1;
2335 nodes.push(Node {
2336 id,
2337 parent: Some(0),
2338 children: Vec::new(),
2339 kind: NodeKind::File,
2340 name: path.rsplit('/').next().unwrap().to_owned(),
2341 path: (*path).to_owned(),
2342 depth: 1,
2343 bytes: 1,
2344 loc: 1,
2345 commits: 1,
2346 weight: 1.0,
2347 language: "javascript".to_owned(),
2348 rect: Rect::default(),
2349 });
2350 path_to_id.insert((*path).to_owned(), id);
2351 }
2352 let symbol = "scip-typescript npm fixture 1.0 src/b.js/execute().";
2353 let indexes = vec![RootedScipIndex {
2354 root: String::new(),
2355 index: ScipIndex {
2356 documents: vec![
2357 ScipDocument {
2358 relative_path: "src/a.js".to_owned(),
2359 language: "javascript".to_owned(),
2360 position_encoding: 2,
2361 occurrences: vec![crate::scip::ScipOccurrence {
2362 range: Some(ScipRange {
2363 start_line: 0,
2364 start_character: 0,
2365 end_line: 0,
2366 end_character: 7,
2367 }),
2368 symbol: symbol.to_owned(),
2369 symbol_roles: 0,
2370 }],
2371 },
2372 ScipDocument {
2373 relative_path: "src/b.js".to_owned(),
2374 language: "javascript".to_owned(),
2375 position_encoding: 2,
2376 occurrences: vec![crate::scip::ScipOccurrence {
2377 range: Some(ScipRange {
2378 start_line: 0,
2379 start_character: 16,
2380 end_line: 0,
2381 end_character: 23,
2382 }),
2383 symbol: symbol.to_owned(),
2384 symbol_roles: 1,
2385 }],
2386 },
2387 ],
2388 },
2389 }];
2390 let mut atlas = Atlas {
2391 root_path: directory.path().to_path_buf(),
2392 revision: "test".to_owned(),
2393 dirty: true,
2394 excluded_test_files: 0,
2395 excluded_hidden_files: 0,
2396 excluded_custom_files: 0,
2397 excluded_paths: Vec::new(),
2398 nodes,
2399 calls: Vec::new(),
2400 path_to_id,
2401 report: AnalyzerReport::default(),
2402 timings: BuildTimings::default(),
2403 };
2404 let mut next_id = 0;
2405
2406 let stats = ingest_scip_calls(&mut atlas, &indexes, &["javascript"], &mut next_id).unwrap();
2407
2408 assert_eq!(stats.files_by_language["javascript"], 2);
2409 assert_eq!(stats.resolved_by_language["javascript"], 1);
2410 assert_eq!(atlas.calls.len(), 1);
2411 assert_eq!(atlas.calls[0].source_line, 1);
2412 assert_eq!(atlas.calls[0].target_line, Some(1));
2413 assert_eq!(atlas.calls[0].analyzer, "scip-typescript");
2414 }
2415
2416 #[test]
2417 fn call_suffix_classifier_handles_methods_generics_macros_and_optional_calls() {
2418 assert!(suffix_is_call("()"));
2419 assert!(suffix_is_call("::<Value>()"));
2420 assert!(suffix_is_call("<Value>()"));
2421 assert!(suffix_is_call("!()"));
2422 assert!(suffix_is_call("?.()"));
2423 assert!(!suffix_is_call(";"));
2424 assert!(!suffix_is_call("::<Value>"));
2425 }
2426
2427 fn rust_node(id: usize, path: &str) -> Node {
2428 Node {
2429 id,
2430 parent: Some(0),
2431 children: Vec::new(),
2432 kind: NodeKind::File,
2433 name: path.to_owned(),
2434 path: path.to_owned(),
2435 depth: 1,
2436 bytes: 1,
2437 loc: 1,
2438 commits: 1,
2439 weight: 1.0,
2440 language: "rust".to_owned(),
2441 rect: Rect::default(),
2442 }
2443 }
2444}