Skip to content

fileTree unexpected behavior when used as task output #30032

Description

@shotmk

Current Behavior

When task defines output as filtered via include/exclude filters fileTree the root directory of fileTree is considered as output.

Expected Behavior

If task output uses a filtered fileTree, the root directory of the fileTree should not be considered as output, only files/directories matching the exclude/include patterns should be considered as outputs.

Context (optional)

No response

Steps to Reproduce

This dummy build.gradle.kts showcases the issue:

plugins {
    base
}

// This mocks the task that takes `.graphqls` files in `src` directory and generates `.go` files from them in the same directory.
// The files are partly generated and supposed to be edited manually after generation, to implement actual resolver logic, thats
// why they sit in the `src` directory
tasks.register<Exec>("generate") {
    commandLine("echo", "Generating..")
    inputs.files(layout.projectDirectory.dir("src").asFileTree.matching{ include("*.graphqls") })
    outputs.files(layout.projectDirectory.dir("src").asFileTree.matching{ include("*.go") })
    doLast{
        println("Generation complete.")
    }
}

// This mocks the task that validates the `.graphqls` files in the `src` directory
// This have no dependency on `generate`, as it only validates the `.graphqls` files that are not generated by the `generate` task
tasks.register<Exec>("validate") {
    commandLine("echo", "Validating...")
    inputs.files(layout.projectDirectory.dir("src").asFileTree.matching { include("*.graphqls") })
    doLast {
        println("Validation complete.")
    }
}

tasks.named("build") {
    dependsOn("validate", "generate")
}

Executing gradle's build task results in the following error:

A problem was found with the configuration of task ':validate' (type 'Exec').
  - Gradle detected a problem with the following location: '/tmp/gradle_tasks/src'.
    
    Reason: Task ':validate' uses this output of task ':generate' without declaring an explicit or implicit dependency. This can lead to incorrect results being produced, depending on what order the tasks are executed.

As a workaround for this i am calling .elements on the fileTree and passing the result as task output:

    outputs.files(layout.projectDirectory.dir("src").asFileTree.matching{ include("*.go") }.elements)

Gradle version

8.9

Build scan URL (optional)

No response

Your Environment (optional)

No response

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    a:bugThis doesn't work as expectedin:configuration-modellazy api, domain object container

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions