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
Fix crash when HOME env variable is not set #1738
Conversation
On Mac Os X and Linux (tried with alpine on docker) it is set correctly regardless of the import scala.scalanative.posix.stdlib.setenv
setenv(c"HOME", c"", 0) as first instruction in the main. |
Here is a discussion I found on SOF - https://stackoverflow.com/questions/2910377/get-home-directory-in-linux |
@lolgab Maybe some code like this? Not sure if you need to look at HOME at all. #include <unistd.h>
#include <pwd.h> // POSIX only
char* home = getenv("HOME"); // STD C
printf("HOME: %s\n", home);
unsetenv("HOME");
const char *homedir;
if ((homedir = getenv("HOME")) == NULL) {
printf("HOME NULL\n");
uid_t uid = getuid();
struct passwd *pw = getpwuid(uid);
homedir = pw->pw_dir;
}
printf("HOME2: %s\n", homedir); Output:
|
Nice! I'll try it on Linux and update the PR if it works! |
2838bf0
to
a53b478
Compare
@ekrich I applied your suggestion! Thank you |
Based on your JVM test, I was thinking that is what they might be doing. |
5ef4d58
to
f25939b
Compare
The function you are calling (5fc1189#diff-9f67b7df156c5096453ed90792be0ad2R69) is a Scala Native wrapper function that you are proposing to remove in #1743 This is the reason I was confused. We are not using error number at all after calling I am unclear what should be done as the POSIX If you add the real |
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.
@lolgab Looks good to me.
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.
Would you mind rebasing on top of the latest master to make sure that changes to scalafmt do not affect this?
Thank you.
@@ -61,7 +63,14 @@ object System { | |||
sysProps.setProperty("user.language", userLang) | |||
sysProps.setProperty("user.country", userCountry) | |||
} | |||
sysProps.setProperty("user.home", getenv("HOME")) | |||
{ |
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.
Consider using
{ | |
locally { |
to make this more readable.
Also, consider wrapping the following code for user.dir
in a similar locally {}
block, since it also has a val buf
.
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.
Did not know about locally
existence! Thank you :-) I'll implement the suggested changes!
@sjrd Ready for another review round. |
Steps to reproduce:
java.lang.System
objectHOME
environment variable$ unset HOME $ target/scala-2.11/hello-out
And it crashes with: