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

home: Handle empty HOME on unix #12023

Closed
wants to merge 1 commit into from

Conversation

utkarshgupta137
Copy link
Contributor

What does this PR try to resolve?

Contrary to the documentation, if the HOME environment variable is empty on Unix, then an empty Path is returned. This is because std::env::home_dir doesn't handle this case.
I've copied the code from standard library & added a .filter(|s| !s.is_empty()) check. The rest of the code is the same.
Also added a test similar to Windows.

How should we test and review this PR?

The current home_dir function doesn't handle an empty HOME environment variable, with this PR, it should.

Additional information

Related discussion: rust-lang/rust#71684 (comment)

@rustbot
Copy link
Collaborator

rustbot commented Apr 24, 2023

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @ehuss (or someone else) soon.

Please see the contribution instructions for more information. Namely, in order to ensure the minimum review times lag, PR authors and assigned reviewers should ensure that the review label (S-waiting-on-review and S-waiting-on-author) stays updated, invoking these commands when appropriate:

  • @rustbot author: the review is finished, PR author should check the comments and take action accordingly
  • @rustbot review: the author is ready for a review, this PR will be queued again in the reviewer's queue

@rustbot rustbot added A-environment-variables Area: environment variables S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Apr 24, 2023
@bors
Copy link
Collaborator

bors commented Apr 24, 2023

☔ The latest upstream changes (presumably #12021) made this pull request unmergeable. Please resolve the merge conflicts.

@ehuss
Copy link
Contributor

ehuss commented Apr 24, 2023

Can you provide some context here about why this would be changed? What is the importance of how it handles an empty HOME value? When would that happen in practice? How do other ecosystems handle this?

@utkarshgupta137
Copy link
Contributor Author

utkarshgupta137 commented Apr 24, 2023

Can you provide some context here about why this would be changed? What is the importance of how it handles an empty HOME value? When would that happen in practice? How do other ecosystems handle this?

  1. The docs falsely claim that if HOME is empty, then it falls back to getpwuid_r.
  2. I hope to see this crate recommended in the standard library & it came up in the linked discussion that this crate doesn't handle this case.
  3. It is not very difficult to get a shell where the HOME variable is not set or set incorrectly. For eg. env -i bash.
  4. A lot of crates outside of cargo & rustup use home as well, so it would be useful to fix it for the ecosystem.
  5. The dirs/dirs-next crate & other "dirs" crate handle this case. These crates have been abandoned in the past, so it would be nice if this crate could handle this case.

@ehuss
Copy link
Contributor

ehuss commented Apr 24, 2023

The docs falsely claim that if HOME is empty, then it falls back to getpwuid_r.

Can you say why we wouldn't just change the docs instead?

It is not very difficult to get a shell where the HOME variable is not set or set incorrectly. For eg. env -i bash.

From what I can tell, at least on my systems, that unsets the HOME variable (which should go to the fallback).

As for the ecosystem question, I was asking more along the lines of what other languages and libraries do. For example, Python doesn't handle it, and it essentially becomes unusable. go also seems to fail with an empty HOME. Are there any languages or common tools or libraries that handle this? Or is this requirement a holdover from a documentation mistake when the initial env module was added?

I'm not completely opposed to checking for this case. However, it's a lot of code to add for an edge case that doesn't seem terribly important without some motivation.

@utkarshgupta137
Copy link
Contributor Author

The docs falsely claim that if HOME is empty, then it falls back to getpwuid_r.

Can you say why we wouldn't just change the docs instead?

It is not very difficult to get a shell where the HOME variable is not set or set incorrectly. For eg. env -i bash.

From what I can tell, at least on my systems, that unsets the HOME variable (which should go to the fallback).

As for the ecosystem question, I was asking more along the lines of what other languages and libraries do. For example, Python doesn't handle it, and it essentially becomes unusable. go also seems to fail with an empty HOME. Are there any languages or common tools or libraries that handle this? Or is this requirement a holdover from a documentation mistake when the initial env module was added?

I'm not completely opposed to checking for this case. However, it's a lot of code to add for an edge case that doesn't seem terribly important without some motivation.

Hmmm. I can't find any resources on any other languages handling this. On top of that, defining $HOME is a requirement of the POSIX spec, so I guess it should be fine to not handle this. I'll raise a new PR to fix the documentation.

@ehuss It would be nice to get your input on rust-lang/rust#71684 & rust-lang/rust#110665.

@utkarshgupta137
Copy link
Contributor Author

@ehuss One thing to note is that this crate currently uses a fallback for Windows, but not for Unix. On Python, it doesn't use a fallback for both unix & windows. Same for Go.
While the documentation could clear that up, it is still unexpected that the 2 platforms are treated differently.

bors added a commit that referenced this pull request Apr 27, 2023
home: fix & enhance documentation

### What does this PR try to resolve?

Fixes the documentation to specify that the `HOME` environment variable will be returned on Unix even if it is empty.
Also cleaned up & improved documentation in other areas.

### Additional information

Related: #12023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-environment-variables Area: environment variables S-waiting-on-review Status: Awaiting review from the assignee but also interested parties.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants