Kotlin: class property with an annotation and no explicit type drops the whole file (UnboundLocalError: 'line' unbound)
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_kotlin1.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 communitiesControl — 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 communitiesRoot 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:
- On a second
graphify updaterun the warning is not repeated (files come from the incremental cache), so the loss is easy to miss. - Developers can set
GRAPHIFY_DEBUG=1to 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.
Source: Graphify-Labs/graphify