I'm trying to use basic-cli's Cmd.Exec to execute a command cd zig-out/lib/"
:
Cmd.exec "cd" ["zig-out/lib/"]
However it's failing. I've tried routing it through Cmd.output
to figure out why it's failing, and it seems it's throwing No such file or directory
However the directory is definitely there. I can cd
to it from terminal. I've also routed the ls
command through Cmd.output
to make sure the directory is showing up in its ouput. I've also got many other commands that work with Cmd.exec
, it just doesn't seem to like the cd
shell command specifically.
Maybe something about running in a child process doesn't like the cd
command?
now that I think about it, even if CD did work, doing CD wouldn't be very useful if each Cmd.exec
runs in its own process right? Subsequent commands would be running from the current working directory, not that one I tried to CD to
Can you have it run Cmd.exec "pwd" []
first?
This sounds like it's running in the wrong directory
yep it's running in the correct directory. Confirmed with ls
and now with pwd
Could you share the code? Maybe in a gist?
Whatever format works
I can try messing with it
I'll try to replicate with a minimal example
yea I'm trying to get it down to an app with just this command, but compiler crashing on me at the moment
alright here it is
app [main] {
cli: platform "https://github.com/roc-lang/basic-cli/releases/download/0.15.0/SlwdbJ-3GR7uBWQo6zlmYWNYOxnvo8r6YABXD-45UOw.tar.br",
}
import cli.Cmd
main : Task {} [Exit I32 Str]
main =
Cmd.exec "cd" ["zig-out/lib"]
|> Task.mapErr \_ -> Exit 1 "Failed"
|> Task.map \_ -> {}
In case it matters, I have this in a file tools/build2.roc
that I execute with roc dev tools/build2.roc
app [main] {
pf: platform "https://github.com/roc-lang/basic-cli/releases/download/0.15.0/SlwdbJ-3GR7uBWQo6zlmYWNYOxnvo8r6YABXD-45UOw.tar.br",
}
import pf.Cmd
main =
testDir = "myTestDir"
Cmd.exec! "mkdir" ["-p", "$(testDir)/child"]
Cmd.exec! "cd" [testDir]
Cmd.exec! "ls" ["-a", testDir]
This works, printing out:
~/dev
❯ roc test.roc
. .. child
Which works in the way you were worried about, which is that it runs everything in a child directory
weird, using that code I'm getting
Program exited with error:
(CmdError (IOError "Os { code: 2, kind: NotFound, message: "No such file or directory" }"))
I'm on OSX
ah, I'm on ubuntu
Hmm
Pulling out the linux machine
I think this is a limitation with rust Command... and not related to roc
Like you may need to spawn a shell like sh
instead of the cd
process directly
ah
https://stackoverflow.com/questions/56895623/why-isnt-my-rust-code-cding-into-the-said-directory
You're attempting to run an external command called cd. Depending on your operating system, this either fails because there is no command called cd, or this does nothing other than test whether the directory exists and you have permission to access it. If a cd command exists, it runs in a subprocess of your program, and its change of directory does not affect your process.
It did also work on my Fedora Silverblue install
Really?
I'm surprised to hear that
~/dev
❯ roc test.roc
roc: /lib64/libtinfo.so.6: no version information available (required by roc)
. .. child
I would have expected each of those calls to run in a separate child process
Well, it didn't cd
But it ran
So it worked the way that SO article explained it would
Jared Cone said:
weird, using that code I'm getting
Program exited with error: (CmdError (IOError "Os { code: 2, kind: NotFound, message: "No such file or directory" }"))
I didn't get this failure, basically
We should add a https://doc.rust-lang.org/std/env/fn.set_current_dir.html if we haven't already...
We have it https://www.roc-lang.org/packages/basic-cli/0.15.0/Env#setCwd
I thought I added that
Last updated: Jul 06 2025 at 12:14 UTC