ktlint-gradle: `filter` config seems to be ignored

A bit of context Due to some problems with Android SafeArgs Gradle plugin I had to add additional source sets into feature modules (Gradle sub-projects)

    sourceSets {
getByName("test").java.srcDir("${project.rootDir}/app/build/generated/source/navigation-args/debug")
    }

Ktlint Issue Now I am experiencing an issue where filter configuration seems to be ignored and checks ...Args classes that are located inside the generated folder (I am not sure if this is related to the above change, but 100% reproducible).

I have added to root build.gradle.kts file:

filter {
   exclude("**/generated/**")
}

However ktlint gradle still seens to check classes in generated folder

/Users/igorwojda/StudioProjects/android-showcase/app/build/generated/source/navigation-args/debug/com/igorwojda/showcase/feature/album/presentation/albumdetails/AlbumDetailFragmentArgs.kt:26:30

Since this class is generated by SafeArgs Gradle plugin I have no control on code format and naturally, I want it not to be checked by ktlint.

Also, note that after adding the same source set to different modules this file is checked 3 times.

Reproduce Here is the branch to test it https://github.com/igorwojda/android-showcase/tree/ktlint-filter-not-working

  1. Open this project in Android Studio
  2. Build project, so AlbumDetailFragmentArgs is generated (Build -> Make project)
  3. Run ktlint ./gradlew ktlintcheck

Result

> Task :feature_favourite:ktlintTestSourceSetCheck FAILED
/Users/igorwojda/StudioProjects/android-showcase/app/build/generated/source/navigation-args/debug/com/igorwojda/showcase/feature/album/presentation/albumdetails/AlbumDetailFragmentArgs.kt:26:30: Unexpected spacing before ":" (colon-spacing)eSetCheck
/Users/igorwojda/StudioProjects/android-showcase/app/build/generated/source/navigation-args/debug/com/igorwojda/showcase/feature/album/presentation/albumdetails/AlbumDetailFragmentArgs.kt:35:29: Unexpected spacing before ":" (colon-spacing)
/Users/igorwojda/StudioProjects/android-showcase/app/build/generated/source/navigation-args/debug/com/igorwojda/showcase/feature/album/presentation/albumdetails/AlbumDetailFragmentArgs.kt:44:24: Unexpected spacing before ":" (colon-spacing)
"checkstyle" report written to /Users/igorwojda/StudioProjects/android-showcase/feature_favourite/build/reports/ktlint/ktlintTestSourceSetCheck.xml

> Task :feature_profile:ktlintTestSourceSetCheck FAILED
/Users/igorwojda/StudioProjects/android-showcase/app/build/generated/source/navigation-args/debug/com/igorwojda/showcase/feature/album/presentation/albumdetails/AlbumDetailFragmentArgs.kt:26:30: Unexpected spacing before ":" (colon-spacing)
/Users/igorwojda/StudioProjects/android-showcase/app/build/generated/source/navigation-args/debug/com/igorwojda/showcase/feature/album/presentation/albumdetails/AlbumDetailFragmentArgs.kt:35:29: Unexpected spacing before ":" (colon-spacing)
/Users/igorwojda/StudioProjects/android-showcase/app/build/generated/source/navigation-args/debug/com/igorwojda/showcase/feature/album/presentation/albumdetails/AlbumDetailFragmentArgs.kt:44:24: Unexpected spacing before ":" (colon-spacing)
"checkstyle" report written to /Users/igorwojda/StudioProjects/android-showcase/feature_profile/build/reports/ktlint/ktlintTestSourceSetCheck.xml

> Task :feature_album:ktlintTestSourceSetCheck FAILED
/Users/igorwojda/StudioProjects/android-showcase/app/build/generated/source/navigation-args/debug/com/igorwojda/showcase/feature/album/presentation/albumdetails/AlbumDetailFragmentArgs.kt:26:30: Unexpected spacing before ":" (colon-spacing)eSetCheck
/Users/igorwojda/StudioProjects/android-showcase/app/build/generated/source/navigation-args/debug/com/igorwojda/showcase/feature/album/presentation/albumdetails/AlbumDetailFragmentArgs.kt:35:29: Unexpected spacing before ":" (colon-spacing)
/Users/igorwojda/StudioProjects/android-showcase/app/build/generated/source/navigation-args/debug/com/igorwojda/showcase/feature/album/presentation/albumdetails/AlbumDetailFragmentArgs.kt:44:24: Unexpected spacing before ":" (colon-spacing)
"checkstyle" report written to /Users/igorwojda/StudioProjects/android-showcase/feature_album/build/reports/ktlint/ktlintTestSourceSetCheck.xml

