{"id":"39f7e7f8d076ebbb","repo":"BurntSushi/ripgrep","slug":"hostname-could-not-be-found-on-unsupported-platfor","errorCode":null,"errorMessage":"hostname could not be found on unsupported platform","messagePattern":"hostname could not be found on unsupported platform","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/cli/src/hostname.rs","lineNumber":28,"sourceCode":"///\n/// On Windows, this currently uses the \"physical DNS hostname\" computer name.\n/// This may change in the future.\n///\n/// On Unix, this returns the result of the `gethostname` function from the\n/// `libc` linked into the program.\npub fn hostname() -> io::Result<OsString> {\n    #[cfg(windows)]\n    {\n        use winapi_util::sysinfo::{ComputerNameKind, get_computer_name};\n        get_computer_name(ComputerNameKind::PhysicalDnsHostname)\n    }\n    #[cfg(unix)]\n    {\n        gethostname()\n    }\n    #[cfg(not(any(windows, unix)))]\n    {\n        Err(io::Error::new(\n            io::ErrorKind::Other,\n            \"hostname could not be found on unsupported platform\",\n        ))\n    }\n}\n\n#[cfg(unix)]\nfn gethostname() -> io::Result<OsString> {\n    use std::os::unix::ffi::OsStringExt;\n\n    // SAFETY: There don't appear to be any safety requirements for calling\n    // sysconf.\n    let limit = unsafe { libc::sysconf(libc::_SC_HOST_NAME_MAX) };\n    if limit == -1 {\n        // It is in theory possible for sysconf to return -1 for a limit but\n        // *not* set errno, in which case, io::Error::last_os_error is\n        // indeterminate. But untangling that is super annoying because std\n        // doesn't expose any unix-specific APIs for inspecting the errno. (We","sourceCodeStart":10,"sourceCodeEnd":46,"githubUrl":"https://github.com/BurntSushi/ripgrep/blob/3fce3b5bb0236da2df6d99672afb8a719642eca7/crates/cli/src/hostname.rs#L10-L46","documentation":"Returned by hostname() when compiled for a target that is neither Windows nor Unix (the function body is gated behind #[cfg(not(any(windows, unix)))]). On such targets there is no libc gethostname and no Win32 computer-name API to call, so the routine cannot produce a hostname and fails immediately. It exists so the crate compiles on exotic tier-3 targets like wasm32 while still erroring cleanly at runtime.","triggerScenarios":"Calling grep_cli::hostname() (directly or transitively through a tool that embeds it) on a target triple such as wasm32-wasi, wasm32-unknown-unknown, or any redox/wasix build where neither the windows nor unix cfg branch is selected.","commonSituations":"Cross-compiling a ripgrep-based binary or library to WebAssembly; running grep tooling inside a wasm sandbox/edge runtime; building for an embedded niche target that lacks a host-name concept.","solutions":["If you do not actually need wasm/redox support, build for a supported target (x86_64-linux, aarch64-apple-darwin, or any windows/msvc target) so the windows or unix branch compiles instead.","If you must run on the unsupported platform, supply the hostname yourself from an environment variable or config and avoid calling hostname().","Feature-gate or stub the call site that invokes hostname() when targeting the unsupported platform, returning a sensible default."],"exampleFix":"// before\nlet host = grep_cli::hostname()?;\n\n// after (guarded for wasm/unsupported targets)\n#[cfg(any(windows, unix))]\nlet host = grep_cli::hostname()?;\n#[cfg(not(any(windows, unix)))]\nlet host = std::env::var(\"HOSTNAME\").unwrap_or_default();","handlingStrategy":"validation","validationCode":"// Only call hostname() on supported targets.\nconst SUPPORTED: bool = cfg!(any(windows, unix));\nfn can_get_hostname() -> bool { SUPPORTED }","typeGuard":null,"tryCatchPattern":"let host = grep_cli::hostname();\nif let Err(e) = host {\n    // fall back to env or a default; expected on wasm/non-unix-non-windows\n    log::warn!(\"hostname unavailable: {e}\");\n}","preventionTips":["Gate hostname() calls behind cfg!(any(windows, unix)) at the call site.","On unsupported targets, source the hostname from an env var or config instead.","Document platform support so downstream callers know hostname() can fail."],"tags":["platform","hostname","wasm","cfg"],"analyzedSha":"3fce3b5bb0236da2df6d99672afb8a719642eca7","analyzedAt":"2026-08-06T01:39:05.073Z","schemaVersion":2}