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

Adam Ahmed

02/23/2024, 4:28 PM
Hey, I have a custom Detekt task that I run only on changed files, but it doesn’t seem to do anything. It’s defined in one of my convention plugins as such. Calling it using
./gradlew fastDetekt
just doesn’t seem to do anything. What am I missing here?
Copy code
abstract class GitChangedFilesValueSource : ValueSource<List<String>, ValueSourceParameters.None> {

    @get:Inject
    abstract val execOperations: ExecOperations

    override fun obtain(): List<String> {
        val output = ByteArrayOutputStream()
        execOperations.exec {
            commandLine("git", "diff", "--name-only", "--diff-filter=ACM")
            standardOutput = output
        }
        val result = String(output.toByteArray(), Charset.defaultCharset()).trim().split("\n")
        println("GitChangedFilesValueSource invoked, changed files: $result") // this prints when I invoke the task
        return result
    }
}
Copy code
// This is called in my Application convention plugin
internal fun Project.registerFastDetekt() {
    val changedFiles = providers.of(GitChangedFilesValueSource::class) {}
    tasks.register<Detekt>("fastDetekt") {
        include(changedFiles.get())
        doLast {
            println("Changed files: ${changedFiles.get()}") // This never prints anything when I invoke the task!
        }
    }
}
g

gammax

02/23/2024, 4:34 PM
What’s the console output? Can you run with
--info
?
a

Adam Ahmed

02/23/2024, 4:38 PM
oh shit yeah it says
NO-SOURCE
which is kinda confusing because I also have this block defined elsewhere to configure tasks of the
Detekt
type
Copy code
tasks.withType<Detekt>().configureEach {
  parallel = true
  buildUponDefaultConfig = true
  ignoreFailures = false
  autoCorrect = target.hasProperty("detektAutoFix")
  config.setFrom(files("$baseConfigFilePath/detekt.yml"))
  baseline.set(file("$baseConfigFilePath/baseline.yml"))
  setSource(file(layout.projectDirectory))
  exclude(resourceFiles, buildFiles)
  reports {
    xml.required.set(false)
    md.required.set(false)
  }
}
Calling
detekt
itself works fine and it does fail when I have errors, but
fastDetekt
evidently has no sources
Copying this same configuration block into
fastDetekt
doesn’t change anything either
Also while I have you here haha, somewhat related question: I see that I can do this general config in the Detekt extension via
extensions.configure<DetektExtension> {}
as well. Does this mean that if I configure these settings in the Extension block, I don’t need to configure the
Detekt
tasks? Because I think that didn’t work for me--would just like to confirm if my assumption is right/wrong
Also I’m using detekt v1.23.5
5 Views