-
-
Notifications
You must be signed in to change notification settings - Fork 312
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Set argv[0] to .roc file passed to 'roc run' #7172
Set argv[0] to .roc file passed to 'roc run' #7172
Conversation
ed8823c
to
8875c37
Compare
This test I added worked for me at some point, now it's failing for me too. I think it might be this bug in basic-cli: I've skipped the tests for now. |
1cf36ef
to
1229819
Compare
@@ -1145,7 +1169,7 @@ fn roc_run_native<I: IntoIterator<Item = S>, S: AsRef<OsStr>>( | |||
use bumpalo::collections::CollectIn; | |||
|
|||
let executable = roc_run_executable_file_path(binary_bytes)?; | |||
let (argv_cstrings, envp_cstrings) = make_argv_envp(arena, &executable, args); | |||
let (argv_cstrings, envp_cstrings) = make_argv_envp(arena, script_path, args); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
From the added tests, it looks like a script in /home/myuser/Documents/myscript.roc
would have the relative path included along with the name, but I think we should prefer just the file_name (a.k.a. myscript.roc
) since that's what will be wanted to display 99% of the time.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the relative name is important if we want to use argv[0] to look up the script location in the application.
For instance, if I'm in /home/jasper and run: roc ./projects/foobar/main.roc
, if argv[0] only tells me I'm running main.roc
, then in the implementation of main.roc
I can't know where the script is located, for instance to read some data.json
file that's supposed to be next to the script file.
Bash works the same way btw! Try put this script somewhere:
#!/usr/bin/env bash
echo "$0"
And run it from a couple different places to observe the result.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay, if we're doing what bash is doing, I'm happy.
When we run `roc run <file>` or `roc <file>` then Roc will compile a binary and run it. Before this commit we would set the path to the compiled binary as argv[0]. This commit changes the behavior to make argv[0] in the binary correspond to the roc file being ran. This benefits the use of roc scripts that make use of a shebang: #!/usr/bin/env roc With this change such scripts will be able to read the path to themselves out of ARGV. This trick is commonly used for instance by bash scripts in order to access files relative to the script itself.
1229819
to
ad55529
Compare
Ugh, accidentally pushed the ignore statement away when I rebased, so the test is failing again. Pushed the ignore statement again. |
I think this is good to merge if tests pass, we just need to put a comment in the roc-lang/basic-cli#82 issue to fix this when that issue is fixed. |
Good idea, done! |
When we run
roc run <file>
orroc <file>
then Roc will compile a binary and run it. Before this commit we would set the path to the compiled binary as argv[0]. This commit changes the behavior to make argv[0] in the binary correspond to the roc file being ran.This is what shells like sh or bash do. If you create a file
test.sh
with the contentsecho "$0"
then runbash test.sh
, that will printtest.sh
and notbash
. Scripts can make use of $0 to get their own location, for instance to find files relative to themselves on the filesystem.Related Zulip conversation:
https://roc.zulipchat.com/#narrow/channel/302903-platform-development/topic/getting.20args.20when.20using.20.60roc.20run.60.3F/near/477528914