openrewrite / rewrite-kotlin

Work-in-progress implementation of Kotlin language support for OpenRewrite.
Apache License 2.0
38 stars 11 forks source link

Parser issue: StringIndexOutOfBounds #564

Closed traceyyoshima closed 5 months ago

traceyyoshima commented 6 months ago
"java.lang.StringIndexOutOfBoundsException: Range [1, 0) out of bounds for length 1
  java.base/jdk.internal.util.Preconditions$1.apply(Preconditions.java:55)
  java.base/jdk.internal.util.Preconditions$1.apply(Preconditions.java:52)
  java.base/jdk.internal.util.Preconditions$4.apply(Preconditions.java:213)
  java.base/jdk.internal.util.Preconditions$4.apply(Preconditions.java:210)
  java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:98)
  java.base/jdk.internal.util.Preconditions.outOfBoundsCheckFromToIndex(Preconditions.java:112)
  java.base/jdk.internal.util.Preconditions.checkFromToIndex(Preconditions.java:349)
  java.base/java.lang.String.checkBoundsBeginEnd(String.java:4861)
  ..."

There is no information in the parse error result, this occurs on hypercube1024/firefly

kunli2 commented 5 months ago

I saw a similar StringIndexOutOfBoundsException from this test

    @Test
    void scalaImport() {
        rewriteRun(
          kotlin(
            """
              import my.org.`x$`

              fun main() {

              }
              """
          )
        );
    }
Caused by: java.lang.StringIndexOutOfBoundsException: begin 1, end 0, length 1
    at java.base/java.lang.String.checkBoundsBeginEnd(String.java:4606)
    at java.base/java.lang.String.substring(String.java:2709)
    at org.openrewrite.java.tree.TypeTree.build(TypeTree.java:77)
    at org.openrewrite.kotlin.internal.KotlinTreeParserVisitor.visitImportDirective(KotlinTreeParserVisitor.java:2540)
    at org.openrewrite.kotlin.internal.KotlinTreeParserVisitor.visitImportDirective(KotlinTreeParserVisitor.java:75)
    at org.jetbrains.kotlin.psi.KtImportDirective.accept(KtImportDirective.java:45)
    at org.openrewrite.kotlin.internal.KotlinTreeParserVisitor.visitKtFile(KotlinTreeParserVisitor.java:1676)
    at org.openrewrite.kotlin.internal.KotlinTreeParserVisitor.parse(KotlinTreeParserVisitor.java:110)
    at org.openrewrite.kotlin.KotlinParser.lambda$parseInputs$4(KotlinParser.java:187)
    ... 18 more

maybe this is related to https://github.com/openrewrite/rewrite-kotlin/issues/559

knutwannheden commented 5 months ago

The problematic code in hypercube1024/firefly are some import statements (like https://github.com/hypercube1024/firefly/blob/628cfb219a48bdfa104c180d3f96a4690ec46ca4/firefly-example/src/main/kotlin/com/fireflysource/example/HttpServerPathParamDemo.kt#L3):

import com.fireflysource.`$`
knutwannheden commented 5 months ago

This is in fact the same as in

import my.org.`x$`
knutwannheden commented 5 months ago

The issue is that $ is being used as one of the delimiters when scanning the string: https://github.com/openrewrite/rewrite/blob/bee6f7634f5d5f5bdca0fa500009666cd8e5f50d/rewrite-java/src/main/java/org/openrewrite/java/tree/TypeTree.java#L39