gitbutlerapp/gitbutler · error

PR template exists but must be valid UTF-8 text or markdown

Error message

PR template exists but must be valid UTF-8 text or markdown

What it means

review_template_content reads a forge PR/review template file and requires its content to be valid UTF-8. If the file exists, has a size, but fails the UTF-8 validity check (i.e. it is binary), this error is thrown. Templates must be text or markdown.

Solutions

  1. Re-encode the template file as UTF-8 (e.g. iconv -f UTF-16 -t UTF-8)
  2. Replace the file with a plain-text/markdown template
  3. Verify with `file .github/PULL_REQUEST_TEMPLATE.md` that it is ASCII/UTF-8 text

Example fix

// before (UTF-16 file at template path)
// after
$ iconv -f UTF-16 -t UTF-8 template.md -o PULL_REQUEST_TEMPLATE.md
Defensive patterns

Strategy: validation

Validate before calling

const buf = await readFile(templatePath);
if (!isValidUtf8(buf)) throw new Error('Template must be UTF-8 text');

Type guard

function isUtf8Text(buf) {
  try { new TextDecoder('utf-8', { fatal: true }).decode(buf); return true; }
  catch { return false; }
}

Try / catch

try {
  const tpl = await getPrTemplate(projectId);
} catch (e) {
  if (String(e).includes('valid UTF-8')) {
    tpl = ''; // fall back to default template
  } else throw e;
}

Prevention

When it happens

Trigger: Fetching a PR template from a repository or forge where the template file (e.g. PULL_REQUEST_TEMPLATE.md or a custom path) contains binary/non-UTF-8 bytes.

Common situations: A repo where the template file was written with a non-UTF-8 encoding (UTF-16, Latin-1 with invalid sequences) or is actually a binary file accidentally placed at the template path.

Related errors


AI-assisted analysis of gitbutlerapp/gitbutler@58e5313667 (2026-09-18). Data as JSON: /api/errors/e945aa82f7cb4315. Report an issue: GitHub.

Appendix: source

Thrown at crates/but-api/src/legacy/forge.rs:81

    let forge_repo_info = but_forge::derive_forge_repo_info(&base_remote_url)
        .context("No forge could be determined for this repository branch")?;
    let forge_push_repo_info = if base_remote_url != push_remote_url {
        Some(
            but_forge::derive_forge_repo_info(&push_remote_url)
                .context("Failed to derive forge information for the push repository")?,
        )
    } else {
        None
    };
    Ok((forge_repo_info, forge_push_repo_info))
}

fn review_template_content(file: FileInfo) -> Result<String> {
    if file.size.is_none() {
        return Ok(String::new());
    }
    if !file.is_valid_utf8() {
        anyhow::bail!("PR template exists but must be valid UTF-8 text or markdown");
    }
    Ok(file.content.unwrap_or_default())
}

/// (Deprecated) Get the list of PR template paths for the given project and forge.
/// This function is deprecated in favor of `list_available_review_templates`.
#[but_api]
#[instrument(err(Debug))]
pub fn pr_templates(ctx: &but_ctx::Context, forge: ForgeName) -> Result<Vec<String>> {
    Ok(available_review_templates(&ctx.workdir_or_fail()?, &forge))
}

/// Get the forge provider name.
///
/// This is determined by the forge the base branch is pointing to.
/// Returns no value when the project has no target yet or its target forge is unknown.
#[but_api(napi)]
#[instrument(err(Debug))]

View on GitHub (pinned to 58e5313667)