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

adding disabledRules=["experimental:argument-list-wrapping"] triggers IndexOutOfBoundsException #1045

Closed
phileo opened this issue Jan 3, 2021 · 5 comments

Comments

@phileo
Copy link

phileo commented Jan 3, 2021

Steps to Reproduce

brew install ktlint
git clone https://github.com/ricknout/android-mdc-theming
cd app/src/main/java

then run the following from command line:
ktlint -F --experimental --android --verbose --debug --disabled_rules=final-newline,parameter-list-wrapping,argument-list-wrapping,no-wildcard-imports,max-line-length,experimental:package-name

@Tapchicoma Tapchicoma added the bug label Jan 14, 2021
@Tapchicoma
Copy link
Collaborator

I could not reproduce to get exactly IndexOutOfBoundsException, but I got another one:

[DEBUG] Checking android-mdc-theming/app/src/main/java/com/ricknout/mdctheming/theming/ThemingActivity.kt
[DEBUG] Internal Error (experimental:multiline-if-else)
com.pinterest.ktlint.core.RuleExecutionException: java.lang.ClassCastException: org.jetbrains.kotlin.com.intellij.psi.impl.source.tree.CompositeElement cannot be cast to org.jetbrains.kotlin.com.intellij.psi.impl.source.tree.LeafPsiElement
        at com.pinterest.ktlint.core.KtLint$format$1.invoke(KtLint.kt:320)
        at com.pinterest.ktlint.core.KtLint$format$1.invoke(KtLint.kt:27)
        at com.pinterest.ktlint.core.KtLint$visitor$2$2.invoke(KtLint.kt:247)
        at com.pinterest.ktlint.core.KtLint$visitor$2$2.invoke(KtLint.kt:27)
        at com.pinterest.ktlint.core.ast.PackageKt.visit(package.kt:225)
        at com.pinterest.ktlint.core.ast.PackageKt.visit(package.kt:226)
        at com.pinterest.ktlint.core.ast.PackageKt.visit(package.kt:226)
        at com.pinterest.ktlint.core.ast.PackageKt.visit(package.kt:226)
        at com.pinterest.ktlint.core.ast.PackageKt.visit(package.kt:226)
        at com.pinterest.ktlint.core.ast.PackageKt.visit(package.kt:226)
        at com.pinterest.ktlint.core.ast.PackageKt.visit(package.kt:226)
        at com.pinterest.ktlint.core.ast.PackageKt.visit(package.kt:226)
        at com.pinterest.ktlint.core.ast.PackageKt.visit(package.kt:226)
        at com.pinterest.ktlint.core.ast.PackageKt.visit(package.kt:226)
        at com.pinterest.ktlint.core.ast.PackageKt.visit(package.kt:226)
        at com.pinterest.ktlint.core.ast.PackageKt.visit(package.kt:226)
        at com.pinterest.ktlint.core.ast.PackageKt.visit(package.kt:226)
        at com.pinterest.ktlint.core.ast.PackageKt.visit(package.kt:226)
        at com.pinterest.ktlint.core.KtLint$visitor$2.invoke(KtLint.kt:246)
        at com.pinterest.ktlint.core.KtLint$visitor$2.invoke(KtLint.kt:27)
        at com.pinterest.ktlint.core.KtLint.format(KtLint.kt:299)
        at com.pinterest.ktlint.internal.FileUtilsKt.formatFile(FileUtils.kt:99)
        at com.pinterest.ktlint.KtlintCommandLine.process(Main.kt:358)
        at com.pinterest.ktlint.KtlintCommandLine.access$process(Main.kt:124)
        at com.pinterest.ktlint.KtlintCommandLine$lintFiles$2$1.call(Main.kt:281)
        at com.pinterest.ktlint.KtlintCommandLine$lintFiles$2$1.call(Main.kt:124)
        at java.util.concurrent.FutureTask.run(FutureTask.java:266)
        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.ClassCastException: org.jetbrains.kotlin.com.intellij.psi.impl.source.tree.CompositeElement cannot be cast to org.jetbrains.kotlin.com.intellij.psi.impl.source.tree.LeafPsiElement
        at com.pinterest.ktlint.ruleset.experimental.MultiLineIfElseRule.autocorrect(MultiLineIfElseRule.kt:53)
        at com.pinterest.ktlint.ruleset.experimental.MultiLineIfElseRule.visit(MultiLineIfElseRule.kt:39)
        at com.pinterest.ktlint.core.KtLint$format$1.invoke(KtLint.kt:306)
        ... 29 more
