vercel/next.js · error

failed to convert rope into string

Error message

failed to convert rope into string

What it means

While parsing an ECMAScript module, Turbopack converts the file content (held in a Rope) to a UTF-8 string via BytesStr::from_utf8. If the bytes are not valid UTF-8, conversion fails. Rather than crashing, Turbopack emits an Issue (Warning when loose_errors is set, Error otherwise), reports the source location, and marks the file as Unparsable so the build can continue or fail gracefully.

Source

Thrown at turbopack/crates/turbopack-ecmascript/src/parse.rs:396

async fn parse_file_content(
    program_source: Rope,
    fs_path: &FileSystemPath,
    ident: &str,
    query: RcStr,
    file_path_hash: u128,
    source: ResolvedVc<Box<dyn Source>>,
    ty: EcmascriptModuleAssetType,
    transforms: &[EcmascriptInputTransform],
    node_env: RcStr,
    loose_errors: bool,
    inline_helpers: bool,
) -> Result<Vc<ParseResult>> {
    let string = match BytesStr::from_utf8(program_source.clone().into_bytes()) {
        Ok(s) => s,
        Err(error) => {
            let error: RcStr = PrettyPrintError(
                &anyhow::anyhow!(error).context("failed to convert rope into string"),
            )
            .to_string()
            .into();
            ReadSourceIssue {
                // Technically we could supply byte offsets to the issue source, but
                // that would cause another utf8 error to be produced when we
                // attempt to infer line/column
                // offsets
                source: IssueSource::from_source_only(source),
                error: error.clone(),
                severity: if loose_errors {
                    IssueSeverity::Warning
                } else {
                    IssueSeverity::Error
                },
            }
            .resolved_cell()
            .emit();

View on GitHub (pinned to 0ae8c72462)

Solutions

  1. Re-save the offending file as UTF-8 (most editors offer 'Save with Encoding → UTF-8').
  2. Verify the file is actually source code and not binary; check its first bytes with a hex viewer.
  3. If vendored, re-fetch the package from npm or re-clone the file from git to undo corruption.
  4. Add a `.gitattributes` rule (`*.js text eol=lf`) to prevent encoding/line-ending mangling.

Example fix

# terminal: re-encode the file to UTF-8
iconv -f LATIN1 -t UTF-8 bad.js > fixed.js && mv fixed.js bad.js
Defensive patterns

Strategy: validation

Validate before calling

// Pre-check that a source file is valid UTF-8 before bundling (Node.js).
const fs = require('fs');
function assertUtf8(file) {
  const buf = fs.readFileSync(file);
  // Node throws on invalid sequences when decoding
  new TextDecoder('utf-8', { fatal: true }).decode(buf);
}

Prevention

When it happens

Trigger: A .js/.jsx/.ts/.tsx/.mjs/.cjs file containing byte sequences that are not valid UTF-8 — e.g. Latin-1/GBK/Shift-JIS encoded text, binary content accidentally given a JS extension, or a file corrupted during git transfer/merge.

Common situations: Source files saved by editors using a legacy locale encoding on Windows; binary blobs mislabeled with a script extension; files damaged by incorrect line-ending or encoding normalization; vendored third-party code containing mojibake.

Related errors


AI-assisted analysis of vercel/next.js@0ae8c72462 (2026-08-06). Data as JSON: /api/errors/8f35754614a5cfbb. Report an issue: GitHub.