Skip to content

Commit

Permalink
use whoami::fallible::hostname() instead of env variable for hostname
Browse files Browse the repository at this point in the history
Summary:
I tested the `util::sys::hostname()` it return null on both linux and macOS. The test results are in D66772175 test plan.

In the past we moved from `whoami::devicename()` to reading hostname from env var. But it has the risk that envvar yields empty string.
As it was suggested on D56724499, I try using  `whoami::fallible::hostname()` which is the new version of deprecated  `whoami::hostname`.
see suggestion in:

https://www.internalfb.com/code/fbsource/[78677baeea59]/third-party/rust/vendor/whoami-1.5.2/src/api.rs?lines=88-95

`whoami::fallible::hostname()` returns `Result<String>` Then it should be safe. It catches and handles the errors.

Reviewed By: muirdm

Differential Revision: D66781930

fbshipit-source-id: 36f6a139110fd6c8136a719f15d92617f0858927
  • Loading branch information
kavehahmadi60 authored and facebook-github-bot committed Dec 5, 2024
1 parent a770e69 commit 88fd291
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
2 changes: 2 additions & 0 deletions eden/scm/lib/util/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ sapling-rewrite-macros = { version = "0.1.0", path = "rewrite-macros" }
shell-escape = "0.1.5"
shellexpand = "2.1.2"
thiserror = "2"
tracing = { version = "0.1.40", features = ["attributes", "valuable"] }
whoami = "1.5"

[dev-dependencies]
memmap2 = "0.5.10"
Expand Down
16 changes: 7 additions & 9 deletions eden/scm/lib/util/src/sys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,13 @@ pub fn hostname() -> String {
// Also, this may fix some debugruntest issues on mac due to whoami issues.
"test-hostname".to_owned()
} else {
std::env::var_os(if cfg!(windows) {
"COMPUTERNAME"
} else if cfg!(target_os = "macos") {
"HOST"
} else {
"HOSTNAME"
})
.and_then(|h| h.to_str().map(|s| s.to_string()))
.unwrap_or("".to_owned())
match whoami::fallible::hostname() {
Ok(hostname) => hostname,
Err(err) => {
tracing::error!(?err, "error getting hostname");
"<UNKNOWN HOSTNAME>".to_string()
}
}
}
}

Expand Down

0 comments on commit 88fd291

Please sign in to comment.