vercel/turborepo · critical

Unable to encode '{s}'

Error message

Unable to encode '{s}'

What it means

While serializing yarn berry (v2+) lockfiles, wrap_string() decides whether a string can be written bare using a YAML-safety regex; complex strings are quoted via serde_json::to_string. Serializing a &str cannot fail, so the unwrap_or_else(panic! "Unable to encode") branch is effectively unreachable and exists only to satisfy the Result.

Source

Thrown at crates/turborepo-lockfiles/src/berry/ser.rs:202

        if let Some(unplugged) = meta.unplugged {
            add_line("unplugged", unplugged);
        }
        if let Some(built) = meta.built {
            add_line("built", built);
        }
    }

    builder.s
}

fn wrap_string(s: &str) -> Cow<'_, str> {
    let simple_string = regex!(r#"^[^-?:,\]\[{}#&*!|>'"%@` \t\r\n]([ \t]*[^,\]\[{}:# \t\r\n])*$"#);
    match simple_string.is_match(s) {
        // Simple strings require no wrapping
        true => Cow::from(s),
        // Complex strings require wrapping
        false => {
            Cow::from(serde_json::to_string(s).unwrap_or_else(|_| panic!("Unable to encode '{s}'")))
        }
    }
}

#[cfg(test)]
mod test {
    use pretty_assertions::assert_eq;

    use super::*;

    #[test]
    fn test_metadata_display() {
        let metadata = Metadata {
            version: "6".into(),
            cache_key: Some("8c0".to_string()),
        };
        assert_eq!(
            metadata.to_string(),

View on GitHub (pinned to f9245100cf)

Solutions

  1. Upgrade turborepo — newer versions restructure or remove the unwrap
  2. If it recurs, build with debug symbols, capture the failing string, and report an issue
Defensive patterns

Strategy: fallback

Try / catch

let result = std::panic::catch_unwind(|| serialize_berry(lockfile));
if result.is_err() {
    // keep the original lockfile on disk and report — never half-write on panic
}

Prevention

When it happens

Trigger: No realistic trigger: it would require serde_json to fail while encoding a valid UTF-8 string slice. If observed, it indicates memory corruption or a serde_json bug, both extraordinary.

Common situations: Should not occur in practice; would surface as an abrupt process abort while turbo writes a berry lockfile during conversion.

Related errors


AI-assisted analysis of vercel/turborepo@f9245100cf (2026-08-17). Data as JSON: /api/errors/03787119b16a4d9f. Report an issue: GitHub.