Skip to content

Kotlin: class property with an annotation and no explicit type drops the whole file (UnboundLocalError: 'line' unbound) #3884

Closed

Description

@SpaMer93

Summary

A Kotlin class property that carries an annotation and no explicit type crashes the Kotlin extractor, and _safe_extract then drops the entire file from the graph (0 nodes, 0 edges for that file). It is silent apart from a single warning: skipped … line on stderr.

Environment

  • graphify 0.9.70 (latest on PyPI at time of writing), installed with uv tool install graphifyy
  • tree_sitter_kotlin 1.1.0
  • Python 3.11.16, Linux x86-64 (Ubuntu, kernel 6.8)
  • Trigger: graphify update . (the AST-only path; no LLM needed to reproduce)

Minimal reproduction

Repro.kt:

class Repro {
    @Volatile
    var counter = 0
}
$ GRAPHIFY_DEBUG=1 graphify update .
Re-extracting code files in . (no LLM needed)...
Traceback (most recent call last):
  File ".../graphify/extract.py", line 192, in _safe_extract
    return extractor(path)
  File ".../graphify/extract.py", line 2852, in extract_kotlin
    return _extract_generic(path, _KOTLIN_CONFIG)
  File ".../graphify/extractors/engine.py", line 5813, in _extract_generic
    walk(root)
  File ".../graphify/extractors/engine.py", line 5811, in walk
    walk(child, parent_class_nid=None)
  File ".../graphify/extractors/engine.py", line 4618, in walk
    walk(child, parent_class_nid=class_nid)
  File ".../graphify/extractors/engine.py", line 4870, in walk
    target_nid = ensure_named_node(anno_name, line)
                                   ^^^^^^^^^
UnboundLocalError: cannot access local variable 'line' where it is not associated with a value
  warning: skipped /tmp/gf-min/Repro.kt (UnboundLocalError: cannot access local variable 'line' where it is not associated with a value)
[graphify watch] Rebuilt: 0 nodes, 0 edges, 0 communities

Control — the same property without the annotation extracts fine:

class Control {
    var counter = 0
}
$ GRAPHIFY_DEBUG=1 graphify update .
[graphify watch] Rebuilt: 2 nodes, 1 edges, 1 communities

Root cause

engine.py, in the property_declaration branch for tree_sitter_kotlin (~line 4853): line is assigned only inside if type_node is not None:, but it is then used unconditionally by the annotation loop further down:

if (config.ts_module == "tree_sitter_kotlin" and t == "property_declaration"):
    if parent_class_nid:
        type_node = _kotlin_property_type_node(node)
        if type_node is not None:
            line = node.start_point[0] + 1          # ← assigned here only
            refs: list[tuple[str, str]] = []
            ...
        annotation_targets: set[str] = set()
        for anno_name, anno_raw in _kotlin_annotation_names(node, source):
            target_nid = ensure_named_node(anno_name, line)   # ← may be unbound
            if target_nid != parent_class_nid and target_nid not in annotation_targets:
                add_edge(parent_class_nid, target_nid, "references", line, context="attribute")

When the property has no explicit type node (val x = 0, var counter = 0, i.e. inferred type) and at least one annotation, line is never bound.

A minimal fix is to hoist the assignment above the if type_node is not None: branch (or compute it inside the annotation loop), e.g.:

if parent_class_nid:
    line = node.start_point[0] + 1
    type_node = _kotlin_property_type_node(node)
    ...

Impact

The failure is not limited to the property: the whole file is discarded, so every symbol, call edge and class in it disappears from graph.json without further notice. Downstream, graphify query, path, affected and explain then return empty or misleading results for that file's symbols — a wrong "nothing references this" is easy to conclude.

Real case: in a 70-file Kotlin Android project, 4 files were dropped this way (FaceView.kt, FaceRuntime.kt, AndroidAudio.kt, AudioStreams.kt) — i.e. the whole Android layer of the app is absent from the graph while the rest of the code is present.

Two secondary observations worth noting:

  1. On a second graphify update run the warning is not repeated (files come from the incremental cache), so the loss is easy to miss.
  2. Developers can set GRAPHIFY_DEBUG=1 to get the traceback — that was the only way to find this, since the default path prints just one line.

Happy to test a patch if that helps.

Activity

  1. github-actions commented on Sep 27, 2026

    @github-actions

    Thanks for opening this issue, @SpaMer93. A maintainer will take a look soon.

    If you would like to discuss it in real time, come say hi on our Discord server. For longer-form questions and ideas there is also GitHub Discussions.

    To help us triage, please make sure the report includes what you expected, what actually happened, and the steps (and a small sample) to reproduce it.

  2. harshaygadekar commented on Sep 28, 2026

    @harshaygadekar
    Contributor

    Confirmed and reproduced.

    Root Cause

    In graphify/extractors/engine.py (lines 4843–4868):
    line = node.start_point[0] + 1 is assigned inside if type_node is not None:. When a class property has an annotation but no explicit type annotation (e.g. @Volatile var counter = 0), type_node is None and line remains unbound when ensure_named_node(anno_name, line) is called in the subsequent loop, raising UnboundLocalError. _safe_extract then catches the error and drops the entire file from the graph.

    Fix

    Hoisting line = node.start_point[0] + 1 out of if type_node is not None: to the top of if parent_class_nid: resolves it cleanly (matching the swift extractor)

    I have a branch with the fix and regressio test ready and will open a PR shortly.

  3. added a commit that references this issue on Sep 29, 2026
  4. added a commit that references this issue on Sep 29, 2026
    af153f8
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