Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Internal Error when upgrading from 0.41.0 to 0.42.1 #1210

Closed
NikolayMetchev opened this issue Aug 11, 2021 · 7 comments · Fixed by #1305
Closed

Internal Error when upgrading from 0.41.0 to 0.42.1 #1210

NikolayMetchev opened this issue Aug 11, 2021 · 7 comments · Fixed by #1305
Labels

Comments

@NikolayMetchev
Copy link

NikolayMetchev commented Aug 11, 2021

After upgrading 2 files cause ktlint to fail with an Internal Error (indent)
SettlementsSpec.kt.zip
generators.kt.zip

Expected Behavior

the file should get formatted

Observed Behavior

SettlementsSpec.kt:0:0: Internal Error (indent). Please create a ticket at https://github.com/pinterest/ktlint/issues (if possible, provide the source code that triggered an error) (cannot be auto-corrected)

Steps to Reproduce

Format the attached file

Your Environment

  • Version of ktlint used: 0.42.1
  • Name and version (or code for custom task) of integration used (Gradle plugin, Maven plugin, command line, custom Gradle task):
    Gradle Task:
    task ktlintFmt(type: JavaExec) {
    mainClass.set("com.pinterest.ktlint.Main")
    classpath = configurations.ktlint
    args "-F", "src/**/*.kt"
    onlyIf {
    System.env['CI'] == null
    }
    }
  • Version of Gradle used (if applicable): 7.1.1
  • Operating System and version: MacOS Big Sur
  • Link to your project (if it's a public repository):
@shashachu shashachu added the bug label Aug 12, 2021
@shashachu
Copy link
Contributor

@NikolayMetchev are you able to test with the latest SNAPSHOT? I believe this commit fixes your issues #1203

@mhernand40
Copy link

FWIW, the following snippet also triggers an Internal Error in ktlint 0.42.1

package com.repro

import kotlin.properties.Delegates

class Repro {
    private var repro
        by Delegates.vetoable("") { _, old, new ->
            true
        }
}

The error message is:

Internal Error (indent). Please create a ticket at https://github.com/pinterest/ktlint/issues (if possible, provide the source code that triggered an error) (cannot be auto-corrected)

No issue if I re-format to

package com.repro

import kotlin.properties.Delegates

class Repro {
    private var repro by Delegates.vetoable("") { _, old, new ->
        true
    }
}

@paul-dingemans
Copy link
Collaborator

paul-dingemans commented Sep 11, 2021

@mhernand40 Tnx for your example and observation how reformat made a difference. The stacktrace of the error is as follows:

[DEBUG] Internal Error (indent)
com.pinterest.ktlint.core.RuleExecutionException: java.lang.IllegalArgumentException: Count 'n' must be non-negative, but was -4.
	at com.pinterest.ktlint.core.KtLint$format$1.invoke(KtLint.kt:406)
	at com.pinterest.ktlint.core.KtLint$format$1.invoke(KtLint.kt:384)
	at com.pinterest.ktlint.core.KtLint$visitor$2.invoke(KtLint.kt:320)
	at com.pinterest.ktlint.core.KtLint$visitor$2.invoke(KtLint.kt:302)
	at com.pinterest.ktlint.core.KtLint.format(KtLint.kt:384)
	at com.pinterest.ktlint.internal.FileUtilsKt.formatFile(FileUtils.kt:202)
	at com.pinterest.ktlint.KtlintCommandLine.process(Main.kt:366)
	at com.pinterest.ktlint.KtlintCommandLine.access$process(Main.kt:89)
	at com.pinterest.ktlint.KtlintCommandLine$lintFiles$3.invoke$lambda-0(Main.kt:289)
	at java.util.concurrent.FutureTask.run$$$capture(FutureTask.java:266)
	at java.util.concurrent.FutureTask.run(FutureTask.java)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
	at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.IllegalArgumentException: Count 'n' must be non-negative, but was -4.
	at kotlin.text.StringsKt__StringsJVMKt.repeat(StringsJVM.kt:790)
	at com.pinterest.ktlint.ruleset.standard.IndentationRule.visitWhiteSpace(IndentationRule.kt:1085)
	at com.pinterest.ktlint.ruleset.standard.IndentationRule.access$visitWhiteSpace(IndentationRule.kt:103)
	at com.pinterest.ktlint.ruleset.standard.IndentationRule$indent$2.invoke(IndentationRule.kt:600)
	at com.pinterest.ktlint.ruleset.standard.IndentationRule$indent$2.invoke(IndentationRule.kt:463)
	at com.pinterest.ktlint.core.ast.PackageKt.visit(package.kt:234)
	at com.pinterest.ktlint.core.ast.PackageKt.visit(package.kt:235)
	at com.pinterest.ktlint.core.ast.PackageKt.visit(package.kt:235)
	at com.pinterest.ktlint.core.ast.PackageKt.visit(package.kt:235)
	at com.pinterest.ktlint.ruleset.standard.IndentationRule.indent(IndentationRule.kt:462)
	at com.pinterest.ktlint.ruleset.standard.IndentationRule.visit(IndentationRule.kt:162)
	at com.pinterest.ktlint.core.KtLint$format$1.invoke(KtLint.kt:391)
	... 13 more

@t-kameyama Could you please have a look at this? This bug is somewhat related to your commit aa47c9e. Problem is that the RPAR leads to an unwanted decrement of the expectedIndent cause its value to go below zero.

@NikolayMetchev
Copy link
Author

I attempted to run head against my code but I couldn't. I think it is because we have the following enabled in our build:

enableFeaturePreview("TYPESAFE_PROJECT_ACCESSORS")

Which produces the following error when it tries to build the ktlint project:

 ./gradlew --include-build ../ktlint ktlintFormat
Type-safe dependency accessors is an incubating feature.
Type-safe project accessors is an incubating feature.

FAILURE: Build failed with an exception.

* What went wrong:
org.gradle.api.internal.catalog.GeneratedClassCompilationException: Unable to compile generated sources:
  - File RootProjectAccessor.java, line: 29, method getKtlint() is already defined in class org.gradle.accessors.dm.RootProjectAccessor
> Unable to compile generated sources:
    - File RootProjectAccessor.java, line: 29, method getKtlint() is already defined in class org.gradle.accessors.dm.RootProjectAccessor

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 2s

@BraisGabin
Copy link

I found other way to reproduce this issue:

object ApplicationComponentFactory : ApplicationComponent.Factory
by DaggerApplicationComponent.factory()

Testing with 0.43.1

if I reformat the code like this ktlint is happy and without errors:

object ApplicationComponentFactory :
    ApplicationComponent.Factory by DaggerApplicationComponent.factory()

@bbrinx
Copy link

bbrinx commented Dec 2, 2021

Is there any update on this? Running into the same issue with the latest version 0.43.2

@paul-dingemans
Copy link
Collaborator

@BraisGabin I really love it when somebody has such a short example to reproduce an issue ;-) I could reproduce and solve it.

Code sample:

object ApplicationComponentFactory : ApplicationComponent.Factory
by DaggerApplicationComponent.factory()

results in following stack trace:

Caused by: java.lang.IllegalArgumentException: Count 'n' must be non-negative, but was -4.
	at kotlin.text.StringsKt__StringsJVMKt.repeat(StringsJVM.kt:795)
	at com.pinterest.ktlint.ruleset.standard.IndentationRule.visitWhiteSpace(IndentationRule.kt:1049)
	at com.pinterest.ktlint.ruleset.standard.IndentationRule.access$visitWhiteSpace(IndentationRule.kt:104)
	at com.pinterest.ktlint.ruleset.standard.IndentationRule$indent$2.invoke(IndentationRule.kt:637)
	at com.pinterest.ktlint.ruleset.standard.IndentationRule$indent$2.invoke(IndentationRule.kt:499)
	at com.pinterest.ktlint.core.ast.PackageKt.visit(package.kt:234)
	at com.pinterest.ktlint.core.ast.PackageKt.visit(package.kt:235)
	at com.pinterest.ktlint.ruleset.standard.IndentationRule.indent(IndentationRule.kt:499)
	at com.pinterest.ktlint.ruleset.standard.IndentationRule.visit(IndentationRule.kt:163)
	at com.pinterest.ktlint.core.KtLint$format$1.invoke(KtLint.kt:389)
	... 12 more

paul-dingemans pushed a commit to paul-dingemans/ktlint that referenced this issue Dec 11, 2021
Forces the BY keyword in a super type call entry to be indented when preceded by a new line

Closes pinterest#1210
shashachu pushed a commit that referenced this issue Dec 20, 2021
* Fix indentation of delegated super type call entry

Forces the BY keyword in a super type call entry to be indented when preceded by a new line

Closes #1210

* Fix negative indent level after formatting a supertype delegate

Co-authored-by: Paul Dingemans <pdingemans@bol.com>
paul-dingemans pushed a commit to paul-dingemans/ktlint that referenced this issue Dec 22, 2021
Original fix for pinterest#1210 solved the runtime exception during formatting but also
reformatted some delegate by constructs which are conflicting with the default
IntelliJ formatting.
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants