From 9aff5866559ad4f62709ff34b194bf9144183926 Mon Sep 17 00:00:00 2001 From: Yuya Nishihara Date: Fri, 3 Jan 2025 00:10:35 +0900 Subject: [PATCH] config: fall back to $USER if username couldn't be obtained by libc If jj is compiled against musl libc, not all name services are available, and getpwuid() can return null even if the system is configured properly. That's the problem reported as #5231. Suppose operation.username exists mainly for logging/tracing purposes, it should be better to include something less reliable than leaving the field empty. This patch also removes TODO comment about empty hostname/username. It's unlikely that the hostname is invalid (as that would cause panic on older jj versions), and $USER would probably be set on Unix. --- cli/src/config.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/cli/src/config.rs b/cli/src/config.rs index 25a4e2495..c3f6041b4 100644 --- a/cli/src/config.rs +++ b/cli/src/config.rs @@ -444,7 +444,6 @@ const OP_USERNAME: &str = "operation.username"; /// Environment variables that should be overridden by config values fn env_base_layer() -> ConfigLayer { let mut layer = ConfigLayer::empty(ConfigSource::EnvBase); - // TODO: warn if hostname/username is empty after loading config files? if let Ok(value) = whoami::fallible::hostname() .inspect_err(|err| tracing::warn!(?err, "failed to get hostname")) { @@ -454,6 +453,10 @@ fn env_base_layer() -> ConfigLayer { .inspect_err(|err| tracing::warn!(?err, "failed to get username")) { layer.set_value(OP_USERNAME, value).unwrap(); + } else if let Ok(value) = env::var("USER") { + // On Unix, $USER is set by login(1). Use it as a fallback because + // getpwuid() of musl libc appears not (fully?) supporting nsswitch. + layer.set_value(OP_USERNAME, value).unwrap(); } if !env::var("NO_COLOR").unwrap_or_default().is_empty() { // "User-level configuration files and per-instance command-line arguments