android-mdc-theming/app/src/main/java/com/ricknout/mdctheming/bottomappbar/BottomAppBarActivity.kt:0:0: Internal Error (experimental:multiline-if-else). 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) ()

@phileo is this actual exception you see on running KtLint?

@phileo
Copy link
Author

phileo commented Jan 16, 2021

@Tapchicoma I retried ktlint on that project, and you're right, it does not repro the IndexOutOfBoundsException

Attached is a Test Class that I crafted which will reproduce the IndexOutOfBoundsException if you run with the same command line parameters as described above. It appears the max-line-length parameter influences this crash, because if you shorten some of the lines, the crash goes away

TestClass.zip

@paul-dingemans
Copy link
Collaborator

I can reproduce the issue with the TestClass example. In this case the "-a" flag is important as the error is not thrown when running without that flag.

Stacktrace:

com.pinterest.ktlint.core.RuleExecutionException: java.lang.IndexOutOfBoundsException: Wrong offset: 4528. Should be in range: [0, 4144]
	at com.pinterest.ktlint.core.KtLint$format$2.invoke(KtLint.kt:346)
	at com.pinterest.ktlint.core.KtLint$format$2.invoke(KtLint.kt:27)
	at com.pinterest.ktlint.core.KtLint$visitor$2$1.invoke(KtLint.kt:241)
	at com.pinterest.ktlint.core.KtLint$visitor$2$1.invoke(KtLint.kt:27)
	at com.pinterest.ktlint.core.ast.PackageKt.visit(package.kt:225)
	at com.pinterest.ktlint.core.ast.PackageKt.visit(package.kt:226)
	at com.pinterest.ktlint.core.ast.PackageKt.visit(package.kt:226)
	at com.pinterest.ktlint.core.ast.PackageKt.visit(package.kt:226)
	at com.pinterest.ktlint.core.ast.PackageKt.visit(package.kt:226)
	at com.pinterest.ktlint.core.ast.PackageKt.visit(package.kt:226)
	at com.pinterest.ktlint.core.ast.PackageKt.visit(package.kt:226)
	at com.pinterest.ktlint.core.ast.PackageKt.visit(package.kt:226)
	at com.pinterest.ktlint.core.ast.PackageKt.visit(package.kt:226)
	at com.pinterest.ktlint.core.ast.PackageKt.visit(package.kt:226)
	at com.pinterest.ktlint.core.ast.PackageKt.visit(package.kt:226)
	at com.pinterest.ktlint.core.ast.PackageKt.visit(package.kt:226)
	at com.pinterest.ktlint.core.KtLint$visitor$2.invoke(KtLint.kt:239)
	at com.pinterest.ktlint.core.KtLint$visitor$2.invoke(KtLint.kt:27)
	at com.pinterest.ktlint.core.KtLint.format(KtLint.kt:326)
	at com.pinterest.ktlint.internal.FileUtilsKt.formatFile(FileUtils.kt:99)
	at com.pinterest.ktlint.KtlintCommandLine.process(Main.kt:358)
	at com.pinterest.ktlint.KtlintCommandLine.access$process(Main.kt:124)
	at com.pinterest.ktlint.KtlintCommandLine$lintFiles$2$1.call(Main.kt:281)
	at com.pinterest.ktlint.KtlintCommandLine$lintFiles$2$1.call(Main.kt:124)
	at java.util.concurrent.FutureTask.run(FutureTask.java:266)
	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.IndexOutOfBoundsException: Wrong offset: 4528. Should be in range: [0, 4144]
	at org.jetbrains.kotlin.com.intellij.openapi.editor.impl.LineSet.findLineIndex(LineSet.java:178)
	at org.jetbrains.kotlin.com.intellij.openapi.editor.impl.DocumentImpl.getLineNumber(DocumentImpl.java:991)
	at com.pinterest.ktlint.core.ast.PackageKt.lineNumber(package.kt:236)
	at com.pinterest.ktlint.ruleset.experimental.ArgumentListWrappingRule.isOnSameLineAsControlFlowKeyword(ArgumentListWrappingRule.kt:241)
	at com.pinterest.ktlint.ruleset.experimental.ArgumentListWrappingRule.visit(ArgumentListWrappingRule.kt:117)
	at com.pinterest.ktlint.core.KtLint$format$2.invoke(KtLint.kt:333)
	... 27 more

