uutils/coreutils · error · io::Error

OS string cannot be converted to bytes

Error message

OS string cannot be converted to bytes

What it means

Error "OS string cannot be converted to bytes" thrown in uutils/coreutils.

Source

Thrown at src/uucore/src/lib/mods/display.rs:93

        {
            self.write_all(buf.as_encoded_bytes())
        }

        #[cfg(not(any(unix, target_os = "wasi")))]
        {
            // It's possible to write a better OsWrite impl for Windows consoles (e.g. Stdout)
            // as those are fundamentally 16-bit. If the OS string is invalid then it can be
            // encoded to 16-bit and written using raw windows_sys calls. But this is quite involved
            // (see `sys/pal/windows/stdio.rs` in the stdlib) and the value-add is small.
            //
            // There's no way to write invalid OS strings to Windows files, as those are 8-bit.

            match buf.to_str() {
                Some(text) => self.write_all(text.as_bytes()),
                // We could output replacement characters instead, but the
                // stdlib errors when sending invalid UTF-8 to the console,
                // so let's follow that.
                None => Err(io::Error::new(
                    io::ErrorKind::InvalidData,
                    "OS string cannot be converted to bytes",
                )),
            }
        }
    }
}

// We do not have a blanket impl for all Write because a smarter Windows impl should
// be able to make use of AsRawHandle. Please keep this in mind when adding new impls.
impl OsWrite for File {}
impl OsWrite for Stdout {}
impl OsWrite for StdoutLock<'_> {}
// A future smarter Windows implementation can first flush the BufWriter before
// doing a raw write.
impl<W: OsWrite> OsWrite for BufWriter<W> {}

impl OsWrite for Box<dyn OsWrite> {

View on GitHub (pinned to 0b77ced485)

Solutions

  1. Use Unix-style OsStrExt bytes APIs on Unix, or convert via to_string_lossy when exact bytes are not required.
  2. Run on a platform whose OS strings support byte conversion.

When it happens

Trigger: Occurs when converting an OS string to bytes on a platform where that conversion is unsupported.

Common situations: Displaying non-UTF-8 file names on platforms without OsStr byte access.


AI-assisted analysis of uutils/coreutils@0b77ced485 (2026-08-19). Data as JSON: /api/errors/7b46de2191c606dd. Report an issue: GitHub.