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

Deprecate `std::env::home_dir` and fix incorrect documentation #51656

Merged
merged 1 commit into from Jul 7, 2018

Conversation

Projects
None yet
@soc
Copy link
Contributor

soc commented Jun 20, 2018

Compare std::env::home_dirs claim:

Returns the value of the 'HOME' environment variable if it is set and not equal to the empty string.

... with its actual behavior:

std::env::set_var("HOME", "");
println!("{:?}", std::env::var_os("HOME")); // Some("")
println!("{:?}", std::env::home_dir());     // Some("")

The implementation is incorrect in two cases:

  • $HOME is set, but empty.
  • An entry for the user exists in /etc/passwd, but it's pw_dir is empty.

In both cases Rust considers an empty string to be a valid home directory. This contradicts the documentation, and is wrong in general.

@rust-highfive

This comment has been minimized.

Copy link
Collaborator

rust-highfive commented Jun 20, 2018

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

If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes.

Please see the contribution instructions for more information.

@soc

This comment has been minimized.

Copy link
Contributor Author

soc commented Jun 20, 2018

@soc soc force-pushed the soc:topic/fix-home-dir branch from 5cc03a8 to 0093ea3 Jun 20, 2018

@soc soc changed the title Fix unix implementation of std::env::home_dir() to follow documentation Fix Unix implementation of std::env::home_dir() to follow documentation Jun 20, 2018

@shepmaster

This comment has been minimized.

Copy link
Member

shepmaster commented Jun 20, 2018

The code seems reasonable to me, but I'm unclear on if such a change is acceptable.

randomly reassigning to...

r? @KodrAus

@rust-highfive rust-highfive assigned KodrAus and unassigned shepmaster Jun 20, 2018

@frewsxcv frewsxcv added the T-libs label Jun 20, 2018

@KodrAus

This comment has been minimized.

Copy link

KodrAus commented Jun 21, 2018

So it looks like the docs were correct once upon a time, but since we stabilized with the current behaviour I think I'd be more comfortable updating the docs to reflect that, unless we do have the described behaviour on Windows.

@soc

This comment has been minimized.

Copy link
Contributor Author

soc commented Jun 21, 2018

@KodrAus Windows is comparably broken (just in different ways) – see https://internals.rust-lang.org/t/deprecate-or-break-fix-std-env-home-dir/7315.

Preferably, I'd fix all the issues, but fixing the bugs on Windows has already been rejected. May it be possible to reevaluate the earlier decision given the new circumstances?

@soc

This comment has been minimized.

Copy link
Contributor Author

soc commented Jun 21, 2018

The reason I'm trying to figure out what's the position here is because I want to work on rust-lang/cargo#5183, which is blocked by the 1.0 release of the dirs/directories crates, which are in turn blocked by the current home_dir situation.

From a user/third-party library author perspective I pretty much have three options:

  1. Keep dirs::home_dir() -> PathBuf calling env::home_dir().unwrap() on Unix-likes. This means:
  • Live with the incorrect behavior on Unix.
  • Use my own implementation for Windows, because env::home_dir is just too broken on Windows.
  • Not having to maintain code for are large number of niche systems which I won't be able to test.
  1. Copy the implementation of env::home_dir and fix the bugs
  • Change the result type of dirs::home_dir from PathBuf to Option<PathBuf>, because there are now realistic cases where home_dir can fail.
  • Correct implementation on both Unix and Windows.
  • Large maintenance burden for all the different platforms that env::home_dir was supposed to cover.
  1. Fix env::home_dir to make sense and simply use it in dirs and directories.
  • Able to drop workarounds for Windows.
  • No workarounds necessary on Linux.
  • Everyone benefits, not just me.

I'd really prefer it if we could make 3. work, although I'm slowly approaching the pain threshold of further delaying 1.0 releases for the crates and the things they keep blocking down the line. So I'd appreciate some communication about whether hoping for fixes is realistic.

@steveklabnik

This comment has been minimized.

Copy link
Member

steveklabnik commented Jun 21, 2018

Nominating for @rust-lang/libs discussion, as they'd have to sign off on major changes here.

@SimonSapin

This comment has been minimized.

Copy link
Contributor

SimonSapin commented Jun 21, 2018

Do we know of a situation other than a test case where the environment variable might exist and be empty?

@soc

This comment has been minimized.

@SimonSapin

This comment has been minimized.

Copy link
Contributor

SimonSapin commented Jun 21, 2018

@soc (Assuming this is in response to me.) This post mentions not having a home directory. Is $HOME typically the empty string in that case, rather than unset?

@soc

This comment has been minimized.

