fix: correct byte indexing for non-ASCII Jinja interpolations - #6099
Merged
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
jinja::find_span(used byprqlc compile --watchfor dbt-style Jinja+PRQL) walked the source withsource.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 byCLAUDE.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 bychar.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_interpolationin the existingjinjatest module, which fails onmain(corrupted span) and passes with the fix. All existing jinja tests continue to pass unchanged.