Stream: beginners

Topic: Any reason for basic-cli "Cmd.exec cd ..." not to work?


view this post on Zulip Jared Cone (Oct 08 2024 at 22:34):

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?

view this post on Zulip Jared Cone (Oct 08 2024 at 22:42):

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

view this post on Zulip Sam Mohr (Oct 08 2024 at 22:43):

Can you have it run Cmd.exec "pwd" [] first?

view this post on Zulip Sam Mohr (Oct 08 2024 at 22:43):

This sounds like it's running in the wrong directory

view this post on Zulip Jared Cone (Oct 08 2024 at 22:44):

yep it's running in the correct directory. Confirmed with ls and now with pwd

view this post on Zulip Sam Mohr (Oct 08 2024 at 22:48):

Could you share the code? Maybe in a gist?

view this post on Zulip Sam Mohr (Oct 08 2024 at 22:49):

Whatever format works

view this post on Zulip Sam Mohr (Oct 08 2024 at 22:50):

I can try messing with it

view this post on Zulip Sam Mohr (Oct 08 2024 at 22:54):

I'll try to replicate with a minimal example

view this post on Zulip Jared Cone (Oct 08 2024 at 22:54):

yea I'm trying to get it down to an app with just this command, but compiler crashing on me at the moment

view this post on Zulip Jared Cone (Oct 08 2024 at 22:55):

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 \_ -> {}

view this post on Zulip Jared Cone (Oct 08 2024 at 22:55):

In case it matters, I have this in a file tools/build2.roc that I execute with roc dev tools/build2.roc

view this post on Zulip Sam Mohr (Oct 08 2024 at 22:58):

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

view this post on Zulip Sam Mohr (Oct 08 2024 at 23:00):

Which works in the way you were worried about, which is that it runs everything in a child directory

view this post on Zulip Jared Cone (Oct 08 2024 at 23:01):

weird, using that code I'm getting

Program exited with error:
    (CmdError (IOError "Os { code: 2, kind: NotFound, message: "No such file or directory" }"))

view this post on Zulip Sam Mohr (Oct 08 2024 at 23:02):

I'm on OSX

view this post on Zulip Jared Cone (Oct 08 2024 at 23:02):

ah, I'm on ubuntu

view this post on Zulip Sam Mohr (Oct 08 2024 at 23:03):

Hmm

view this post on Zulip Sam Mohr (Oct 08 2024 at 23:08):

Pulling out the linux machine

view this post on Zulip Luke Boswell (Oct 08 2024 at 23:21):

I think this is a limitation with rust Command... and not related to roc

view this post on Zulip Luke Boswell (Oct 08 2024 at 23:22):

Like you may need to spawn a shell like sh instead of the cd process directly

view this post on Zulip Jared Cone (Oct 08 2024 at 23:22):

ah

view this post on Zulip Luke Boswell (Oct 08 2024 at 23:23):

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.

view this post on Zulip Sam Mohr (Oct 08 2024 at 23:28):

It did also work on my Fedora Silverblue install

view this post on Zulip Luke Boswell (Oct 08 2024 at 23:29):

Really?

view this post on Zulip Luke Boswell (Oct 08 2024 at 23:30):

I'm surprised to hear that

view this post on Zulip Sam Mohr (Oct 08 2024 at 23:30):

~/dev
❯ roc test.roc
roc: /lib64/libtinfo.so.6: no version information available (required by roc)
.  ..  child

view this post on Zulip Luke Boswell (Oct 08 2024 at 23:30):

I would have expected each of those calls to run in a separate child process

view this post on Zulip Sam Mohr (Oct 08 2024 at 23:30):

Well, it didn't cd

view this post on Zulip Sam Mohr (Oct 08 2024 at 23:30):

But it ran

view this post on Zulip Sam Mohr (Oct 08 2024 at 23:31):

So it worked the way that SO article explained it would

view this post on Zulip Sam Mohr (Oct 08 2024 at 23:31):

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

view this post on Zulip Luke Boswell (Oct 08 2024 at 23:31):

We should add a https://doc.rust-lang.org/std/env/fn.set_current_dir.html if we haven't already...

view this post on Zulip Luke Boswell (Oct 08 2024 at 23:32):

We have it https://www.roc-lang.org/packages/basic-cli/0.15.0/Env#setCwd

view this post on Zulip Luke Boswell (Oct 08 2024 at 23:32):

I thought I added that


Last updated: Jul 06 2025 at 12:14 UTC