https://kotlinlang.org logo
#detekt
Title
# detekt
a

AG

11/25/2021, 1:59 PM
I'm generating baseline for whole project using this custom task
Copy code
task detektProjectBaseline(type: DetektCreateBaselineTask,  group: "detekt") {
    description = "Overrides current baseline."
    buildUponDefaultConfig = false
    ignoreFailures.set(true)
    parallel.set(true)
    setSource(files(rootDir))
    config.setFrom(files("$rootDir/quality/detekt/detekt-config.yml"))
    baseline.set(file("$rootDir/quality/detekt/baseline.xml"))
    include("**/*.kt")
    exclude("**/res/**", "**/build/**", "**/*.kts", "**/test/**", "**/androidTest/**")
    classpath.setFrom(project.configurations.getByName("detekt"))
}
and I have another task which runs on files that I'm passing through
-PinputFiles
gradle param
Copy code
task detektChangedFiles(type: Detekt, group: "detekt") {
    description = "Runs over changed files."
    buildUponDefaultConfig = false
    ignoreFailures = false
    parallel = true
    if (project.hasProperty("inputFiles")) {
        setSource(files(inputFiles.split('\n')))
    }
    config.setFrom(file("$rootDir/quality/detekt/detekt-config.yml"))
    baseline.set(file("$rootDir/quality/detekt/baseline.xml"))
    include("**/*.kt")
    exclude("**/res/**", "**/build/**", "**/*.kts", "**/test/**", "**/androidTest/**")
    classpath.setFrom(project.configurations.getByName("detekt"))
    reports {
        xml {
            enabled = true
            destination = file("build/reports/detekt.xml")
        }
        html {
            enabled = true
            destination = file("build/reports/detekt.html")
        }
    }
}
The problem is that after running detektChangedFiles I'm getting issues like UnnecessaryLet that my baseline doesn't contain for that file. Seems baseline generation task ignores some rules
g

gammax

11/25/2021, 2:13 PM
I would suggest to entirely remove this line:
classpath.setFrom(project.configurations.getByName(“detekt”))
From both tasks. What is happening is that, for some reasons, the
detektChangedFiles
is running with type resolution while the former task is not.
UnnecessaryLet
is a rule that requires type resolution, and would not work with your approach.
a

AG

11/25/2021, 2:28 PM
I want to enable type resolution for android project with custom tasks and I found this https://github.com/detekt/detekt/issues/2259 thats why I've used
classpath.setFrom(project.configurations.getByName("detekt"))
g

gammax

11/25/2021, 2:36 PM
That’s what is causing inconsistent results.
classpath.setFrom(project.configurations.getByName("detekt"))
will set the classpath to just the files in the
detekt
configuration, which is not enough to resolve all the types for your Android project (you’re missing out all the 3rd party dependencies). You should either: • Use the tasks that the Detekt Gradle Plugin is creating from you • Switch to don’t use type resolution at all (i.e. don’t pass an incomplete
classpath
).
a

AG

11/25/2021, 2:57 PM
ok ok, thanks @gammax 🙌 , I'll try with plugin tasks
g

gammax

11/25/2021, 2:57 PM
If the plugin doesnt work for you let us know and we will try to address your use case
👍 2
a

AG

11/25/2021, 5:56 PM
@gammax I used plugin tasks and it worked, thanks 👍
g

gammax

11/25/2021, 5:57 PM
Awesome 🙌
118 Views