fix(parser): stop folding trailing comments into Kotlin import targets - #824
Open
songguohfut wants to merge 1 commit into
Open
fix(parser): stop folding trailing comments into Kotlin import targets#824songguohfut wants to merge 1 commit into
songguohfut wants to merge 1 commit into
Conversation
tree-sitter-kotlin folds any comment that follows the LAST import into that import_header node. `_extract_import` had no Kotlin branch, so it fell through to the generic `imports.append(text)` fallback and recorded the entire node text — import statement plus the trailing comment — as the module name. `_do_resolve_module` then turned that string into a filesystem path and called `os.stat` on it. A trailing KDoc block long enough to exceed the path limit raised `OSError: [Errno 63] File name too long`, which `_extract_imports` does not catch, so the whole file failed to parse. Every Kotlin file was affected: the last import always produced an IMPORTS_FROM edge whose target carried the trailing comment. Only files whose trailing comment was long enough to overflow the path limit actually crashed. Add a Kotlin branch that reads the `identifier` child (comments are separate `multiline_comment` siblings) and re-appends ".*" for wildcard imports. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Linked issue
No separate issue filed — the repro and root cause are self-contained below.
What & why
Kotlin files whose last import is followed by a long KDoc block fail to
parse entirely, with:
Note the "path" in that error is actually a whole source comment.
Root cause
Three layers:
tree-sitter-kotlinfolds any comment that follows the last import intothat
import_headernode. Minimal case — the node text forimport b.Cbelow is
'import b.C\n\n/** hi */':_extract_importhas branches for python, javascript, go, rust, c/cpp,java/csharp, solidity, scala, r, ruby, dart, verilog and julia — but not for
Kotlin. Kotlin therefore falls through to the generic fallback:
so the module name becomes the import statement plus the trailing comment.
_do_resolve_modulejoins that string into a filesystem path and callstarget.is_file()→os.stat. When the trailing comment is long enough thepath exceeds the OS limit and raises
OSError._extract_importshas notry/except, so the exception propagates and the whole file is dropped.Blast radius is wider than the crash
Every Kotlin file was affected — the last import always produced an
IMPORTS_FROMedge whose target carried the trailing comment. Only files whosecomment was long enough to overflow the path limit actually raised; the rest
silently emitted an unresolvable edge. In one 398-file Kotlin repo exactly one
file crashed, but every file with a trailing comment had a polluted edge.
Fix
Add a Kotlin branch to
_extract_importthat reads theidentifierchildrather than the node text. Comments are separate
multiline_commentsiblings,so this is immune to the folding.
wildcard_importsiblings get.*re-appended, and
import_aliasis ignored so the original module is recorded.The node structure this relies on:
import_headerimport b.Cimport,identifier('b.C')import b.C+ trailing commentimport,identifier('b.C'),multiline_commentimport b.C as Eimport,identifier('b.C'),import_alias('as E')import b.*import,identifier('b'),.,wildcard_import('*')14 lines, no behavior change for any other language.
How it was tested
New
tests/test_kotlin_imports.py(8 cases: plain, trailing block comment,trailing line comment, multi-import, alias, wildcard, wildcard + comment, and
the long-KDoc crash). All 8 fail on
mainand pass with this change — thelong-KDoc case reproduces the original
OSErrorexactly.Checklist
uv run pytest tests/ --tb=short -quv run ruff check code_review_graph/uv run mypy code_review_graph/ --ignore-missing-imports --no-strict-optional