addSystemCommand cannot find exe available on PATH?

I am trying to run the glslangValidator exe as a systemCommand during build, I have the following code:

fn add_shader(b: *std.Build, exe: *std.Build.Step.Compile, path: []const u8, name: []const u8) void {
    const dir = std.fs.path.dirname(path).?;
    const source = b.path(std.fmt.allocPrint(b.allocator, "assets/{s}", .{path}) catch @panic("OOM"));
    const destination = b.path(std.fmt.allocPrint(b.allocator, "assets/{s}/{s}.spv", .{ dir, name }) catch @panic("OOM"));

    const shader_compilation = b.addSystemCommand(&.{"glslangValidator"});
    shader_compilation.addPrefixedFileArg("-V ", source);
    shader_compilation.addPrefixedFileArg("-o ", destination);

    exe.step.dependOn(&shader_compilation.step);

    exe.root_module.addAnonymousImport(name, .{ .root_source_file = destination });
}

This results in the following exception:

error: unable to spawn glslangValidator: FileNotFound

The strange thing is that when running in pws, or cmd directly the exe is found and has no trouble executing. I would expect addSystemCommand could execute exe’s on the PATH env variable but this is not the case?

I think you can use findProgram, from Build System Tricks :

From the docs of std.Build.addSystemCommand:

Initializes a Step.Run with argv, which must at least have the path to the executable.

It is not like a shell that automatically resolves the command name to an executable, instead you have to do that resolution to the actual executable explicitly by using findProgram or something like it.

According to @dimdin resolving a command to an executable should work, if it is available in one of the pathes in the PATH environment variable.

1 Like

It means that you must provide at least one element in the argv (the path to executable).

I have used addSystemCommand to invoke tools that are on the PATH and it works perfectly every time.


@ricknijhuis ensure that the correct PATH is set, by calling:
b.addSystemCommand(&.{"cmd /c \"echo %PATH%\""}; for windows
b.addSystemCommand(&.{"sh -c 'echo $PATH'"}; for linux/mac/etc.

EDIT: this is wrong, each command argument must be a separate element.
b.addSystemCommand(&.{"cmd", "/c", "echo %PATH%"}; for windows
b.addSystemCommand(&.{"sh", "-c", "echo $PATH"}; for linux/mac/etc.

1 Like

I tried this one:

const echo = b.addSystemCommand(&.{“cmd /c "echo %PATH%"”});

but again the same issue:

error: unable to spawn cmd /c “echo %PATH%”: FileNotFound

I was able to fix it by installing the latest master, I had a master build of a while back.