Copy link
Contributor Author

soc commented Jun 22, 2018

@SimonSapin It depends. It seems like creating a user and using the flags to not create a home directory usually causes some non-existing dir to end up in /etc/passwd. That may depend on the tool being used, though.
$HOME is usually derived from that and there is nothing preventing someone from having an entry like

soc:x:1001:1001:::/bin/false

std::env::home_dir should not pretend that there is some valid home directory in that case by returning Some("").

@SimonSapin

This comment has been minimized.

Copy link
Contributor

SimonSapin commented Jun 27, 2018

The libs team discussed this today and consensus was that, since this function is already not doing what people want on Windows, we’d rather deprecate it in favor of something on crates.io and not change its behavior.

@rfcbot fcp close

@rfcbot

This comment has been minimized.

Copy link

rfcbot commented Jun 27, 2018

Team member @SimonSapin has proposed to close this. The next step is review by the rest of the tagged teams:

No concerns currently listed.

Once a majority of reviewers approve (and none object), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up!

See this document for info about what commands tagged team members can give me.

@soc

This comment has been minimized.

Copy link
Contributor Author

soc commented Jul 5, 2018

I released 1.0 versions of dirs and directories with correct implementations of home_dir. What needs to be done here now?

Shall I change this PR to deprecate std::env::home_dir?

@soc soc force-pushed the soc:topic/fix-home-dir branch from 87ccd06 to 0afc16a Jul 6, 2018

@soc

This comment has been minimized.

Copy link
Contributor Author

soc commented Jul 6, 2018

@SimonSapin Done!

@SimonSapin

This comment has been minimized.

Copy link
Contributor

SimonSapin commented Jul 6, 2018

@bors r+

@bors

This comment has been minimized.

Copy link
Contributor

bors commented Jul 6, 2018

📌 Commit 0afc16a has been approved by SimonSapin

@bors

This comment has been minimized.

Copy link
Contributor

bors commented Jul 7, 2018

⌛️ Testing commit 0afc16a with merge 99b0ddb...

bors added a commit that referenced this pull request Jul 7, 2018

Auto merge of #51656 - soc:topic/fix-home-dir, r=SimonSapin
Deprecate `std::env::home_dir` and fix incorrect documentation

Compare `std::env::home_dir`s claim:

> Returns the value of the 'HOME' environment variable if it is set and not equal to the empty string.

... with its actual behavior:

```
std::env::set_var("HOME", "");
println!("{:?}", std::env::var_os("HOME")); // Some("")
println!("{:?}", std::env::home_dir());     // Some("")
```

The implementation is incorrect in two cases:
- `$HOME` is set, but empty.
- An entry for the user exists in `/etc/passwd`, but it's `pw_dir` is empty.

In both cases Rust considers an empty string to be a valid home directory. This contradicts the documentation, and is wrong in general.
@bors

This comment has been minimized.

Copy link
Contributor

bors commented Jul 7, 2018

☀️ Test successful - status-appveyor, status-travis
Approved by: SimonSapin
Pushing 99b0ddb to master...

@bors bors merged commit 0afc16a into rust-lang:master Jul 7, 2018

2 checks passed

continuous-integration/travis-ci/pr The Travis CI build passed
Details
homu Test successful
Details

niklasad1 added a commit to paritytech/parity-ethereum that referenced this pull request Jul 9, 2018

Replace `std::env::home_dir` with `dirs::home_dir`
`std::env::home_dir` is deprecated but will probably take a while until
it is deprecated on stable. For more info see rust-lang/rust#51656

5chdn added a commit to paritytech/parity-ethereum that referenced this pull request Jul 9, 2018

