detekt icon indicating copy to clipboard operation
detekt copied to clipboard

Custom Detekt task doesn't respect `include()` function

Open oheyadam opened this issue 3 months ago • 5 comments

Expected Behavior

I have this custom detekt task to which I pass only the files that were changed because I don't want to run full detekt. I'm passing the files via include(). I expect it to run against this subset of files only

Observed Behavior

Running the task with ``--infosays it hasNO-SOURCE`, even though I am passing a list of files.

Steps to Reproduce

Create the task as such:

    val changedFilesProvider = providers.of(GitChangedFilesValueSource::class) {
        parameters.rootDir.set(rootProject.layout.projectDirectory.asFile.name)
    }
    tasks.register<Detekt>("fastDetekt") {
        changedFilesProvider.get()
            .forEach { file -> include(file) }
    }

GitChangedFilesValueSource is defined as such:

abstract class GitChangedFilesValueSource : ValueSource<List<String>, Parameters> {

    @get:Inject
    abstract val execOperations: ExecOperations

    interface Parameters : ValueSourceParameters {
        val rootDir: Property<String>
    }

    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")
            .map { line -> "${parameters.rootDir.get()}/$line" }
        println("Changed files: $result") // this is here for debugging. it prints the correct list of files
        return result
    }
}

Run ./gradlew fastDetekt, it always succeeds even if you intentionally break some detekt rules in any of your files

Context

Your Environment

  • Version of detekt used: 1.23.5
  • Version of Gradle used (if applicable): 8.5
  • Gradle scan link (add --scan option when running the gradle task):
  • Operating System and version: MacOS Sonoma 14.3.1
  • Link to your project (if it's a public repository):

oheyadam avatar Mar 06 '24 17:03 oheyadam

Includes/excludes applies filters to the paths passed to source - you should pass the files you wish to scan to that parameter.

3flex avatar Mar 06 '24 20:03 3flex

Includes/excludes applies filters to the paths passed to source - you should pass the files you wish to scan to that parameter.

Sorry I failed to mention that I have a global Detekt config block that passes the source. Also doesn't detekt have a default value for this anyway?

I already have the following:

tasks.withType<Detekt>.configureEach {
    setSource(project.layout.projectDirectory)
}

Typing down this code on my phone, so it might be wrong, but you get the idea. I even copied this over to my custom task and it still doesn't work.

oheyadam avatar Mar 06 '24 21:03 oheyadam

Can you please put a self contained reproducer together? That will make it much easier to have a look at what's going on.

3flex avatar Mar 06 '24 22:03 3flex

Hi @3flex, sorry for the delay. Here's a reproducer. An easy way to trigger this behavior is to add this line back to MainActivity and run ./gradlew detekt. This fails as expected, but running ./gradlew fastDetekt doesn't. Thanks

oheyadam avatar Mar 19 '24 09:03 oheyadam

Hey @3flex, just wanted to add another thing I noticed. In the same project structure that I linked, the detektBaseline function doesn't seem to work. Every time you invoke it, it just generates an empty file, even if you have new issues in your project.

What's wrong here? Does Detekt just not play nice when used in included builds?

oheyadam avatar Apr 26 '24 15:04 oheyadam