I think srcDirs += is danger and should be eliminated - insight from moving to Gradle 8

def genCode = tasks.register("generateSrc") {

    it.outputs.dir project.layout.buildDirectory.file("mycode1")


sourceSets {
    main.java {
        srcDirs genCode

Now tasktree shows correctly:

\--- :generateSrc


sourceSets {
    main.java {
        srcDirs += project.layout.buildDirectory.file("mycode2")

breaks the above dependency.
And it is clear why:

srcDirs += "x"   -->  setSrcDirs(getSrcDirs() + "x" )
and getSrcDirs() return Set<File> that strips out the 'builtBy' information

Although seems trivial, took me several hours to figure out why.

(why just now ? because gradle 8 forces me to depend on generateSrc, which is great, and eliminates some of race conditions when running with --parallel)

What do you think ?

You have the same potential problem at many other places I think.
I doubt it is a good idea or manageable to forbid setting the field(s) as there are quite some usecases where it is essential that you can clear out pre-existing values when setting a new one.

But that’s just my opinion.
If you want official feedback by Gradle guys, you probably need to post this as official feature / change request on GitHub.

Btw. maybe a tad better semantically, replace srcDirs genCode by srcDir genCode.

And a personal recommendation, use Kotlin DSL, then the IDE support is far superior and you might have seen earlier that you get a Set<File> there maybe. :slight_smile:

Thank you for the comments and recommendation, never did it before but I will try