Stream: beginners

Topic: Using ~ to access home


view this post on Zulip Scally (Nov 17 2024 at 18:03):

Am I being stupid or does ~ not work to refer to the home directory in path specifiers on linux?

I've tried in import statements when trying to load text files (I could somewhat understand it not working there since from what I've understood it's compile time, even then it should probably work though) and with things like File.readUtf8 and with both I get file not found errors even though I know the file is there.

view this post on Zulip Brendan Hansknecht (Nov 17 2024 at 18:06):

File.readUtf8 would be up to basic cli. It should work with ~ I think. If it doesn't, that is a bug

view this post on Zulip Brendan Hansknecht (Nov 17 2024 at 18:07):

For import statements, they definitely should not work with ~. That would be a huge security risk of downloaded scripts. They should only work with relative paths and probably shouldn't allow using .. to escape the directory the script is in (though I'm not fully sure on that one). We probably actually need to double check the security around this and add explicit errors.

view this post on Zulip Scally (Nov 17 2024 at 18:11):

Good point in terms of security risk, haven't even considered that. I guess in terms of the basic-cli platform it doesn't matter too much since it could still work with file read but with the safe cli platform and other platforms in mind it's probably still good to secure imports against random file access.

view this post on Zulip Brendan Hansknecht (Nov 17 2024 at 18:11):

yeah, exactly that

view this post on Zulip Scally (Nov 17 2024 at 18:13):

Yeah, I just tested it, both escaping with .. as well as providing absolute paths (i.e. /home/user) works in imports.
Given that it's really strange ~ does not.

view this post on Zulip Scally (Nov 17 2024 at 19:32):

Okay, looking at the source code it seems like the the basic cli platform completely defers file and path operations to Rust which doesn't seem to do ~ expansion to home directory by default (there are crates that do that https://blog.liw.fi/posts/2021/10/12/tilde-expansion-crates/ ).
I haven't fully understood everything yet but it seems that is what's happening. Still for Linux users this expansion not happening is pretty unexpected.

view this post on Zulip Scally (Nov 17 2024 at 19:45):

For now I guess this does the trick but it seems a bit ugly and I'd consider it a workaround due to non-functional expected behavior.

home = Env.var! "HOME"
filepath = Str.joinWith [home, "file.txt"] "/"

view this post on Zulip Luke Boswell (Nov 17 2024 at 20:01):

One of the design goals is that roc code should run the same on Linux, Mac, Windows. Working with paths nicely is a deep rabbit hole to make a nice API. We have the WIP roc-lang/path package which aims to do this. But I don't know if we've discussed ~ before.

view this post on Zulip Scally (Nov 17 2024 at 20:09):

Do you have any recommendations on how to handle typically platform dependent things then?
In my case for instance I'd like to have a config file outside the project ecosystem.
A typical pattern on Linux is to have dotfiles (usually either in ~ or ~/.config). As far as I know there is no similar pattern for Windows (maybe %AppData%).
Maybe provide a path to the config file through an environment variable but that seems kind of cumbersome.

view this post on Zulip Luke Boswell (Nov 17 2024 at 20:36):

I'm not sure. This feels like the kind of thing we are exploring with the design of roc. I would research it and do some experiments to explore the space a little.

Would a config file in the same directory as the executable work for your use case?

view this post on Zulip Scally (Nov 17 2024 at 20:43):

I'd like to avoid it. In my case I'm streaming my development on Twitch and I'd like to avoid my IDE accidentally picking up on and showing things contained in my config file.

I'm aware the use-case is somewhat niche but I'd assume there could be other reasons for wanting to do something similar (i.e. doing deployments on a server and having the config be in the user's home folder instead of connected to the application.

view this post on Zulip Luke Boswell (Nov 17 2024 at 20:47):

Maybe we should add some effects that wrap something like this? https://crates.io/crates/directories

view this post on Zulip Luke Boswell (Nov 17 2024 at 20:49):

Maybe starting with just home and cache dirs


Last updated: Jul 06 2025 at 12:14 UTC