diff --git a/src/uucore/src/lib/lib.rs b/src/uucore/src/lib/lib.rs index 5c406356b..000bd23fd 100644 --- a/src/uucore/src/lib/lib.rs +++ b/src/uucore/src/lib/lib.rs @@ -328,7 +328,14 @@ static UTIL_NAME: LazyLock = LazyLock::new(|| { let is_man = usize::from(ARGV[base_index].eq("manpage")); let argv_index = base_index + is_man; - ARGV[argv_index].to_string_lossy().into_owned() + // Strip directory path to show only utility name + // (e.g., "mkdir" instead of "./target/debug/mkdir") + // in version output, error messages, and other user-facing output + std::path::Path::new(&ARGV[argv_index]) + .file_name() + .unwrap_or(&ARGV[argv_index]) + .to_string_lossy() + .into_owned() }); /// Derive the utility name. diff --git a/tests/by-util/test_mkdir.rs b/tests/by-util/test_mkdir.rs index 1c734449f..664b72023 100644 --- a/tests/by-util/test_mkdir.rs +++ b/tests/by-util/test_mkdir.rs @@ -24,6 +24,42 @@ fn test_invalid_arg() { new_ucmd!().arg("--definitely-invalid").fails_with_code(1); } +#[test] +fn test_version_no_path() { + use std::process::Command; + use uutests::get_tests_binary; + + // This test verifies that when an individual utility binary is invoked with its full path, + // the version output shows just "mkdir", not the full path like "/path/to/mkdir". + // + // Note: The multicall binary (coreutils) doesn't have this issue because it reads + // the utility name from ARGV[1], not ARGV[0]. This bug only affects individual binaries. + + let tests_binary = get_tests_binary!(); + let mkdir_binary_path = std::path::Path::new(tests_binary) + .parent() + .unwrap() + .join("mkdir"); + + // If the individual mkdir binary exists, test it + let output = if mkdir_binary_path.exists() { + // Invoke the individual mkdir binary with its full path + Command::new(&mkdir_binary_path) + .arg("--version") + .output() + .expect("Failed to execute mkdir binary") + } else { + // If only multicall binary exists, test that (it should already pass) + Command::new(tests_binary) + .args(["mkdir", "--version"]) + .output() + .expect("Failed to execute mkdir via multicall binary") + }; + + let stdout = String::from_utf8_lossy(&output.stdout); + assert!(stdout.starts_with("mkdir (uutils coreutils)")); +} + #[test] fn test_no_arg() { new_ucmd!()