Skip to content

Commit

Permalink
config: fall back to $USER if username couldn't be obtained by libc
Browse files Browse the repository at this point in the history
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 jj-vcs#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.
  • Loading branch information
yuja committed Jan 4, 2025
1 parent cffa5bb commit 036e605
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion cli/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
{
Expand All @@ -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
Expand Down

0 comments on commit 036e605

Please sign in to comment.