-
Notifications
You must be signed in to change notification settings - Fork 47
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
sudo and $HOME #29
Comments
A very good observation, thanks @zertosh! I'm trying to think of a solution that "just works" but haven't found anything yet ... |
There isn't an universal solution, but you can take a look at the |
Hey, Was this issue solved or not yet? |
I was looking at solving this, but the sudo manual describes Would simply using the |
Can I ask which OS you were running with this behaviour? I have just run into this sudo issue with an application I am working on, and on all the systems I've tested on (albeit on a couple of Ubuntu systems, and a couple or raspberry pi's running PiOs), the |
I was testing using Fish and Arch Linux. I can test again later using bash, perhaps? |
Ah ok, I'm only using bash. Could a potential fix for this first check for |
I'm not getting
Perhaps I'm misunderstanding, but I thought the goal was to make |
I have the same result with |
You are correct. It's quite odd, because even using
EDIT: A small test project using |
So based on your observations y'all, is there any change that should be discussed? |
This issue I was having was that running an application via sudo would return the wrong home dir. This program is called fn main() {
let home = dirs::home_dir();
println!("{home:?}")
} Running I, although this may be a mistaken belief, think that both of these executions should return the same result, i.e. I haven't delved into the code properly, but my assumption was that the |
And now it seems that |
I thought But if not, maybe it could try to use |
I wonder where the different behavior comes from ... |
That I'm not sure, something about variable expansion happening before sudo is executed - which then doesn't inherit the expanded values? |
I'm thoroughly confused at this point. I tested with
This seems like something |
Yeah probably a good idea not to replace current behavior, and instead have a new function. What cross platform issues do you envision? Running the aforementioned I don't currently have access to macOS, so sadly can't test on there |
That's true, Windows doesn't really have the same concepts so the two functions would essentially alias. I don't know what the behavior on Mac would be, perhaps someone here has one they could test on? |
When running with
sudo
,dirs-rs
returns directories for the user callingsudo
and not the user that's running undersudo
.On the surface this can seem like expected behavior but it becomes a real problem when a tool is run with
sudo
, usesdirs::cache_dir
, and ends up creating that directory when it doesn't exist.Unsetting
$HOME
before usingdirs-rs
causesdirs-sys-rs
'shome_dir
to usegetpwuid_r
in the "fallback" logic. This ends up returning the home dir for the user running undersudo
. But it's a really ugly workaround. Althoughsudo
has an-H
flag to replace$HOME
, you don't tend to expect a tool to end up creating a~/.cache
owned by sayroot
when you don't use-H
.I'm not sure if it's a good idea to never trust
$HOME
, but would it be possible to offer another set of methods that use the "real home dir"?The text was updated successfully, but these errors were encountered: