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

Missing newline after "{" (wrapping) false positive on line that has exactly 140 characters #1808

Closed
mateuszkwiecinski opened this issue Feb 4, 2023 · 3 comments · Fixed by #1854

Comments

@mateuszkwiecinski
Copy link
Contributor

mateuszkwiecinski commented Feb 4, 2023

Expected Behavior

I'd either expect the ktlint to not report wrapping offence, or keep reporting the offence when an unrelated piece of code changes.
IntelliJ doesn't perform any fixes when reformatting, the line that has 140 characters is considered valid

Observed Behavior

ktlint fails with

{path}/src/main/kotlin/KotlinClass.kt:3:121: Missing newline after "{" (wrapping)
ktlint src/main/kotlin --log-level=debug
21:14:50.417 [main] DEBUG com.pinterest.ktlint.internal.KtlintCommandLine - Discovered reporter with "baseline" id.
21:14:50.420 [main] DEBUG com.pinterest.ktlint.internal.KtlintCommandLine - Discovered reporter with "checkstyle" id.
21:14:50.420 [main] DEBUG com.pinterest.ktlint.internal.KtlintCommandLine - Discovered reporter with "json" id.
21:14:50.420 [main] DEBUG com.pinterest.ktlint.internal.KtlintCommandLine - Discovered reporter with "format" id.
21:14:50.420 [main] DEBUG com.pinterest.ktlint.internal.KtlintCommandLine - Discovered reporter with "html" id.
21:14:50.420 [main] DEBUG com.pinterest.ktlint.internal.KtlintCommandLine - Discovered reporter with "plain" id.
21:14:50.421 [main] DEBUG com.pinterest.ktlint.internal.KtlintCommandLine - Discovered reporter with "plain-summary" id.
21:14:50.421 [main] DEBUG com.pinterest.ktlint.internal.KtlintCommandLine - Discovered reporter with "sarif" id.
21:14:50.421 [main] DEBUG com.pinterest.ktlint.internal.KtlintCommandLine - Initializing "plain" reporter with {color=false, color_name=DARK_GRAY, format=false}
21:14:50.461 [main] DEBUG com.pinterest.ktlint.internal.FileUtils - Start walkFileTree for rootDir: '{{redacted}}'
   include:
[      - sun.nio.fs.UnixFileSystem$3@4e91d63f,       - sun.nio.fs.UnixFileSystem$3@d4342c2,       - sun.nio.fs.UnixFileSystem$3@2bbf180e,       - sun.nio.fs.UnixFileSystem$3@163e4e87]
   exclude:
[]
21:14:50.467 [main] DEBUG com.pinterest.ktlint.internal.FileUtils - Discovered 1 files to be processed in 6 ms
21:14:50.970 [pool-1-thread-1] DEBUG com.pinterest.ktlint.core.internal.EditorConfigLoader - Resolving .editorconfig files for {{redacted}}/src/main/kotlin/KotlinClass.kt file path:
	max_line_length: 140
21:14:50.970 [pool-1-thread-1] DEBUG com.pinterest.ktlint.core.internal.RuleExecutionContext - Editor config properties for file '{{redacted}}/src/main/kotlin/KotlinClass.kt': {max_line_length=max_line_length = 140}
21:14:50.977 [pool-1-thread-1] DEBUG com.pinterest.ktlint.core.internal.RuleRunnerSorter - Rules will be executed in order below (unless disabled):
           - standard:annotation, 
           - standard:annotation-spacing, 
           - standard:chain-wrapping, 
           - standard:colon-spacing, 
           - standard:comma-spacing, 
           - standard:comment-spacing, 
           - standard:curly-spacing, 
           - standard:dot-spacing, 
           - standard:double-colon-spacing, 
           - standard:enum-entry-name-case, 
           - standard:filename, 
           - standard:final-newline, 
           - standard:import-ordering, 
           - standard:keyword-spacing, 
           - standard:modifier-order, 
           - standard:multiline-if-else, 
           - standard:no-blank-line-before-rbrace, 
           - standard:no-blank-lines-in-chained-method-calls, 
           - standard:no-consecutive-blank-lines, 
           - standard:no-empty-class-body, 
           - standard:no-empty-first-line-in-method-block, 
           - standard:no-line-break-after-else, 
           - standard:no-line-break-before-assignment, 
           - standard:no-multi-spaces, 
           - standard:no-semi, 
           - standard:no-trailing-spaces, 
           - standard:no-unit-return, 
           - standard:no-unused-imports, 
           - standard:no-wildcard-imports, 
           - standard:op-spacing, 
           - standard:package-name, 
           - standard:parameter-list-wrapping, 
           - standard:paren-spacing, 
           - standard:range-spacing, 
           - standard:spacing-around-angle-brackets, 
           - standard:spacing-between-declarations-with-annotations, 
           - standard:spacing-between-declarations-with-comments, 
           - standard:string-template, 
           - standard:unary-op-spacing, 
           - standard:wrapping, 
           - experimental:class-naming, 
           - experimental:comment-wrapping, 
           - experimental:context-receiver-wrapping, 
           - experimental:discouraged-comment-location, 
           - experimental:fun-keyword-spacing, 
           - experimental:function-naming, 
           - experimental:function-return-type-spacing, 
           - experimental:function-start-of-body-spacing, 
           - experimental:function-type-reference-spacing, 
           - experimental:kdoc-wrapping, 
           - experimental:modifier-list-spacing, 
           - experimental:nullable-type-spacing, 
           - experimental:parameter-list-spacing, 
           - experimental:property-naming, 
           - experimental:spacing-between-function-name-and-opening-parenthesis, 
           - experimental:type-argument-list-spacing, 
           - experimental:type-parameter-list-spacing, 
           - experimental:unnecessary-parentheses-before-trailing-lambda, 
           - standard:trailing-comma-on-call-site, 
           - standard:trailing-comma-on-declaration-site, 
           - experimental:function-signature, 
           - standard:argument-list-wrapping, 
           - standard:indent, 
           - standard:max-line-length, 
           - experimental:block-comment-initial-star-alignment
