jdx/mise · error

truncated text: bytes is not a whole number of code units

Error message

truncated {label} text: {} bytes is not a whole number of code units

What it means

PowerShell's `Out-File` can emit UTF-16 text; `decode_text` decodes UTF-16LE/BE only when a BOM is present and requires the byte length to be a whole number of 2-byte code units. Odd-length input means the byte stream is truncated or misidentified, so decoding is refused instead of producing corrupted text.

Solutions

  1. Re-capture the output without truncation so byte length is even
  2. Strip the broken trailing byte only if you know it's a cut artifact, then retry
  3. Force the producer to emit UTF-8 (e.g. `Out-File -Encoding utf8`) so the UTF-16 path is skipped

Example fix

// before
command | Out-File out.txt   # utf16, truncated capture
// after
command | Out-File out.txt -Encoding utf8
Defensive patterns

Strategy: validation

Validate before calling

function isEvenLength(buf) {
  return buf.length % 2 === 0; // check before decoding UTF-16 captures
}
// also prefer UTF-8: strip BOMs and validate with TextDecoder('utf-8', { fatal: true })

Type guard

function hasUtf16Bom(bytes) {
  return (bytes[0] === 0xff && bytes[1] === 0xfe) || (bytes[0] === 0xfe && bytes[1] === 0xff);
}
const isDecodableUtf16 = (bytes) => hasUtf16Bom(bytes) && bytes.length % 2 === 0;

Try / catch

try {
  text = decodeText(bytes);
} catch (e) {
  if (String(e).includes('truncated')) {
    text = new TextDecoder('utf-8').decode(bytes.subarray(0, bytes.length - (bytes.length % 2 === 0 ? 0 : 1)));
  } else throw e;
}

Prevention

When it happens

Trigger: Calling `from_utf16` (via `decode_text`) with bytes whose length is not a multiple of 2 while a UTF-16 BOM selects that decoder — typically a truncated capture of PowerShell output.

Common situations: Capturing/truncating PowerShell `Out-File` output mid-code-unit; piping partial output through a tool that cut the stream at an odd byte boundary; corrupted log files.

Understand the failure class

Background: "Invalid ... format", "must be in format X", "does not look like a ..." — invalid argument format errors across CLI tools and libraries — this error's family across 17 libraries.

Related errors


AI-assisted analysis of jdx/mise@533346cc37 (2026-09-17). Data as JSON: /api/errors/4ec0f5f7be20d6e3. Report an issue: GitHub.

Appendix: source

Thrown at src/file.rs:660

/// ordinary on Windows -- see [`decode_text`], which names them.
pub(crate) fn strip_utf8_bom(s: &str) -> &str {
    s.strip_prefix('\u{feff}').unwrap_or(s)
}

/// Decode text that may begin with a byte-order mark.
///
/// `std`'s UTF-8-only readers reject UTF-16 outright, which is how a checksum file sank an install
/// in #5399: PowerShell shipped `hashes.sha256` as UTF-16LE and mise stopped at "stream did not
/// contain valid UTF-8". Windows PowerShell 5.1's `Out-File` writes UTF-16LE by default, so any
/// project generating checksums that way produces the same thing.
///
/// Only a BOM switches the encoding. Detecting UTF-16 without one means guessing from the density
/// of NUL bytes, which can misfire on binary input; `Out-File` always writes a BOM, so the guess
/// buys nothing here. Input with no BOM is decoded as UTF-8, exactly as before.
pub(crate) fn decode_text(bytes: &[u8]) -> Result<String> {
    fn from_utf16(bytes: &[u8], to_u16: fn([u8; 2]) -> u16, label: &str) -> Result<String> {
        if !bytes.len().is_multiple_of(2) {
            bail!(
                "truncated {label} text: {} bytes is not a whole number of code units",
                bytes.len()
            );
        }
        let units = bytes
            .as_chunks::<2>()
            .0
            .iter()
            .map(|c| to_u16(*c))
            .collect_vec();
        String::from_utf16(&units).wrap_err_with(|| format!("invalid {label} text"))
    }

    match bytes {
        [0xef, 0xbb, 0xbf, rest @ ..] => {
            String::from_utf8(rest.to_vec()).wrap_err("invalid UTF-8 text after a UTF-8 BOM")
        }
        [0xff, 0xfe, rest @ ..] => from_utf16(rest, u16::from_le_bytes, "UTF-16LE"),

View on GitHub (pinned to 533346cc37)