Replace `std::env::home_dir` with `dirs::home_dir` (#9077)
`std::env::home_dir` is deprecated but will probably take a while until
it is deprecated on stable. For more info see rust-lang/rust#51656

dvdplm added a commit to paritytech/parity-common that referenced this pull request Jul 9, 2018

Replace `std::env::home_dir` with `dirs::home_dir` (#9077)
`std::env::home_dir` is deprecated but will probably take a while until
it is deprecated on stable. For more info see rust-lang/rust#51656

hoodie added a commit to ascii-dresden/asciii that referenced this pull request Jul 11, 2018

feat: replace `std::home_dir()` with `dirs::home_dir()`
`std::home_dir()` is deprecated since 1.29
rust-lang/rust#51656
@tjkirch

This comment has been minimized.

Copy link

tjkirch commented Jul 30, 2018

I don't understand this decision. Removing the API seems like a bigger breakage than fixing the behavior, particularly when it works in normal cases.

@sfackler

This comment has been minimized.

Copy link
Member

sfackler commented Jul 30, 2018

Deprecation is not removal.

@tjkirch

This comment has been minimized.

Copy link

tjkirch commented Jul 30, 2018

Deprecation usually implies future removal. And now anyone using this API gets yelled at by the compiler, and so can't maintain a clean build without bringing in a dependency.

@soc

This comment has been minimized.

Copy link
Contributor Author

soc commented Jul 30, 2018

@tjkirch Yeah, I'm not too happy with the decision. I'm now basically on the hook for supporting every platform ever invented under the sun, which I'm practically unable to test.

kornelski added a commit to mmstick/cargo-deb that referenced this pull request Aug 5, 2018

netbsd-srcmastr pushed a commit to NetBSD/pkgsrc that referenced this pull request Sep 14, 2018

rust: Update to 1.29.0.
Version 1.29.0 (2018-09-13)
==========================

Compiler
--------
- [Bumped minimum LLVM version to 5.0.][51899]
- [Added `powerpc64le-unknown-linux-musl` target.][51619]
- [Added `aarch64-unknown-hermit` and `x86_64-unknown-hermit` targets.][52861]

Libraries
---------
- [`Once::call_once` now no longer requires `Once` to be `'static`.][52239]
- [`BuildHasherDefault` now implements `PartialEq` and `Eq`.][52402]
- [`Box<CStr>`, `Box<OsStr>`, and `Box<Path>` now implement `Clone`.][51912]
- [Implemented `PartialEq<&str>` for `OsString` and `PartialEq<OsString>`
  for `&str`.][51178]
- [`Cell<T>` now allows `T` to be unsized.][50494]
- [`SocketAddr` is now stable on Redox.][52656]

Stabilized APIs
---------------
- [`Arc::downcast`]
- [`Iterator::flatten`]
- [`Rc::downcast`]

Cargo
-----
- [Cargo can silently fix some bad lockfiles ][cargo/5831] You can use
  `--locked` to disable this behaviour.
- [`cargo-install` will now allow you to cross compile an install
  using `--target`][cargo/5614]
- [Added the `cargo-fix` subcommand to automatically move project code from
  2015 edition to 2018.][cargo/5723]

Misc
----
- [`rustdoc` now has the `--cap-lints` option which demotes all lints above
  the specified level to that level.][52354] For example `--cap-lints warn`
  will demote `deny` and `forbid` lints to `warn`.
- [`rustc` and `rustdoc` will now have the exit code of `1` if compilation
  fails, and `101` if there is a panic.][52197]
- [A preview of clippy has been made available through rustup.][51122]
  You can install the preview with `rustup component add clippy-preview`

Compatibility Notes
-------------------
- [`str::{slice_unchecked, slice_unchecked_mut}` are now deprecated.][51807]
  Use `str::get_unchecked(begin..end)` instead.
- [`std::env::home_dir` is now deprecated for its unintuitive behaviour.][51656]
  Consider using the `home_dir` function from
  https://crates.io/crates/dirs instead.
- [`rustc` will no longer silently ignore invalid data in target spec.][52330]

[52861]: rust-lang/rust#52861
[52656]: rust-lang/rust#52656
[52239]: rust-lang/rust#52239
[52330]: rust-lang/rust#52330
[52354]: rust-lang/rust#52354
[52402]: rust-lang/rust#52402
[52103]: rust-lang/rust#52103
[52197]: rust-lang/rust#52197
[51807]: rust-lang/rust#51807
[51899]: rust-lang/rust#51899
[51912]: rust-lang/rust#51912
[51511]: rust-lang/rust#51511
[51619]: rust-lang/rust#51619
[51656]: rust-lang/rust#51656
[51178]: rust-lang/rust#51178
[51122]: rust-lang/rust#51122
[50494]: rust-lang/rust#50494
[cargo/5614]: rust-lang/cargo#5614
[cargo/5723]: rust-lang/cargo#5723
[cargo/5831]: rust-lang/cargo#5831
[`Arc::downcast`]: https://doc.rust-lang.org/std/sync/struct.Arc.html#method.downcast
[`Iterator::flatten`]: https://doc.rust-lang.org/std/iter/trait.Iterator.html#method.flatten
[`Rc::downcast`]: https://doc.rust-lang.org/std/rc/struct.Rc.html#method.downcast

vldmkr pushed a commit to iolitelabs/iolite-parity that referenced this pull request Oct 8, 2018

Replace `std::env::home_dir` with `dirs::home_dir` (#9077)
`std::env::home_dir` is deprecated but will probably take a while until
it is deprecated on stable. For more info see rust-lang/rust#51656
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.