Skip to content

Fix Include resolve named argument values when StrictVariables is enabled#648

Closed
Benkei wants to merge 10 commits intoscriban:masterfrom
Benkei:master
Closed

Fix Include resolve named argument values when StrictVariables is enabled#648
Benkei wants to merge 10 commits intoscriban:masterfrom
Benkei:master

Conversation

@Benkei
Copy link
Copy Markdown
Contributor

@Benkei Benkei commented Mar 9, 2026

This PR fixes a bug that occurs when StrictVariables is enabled during the Include process.

I hope this fixes the problem correctly.

@xoofx
Copy link
Copy Markdown
Member

xoofx commented Mar 10, 2026

Could you elaborate what is it solving? What I see is that with strict variables, we silently discard a runtime error, which feels... counter-intuitive? What is the use case and problem that this PR is trying to solve?

@Benkei Benkei marked this pull request as draft March 10, 2026 20:12
@Benkei
Copy link
Copy Markdown
Contributor Author

Benkei commented Mar 10, 2026

Adjustment for #643 regarding templates in StrictVariables mode.

When using 'include', the code checks whether named parameters collide with current local variables.

Problem:
{{ include 'name' x: 'hello' }}
The named parameter 'x' is not defined in the current scope, so the lookup would trigger a ScriptRuntimeException.

I have refactored the variable resolution logic and implemented a non-throwing TryGetValue method in TemplateContext.

Test Changes:

  • Parameterized the entire TestIncludes class using [TestFixtureSource]
  • It now runs in two modes: with StrictVariables enabled and disabled
  • Refactored/fixed several tests

I hope this isn't too excessive 😅

@Benkei Benkei marked this pull request as ready for review March 10, 2026 23:32
@xoofx
Copy link
Copy Markdown
Member

xoofx commented Mar 11, 2026

I'm not sure that the previous fix #643 was actually the right thing to do, and looking at this PR, we are building maybe more workaround on something that should be simpler.

Template arguments are transformed to local variables (my_arg => $my_arg) and in the code here we are saving previous local and restoring them, and then we have to deal with restrict variables.

TemplateContext actually has a PushLocal/PopLocal that creates a new local context for local variable. We should probably use that instead, along the PushOutput/PopOutput that we already have for RenderTemplate. That way we won't have to save/restore anything manually, and the code should be simpler/safer.

Probably closing this PR and if you want to open a new PR with my proposal above, it will be welcome, thank you.

@xoofx xoofx closed this Mar 11, 2026
@xoofx xoofx added the invalid label Mar 11, 2026
@Benkei
Copy link
Copy Markdown
Contributor Author

Benkei commented Mar 11, 2026

Thanks for the review. I'll check it again.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants