Skip to content

fix: correct byte indexing for non-ASCII Jinja interpolations - #6099

Merged
max-sixty merged 2 commits into
mainfrom
fix/jinja-non-ascii-span
Jul 25, 2026
Merged

fix: correct byte indexing for non-ASCII Jinja interpolations#6099
max-sixty merged 2 commits into
mainfrom
fix/jinja-non-ascii-span

Conversation

@prql-bot

Copy link
Copy Markdown
Collaborator

Problem

jinja::find_span (used by prqlc compile --watch for dbt-style Jinja+PRQL) walked the source with source.chars().enumerate(), which yields char indices, but then sliced the result with &source[start_index..end_index], which indexes by bytes. For ASCII source the two coincide, so the bug was latent. As soon as a multi-byte character appears before an interpolation the indices diverge, producing a mis-sliced span — and, if a boundary lands inside a multi-byte character, a panic (byte index is not a char boundary). Panicking on user input is explicitly disallowed by CLAUDE.md.

Concretely, from café = {{ source('salesforce', 'in_process') }} extracted "= {{ source('salesforce', 'in_process') }" (shifted one byte: it absorbs the = and drops the closing }) instead of " {{ source('salesforce', 'in_process') }}", so the reconstructed SQL was corrupted.

Solution

Iterate with source.char_indices() (byte offsets) and advance the end index by char.len_utf8() instead of + 1. The line/column matching logic is unchanged, so ASCII behavior — including the pre-existing benign leading-space quirk and all existing snapshots — is identical; only multi-byte input is corrected.

Testing

Added test_non_ascii_before_interpolation in the existing jinja test module, which fails on main (corrupted span) and passes with the fix. All existing jinja tests continue to pass unchanged.

find_span used char indices from chars().enumerate() to slice source by
bytes, corrupting (or panicking on) spans when the source contained
multi-byte characters before an interpolation. Use char_indices() and
len_utf8() so the slice stays on char boundaries.
@max-sixty
max-sixty merged commit 1410e34 into main Jul 25, 2026
36 checks passed
@max-sixty
max-sixty deleted the fix/jinja-non-ascii-span branch July 25, 2026 20:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants