Configure cache poisoning should solve it for you. Specifically, either std.Build.dependOnDirectoryMetadata or std.Build.dependOnDirectoryContents. The difference is described in their documentation. Beware: they are not recursive.
std.Build.dependOnDirectoryMetadata
std.Build.dependOnDirectoryContents
To a similar problem: