Skip to content

Codebase intelligence for AI and humans: code health scores, auto-generated docs, git analytics, dead code detection, and architectural decisions via MCP.

fix(graph): keep name-only call tiers inside the caller's build (#3682)

Swati Ahuja151fccfefe77
diff --git a/packages/core/src/repowise/core/ingestion/build_roots.py b/packages/core/src/repowise/core/ingestion/build_roots.py
new file mode 100644
index 000000000..a7658e64b
--- /dev/null
+++ b/packages/core/src/repowise/core/ingestion/build_roots.py
@@ -0,0 +1,116 @@
+"""Which independent build contains a file.
+
+A repo can hold several builds that never see each other's code: Ktor's
+samples are thirty Gradle builds side by side, a Flutter monorepo has one
+pubspec per package, an iOS app ships local Swift packages. A bare name in one
+build cannot mean a top-level symbol of another, so the name-only call tiers
+(same package, global unique) stay inside the caller's build unless the build
+composes the other in (`includeBuild`, `buildSrc`) or the file imports it.
+
+A build root is the deepest directory holding ``settings.gradle(.kts)``,
+``pubspec.yaml`` or ``Package.swift``, or named ``buildSrc`` (Gradle builds it
+on its own). A Gradle project with no settings file anywhere above it is its
+own build, rooted at its outermost ``build.gradle(.kts)``. A file under no
+root belongs to no build, and nothing is refused for it.
+"""
+
+from __future__ import annotations
+
+import os
+import posixpath
+from collections.abc import Iterable
+from pathlib import Path
+
+from .resolvers.jvm_gradle import included_builds
+
+_SETTINGS = ("settings.gradle.kts", "settings.gradle")
+_ROOT_FILES = (*_SETTINGS, "pubspec.yaml", "Package.swift")
+_GRADLE_BUILDS = ("build.gradle.kts", "build.gradle")
+
+
+def _ancestors(path: str) -> list[str]:
+ """Every directory above *path*, the repo root ``""`` first."""
+ parts = path.split("/")[:-1]
+ return ["", *("/".join(parts[: i + 1]) for i in range(len(parts)))]
+
+
+def _root_marks(here: Path, inside_gradle: bool) -> tuple[bool, bool]:
+ """``(roots a Gradle build, roots a Dart or Swift package)`` for directory *here*."""
+ gradle = any(os.path.isfile(here / name) for name in _SETTINGS)
+ if not gradle and not inside_gradle:
+ gradle = any(os.path.isfile(here / name) for name in _GRADLE_BUILDS)
+ return gradle, any(os.path.isfile(here / name) for name in _ROOT_FILES[2:])
+
+
+def _scan_roots(repo: Path, dirs: set[str]) -> set[str]:
+ """The build roots among *dirs*."""
+ roots: set[str] = set()
+ # Directories already inside a Gradle build, where a build.gradle is a
+ # subproject rather than a build of its own.
+ in_gradle: set[str] = set()
+ for d in sorted(dirs, key=lambda d: d.count("/")): # parents before children
+ inside = bool(d) and posixpath.dirname(d) in in_gradle
+ gradle_root, other_root = _root_marks(repo / d, inside)
+ if gradle_root or other_root or posixpath.basename(d) == "buildSrc":
+ roots.add(d)
+ if gradle_root or inside:
+ in_gradle.add(d)
+ return roots
+
+
+class BuildRoots:
+ """The build roots of one repo, read from the indexed paths and the disk."""
+
+ def __init__(self, repo_path: str | None, paths: Iterable[str]) -> None:
+ self._roots: set[str] = set()
+ self._includes: dict[str, frozenset[str]] = {}
+ if repo_path is None:
+ return
+ repo = Path(repo_path)
+ self._roots = _scan_roots(repo, {d for path in paths for d in _ancestors(path)})
+ for root in self._roots:
+ self._includes[root] = frozenset(self._composed(repo, root))
+
+ def _composed(self, repo: Path, root: str) -> Iterable[str]:
+ """The roots *root*'s Gradle build composes in: its ``buildSrc`` and included builds."""
+ build_src = posixpath.join(root, "buildSrc")
+ if build_src in self._roots:
+ yield build_src
+ for name in _SETTINGS:
+ settings = repo / root / name
+ if not settings.is_file():
+ continue
+ text = settings.read_text(encoding="utf-8", errors="ignore")
+ for included in included_builds(text):
+ target = posixpath.normpath(posixpath.join(root, included))
+ yield "" if target == "." else target
+
+ def root_of(self, path: str) -> str | None:
+ """The deepest build root above *path*, or None."""
+ for d in reversed(_ancestors(path)):
+ if d in self._roots:
+ return d
+ return None
+
+ def composes(self, root: str | None, other: str | None) -> bool:
+ """Does the build at *root* compose the build at *other* in?"""
+ return root is not None and other in self._includes.get(root, ())
+
+ def script_sees(self, script: str, other: str | None) -> bool:
+ """Does Gradle Kotlin *script* compile against code in build root *other*?
+
+ A build script sees only the build logic its build composes in. A
+ precompiled script plugin (under a ``src`` directory of its build)
+ also sees the sources it is compiled with.
+ """
+ own = self.root_of(script)
+ if self.composes(own, other):
+ return True
+ below = script[len(own) + 1 :] if own else script
+ return other == own and own is not None and "src" in below.split("/")[:-1]
+
+ def separates(self, root: str | None, other: str | None) -> bool:
+ """Are *root* and *other* two builds, neither composed into *root*?"""
+ if root is None or other is None or root == other:
+ return False
+ return not self.composes(root, other)
diff --git a/packages/core/src/repowise/core/ingestion/call_language_strategies.py b/packages/core/src/repowise/core/ingestion/call_language_strategies.py
index 356843e71..35b80fa5d 100644
--- a/packages/core/src/repowise/core/ingestion/call_language_strategies.py
+++ b/packages/core/src/repowise/core/ingestion/call_language_strategies.py
@@ -43,6 +43,9 @@ class _LanguageCallStrategies:
_NO_LANGUAGE_STRATEGIES = _LanguageCallStrategies()
+# Languages whose builds are told apart by ``build_roots``.
+_BUILD_ROOT_LANGUAGES = frozenset({"dart", "java", "kotlin", "scala", "swift"})
+
_TYPED_RECEIVER = ("_resolve_typed_receiver",)
@@ -134,6 +137,11 @@ class LanguageStrategiesMixin:
self._cpp_index: Any = None
self._cpp_index_built = False
+ # Independent builds in the repo (lazy BuildRoots), and the build roots
+ # each file's imports land in.
+ self._build_roots: Any = None
+ self._imported_roots: dict[str, frozenset[str | None]] = {}
+
def _get_rust_crate_src(self) -> dict[str, str]:
"""Lazily build a mapping from normalised crate name to src/ dir."""
if self._rust_crate_src is not None:
@@ -281,6 +289,61 @@ class LanguageStrategiesMixin:
return ResolvedCall(caller_id, sym_id, 0.85, call.line, "same_target")
return None
+ def _in_reach_of_build(self, file_path: str, target_path: str) -> bool:
+ """May a bare name in *file_path* mean a symbol in *target_path*, by name alone?
+
+ Not across independent builds, unless the caller's build composes the
+ target's in or the caller imports a file there. A Gradle build
+ script's classpath is its build logic, not the sources it builds, so a
+ script's name reaches only into a build its own composes in.
+ """
+ if self._language_of(file_path) not in _BUILD_ROOT_LANGUAGES:
+ return True
+ if self._build_roots is None:
+ from .build_roots import BuildRoots
+
+ self._build_roots = BuildRoots(self._repo_path, self._parsed_files)
+ roots = self._build_roots
+ other = roots.root_of(target_path)
+ if file_path.endswith(".gradle.kts"):
+ return roots.script_sees(file_path, other)
+ own = roots.root_of(file_path)
+ return not roots.separates(own, other) or other in self._roots_imported_by(file_path)
+
+ def _roots_imported_by(self, file_path: str) -> frozenset[str | None]:
+ """The build roots *file_path*'s imports land in, re-exports followed."""
+ imported = self._imported_roots.get(file_path)
+ if imported is None:
+ assert self._build_roots is not None
+ imported = frozenset(map(self._build_roots.root_of, self._visible_files(file_path)))
+ self._imported_roots[file_path] = imported
+ return imported
+
+ def _visible_files(self, file_path: str) -> set[str]:
+ """The files *file_path* imports, and every file those re-export (Dart ``export``)."""
+ seen: set[str] = set()
+ pending = list(self._import_targets.get(file_path, ()))
+ while pending:
+ target = pending.pop()
+ if target in seen:
+ continue
+ seen.add(target)
+ parsed = self._parsed_files.get(target)
+ pending.extend(
+ imp.resolved_file
+ for imp in (parsed.imports if parsed else ())
+ if imp.is_reexport and imp.resolved_file in self._parsed_files
+ )
+ return seen
+
+ def _same_package_siblings(self, index: Any, file_path: str) -> list[str]:
+ """*file_path*'s JVM package siblings within its own build."""
+ return [
+ sibling
+ for sibling in index.same_package_files(file_path)
+ if self._in_reach_of_build(file_path, sibling)
+ ]
+
def _get_jvm_index(self) -> Any:
"""Lazily build the JvmWorkspaceIndex (or None if unavailable)."""
if self._jvm_index_built:
@@ -344,7 +407,7 @@ class LanguageStrategiesMixin:
if index is None:
return None
found: str | None = None
- for sibling in index.same_package_files(file_path):
+ for sibling in self._same_package_siblings(index, file_path):
sym_id = self._package_symbol(sibling, call.target_name, caller_id)
if sym_id is not None:
if not unique_only:
@@ -392,7 +455,7 @@ class LanguageStrategiesMixin:
index = self._get_jvm_index()
if index is None:
return None
- for sibling in index.same_package_files(file_path):
+ for sibling in self._same_package_siblings(index, file_path):
sym_id = self._file_methods.get(sibling, {}).get(key)
if sym_id is not None:
return ResolvedCall(caller_id, sym_id, 0.90, call.line, "receiver_same_package")
diff --git a/packages/core/src/repowise/core/ingestion/call_resolver.py b/packages/core/src/repowise/core/ingestion/call_resolver.py
index b465fdff4..4a26abd85 100644
--- a/packages/core/src/repowise/core/ingestion/call_resolver.py
+++ b/packages/core/src/repowise/core/ingestion/call_resolver.py
@@ -1786,9 +1786,13 @@ class CallResolver(LanguageStrategiesMixin, ReceiverTypingMixin):
Not when *sym_id* has internal linkage in another translation unit: a
``static`` function in one .c file cannot be linked from any other. The
include-grounded tiers need no check, since an included file is part of
- the includer's translation unit.
+ the includer's translation unit. Nor across independent builds
+ (``_in_reach_of_build``).
"""
- return sym_id not in self._tu_local_ids or self._symbol_paths_by_id.get(sym_id) == file_path
+ path = self._symbol_paths_by_id.get(sym_id, "")
+ if sym_id in self._tu_local_ids and path != file_path:
+ return False
+ return self._in_reach_of_build(file_path, path)
def _implicit_inherited_call(
self,
diff --git a/packages/core/src/repowise/core/ingestion/resolvers/jvm_gradle.py b/packages/core/src/repowise/core/ingestion/resolvers/jvm_gradle.py
index 03ca1ac44..9e5db10e7 100644
--- a/packages/core/src/repowise/core/ingestion/resolvers/jvm_gradle.py
+++ b/packages/core/src/repowise/core/ingestion/resolvers/jvm_gradle.py
@@ -34,7 +34,7 @@ log = structlog.get_logger(__name__)
_INCLUDE_RE = re.compile(r'include\s*\(\s*((?:"[^"]+"\s*,?\s*)+)\)')
_INCLUDE_ITEM_RE = re.compile(r'"([^"]+)"')
_INCLUDE_LINE_RE = re.compile(r'include\s+([\'"])([^\'"]+)\1')
-_INCLUDE_BUILD_RE = re.compile(r'includeBuild\s*\(\s*"([^"]+)"\s*\)')
+_INCLUDE_BUILD_RE = re.compile(r"""includeBuild\s*\(?\s*(["'])([^"']+)\1""")
_PROJECT_DIR_RE = re.compile(
r'project\s*\(\s*"([^"]+)"\s*\)\s*\.\s*projectDir\s*=\s*file\s*\(\s*"([^"]+)"\s*\)'
)
@@ -169,6 +169,11 @@ def parse_settings(settings_file: Path) -> tuple[list[str], dict[str, str]]:
return result, overrides
+def included_builds(settings_text: str) -> list[str]:
+ """The directories a settings script composes in with ``includeBuild``, as written."""
+ return [match.group(2) for match in _INCLUDE_BUILD_RE.finditer(settings_text)]
+
+
def _module_dir(repo_path: Path, module_name: str, overrides: dict[str, str]) -> Path:
if module_name in overrides:
return repo_path / overrides[module_name]
diff --git a/tests/unit/ingestion/test_build_roots.py b/tests/unit/ingestion/test_build_roots.py
new file mode 100644
index 000000000..c4f575928
--- /dev/null
+++ b/tests/unit/ingestion/test_build_roots.py
@@ -0,0 +1,180 @@
+"""The name-only call tiers stay inside the caller's build.
+
+Ktor's samples are independent Gradle builds side by side: a bare call in one
+cannot mean a top-level function of another, however unique its name. A build
+that composes another in (``includeBuild``, ``buildSrc``) sees it, and a Gradle
+build script sees only the build logic its build composes in, never the
+sources it builds.
+"""
+
+from __future__ import annotations
+
+from datetime import datetime
+from pathlib import Path
+
+from repowise.core.ingestion.build_roots import BuildRoots
+from repowise.core.ingestion.call_resolver import CallResolver
+from repowise.core.ingestion.models import FileInfo, ParsedFile
+from repowise.core.ingestion.parser import parse_file
+from repowise.core.ingestion.resolvers.jvm_gradle import included_builds
+
+_LANGUAGES = {"kt": "kotlin", "kts": "kotlin", "dart": "dart"}
+
+
+def _write(tmp_path: Path, files: dict[str, str]) -> None:
+ for rel, content in files.items():
+ path = tmp_path / rel
+ path.parent.mkdir(parents=True, exist_ok=True)
+ path.write_text(content, encoding="utf-8")
+
+
+def _parse(tmp_path: Path, files: dict[str, str]) -> dict[str, ParsedFile]:
+ _write(tmp_path, files)
+ out: dict[str, ParsedFile] = {}
+ for rel, content in files.items():
+ language = _LANGUAGES.get(rel.rpartition(".")[2])
+ if language is None:
+ continue
+ info = FileInfo(
+ path=rel,
+ abs_path=str(tmp_path / rel),
+ language=language,
+ size_bytes=len(content),
+ git_hash="",
+ last_modified=datetime.now(),
+ is_test=False,
+ is_config=False,
+ is_api_contract=False,
+ is_entry_point=False,
+ )
+ out[rel] = parse_file(info, content.encode("utf-8"))
+ return out
+
+
+def _callees(
+ tmp_path: Path, files: dict[str, str], caller: str, imports: dict[str, str] | None = None
+) -> set[str]:
+ """Callees of *caller*; *imports* maps a file to the one file it imports."""
+ parsed = _parse(tmp_path, files)
+ targets: dict[str, set[str]] = {p: set() for p in parsed}
+ for importer, imported in (imports or {}).items():
+ targets[importer].add(imported)
+ for imp in parsed[importer].imports:
+ imp.resolved_file = imported
+ resolver = CallResolver(parsed, targets, repo_path=str(tmp_path))
+ path = caller.partition("::")[0]
+ return {
+ rc.callee_id
+ for rc in resolver.resolve_file(path, parsed[path].calls)
+ if rc.caller_id == caller
+ }
+
+
+HELPER = "package b\n\nfun registerRoutes(x: Int) = x\n"
+CALLER = "package a\n\nfun main() {\n registerRoutes(1)\n}\n"
+
+
+class TestBuildRoots:
+ def test_roots_are_settings_and_lone_builds(self, tmp_path: Path) -> None:
+ files = {
+ "settings.gradle.kts": "",
+ "app/build.gradle.kts": "",
+ "app/src/A.kt": "",
+ "logic/settings.gradle": "",
+ "logic/src/L.kt": "",
+ "buildSrc/src/B.kt": "",
+ "pkg/pubspec.yaml": "name: pkg\n",
+ "pkg/lib/p.dart": "",
+ }
+ _write(tmp_path, files)
+ roots = BuildRoots(str(tmp_path), files)
+ assert roots.root_of("app/src/A.kt") == ""
+ assert roots.root_of("logic/src/L.kt") == "logic"
+ assert roots.root_of("buildSrc/src/B.kt") == "buildSrc"
+ assert roots.root_of("pkg/lib/p.dart") == "pkg"
+ assert roots.composes("", "buildSrc")
+ assert not roots.composes("", "logic")
+
+ def test_a_build_without_settings_is_its_own(self, tmp_path: Path) -> None:
+ files = {"one/build.gradle.kts": "", "one/sub/build.gradle.kts": "", "one/sub/X.kt": ""}
+ _write(tmp_path, files)
+ assert BuildRoots(str(tmp_path), files).root_of("one/sub/X.kt") == "one"
+
+ def test_included_builds_read_both_dialects(self) -> None:
+ text = 'pluginManagement {\n includeBuild("build-logic")\n}\nincludeBuild \'../lib\'\n'
+ assert included_builds(text) == ["build-logic", "../lib"]
+
+
+class TestNameTiersStayInTheBuild:
+ def test_a_sibling_build_does_not_answer(self, tmp_path: Path) -> None:
+ files = {
+ "a/build.gradle.kts": "",
+ "a/src/Main.kt": CALLER,
+ "b/build.gradle.kts": "",
+ "b/src/Helper.kt": HELPER,
+ }
+ assert _callees(tmp_path, files, "a/src/Main.kt::main") == set()
+
+ def test_one_build_still_answers(self, tmp_path: Path) -> None:
+ files = {
+ "settings.gradle.kts": 'include(":a", ":b")\n',
+ "a/build.gradle.kts": "",
+ "a/src/Main.kt": CALLER,
+ "b/build.gradle.kts": "",
+ "b/src/Helper.kt": HELPER,
+ }
+ assert _callees(tmp_path, files, "a/src/Main.kt::main") == {
+ "b/src/Helper.kt::registerRoutes"
+ }
+
+ def test_an_included_build_answers(self, tmp_path: Path) -> None:
+ files = {
+ "a/settings.gradle.kts": 'includeBuild("../b")\n',
+ "a/src/Main.kt": CALLER,
+ "b/settings.gradle.kts": "",
+ "b/src/Helper.kt": HELPER,
+ }
+ assert _callees(tmp_path, files, "a/src/Main.kt::main") == {
+ "b/src/Helper.kt::registerRoutes"
+ }
+
+
+SCRIPT_FILES = {
+ "settings.gradle.kts": 'includeBuild("build-logic")\n',
+ "app/build.gradle.kts": "plugins {\n}\napi(1)\nconfigureFlavors(2)\n",
+ "app/src/Api.kt": "package app\n\nfun api(x: Int) = x\n",
+ "build-logic/settings.gradle.kts": "",
+ "build-logic/src/Flavors.kt": "fun configureFlavors(x: Int) = x\n",
+}
+
+
+class TestBuildScripts:
+ def test_a_script_reaches_its_build_logic_only(self, tmp_path: Path) -> None:
+ callees = _callees(tmp_path, SCRIPT_FILES, "app/build.gradle.kts::__module__")
+ assert callees == {"build-logic/src/Flavors.kt::configureFlavors"}
+
+ def test_a_precompiled_script_plugin_sees_its_own_build(self, tmp_path: Path) -> None:
+ script = "buildSrc/src/main/kotlin/my-conv.gradle.kts"
+ files = {
+ "settings.gradle.kts": "",
+ "buildSrc/src/main/kotlin/Utils.kt": "fun configureKotlin() {}\n",
+ script: "configureKotlin()\n",
+ }
+ assert _callees(tmp_path, files, f"{script}::__module__") == {
+ "buildSrc/src/main/kotlin/Utils.kt::configureKotlin"
+ }
+
+
+class TestReexports:
+ def test_a_package_re_exported_by_an_import_is_reachable(self, tmp_path: Path) -> None:
+ files = {
+ "a/pubspec.yaml": "name: a\n",
+ "b/pubspec.yaml": "name: b\n",
+ "c/pubspec.yaml": "name: c\n",
+ "a/lib/a.dart": "import 'package:b/b.dart';\nvoid useA() { fromC(); }\n",
+ "b/lib/b.dart": "export 'package:c/c.dart';\nvoid fromB() {}\n",
+ "c/lib/c.dart": "void fromC() {}\n",
+ }
+ imports = {"a/lib/a.dart": "b/lib/b.dart", "b/lib/b.dart": "c/lib/c.dart"}
+ callees = _callees(tmp_path, files, "a/lib/a.dart::useA", imports)
+ assert callees == {"c/lib/c.dart::fromC"}