Skip to content

Kotlin: bundled grammar rejects one-line type bodies (class C { val v = 1 }) — 21/541 files partially extracted, exit 0, no warning #2551

Closed

Description

@thomasrengot-hub

Summary

class C { val v = 1 } — a type body whose last declaration is not followed by a
newline or ; — is rejected by the bundled tree-sitter-kotlin 1.1.0 grammar.
It is valid Kotlin, and the multi-line spelling of the same code parses fine.

On a 541-file Kotlin codebase (Android app + Wear module), 21 files (3.9%) fail
to parse
, and 60 of the repo's 1183 top-level functions (5.1%) are missing
from graph.json — including 11 of the 65 screens reachable from the navigation
entry point, which is what made me look.

Nothing is reported. Exit code 0, no warning line, and the summary prints the
reduced node count as a plain result. The existing zero nodes warning does not
cover it: these files each yield 12–21 nodes, so they never qualify as empty. I
spent a while suspecting the call resolver before thinking to run the grammar
over the files by hand.

Same shape as #2520 (.luau constructs silently truncate a file, exit 0, no
warning), different language and trigger.

Reproduction — the parse failure

graphify 0.9.36. Grammar-level, so it needs no extraction run:

from tree_sitter import Language, Parser
import tree_sitter_kotlin as tsk
p = Parser(Language(tsk.language()))

def check(code):
    print("ERROR" if p.parse(code.encode()).root_node.has_error else "ok   ", repr(code))

check("package p\nclass C { val v = 1 }\n")        # ERROR  <-- valid Kotlin
check("package p\nclass C { val v = 1; }\n")       # ok
check("package p\nclass C {\n    val v = 1\n}\n")  # ok
check("package p\nclass C { }\n")                  # ok    (empty body)
check("package p\nfun f() { val v = 1 }\n")        # ok    (function body unaffected)

The trigger is a missing statement terminator before the closing brace of a
type body. It applies to class, object and interface, to properties and
to functions, and to the very common one-line companion:

class VM { fun f() = 1 }                               // ERROR
object O { val v = 1 }                                 // ERROR
interface I { fun f() }                                // ERROR
class VM {
    fun load() {}
    private companion object { const val LIMIT = 20 }   // ERROR (this one line)
}

Function bodies are unaffected — fun f() { println(1) } parses — which is why
this survives review: the same brace style is fine one scope out.

What it costs on a real corpus

Kotlin files scanned 541
files where root_node.has_error 21 (3.9%)
top-level functions declared (excluding extension functions) 1183
top-level functions absent from graph.json 60 (5.1%)
files those 60 live in 10 — every one of them is inside the 21 above

In the files I inspected, the ERROR node covers the region the missing
declarations are in
:

KbHealthScreen.kt        ERROR at L119 spanning `KbHealthScreen`   -> that one function missing
KbInsightsScreen.kt      ERROR at L124                              -> that one function missing
ThemesScreen.kt          ERROR at L251 spanning `ThemesScreen`      -> 5 functions missing
LinkedInIdeasScreen.kt   ERROR L176-736                             -> 7 functions missing
SynthesesScreen.kt       ERROR L269-958                             -> 9 functions missing
CortexScreen.kt          ERROR L1-1175 (whole file)                 -> 19 functions missing;
                                                                       nothing extracted past L165

Damage varies with where the parser gives up: some files lose exactly one
function, others stop mid-file and lose everything after.

Honest limit of this report

A parse error does not always cost nodes — the extractor often recovers. I
tried five minimal files carrying the bad construct (companion one-liner, nested
sealed interface, Compose-shaped bodies): all of them report has_error and yet
extract every declaration. So I can show the parse failure minimally, and I can
show the correlation and the overlapping spans on the real corpus, but I do not
have a minimal end-to-end file that loses a node. I did not want to dress the
correlation up as a reduction.

What does reproduce end-to-end, reliably, is a real affected file on its own — a
single-file directory is enough: graphify extract <dir> --code-only returns the
ViewModel class and its members, but not the composable declared after it. So the
loss does not need a large corpus to show up; it needs whatever makes the parser's
ERROR span the declaration rather than stop short of it, and that is the part I
could not reduce.

What I am asking for

Warn on a partial parse. Whatever happens to the grammar, extract already
has root_node.has_error in hand and can say so — that alone would have saved
the investigation:

warning: 21 file(s) had syntax errors and may be partially extracted:
  app/.../KbHealthScreen.kt (first error at line 119), ... (+20 more)

Cheap check, and the existing zero nodes warning already sets the precedent and
the phrasing.

The grammar bug itself is upstream at tree-sitter-grammars/tree-sitter-kotlin
— 1.1.0 is the newest on PyPI, so there is no version to bump to. I am filing it
here because graphify is where it is observable and where the silence is fixable.
Happy to file upstream as well and link both ways.

If you want a stopgap before upstream moves, re-parsing a failed file after
inserting ; before the closing brace of one-line type bodies would recover
these files — but I would not push for that over the warning. The silent part is
what actually cost time; a graph that is wrong and says so is a much smaller
problem.

Environment

graphify 0.9.36 · tree-sitter 0.25.2 · tree-sitter-kotlin 1.1.0 · Python 3.11 ·
Linux · extract --code-only (AST only, no LLM backend).

Activity

  1. safishamsi commented on Aug 8, 2026

    @safishamsi
    Member

    Fixed in v0.9.37. A Kotlin file the bundled grammar cannot fully parse (such as a one-line class C { val x }) now emits a partial-parse warning naming the file and first error line instead of silently extracting nothing, and declarations recovered inside an error span keep their enclosing class. The one-line-body parse limitation itself is upstream in tree-sitter-kotlin. Thanks.

  2. added a commit that references this issue on Aug 11, 2026
  3. added a commit that references this issue on Aug 19, 2026
    4f104e6
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