Raise a helpful error for {{ index }} outside an items expansion (fixes #186)#232
Merged
Conversation
…ixes #186) `index` is only seeded inside with_items / with_items_range / with_items_from_csv / with_items_from_file requests. Referencing it elsewhere panicked (or warned under --relaxed-interpolations) with a generic "Unknown 'index' variable" message that gave no clue about the cause. Detect that specific case and raise a dedicated message explaining the variable's scope and suggesting `iteration` for the current iteration number. Also document the built-in interpolation variables in SYNTAX.md.
Closed
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.
Fixes #186.
Problem
{{ index }}is only seeded into the context insidewith_items,with_items_range,with_items_from_csvandwith_items_from_filerequests, where it is the item's position in the list. Referencing it anywhere else (e.g. a plain request, or anassertonindex) failed with a generic, unhelpful message:Unknown 'index' variable!--relaxed-interpolationsNeither gave any clue that
indexis scope-limited, and the user almost always actually wanted the iteration counter — which already exists asiteration.Fix
Rather than overloading
indexwith a second meaning, detect this specific case and raise a dedicated message that explains the variable's scope and points toiteration:The message is used both for the strict-mode panic and the relaxed-mode warning.
with_items*requests are unaffected —indexstill resolves there as before.Docs
Documented the built-in interpolation variables (
base,iteration,item,index) andindex's scope inSYNTAX.md.Verification
Unknown 'index' variable!.with_itemsrequest still resolves{{ index }}(e.g./a/0,/b/1).index_outside_expansion_suggests_iteration.cargo fmt,cargo clippy -- -D warnings,cargo test(62) all pass.