`os.open()` depends on directory where `onyx run` was executed
benstt opened this issue · comments
When opening files with os.open()
or similar functions (such as os.get_contents()
), the path specified in the parameter is currently being opened relative to the directory in which onyx run
was called. This leads to situations in which reading the file may fail depending on where we are calling it from.
Reproducing
Given the project:
my_onyx_project/
|- subdirectory/
|--|- main.onyx
|--|- input.txt
and main.onyx
:
use core {println, os}
main :: () {
file_contents := os.get_contents("input.txt");
println(file_contents);
}
The programs outputs an empty string when executing onyx run subdirectory/main.onyx
but doesn't for (cd subdirectory; onyx run main.onyx)
.
We could change the current working directory when executing each Onyx file as a proposed solution.
Build version
$ onyx version [14:36:56]
Onyx toolchain version v0.1.8
Built on Wed Nov 29 01:49:45 2023
Runtime: wasmer-custom
For me, this is actually I would prefer it to work. I believe it is how most scripting languages (Javascript, Python, and Lua to name a few) do it this way where you have be aware of where you are running the script. Onyx is obviously not a scripting language, so this gets even more complicated if you compile to a WASM binary, then move the binary to a new folder.
For those reasons, I think I'm going to leave it how it is. Thanks for the feedback tho! Hope you are enjoying Onyx so far!