@paul-dingemans
Copy link
Collaborator

In case my .editorconfig has an indent_size not equal to 2 (this is de default indent used in the example) then lint results in following messages:

Loaded .editorconfig: [indent_size: 4, disabled_rules: final-newline,parameter-list-wrapping,argument-list-wrapping,no-wildcard-imports,max-line-length,experimental:package-name, tab_width: 4]
/Users/pdingemans/git/non-opex/ktlint-example/issue1045.kt:20:1: Unexpected indentation (2) (should be 4) (indent)
/Users/pdingemans/git/non-opex/ktlint-example/issue1045.kt:21:1: Unexpected indentation (4) (should be 8) (indent)
/Users/pdingemans/git/non-opex/ktlint-example/issue1045.kt:22:1: Unexpected indentation (4) (should be 8) (indent)
/Users/pdingemans/git/non-opex/ktlint-example/issue1045.kt:23:1: Unexpected indentation (6) (should be 12) (indent)
/Users/pdingemans/git/non-opex/ktlint-example/issue1045.kt:24:1: Unexpected indentation (6) (should be 12) (indent)
/Users/pdingemans/git/non-opex/ktlint-example/issue1045.kt:25:1: Unexpected indentation (6) (should be 12) (indent)
/Users/pdingemans/git/non-opex/ktlint-example/issue1045.kt:26:1: Unexpected indentation (6) (should be 12) (indent)
/Users/pdingemans/git/non-opex/ktlint-example/issue1045.kt:28:1: Unexpected indentation (6) (should be 12) (indent)
/Users/pdingemans/git/non-opex/ktlint-example/issue1045.kt:29:1: Unexpected indentation (4) (should be 8) (indent)
/Users/pdingemans/git/non-opex/ktlint-example/issue1045.kt:30:1: Unexpected indentation (2) (should be 4) (indent)
/Users/pdingemans/git/non-opex/ktlint-example/issue1045.kt:34:1: Unexpected indentation (2) (should be 4) (indent)
/Users/pdingemans/git/non-opex/ktlint-example/issue1045.kt:37:1: Unexpected indentation (2) (should be 4) (indent)
/Users/pdingemans/git/non-opex/ktlint-example/issue1045.kt:38:1: Unexpected indentation (4) (should be 8) (indent)
/Users/pdingemans/git/non-opex/ktlint-example/issue1045.kt:39:1: Unexpected indentation (6) (should be 12) (indent)
/Users/pdingemans/git/non-opex/ktlint-example/issue1045.kt:40:1: Unexpected indentation (8) (should be 16) (indent)
/Users/pdingemans/git/non-opex/ktlint-example/issue1045.kt:41:1: Unexpected indentation (10) (should be 20) (indent)
/Users/pdingemans/git/non-opex/ktlint-example/issue1045.kt:42:1: Unexpected indentation (8) (should be 16) (indent)
/Users/pdingemans/git/non-opex/ktlint-example/issue1045.kt:43:1: Unexpected indentation (6) (should be 12) (indent)
/Users/pdingemans/git/non-opex/ktlint-example/issue1045.kt:44:1: Unexpected indentation (6) (should be 12) (indent)
/Users/pdingemans/git/non-opex/ktlint-example/issue1045.kt:45:1: Unexpected indentation (8) (should be 16) (indent)
/Users/pdingemans/git/non-opex/ktlint-example/issue1045.kt:46:1: Unexpected indentation (6) (should be 12) (indent)
/Users/pdingemans/git/non-opex/ktlint-example/issue1045.kt:47:1: Unexpected indentation (4) (should be 8) (indent)
/Users/pdingemans/git/non-opex/ktlint-example/issue1045.kt:48:1: Unexpected indentation (2) (should be 4) (indent)
/Users/pdingemans/git/non-opex/ktlint-example/issue1045.kt:52:1: Unexpected indentation (2) (should be 4) (indent)
/Users/pdingemans/git/non-opex/ktlint-example/issue1045.kt:54:1: Unexpected indentation (2) (should be 4) (indent)
/Users/pdingemans/git/non-opex/ktlint-example/issue1045.kt:56:1: Unexpected indentation (4) (should be 8) (indent)
/Users/pdingemans/git/non-opex/ktlint-example/issue1045.kt:57:1: Unexpected indentation (6) (should be 12) (indent)
/Users/pdingemans/git/non-opex/ktlint-example/issue1045.kt:58:1: Unexpected indentation (8) (should be 16) (indent)
/Users/pdingemans/git/non-opex/ktlint-example/issue1045.kt:59:1: Unexpected indentation (8) (should be 16) (indent)
/Users/pdingemans/git/non-opex/ktlint-example/issue1045.kt:60:1: Unexpected indentation (6) (should be 12) (indent)
/Users/pdingemans/git/non-opex/ktlint-example/issue1045.kt:61:1: Unexpected indentation (8) (should be 16) (indent)
/Users/pdingemans/git/non-opex/ktlint-example/issue1045.kt:62:1: Unexpected indentation (6) (should be 12) (indent)
/Users/pdingemans/git/non-opex/ktlint-example/issue1045.kt:63:1: Unexpected indentation (2) (should be 4) (indent)
/Users/pdingemans/git/non-opex/ktlint-example/issue1045.kt:65:1: Unexpected indentation (2) (should be 4) (indent)
/Users/pdingemans/git/non-opex/ktlint-example/issue1045.kt:66:1: Unexpected indentation (4) (should be 8) (indent)
/Users/pdingemans/git/non-opex/ktlint-example/issue1045.kt:67:1: Unexpected indentation (6) (should be 12) (indent)
/Users/pdingemans/git/non-opex/ktlint-example/issue1045.kt:68:1: Unexpected indentation (8) (should be 16) (indent)
/Users/pdingemans/git/non-opex/ktlint-example/issue1045.kt:69:1: Unexpected indentation (10) (should be 20) (indent)
/Users/pdingemans/git/non-opex/ktlint-example/issue1045.kt:70:1: Unexpected indentation (8) (should be 16) (indent)
/Users/pdingemans/git/non-opex/ktlint-example/issue1045.kt:71:1: Unexpected indentation (6) (should be 12) (indent)
/Users/pdingemans/git/non-opex/ktlint-example/issue1045.kt:72:1: Unexpected indentation (6) (should be 12) (indent)
/Users/pdingemans/git/non-opex/ktlint-example/issue1045.kt:73:1: Unexpected indentation (8) (should be 16) (indent)
/Users/pdingemans/git/non-opex/ktlint-example/issue1045.kt:74:1: Unexpected indentation (6) (should be 12) (indent)
/Users/pdingemans/git/non-opex/ktlint-example/issue1045.kt:75:1: Unexpected indentation (4) (should be 8) (indent)
/Users/pdingemans/git/non-opex/ktlint-example/issue1045.kt:76:1: Unexpected indentation (2) (should be 4) (indent)
/Users/pdingemans/git/non-opex/ktlint-example/issue1045.kt:80:1: Unexpected indentation (2) (should be 4) (indent)
/Users/pdingemans/git/non-opex/ktlint-example/issue1045.kt:82:1: Unexpected indentation (2) (should be 4) (indent)
/Users/pdingemans/git/non-opex/ktlint-example/issue1045.kt:83:1: Unexpected indentation (4) (should be 8) (indent)
/Users/pdingemans/git/non-opex/ktlint-example/issue1045.kt:84:1: Unexpected indentation (6) (should be 12) (indent)
/Users/pdingemans/git/non-opex/ktlint-example/issue1045.kt:85:1: Unexpected indentation (8) (should be 16) (indent)
/Users/pdingemans/git/non-opex/ktlint-example/issue1045.kt:86:1: Unexpected indentation (10) (should be 20) (indent)
/Users/pdingemans/git/non-opex/ktlint-example/issue1045.kt:87:1: Unexpected indentation (10) (should be 20) (indent)
/Users/pdingemans/git/non-opex/ktlint-example/issue1045.kt:88:1: Unexpected indentation (8) (should be 16) (indent)
/Users/pdingemans/git/non-opex/ktlint-example/issue1045.kt:89:1: Unexpected indentation (6) (should be 12) (indent)
/Users/pdingemans/git/non-opex/ktlint-example/issue1045.kt:90:1: Unexpected indentation (6) (should be 12) (indent)
/Users/pdingemans/git/non-opex/ktlint-example/issue1045.kt:91:1: Unexpected indentation (8) (should be 16) (indent)
/Users/pdingemans/git/non-opex/ktlint-example/issue1045.kt:92:1: Unexpected indentation (10) (should be 20) (indent)
/Users/pdingemans/git/non-opex/ktlint-example/issue1045.kt:93:1: Unexpected indentation (10) (should be 20) (indent)
/Users/pdingemans/git/non-opex/ktlint-example/issue1045.kt:94:1: Unexpected indentation (8) (should be 16) (indent)
/Users/pdingemans/git/non-opex/ktlint-example/issue1045.kt:95:1: Unexpected indentation (6) (should be 12) (indent)
/Users/pdingemans/git/non-opex/ktlint-example/issue1045.kt:96:1: Unexpected indentation (6) (should be 12) (indent)
/Users/pdingemans/git/non-opex/ktlint-example/issue1045.kt:97:1: Unexpected indentation (8) (should be 16) (indent)
/Users/pdingemans/git/non-opex/ktlint-example/issue1045.kt:98:1: Unexpected indentation (6) (should be 12) (indent)
/Users/pdingemans/git/non-opex/ktlint-example/issue1045.kt:99:1: Unexpected indentation (4) (should be 8) (indent)
/Users/pdingemans/git/non-opex/ktlint-example/issue1045.kt:100:1: Unexpected indentation (2) (should be 4) (indent)
/Users/pdingemans/git/non-opex/ktlint-example/issue1045.kt:104:1: Unexpected indentation (2) (should be 4) (indent)
/Users/pdingemans/git/non-opex/ktlint-example/issue1045.kt:106:1: Unexpected indentation (2) (should be 4) (indent)
/Users/pdingemans/git/non-opex/ktlint-example/issue1045.kt:107:1: Unexpected indentation (4) (should be 8) (indent)
/Users/pdingemans/git/non-opex/ktlint-example/issue1045.kt:108:1: Unexpected indentation (6) (should be 12) (indent)
/Users/pdingemans/git/non-opex/ktlint-example/issue1045.kt:109:1: Unexpected indentation (6) (should be 12) (indent)
/Users/pdingemans/git/non-opex/ktlint-example/issue1045.kt:110:1: Unexpected indentation (4) (should be 8) (indent)
/Users/pdingemans/git/non-opex/ktlint-example/issue1045.kt:112:1: Unexpected indentation (4) (should be 8) (indent)
/Users/pdingemans/git/non-opex/ktlint-example/issue1045.kt:113:1: Unexpected indentation (6) (should be 12) (indent)
/Users/pdingemans/git/non-opex/ktlint-example/issue1045.kt:114:1: Unexpected indentation (8) (should be 16) (indent)
/Users/pdingemans/git/non-opex/ktlint-example/issue1045.kt:115:1: Unexpected indentation (10) (should be 20) (indent)
/Users/pdingemans/git/non-opex/ktlint-example/issue1045.kt:116:1: Unexpected indentation (8) (should be 16) (indent)
/Users/pdingemans/git/non-opex/ktlint-example/issue1045.kt:117:1: Unexpected indentation (6) (should be 12) (indent)
/Users/pdingemans/git/non-opex/ktlint-example/issue1045.kt:119:1: Unexpected indentation (6) (should be 12) (indent)
/Users/pdingemans/git/non-opex/ktlint-example/issue1045.kt:120:1: Unexpected indentation (8) (should be 16) (indent)
/Users/pdingemans/git/non-opex/ktlint-example/issue1045.kt:121:1: Unexpected indentation (6) (should be 12) (indent)
/Users/pdingemans/git/non-opex/ktlint-example/issue1045.kt:122:1: Unexpected indentation (4) (should be 8) (indent)
/Users/pdingemans/git/non-opex/ktlint-example/issue1045.kt:123:1: Unexpected indentation (6) (should be 12) (indent)
/Users/pdingemans/git/non-opex/ktlint-example/issue1045.kt:124:1: Unexpected indentation (4) (should be 8) (indent)
/Users/pdingemans/git/non-opex/ktlint-example/issue1045.kt:125:1: Unexpected indentation (2) (should be 4) (indent)

