Hi all, This is probably a dumb issue, but I've be...
# community-support
r
Hi all, This is probably a dumb issue, but I've been banging my head on my desk for too long now. For some reason, the
exclude
filters aren't taking affect in the bellow snippet and I have no clue why. I also tried to create two `FileTree`s and
minus
them which didn't work as expected either. Any idea what I'm doing wrong? Thanks!
Copy code
val touchedFiles = mutableListOf<String>()
// ...
val fileTree = files(touchedFiles).asFileTree.matching {
    include("**/*.yaml")
    include("**/*.yml")
    exclude("**/.idea/**")
    exclude("**/secrets.yml")
}
v
Hm, either I miss something too, or you hit a bug. The .idea line, while it could be simplified to
"**/.idea/"
seems to not work as expected. As a work-around you could instead use
include { ... }
and / or
exclude { ... }
instead of the Ant-style patterns.
Ah, maybe not a bug but maybe works as designed.
The Ant-style include and exclude patterns work on the relative path.
If you add the individual files to the collection they are directly the roots in the tree and so the relative path is only the filename so the exclude pattern for the directory does not match.
If in my example I instead add
files(layout.buildDirectory.dir("foo"))
the relative path starts after
foo
and so the directory exclude is matching.
I expect this is a good idea actually, otherwise the directory in which the project is checked out right now would influence the result of the include and exclude, for example if you exclude
**/build/
and then put your project under
/my/code/build/foo/project/
all files would be excluded, if you put it in
/my/code/foo/project/
then not.
For more fine-grained control you can either implicitly control what the relative path is, or you can use the spec-variants for includes and / or excludes. (you can also mix them with Ant-style patterns)
r
Thanks @Vampire. I think that I'm starting to comprehend what's going on here.
I'm going to try to switch to the spec-variants and see how that goes
👌 1
v
Make sure to there consider what I just said. If you use the absolute path in the spec and check for a specific path part, you might produce the problem I described, depending on where the project is put. So maybe only consider the relative paths starting from the project directory or something similar.
r
I think I'm a bit confused about
FileTree
in general.
touchedFiles
is a list of paths relative to the project's root directory. So I understand that calling
files
on it creates a non-trivial file tree with the wrong relative path. I guess I should've done something like
project.rootDir.files(touchedFiles).asFileTree
and then my Ant-style includes and excludes could work but that's just too many files to initiate a
FileTree
with.
Prompted by you suggestions I switched to using Spec based include and exclude which are less sexy but at least no one else will waste 1/2 a day in the future figuring out why files aren't being excluded as expected.