Skip to content
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

Merged
merged 1 commit into from
Oct 21, 2024

Conversation

jwoudenberg
Copy link
Contributor

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 is what shells like sh or bash do. If you create a file test.sh with the contents echo "$0" then run bash test.sh, that will print test.sh and not bash. 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

@jwoudenberg
Copy link
Contributor Author

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:
roc-lang/basic-cli#82

I've skipped the tests for now.

@jwoudenberg jwoudenberg force-pushed the expose-original-script-to-host branch 2 times, most recently from 1cf36ef to 1229819 Compare October 21, 2024 18:07
@@ -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);
Copy link
Collaborator

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.

Copy link
Contributor Author

@jwoudenberg jwoudenberg Oct 21, 2024

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.

Copy link
Collaborator

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.
@jwoudenberg
Copy link
Contributor Author

Ugh, accidentally pushed the ignore statement away when I rebased, so the test is failing again. Pushed the ignore statement again.

@smores56
Copy link
Collaborator

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.

@jwoudenberg
Copy link
Contributor Author

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!
roc-lang/basic-cli#82 (comment)

@smores56 smores56 merged commit ff83fd6 into roc-lang:main Oct 21, 2024
18 checks passed
@jwoudenberg jwoudenberg deleted the expose-original-script-to-host branch October 21, 2024 21:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants