config: fall back to $USER if username couldn't be obtained by libc
Some checks are pending
binaries / Build binary artifacts (push) Waiting to run
nix / flake check (push) Waiting to run
build / build (, macos-13) (push) Waiting to run
build / build (, macos-14) (push) Waiting to run
build / build (, ubuntu-latest) (push) Waiting to run
build / build (, windows-latest) (push) Waiting to run
build / build (--all-features, ubuntu-latest) (push) Waiting to run
build / Build jj-lib without Git support (push) Waiting to run
build / Check protos (push) Waiting to run
build / Check formatting (push) Waiting to run
build / Run doctests (push) Waiting to run
build / Check that MkDocs can build the docs (push) Waiting to run
build / Check that MkDocs can build the docs with latest Python and uv (push) Waiting to run
build / cargo-deny (advisories) (push) Waiting to run
build / cargo-deny (bans licenses sources) (push) Waiting to run
build / Clippy check (push) Waiting to run
Codespell / Codespell (push) Waiting to run
website / prerelease-docs-build-deploy (ubuntu-latest) (push) Waiting to run
Scorecards supply-chain security / Scorecards analysis (push) Waiting to run

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.
This commit is contained in:
Yuya Nishihara 2025-01-03 00:10:35 +09:00
parent fcac7ed39c
commit 9aff586655

View file

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