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

ktlint doesn't report issues for all files, if path of one file is a subpath of another #942

Closed
volo-droid opened this issue Oct 13, 2020 · 4 comments · Fixed by #984
Closed
Assignees
Labels
Milestone

Comments

@volo-droid
Copy link

Observed Behavior

If file paths are passed in the command line, and path of one file is a subpath of another file, then ktlint only reports issues of the file with the shortest path.

For example if there're 2 files with issues:

  1. /src/main/java/org/example/bugtest/SampleBadClass.kt
  2. /src/main/java/org/example/bug/AnotherBadClass.kt

then only the issues of the AnotherBadClass.kt are reported.

This only happens if one's file path is a substring of another's file path. In the example above: /src/main/java/org/example/bug is a substring of src/main/java/org/example/bugtest. The case would also reproduce on the following paths of the second file:

  • /src/main/java/org/example/bugtes/AnotherBadClass.kt
  • /src/main/java/org/example/bugte/AnotherBadClass.kt
  • /src/main/java/org/example/b/AnotherBadClass.kt

If the file with the shortest path doesn't have issues or that file doesn't exist then no issues are reported at all:

  1. /src/main/java/org/example/bugtest/SampleBadClass.kt
  2. /src/main/java/org/example/bug/NonExistingFile.kt

Expected Behavior

Issues from all files are correctly reported

Steps to Reproduce

Files setup

/src/main/java/org/example/bugtest/SampleBadClass.kt:

package org.example.bugtest

// Unnecessary block ("{}")
// Unexpected blank line(s) before "}"
// Needless blank line(s)
object SampleBadClass {


}

/src/main/java/org/example/bug/AnotherBadClass.kt:

package org.example.bug

// unnecessary block {}
object AnotherBadClass {}

/src/main/java/org/example/bug/AnotherGoodClass.kt:

package org.example.bug

// no issues
object AnotherGoodClass {}

Check results

ktlint "src/main/java/org/example/bugtest/SampleBadClass.kt" finds issues as expected:

/Users/…/src/main/java/org/example/bugtest/SampleClass.kt:3:20: Unnecessary block ("{}")
/Users/…/src/main/java/org/example/bugtest/SampleClass.kt:4:1: Unexpected blank line(s) before "}"
/Users/…/src/main/java/org/example/bugtest/SampleClass.kt:5:1: Needless blank line(s)

ktlint "src/main/java/org/example/bugtest/SampleBadClass.kt" "src/main/java/org/example/bug/AnotherGoodClass.kt" reports no issues (issues in SampleBadClass found before are now not reported):

ktlint "src/main/java/org/example/bugtest/SampleBadClass.kt" "src/main/java/org/example/bug/AnotherBadClass.kt" reports only issues from AnotherBadClass.kt (issues in SampleBadClass found before are again not reported):

/Users/…/src/main/java/org/example/bug/AnotherBadClass.kt:4:24: Unnecessary block ("{}")

The bug also reproduces if multiple wildcards are used:
ktlint "src/main/java/org/example/bug/*.kt" "src/main/java/org/example/bugtest/*.kt" only reports issues from AnotherBadClass.kt skipping SampleBadClass.kt:

/Users/…/src/main/java/org/example/bug/AnotherBadClass.kt:4:24: Unnecessary block ("{}")

Additional info

ktlint gradle plugin runs ktlint exaclty like in the examples above, specifying a path to every single file. That currently leads to inconsistent reports between ktlint command line run (without file paths) and gradlew ktlintCheck.

Your Environment

  • Version of ktlint used: 0.39.0
  • Name and version (or code for custom task) of integration used (Gradle plugin, Maven plugin, command line, custom Gradle task): command line
  • Version of Gradle used (if applicable):
  • Operating System and version: macOS 10.15.7
@jpalawaga
Copy link

Seeing the same issue.

@artnc
Copy link
Contributor

artnc commented Oct 21, 2020

Also seeing the same issue. I'm guessing there's a problem here

@Tapchicoma Tapchicoma self-assigned this Oct 22, 2020
@Tapchicoma Tapchicoma added the bug label Oct 22, 2020
@Tapchicoma Tapchicoma added this to the 0.40.0 milestone Oct 22, 2020
@Tapchicoma
Copy link
Collaborator

Was able to reproduce, will fix for the next release.

@thecoden
Copy link

thecoden commented Dec 8, 2020

Hi @Tapchicoma,

The current workaround kills any ability to use negated globs (begin with "!") because it is only additive with the results. I have forked the klob library and fixed the issue and published a package to bintray for now, as well as submitted pull requests to the original project. The new package was just approved for inclusion into jcenter under the artifact io.codetactics.klob:klob:0.2.1.

I will also submit a pull request to revert the workaround commit, with a small addition to also skip hidden files similar to the default paths. By my testing, this will allow inclusive & exclusive globs, while also correcting this issue at the root of the problem.

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

Successfully merging a pull request may close this issue.

5 participants