When running format the IndexOutOfBounds exception occurs as described in previous comment. I expect that the IndentationRule and the ArgumentListWrapping rule have conflicts about the correct way to indent lines.

This is confirmed by disabling the indent rule as well. No IndexOutOfBounds occurs in that situation.

If I run with indent rule enabled on my local machine with all unmerged branches regarding changes in the indentation rule, the IndexOutOfBounds also does not occur. But in that case the ArgumentListWrapping rules does report lint errors while running the format command:

Loaded .editorconfig: [indent_size: 2, disabled_rules: final-newline,parameter-list-wrapping,argument-list-wrapping,no-wildcard-imports,max-line-length,experimental:package-name, tab_width: 2]
/Users/pdingemans/git/non-opex/ktlint-example/issue1045.kt:41:11: Unexpected indentation (expected 8, actual 10) (experimental:argument-list-wrapping)
/Users/pdingemans/git/non-opex/ktlint-example/issue1045.kt:42:9: Unexpected indentation (expected 6, actual 8) (experimental:argument-list-wrapping)
/Users/pdingemans/git/non-opex/ktlint-example/issue1045.kt:45:9: Unexpected indentation (expected 6, actual 8) (experimental:argument-list-wrapping)
/Users/pdingemans/git/non-opex/ktlint-example/issue1045.kt:46:7: Unexpected indentation (expected 4, actual 6) (experimental:argument-list-wrapping)
/Users/pdingemans/git/non-opex/ktlint-example/issue1045.kt:69:11: Unexpected indentation (expected 8, actual 10) (experimental:argument-list-wrapping)
/Users/pdingemans/git/non-opex/ktlint-example/issue1045.kt:70:9: Unexpected indentation (expected 6, actual 8) (experimental:argument-list-wrapping)
/Users/pdingemans/git/non-opex/ktlint-example/issue1045.kt:73:9: Unexpected indentation (expected 6, actual 8) (experimental:argument-list-wrapping)
/Users/pdingemans/git/non-opex/ktlint-example/issue1045.kt:74:7: Unexpected indentation (expected 4, actual 6) (experimental:argument-list-wrapping)

Those messages need to be investigated further. I expect they are caused by having non consistent indentation logic in the IndentationRule and the ArgumentListWrappingRule.

paul-dingemans pushed a commit to paul-dingemans/ktlint that referenced this issue Nov 10, 2021
Only the indentation rule should emit warnings about incorrect indentation to avoid
conflicting warnings from different rules about the indentation of the exact same
line. However, those other rules should still fix the indentation as good as they
can for the case that the indent rule is not run at all.

Closes pinterest#1267, pinterest#1119, pinterest#1045
@paul-dingemans
Copy link
Collaborator

Closed by #1284

@paul-dingemans paul-dingemans added this to the 0.44.0 milestone Jan 28, 2022
# for free to join this conversation on GitHub. Already have an account? # to comment
Projects
None yet
Development

No branches or pull requests

3 participants