From 630551a610380a0ea9952c60b1b215318def7b82 Mon Sep 17 00:00:00 2001 From: Saul Gutierrez Date: Fri, 3 May 2024 12:30:33 -0700 Subject: [PATCH] util: use env vars instead of Rust crates for determining host and usernames Summary: Using `whoami::devicename` causes issues on macOS sometimes, plus it gives us funny looking hostnames on POSIX in general, so let's drop it. Using environment variables to getting this value as well as getting the username is probably fine, so let's do that instead. Reviewed By: mitrandir77 Differential Revision: D56724499 fbshipit-source-id: b36a08a48121cecfdb34ed33f68422d687b0346d --- eden/scm/lib/journal/src/lib.rs | 2 +- eden/scm/lib/util/Cargo.toml | 1 - eden/scm/lib/util/TARGETS | 1 - eden/scm/lib/util/src/sys.rs | 23 +++++++++++++++++------ 4 files changed, 18 insertions(+), 9 deletions(-) diff --git a/eden/scm/lib/journal/src/lib.rs b/eden/scm/lib/journal/src/lib.rs index ec0fdd1176..5d862b927b 100644 --- a/eden/scm/lib/journal/src/lib.rs +++ b/eden/scm/lib/journal/src/lib.rs @@ -141,7 +141,7 @@ impl Journal { let command = util::sys::shell_escape(raw_args); let timestamp = hgtime::HgTime::now() .context("unable to determine current time when writing to journal")?; - let user = util::sys::username(); + let user = util::sys::username()?; let command = if let Some((left, _)) = command.split_once('\n') { format!("{} ...", left) } else { diff --git a/eden/scm/lib/util/Cargo.toml b/eden/scm/lib/util/Cargo.toml index a79b9663b5..aaffec9002 100644 --- a/eden/scm/lib/util/Cargo.toml +++ b/eden/scm/lib/util/Cargo.toml @@ -16,7 +16,6 @@ rand = { version = "0.8", features = ["small_rng"] } shell-escape = "0.1.5" shellexpand = "2.1.2" thiserror = "1.0.49" -whoami = "1.4" [dev-dependencies] memmap2 = "0.5.10" diff --git a/eden/scm/lib/util/TARGETS b/eden/scm/lib/util/TARGETS index a6c18d04e3..da8ab6882b 100644 --- a/eden/scm/lib/util/TARGETS +++ b/eden/scm/lib/util/TARGETS @@ -70,7 +70,6 @@ rust_library( "fbsource//third-party/rust:shell-escape", "fbsource//third-party/rust:shellexpand", "fbsource//third-party/rust:thiserror", - "fbsource//third-party/rust:whoami", "//eden/scm/lib/atomicfile:atomicfile", "//eden/scm/lib/lazystr:lazystr", ], diff --git a/eden/scm/lib/util/src/sys.rs b/eden/scm/lib/util/src/sys.rs index 6b57d34bf9..5bf893ac77 100644 --- a/eden/scm/lib/util/src/sys.rs +++ b/eden/scm/lib/util/src/sys.rs @@ -7,23 +7,34 @@ use std::borrow::Cow; +use anyhow::Context; +use anyhow::Result; + pub fn hostname() -> String { if std::env::var_os("TESTTMP").is_some() || cfg!(test) { // Doesn't seem like we want to use the real hostname in tests. // Also, this may fix some debugruntest issues on mac due to whoami issues. "test-hostname".to_owned() } else { - // On Mac this calls system library functions, so may not be post fork safe. - // This could be one of the reasons chg doesn't work on mac anymore... - whoami::devicename() + std::env::var_os(if cfg!(windows) { + "COMPUTERNAME" + } else if cfg!(macos) { + "HOST" + } else { + "HOSTNAME" + }) + .map_or(None, |h| h.to_str().map(|s| s.to_string())) + .unwrap_or("".to_owned()) } } -pub fn username() -> String { +pub fn username() -> Result { if std::env::var_os("TESTTMP").is_some() || cfg!(test) { - "test".to_owned() + Ok("test".to_owned()) } else { - whoami::username() + std::env::var_os(if cfg!(windows) { "USERNAME" } else { "USER" }) + .context("to get username") + .map(|k| k.to_string_lossy().to_string()) } }