Skip to content

Kotlin: calls in a class property initializer are never collected — only function bodies are walked (DI-container shape invisible) #2565

Closed

Description

@kskchaitanya1993

Environment

  • graphify 0.9.37 (PyPI graphifyy), macOS 15, Python 3.14
  • graphify extract <dir> --code-only --no-cluster — local AST only, no LLM, no API key

Summary

Calls written in a class property initializer are never collected. The call pass appears to
descend only into function bodies, so class C { val x = Foo(1) } produces no edge from C to
Foo at all — no calls, no references, nothing.

This is not about constructors and not about packages; both isolate out below. It is purely the
position of the call.

It matters more than it looks, because a property initializer is the standard shape for
dependency-injection roots and for Kotlin singletons/containers. On a multi-module KMP repo the
container that constructs ~15 repositories has zero outgoing edges to any of them, so
affected on any of those repositories cannot reach the object that owns them.

Reproduction

Four files, one package, no build system.

// src/Lib.kt
package demo.repo

class Settings(val n: Int)
fun helper(x: Int) = x
// src/PropInit.kt
package demo.repo

class PropInit {
    val a = Settings(1)     // constructor call in a property initializer
    val b = helper(2)       // plain function call in a property initializer
}
// src/FnBody.kt
package demo.repo

class FnBody {
    fun go() {
        val a = Settings(3) // identical call, inside a function body
        val b = helper(4)
    }
}
$ graphify extract . --code-only --no-cluster

$ graphify affected "Settings" --depth 3
- .go() [calls] src/FnBody.kt:L5

$ graphify affected "helper()" --depth 3
- .go() [calls] src/FnBody.kt:L6

FnBody resolves for both call kinds. PropInit appears in neither result, for either kind.

Isolation

Call site Constructor Settings(n) Function helper(n)
Function body (fun go() { … }) calls ✅ calls ✅
Expression body (fun make() = Settings(n)) calls ✅ —
Class property initializer (val x = …) none ❌ none ❌

So constructors are fine, and same-package resolution is fine. Only the property-initializer
position fails.

One adjacent detail worth noting: when the enclosing function carries an explicit return type,
the edge that appears comes from the type annotation, not from the call.

class Annotated { fun make(): Settings = Settings(1) }   // -> references  (from the annotation)
class Inferred  { fun make()           = Settings(2) }   // -> calls       (from the call)

Both call Settings(…) identically, and they produce different relations. That suggests the
annotation path and the call path are independent, and only the latter is missing for property
initializers.

Why this isn't #2550 or #2526

Effect at scale

On a private multi-module Kotlin/Swift KMP repo (0.9.37, imports fix included): the DI container
class has 20 outgoing edges and none to the repository it constructs on the line
val settings = SettingsRepository(keyValueStore, analytics, blobStore). Reverse traversal from
that repository returns 18 files and omits its own owner. The file parses clean — no ERROR nodes
from the bundled grammar — so this is not the parse issue from #2551.

Suggestion

Whatever walk collects call_expression for function_declaration bodies likely needs to run for
property_declaration initializers too (and probably class_body init blocks), attributing the
edge to the enclosing class rather than to an enclosing function. Happy to test a patch against
both the repro above and the larger repo.


Investigated and written with Claude Code (Claude Opus 5).

Activity

  1. safishamsi commented on Aug 9, 2026

    @safishamsi
    Member

    Fixed in v0.9.38. Kotlin property initializers now seed call extraction: a class property (val repo = createRepo()), a by lazy { ... } delegate, a companion-object property, and a top-level property initializer produce calls edges attributed to the enclosing class (or file), including fully-qualified calls (composing with #2550). A plain literal initializer produces no edge. Thanks for the report.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions