diff --git a/src/hyperlink.rs b/src/hyperlink.rs index 4ddd3efad..83cf3a1de 100644 --- a/src/hyperlink.rs +++ b/src/hyperlink.rs @@ -42,16 +42,21 @@ fn encode(f: &mut Formatter, byte: u8) -> fmt::Result { #[cfg(unix)] fn host() -> &'static str { - use std::sync::OnceLock; + use std::{env, sync::OnceLock}; static HOSTNAME: OnceLock = OnceLock::new(); HOSTNAME .get_or_init(|| { - nix::unistd::gethostname() - .ok() - .and_then(|h| h.into_string().ok()) - .unwrap_or_default() + env::var("WSL_DISTRO_NAME").map_or_else( + |_| { + nix::unistd::gethostname() + .ok() + .and_then(|h| h.into_string().ok()) + .unwrap_or_default() + }, + |distro| format!("wsl$/{distro}"), + ) }) .as_ref() } diff --git a/tests/tests.rs b/tests/tests.rs index 3d11cdcda..e7b032847 100644 --- a/tests/tests.rs +++ b/tests/tests.rs @@ -2,10 +2,10 @@ mod testenv; #[cfg(unix)] use nix::unistd::{Gid, Group, Uid, User}; -use std::fs; use std::io::Write; use std::path::Path; use std::time::{Duration, SystemTime}; +use std::{env, fs}; use test_case::test_case; use normpath::PathExt; @@ -2677,10 +2677,19 @@ fn test_gitignore_parent() { fn test_hyperlink() { let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES); - #[cfg(unix)] - let hostname = nix::unistd::gethostname().unwrap().into_string().unwrap(); #[cfg(not(unix))] - let hostname = ""; + let hostname = String::new(); + + #[cfg(unix)] + let hostname = env::var("WSL_DISTRO_NAME").map_or_else( + |_| { + nix::unistd::gethostname() + .ok() + .and_then(|h| h.into_string().ok()) + .unwrap_or_default() + }, + |distro| format!("wsl$/{distro}"), + ); let expected = format!( "\x1b]8;;file://{}{}/a.foo\x1b\\a.foo\x1b]8;;\x1b\\",