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

soc
Copy link
Contributor

@soc 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
Copy link
Collaborator

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.

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jun 20, 2018
@soc
Copy link
Contributor Author

soc commented Jun 20, 2018

@soc soc force-pushed the topic/fix-home-dir branch from 5cc03a8 to 0093ea3 Compare June 20, 2018 12:06
@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
Copy link
Member

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-api Relevant to the library API team, which will review and decide on the PR/issue. label Jun 20, 2018
@KodrAus
Copy link
Contributor

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
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
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
Copy link
Member

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

@SimonSapin
Copy link
Contributor

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

@soc
Copy link
Contributor Author

soc commented Jun 21, 2018

@SimonSapin
Copy link
Contributor

@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
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("").

@TimNN TimNN added S-waiting-on-team Status: Awaiting decision from the relevant subteam (see the T-<team> label). and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jun 26, 2018
@SimonSapin
Copy link
Contributor

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
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.

@rfcbot rfcbot added proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. disposition-close This PR / issue is in PFCP or FCP with a disposition to close it. labels Jun 27, 2018
@SimonSapin SimonSapin removed I-nominated S-waiting-on-team Status: Awaiting decision from the relevant subteam (see the T-<team> label). labels Jun 27, 2018
@shepmaster shepmaster added the S-waiting-on-team Status: Awaiting decision from the relevant subteam (see the T-<team> label). label Jun 29, 2018
@soc
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?

@bors bors merged commit 0afc16a into rust-lang:master Jul 7, 2018
@rfcbot rfcbot removed proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. disposition-close This PR / issue is in PFCP or FCP with a disposition to close it. labels Jul 7, 2018
niklasad1 added a commit to openethereum/parity-ethereum that referenced this pull request Jul 9, 2018
`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 pushed a commit to openethereum/parity-ethereum that referenced this pull request Jul 9, 2018
`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 pushed a commit to paritytech/parity-common that referenced this pull request Jul 9, 2018
`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
@tjkirch
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
Copy link
Member

Deprecation is not removal.

@tjkirch
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.

@sfackler
Copy link
Member

https://play.rust-lang.org/?gist=ca688815ab9f69a9e87e7550ed389cdc&version=nightly&mode=debug&edition=2015

@soc
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
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
`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
Timmmm added a commit to Timmmm/rust that referenced this pull request Sep 10, 2022
This improves the documentation to say *why* it was deprecated. The reason was because it reads `HOME` on Windows which is meaningless there. Note that the PR that deprecated it stated that returning an empty string if `HOME` is set to an empty string was a problem, however I can find no evidence that this is the case. `cd` handles it fine whereas if `HOME` is unset it gives an explicit `HOME not set` error.

* Original deprecation reason: https://internals.rust-lang.org/t/deprecate-or-break-fix-std-env-home-dir/7315
* Original deprecation PR: rust-lang#51656

See rust-lang#71684
Dylan-DPC added a commit to Dylan-DPC/rust that referenced this pull request Dec 11, 2022
Better documentation for env::home_dir()'s broken behaviour

This improves the documentation to say *why* it was deprecated. The reason was because it reads `HOME` on Windows which is meaningless there. Note that the PR that deprecated it stated that returning an empty string if `HOME` is set to an empty string was a problem, however I can find no evidence that this is the case. `cd` handles it fine whereas if `HOME` is unset it gives an explicit `HOME not set` error.

* Original deprecation reason: https://internals.rust-lang.org/t/deprecate-or-break-fix-std-env-home-dir/7315
* Original deprecation PR: rust-lang#51656

See rust-lang#71684
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Dec 11, 2022
Better documentation for env::home_dir()'s broken behaviour

This improves the documentation to say *why* it was deprecated. The reason was because it reads `HOME` on Windows which is meaningless there. Note that the PR that deprecated it stated that returning an empty string if `HOME` is set to an empty string was a problem, however I can find no evidence that this is the case. `cd` handles it fine whereas if `HOME` is unset it gives an explicit `HOME not set` error.

* Original deprecation reason: https://internals.rust-lang.org/t/deprecate-or-break-fix-std-env-home-dir/7315
* Original deprecation PR: rust-lang#51656

See rust-lang#71684
thomcc pushed a commit to tcdi/postgrestd that referenced this pull request May 31, 2023
This improves the documentation to say *why* it was deprecated. The reason was because it reads `HOME` on Windows which is meaningless there. Note that the PR that deprecated it stated that returning an empty string if `HOME` is set to an empty string was a problem, however I can find no evidence that this is the case. `cd` handles it fine whereas if `HOME` is unset it gives an explicit `HOME not set` error.

* Original deprecation reason: https://internals.rust-lang.org/t/deprecate-or-break-fix-std-env-home-dir/7315
* Original deprecation PR: rust-lang/rust#51656

See #71684
thomcc pushed a commit to tcdi/postgrestd that referenced this pull request May 31, 2023
Better documentation for env::home_dir()'s broken behaviour

This improves the documentation to say *why* it was deprecated. The reason was because it reads `HOME` on Windows which is meaningless there. Note that the PR that deprecated it stated that returning an empty string if `HOME` is set to an empty string was a problem, however I can find no evidence that this is the case. `cd` handles it fine whereas if `HOME` is unset it gives an explicit `HOME not set` error.

* Original deprecation reason: https://internals.rust-lang.org/t/deprecate-or-break-fix-std-env-home-dir/7315
* Original deprecation PR: rust-lang/rust#51656

See #71684
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.