Build.zig do step if certain directory changes?

Im trying to migrate my project to the new build system. Is there a way to say “rerun this step if a directory and its content changes”?
This is my code rn, java-sources.zig just walks the first arg dir and writes the abs path of it into the second arg.

const java_sources = b.addRunArtifact(b.addExecutable(.{
    .name = "java-sources",
    .root_module = b.createModule(.{
        .root_source_file = b.path("src/build-tools/java-sources.zig"),
        .target = target,
        .optimize = .debug,
    }),
}));

java_sources.addDirectoryArg2(b.path("src/runtime-java/"), .{});
const java_sources_file = java_sources.addOutputFileArg2("java-sources.txt", .{});

const javac = b.addSystemCommand(&.{
    "javac",
    "-cp",
    "asset/jar-libs/*",
    "-d"
});
javac.step.dependOn(&java_sources.step);
const class_dir = javac.addOutputDirectoryArg2("class", .{});
javac.addPrefixedFileArg("@", java_sources_file);

const jar = b.addSystemCommand(&.{ // jar cvf myapp.jar -C bin .
    "jar",
    "cvf"
});
jar.step.dependOn(&javac.step);
const jar_file = jar.addOutputFileArg("runtime.jar");
jar.addArg("-C");
jar.addDirectoryArg(class_dir);
jar.addArg(".");

b.getInstallStep().dependOn(&jar.step);
b.getInstallStep().dependOn(&b.addInstallBinFile(jar_file, "runtime.jar").step);
b.getInstallStep().dependOn(&b.addInstallBinFile(java_sources_file, "java-source.txt").step);

i thought it was enough to just reference the dir with b.path but its not apparently

I found this:

std.Build.dependOnDirectoryMetadata - indicates that the build.zig logic depends on a particular directory’s last modification date.

from here: 0.17.0 Release Notes ⚡ The Zig Programming Language

I myself had to sift through build system code to get my apk producer working inside termux and it was a great success. And I think it’s really worth just browsing definitions with the help of lsp.

Pretty sure dependOnDirectoryMetadata will work for you

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.

To a similar problem:

You don’t want the configure phase to be re-run when the contents of the directory change, just the step, so dependOnDirectoryMetadata/dependOnDirectoryContents is not the right tool.

I believe one option would be to use an intermediate WriteFile step (addWriteFiles) and addCopyDirectory, and then use the return of addCopyDirectory as the directory arg for java_sources. I believe that should behave as expected.

If not, another option would be to use addDepFileOutputArg with your java_sources run step, and then make it output a dep file (to the path given in that argument) containing all the discovered sources. The build system wants it in make format; here’s an example dep file writer from Aro, and here’s a very similar one from resinator.

For reference, the basic syntax of the format looks like this:

output_file: input_file1 input_file2

Nice ty this works. Yea i was avoiding suing dependOnDirectory stuff since it reruns configure phase