21:14:51.015 [pool-1-thread-1] DEBUG com.pinterest.ktlint.core.KtLint - Finished with linting file '{{redacted}}/src/main/kotlin/KotlinClass.kt'
{{redacted}}/src/main/kotlin/KotlinClass.kt:3:121: Missing newline after "{" (wrapping)

Summary error count (descending) by rule:
  wrapping: 1
21:14:51.017 [main] DEBUG com.pinterest.ktlint.internal.KtlintCommandLine - Finished processing in 609ms / 1 file(s) scanned / 1 error(s) found
21:14:51.017 [main] DEBUG com.pinterest.ktlint.internal.KtlintCommandLine - Exit ktlint with exit code: 1

Steps to Reproduce

Given Kotlin file with line that has exactly 140 characters

package com.repro

fun String.tryingToReproStuff(): String = "sdfghjhgasdasdasfcdcfvgbhnjmnbvcdxcvbnjmhgfdfghjhgfdfghjhgfdfghjkhgfs".also { it.toBigDecimal() }

and .editorconfig: with max_line_length set:

root = true

[*.kt]
max_line_length = 140

When

  1. I run ktlint - it fails with wrapping requirement being not met.
  2. I remove package definition - it passes
  3. I remove one character in the string, so the line now has 139 characters - it passes
  4. (additionally, in case that's helpful) In my $workProjct where I originally stumbled upon this issue, adding one more character (so the line has now 141 chars) - I only saw max_line_lenght offence only (I wasn't able to repro here though)

So either 1) is a false positive, or 2) and 3) are false negatives. I'm not sure what's the intended behavior here 👀

Your Environment

  • Version of ktlint used: 0.48.2
  • Relevant parts of the .editorconfig settings: max_line_length = 140
  • Name and version (or code for custom task) of integration used (Gradle plugin, Maven plugin, command line, custom Gradle task): CLI
  • Version of Gradle used (if applicable): N/A
  • Operating System and version: ubuntu-latest on GA
@paul-dingemans
Copy link
Collaborator

This seems to be an error in the wrapping rule. The newline chararacter before the rule is counted as well. This also explains why the error disappears when the package statement is removed.

@paul-dingemans paul-dingemans added this to the 0.49.0 milestone Feb 6, 2023
paul-dingemans added a commit that referenced this issue Mar 12, 2023
…ving exactly the maximum line length is preceded by a blank line

Closes #1808
paul-dingemans added a commit that referenced this issue Mar 12, 2023
…ving exactly the maximum line length is preceded by a blank line

Closes #1808
paul-dingemans added a commit that referenced this issue Mar 13, 2023
* Fix false positive when a single line statement containing a block having exactly the maximum line length is preceded by a blank line

Closes #1808
@smedelyan
Copy link

ktlint-gradle: 11.2.0
ktlint: 0.48.2

Btw, I'm receiving the message Missing newline after "{" while the error is actually too long line (it was 121 while the max is 120 in the project). Let's maybe make the message clearer? Something about line too long so that one does not need to figure out the rule that enforces new lines after {-s (which is not there :) )

@paul-dingemans
Copy link
Collaborator

ktlint-gradle: 11.2.0 ktlint: 0.48.2

Btw, I'm receiving the message Missing newline after "{" while the error is actually too long line (it was 121 while the max is 120 in the project). Let's maybe make the message clearer? Something about line too long so that one does not need to figure out the rule that enforces new lines after {-s (which is not there :) )

Tnx for the suggestion. It is a bit problematic to implement in the WrappingRule as there are different reasons why a linebreak is needed and only of them is related to exceeding the maximum line length. Personally, I use the linting only to debug problems in KtLint and for resolving lint-errors that can not be fixed automatically. In all other cases I just trust the formatting of KtLint blindly.

If you want to try to implement this in KtLint, please give it a try and submit a PR.

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

Successfully merging a pull request may close this issue.

3 participants