Codebase intelligence for AI and humans: code health scores, auto-generated docs, git analytics, dead code detection, and architectural decisions via MCP.
Python aicode-complexitycode-healthcode-intelligencecode-qualitydead-codedeveloper-toolsdocumentationgit-analyticsmcpopen-sourcepythonrefactoringstatic-analysistechnical-debt
fix(graph): type Go builder chains through repository return types (#3036)
Raghav Chamadiya52b53859c1c7
diff --git a/packages/core/src/repowise/core/ingestion/call_resolver.py b/packages/core/src/repowise/core/ingestion/call_resolver.pyindex eba0ba6e9..ef5f9bea9 100644--- a/packages/core/src/repowise/core/ingestion/call_resolver.py+++ b/packages/core/src/repowise/core/ingestion/call_resolver.py@@ -959,10 +959,27 @@ class CallResolver(LanguageStrategiesMixin, ReceiverTypingMixin):tabled = self._external_chain_return_type(file_path, inner, language)from_table = tabled is not None+ inner_callee = (+ None+ if tabled is not None+ else self._chain_inner_callee(file_path, call, inner, caller_id, language)+ )+ if language == "go" and inner_callee is not None:+ # A go method may sit in any file of its type's package, so the+ # type is read by identity and the method asked for across it.+ type_id = self._returned_type_id(inner_callee)+ hit = (+ None+ if type_id is None+ else self._call_typed_receiver(file_path, call, caller_id, type_id)+ )+ if hit is not None:+ tier = hit.origin.removeprefix("receiver_typed_")+ return True, self._return_typed_call(caller_id, hit.callee_id, tier, call.line)type_name = (tabledif tabled is not None- else self._inferred_chain_return_type(file_path, call, inner, caller_id, language)+ else self._callee_chain_return_type(inner_callee, inner, language))if type_name is None:return False, None@@ -1112,7 +1129,7 @@ class CallResolver(LanguageStrategiesMixin, ReceiverTypingMixin):return Nonereturn methods.get(inner.target_name)- def _inferred_chain_return_type(+ def _chain_inner_callee(self,file_path: str,call: CallSite,@@ -1120,7 +1137,7 @@ class CallResolver(LanguageStrategiesMixin, ReceiverTypingMixin):caller_id: str,language: str,) -> str | None:- """The head's type read off the repository symbol the inner call resolves to."""+ """The repository symbol the inner call of a chain resolves to."""if language not in self._return_type_chain_languages:# Admitted by its table alone; inferring from repository return# types is not admitted for this language.@@ -1148,6 +1165,13 @@ class CallResolver(LanguageStrategiesMixin, ReceiverTypingMixin):rep = self._overload_rep.get(callee_id)if rep is not None:callee_id = self._pick_overload(rep, inner.argument_count) or callee_id+ return callee_id++ def _callee_chain_return_type(+ self, callee_id: str | None, inner: CallReceiver, language: str+ ) -> str | None:+ if callee_id is None:+ return Nonereturn self._callee_return_type(callee_id, inner.argument_count, language)def _nested_receiver_call(@@ -1158,9 +1182,10 @@ class CallResolver(LanguageStrategiesMixin, ReceiverTypingMixin):A receiver records one hop, but the parser keeps every link of a chainas its own site on the chain's first line, so the hop below is readoff the site *inner* names. Only C# reads it; elsewhere an inner call- is still typed as if nothing were chained under it.+ is still typed as if nothing were chained under it. Go reads it too, so a+ builder chain types each link from the one before."""- if language != "csharp":+ if language not in ("csharp", "go"):return Nonesites = self._chained_sites.get(file_path)if sites is None:diff --git a/tests/unit/ingestion/test_return_type_chain_resolution.py b/tests/unit/ingestion/test_return_type_chain_resolution.pyindex f5006b4df..7ad475b7d 100644--- a/tests/unit/ingestion/test_return_type_chain_resolution.py+++ b/tests/unit/ingestion/test_return_type_chain_resolution.py@@ -463,6 +463,79 @@ def test_go_external_return_type_does_not_bind_a_same_named_repo_type(tmp_path:assert resolved == []+_GO_BUILDER = (+ "package m\n\ntype Builder struct{}\n\n"+ "func NewReport(a string) *Builder { return &Builder{} }\n"+ "func MakeValue() Builder { return Builder{} }\n"+ "func (b *Builder) WithPhase(p int) *Builder { return b }\n"+ "func (b Builder) WithClosest(s string) *Builder { return &b }\n"+ "func (b *Builder) Next() Builder { return *b }\n"+ "func (b *Builder) Build() int { return 1 }\n"+)+++def _go_builder_edges(tmp_path: Path, test_body: str, extra: dict[str, str] | None = None):+ files = {"m/builder.go": _GO_BUILDER, "m/builder_test.go": f"package m\n\n{test_body}\n"}+ files.update(extra or {})+ parsed: dict[str, ParsedFile] = {}+ for path, source in files.items():+ (tmp_path / path).parent.mkdir(parents=True, exist_ok=True)+ parsed.update(_parse(tmp_path, path, "go", source))+ calls = parsed["m/builder_test.go"].calls+ imports = {path: set() for path in parsed}+ resolved = CallResolver(parsed, imports, repo_path=str(tmp_path)).resolve_file(+ "m/builder_test.go", calls+ )+ return sorted(edge.callee_id for edge in resolved)+++def test_go_builder_chain_from_a_constructor_types_every_link(tmp_path: Path) -> None:+ """The type and its methods sit in another file of the package, as with a test file."""+ edges = _go_builder_edges(+ tmp_path,+ 'func TestX() {\n\tNewReport("a").\n\t\tWithPhase(3).\n\t\tWithClosest("x").Build()\n}',+ )+ assert edges == [+ "m/builder.go::Builder::Build",+ "m/builder.go::Builder::WithClosest",+ "m/builder.go::Builder::WithPhase",+ "m/builder.go::NewReport",+ ]+++def test_go_chain_from_a_method_returning_its_own_type(tmp_path: Path) -> None:+ edges = _go_builder_edges(+ tmp_path, "func TestX(b *Builder) {\n\tb.WithPhase(1).WithPhase(2).Build()\n}"+ )+ assert "m/builder.go::Builder::Build" in edges+ assert "m/builder.go::Builder::WithPhase" in edges+++def test_go_chain_types_through_value_and_pointer_receivers(tmp_path: Path) -> None:+ edges = _go_builder_edges(+ tmp_path, 'func TestX() {\n\tMakeValue().WithClosest("a").Next().Build()\n}'+ )+ assert "m/builder.go::Builder::WithClosest" in edges+ assert "m/builder.go::Builder::Next" in edges+ assert "m/builder.go::Builder::Build" in edges+++def test_go_builder_chain_on_an_external_type_still_refuses_the_bare_name(+ tmp_path: Path,+) -> None:+ edges = _go_builder_edges(+ tmp_path,+ "func TestX() {\n\tWrap().WithPhase(3).Build()\n}",+ {+ "m/ext.go": (+ 'package m\n\nimport "github.com/rs/zerolog"\n\n'+ "func Wrap() *zerolog.Event { return nil }\n"+ )+ },+ )+ assert "m/builder.go::Builder::WithPhase" not in edges++# A fluent C# API: every link returns an interface, and most links are# extension methods on it or on an interface it extends._FLUENT_SYNTAX = (