Expected result All checks related to AlbumDetailFragmentArgs.kt would be ignored (due to filter rule). Also is this class would not be ignored I would expect no duplicated checks.

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 2
  • Comments: 22 (2 by maintainers)

Commits related to this issue

Most upvoted comments

But adding

ktlint {
    enableExperimentalRules.set(true)
    filter {
        // exclude("**/generated/**")
        exclude { projectDir.toURI().relativize(it.file.toURI()).path.contains("/generated/") }
    }
}

as mentioned here: https://github.com/JLLeitschuh/ktlint-gradle/issues/222#issuecomment-480758375 does work for me. Not quite sure why the one works and the other one not to be honest.

@igorwojda your issue is that added files are located outside module project root dir:

This is because regex-based matching is done on the relative path, i.e. the path following the root directory of each tree.

Proper solution would be replace regex with:

exclude { element -> element.file.path.contains("generated/") }

More details you could find in this issue: https://github.com/gradle/gradle/issues/3417

@Globegitter @plannigan I don’t know details of you issues, but could you check if solution above is working for your cases as well.

I would add this solution to project README.

@plusmobileapps I had to adapt the exclusion rule so it works regardless of OS (something to do with different path separators, doesn’t work on Windows)

ktlint {
    filter {
        exclude { it.file.path.contains(layout.buildDirectory.dir("generated").get().toString()) }
    }
}

If someone else ends up here and is wondering how to do this without the deprecated buildDir property, this is what worked for me.

ktlint {
    filter {
        exclude { it.file.path.contains("${layout.buildDirectory.get()}/generated/") }
        include("**/kotlin/**")
    }
}
ktlint {
    filter {
        exclude { it.file.path.contains("$buildDir/generated/") }
    }
}
configure<KtlintExtension> {
    filter {
        exclude { it.file.path.contains("$buildDir/generated/") }
    }
}

Above workaround works only when ktlintCheck task explicetly defined in command line invocation.

@Tapchicoma I think I have encountered a variation of this problem. I just want to post is here, so you can take a look while fixing this issue:

When running ./gradlew check also fails for me with the same problem (ignored filters), but this time excluding source above fix does not work (even if wide the scope to all ktlint tasks if (!gradle.startParameter.taskNames.contains("ktlint"))).

> Task :feature_favourite:ktlintTestSourceSetCheck FAILED
/Users/igorwojda/StudioProjects/android-showcase/app/build/generated/source/navigation-args/debug/com/igorwojda/showcase/feature/album/presentation/albumdetail/AlbumDetailFragmentArgs.kt:26:30: Unexpected spacing before ":" (colon-spacing)
/Users/igorwojda/StudioProjects/android-showcase/app/build/generated/source/navigation-args/debug/com/igorwojda/showcase/feature/album/presentation/albumdetail/AlbumDetailFragmentArgs.kt:35:29: Unexpected spacing before ":" (colon-spacing)
/Users/igorwojda/StudioProjects/android-showcase/app/build/generated/source/navigation-args/debug/com/igorwojda/showcase/feature/album/presentation/albumdetail/AlbumDetailFragmentArgs.kt:44:24: Unexpected spacing before ":" (colon-spacing)
"checkstyle" report written to /Users/igorwojda/StudioProjects/android-showcase/feature_favourite/build/reports/ktlint/ktlintTestSourceSetCheck.xml
...

This is line that causing this issue: https://github.com/igorwojda/android-showcase/blob/3468173b90c206574953da1ff505938b2d1df169/feature_profile/build.gradle.kts#L52

Commenting it out makes ktlint green again.

Though plugin should support such case as well, I will try to fix it.

Thank you for such detailed report 👍