#resolve a path with a '~'
1 messages · Page 1 of 1 (latest)
the "easy" way would be to check if the path starts with ~/, and if it does, find and open $HOME as directory and use Dir.openFile with the rest of the path to open the file
it doesn't because it potentially requires allocation
i mean resolve(Posix) takes in an allocator
but that doesn't matter anyways because the only syscall in resolvePosix is via getCwdAlloc which obviously doesn't help resolve ~ so yeha :(
Isn't that a shell thing though
a good shell shouldn't pass ~ into any programs
but a lot of people use ~/... to indicate a path relative to their home
eg. user input, configuration, etc.
true, but I'm not sure that should be handled by the std.fs api
I, Community, approve this package
While I agree that std.fs might not be the right place for it, I guess I expected something in std.os or something to know how to resolve ~ in a path to $HOMEDIR. But this answers my question, in any case. Thanks!
what shell are you using anyway?
I use zsh, but ~ can appear in user input or in configuration files, etc.
ahh, gotcha
in this case a file that was being loaded that tells this program where to find other things had a ~ in it
well, in case you decide to roll it yourself, you can get the $HOME variable from std.process.getEnvVarOwned
I might file an issue about this and see what they say, I can see myself bumping into this again in the future
I'm very skeptical this would be added to the stdlib, but you're free to try
A general filepath package should probably be added IMO
I implemented this for Go and my package has been used in almost all major Go programs in the ecosystem (i.e. all the HashiCorp tools, Kubernetes, Docker, etc.). If you want to steal this logic, it has been production proven for almost a decade: https://github.com/mitchellh/go-homedir/blob/main/homedir.go
Its surprisingly messy to get the home directory, but not overly complicated! 🙂
posted issue here: https://github.com/ziglang/zig/issues/13228