Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Unable to autocorrect trailing comma #2794

Closed
ccjernigan opened this issue Sep 12, 2024 · 3 comments · Fixed by #2806
Closed

Unable to autocorrect trailing comma #2794

ccjernigan opened this issue Sep 12, 2024 · 3 comments · Fixed by #2806
Milestone

Comments

@ccjernigan
Copy link

Expected Behavior

ktlint --format should be able to autocorrect violations

Observed Behavior

07:48:17.149 [pool-1-thread-7] WARN com.pinterest.ktlint.rule.engine.internal.CodeFormatter -- Format was not able to resolve all violations which (theoretically) can be autocorrected in file

Steps to Reproduce

  1. Add some code like this
    private fun breakKtlint(int: Int,) {
    }
  1. Run ktlint --format

Your Environment

  • Version of ktlint used: 1.3.1
  • Relevant parts of the .editorconfig settings
  • Name and version (or code for custom task) of integration used (Gradle plugin, Maven plugin, command line, custom Gradle task):
  • Version of Gradle used (if applicable):
  • Operating System and version:
@paul-dingemans
Copy link
Collaborator

The given code sample does not reproduce the problem, given .editorconfig below:

root = true

[*.{kt,kts}]
ktlint_code_style = ktlint_official
ktlint_standard = enabled
ktlint_experimental = enabled

Please provide additional information to reproduce.

@ccjernigan
Copy link
Author

ccjernigan commented Sep 12, 2024

Ah, this is my .editorconfig

root = true

[*.{kt,kts}]
ktlint_standard_trailing-comma-on-call-site = disabled
ktlint_standard_trailing-comma-on-declaration-site = disabled
ktlint_standard_function-naming = disabled # Disabled because of Compose

Given the rule is disabled, #2794 is probably invalid (or perhaps the error message could be more clear) but #2795 is probably valid.

@paul-dingemans
Copy link
Collaborator

Ok tnx. Reproducable with:

root = true

[*.{kt,kts}]
ktlint_standard_trailing-comma-on-declaration-site = disabled

The problem is caused by the parameter-list-spacing rule. It requires a whitespace after each comma but this does not make sense for a trailing comma. The output of lint:

src/main/kotlin/Foo.kt:1:33: Whitespace after ',' is missing (standard:parameter-list-spacing)
20:27:12.345 [main] WARN com.pinterest.ktlint.cli.internal.KtlintCommandLine -- Lint has found errors than can be autocorrected using 'ktlint --format'

Summary error count (descending) by rule:
  standard:parameter-list-spacing: 1

When format is run, the violation above is found and resolved. But as a result of this fix, the function-signature, which runs later, removes this space but leaves the comma. After both rules have run, the result is identical to the original file. So actually ktlint finds and resolves two lint violations. This is repeated three times with identical results (this also does not make sense) which results in message Format was not able to resolve all violations which (theoretically) can be autocorrected in file.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants