# addSystemCommand cannot find exe available on PATH?

**URL:** <https://ziggit.dev/t/addsystemcommand-cannot-find-exe-available-on-path/6966>\
**Category:** Help\
**Tags:** build-system\
**Created:** [November 22, 2024, 6:57pm UTC](https://ziggit.dev/t/addsystemcommand-cannot-find-exe-available-on-path/6966 "2024-11-22T18:57:17Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ricknijhuis](https://ziggit.dev/user_avatar/ziggit.dev/ricknijhuis/32/1402_2.png) [@ricknijhuis](https://ziggit.dev/u/ricknijhuis)\
**Post date:** [November 22, 2024, 6:57pm UTC](https://ziggit.dev/t/addsystemcommand-cannot-find-exe-available-on-path/6966/1 "2024-11-22T18:57:17Z")

</div>

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

```zig
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?

---

<div class="post-metadata">

**Author:** ![Sze](https://ziggit.dev/user_avatar/ziggit.dev/sze/32/496_2.png) [@Sze](https://ziggit.dev/u/Sze)\
**Post date:** [November 22, 2024, 7:18pm UTC](https://ziggit.dev/t/addsystemcommand-cannot-find-exe-available-on-path/6966/2 "2024-11-22T19:18:40Z")

</div>

I think you can use `findProgram`, from [Build System Tricks](https://ziggit.dev/t/build-system-tricks/3531#h-7-system-tool-tricks-16) :

> [@Build System Tricks](https://ziggit.dev/t/build-system-tricks/3531/1):
>
> ### 7) System tool tricks
> 
> ```zig
> // Check if a system tool exists
> const echo_program = try b.findProgram(&.{"echo"}, &.{"check/this/path/", "and/this/one"});
> 
> // Get string output from a system tool
> const string = b.run(&.{echo_program, "hello/world"});
> const some_path = b.fmt("/my/other/path/{s}", .{string});
> 
> // Specify a system command as a build dependency
> // Note: currently will not run as nothing depends on it!
> const sys_command_build_dependency = b.addSystemCommand(&.{ echo_program, "I'm generating some text!" });
> 
> // Add a dependency on this command that takes the command's stdout and puts it in a file `zig-out/output.txt`
> // Note: Now this does something!
> b.default_step.dependOn(&b.addInstallFile(sys_command_build_dependency.captureStdOut(), "output.txt").step);
> 
> ```

From the docs of [std.Build.addSystemCommand](https://ziglang.org/documentation/master/std/#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.

---

<div class="post-metadata">

**Author:** ![dimdin](https://ziggit.dev/user_avatar/ziggit.dev/dimdin/32/1457_2.png) [@dimdin](https://ziggit.dev/u/dimdin)\
**Post date:** [November 22, 2024, 7:43pm UTC](https://ziggit.dev/t/addsystemcommand-cannot-find-exe-available-on-path/6966/3 "2024-11-22T19:43:56Z")

</div>

> [@Sze](#):
>
> From the docs of [std.Build.addSystemCommand](https://ziglang.org/documentation/master/std/#std.Build.addSystemCommand):
> 
> > Initializes a `Step.Run` with argv, which must at least have the path to the executable.

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.

---

<div class="post-metadata">

**Author:** ![ricknijhuis](https://ziggit.dev/user_avatar/ziggit.dev/ricknijhuis/32/1402_2.png) [@ricknijhuis](https://ziggit.dev/u/ricknijhuis)\
**Post date:** [November 23, 2024, 7:11am UTC](https://ziggit.dev/t/addsystemcommand-cannot-find-exe-available-on-path/6966/4 "2024-11-23T07:11:05Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ricknijhuis](https://ziggit.dev/user_avatar/ziggit.dev/ricknijhuis/32/1402_2.png) [@ricknijhuis](https://ziggit.dev/u/ricknijhuis)\
**Post date:** [November 23, 2024, 7:51am UTC](https://ziggit.dev/t/addsystemcommand-cannot-find-exe-available-on-path/6966/5 "2024-11-23T07:51:15Z")

</div>

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