Skip to content

Commit

Permalink
Don't cause set tags for current_path to disappear in nested
Browse files Browse the repository at this point in the history
interpretation
  • Loading branch information
jasmith-hs committed Sep 11, 2024
1 parent d0f329e commit bc15907
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.hubspot.jinjava.interpret.JinjavaInterpreter;
import com.hubspot.jinjava.interpret.MetaContextVariables;
import com.hubspot.jinjava.lib.tag.SetTag;
import com.hubspot.jinjava.loader.RelativePathResolver;
import com.hubspot.jinjava.tree.TagNode;
import com.hubspot.jinjava.util.EagerReconstructionUtils;
import com.hubspot.jinjava.util.PrefixToPreserveState;
Expand Down Expand Up @@ -53,9 +54,14 @@ public String run(TagNode tagNode, JinjavaInterpreter interpreter) {
expression,
interpreter
);
boolean triedResolve = false;
if (
eagerExecutionResult.getResult().isFullyResolved() &&
!interpreter.getContext().isDeferredExecutionMode()
!interpreter.getContext().isDeferredExecutionMode() &&
(Arrays
.stream(variables)
.noneMatch(RelativePathResolver.CURRENT_PATH_CONTEXT_KEY::equals) ||
interpreter.getContext().getPenultimateParent().getDeferredTokens().isEmpty()) // Prevents set tags from disappearing in nested interpretation
) {
EagerReconstructionUtils.commitSpeculativeBindings(
interpreter,
Expand All @@ -67,6 +73,7 @@ public String run(TagNode tagNode, JinjavaInterpreter interpreter) {
eagerExecutionResult,
interpreter
);
triedResolve = true;
if (maybeResolved.isPresent()) {
return maybeResolved.get();
}
Expand All @@ -77,10 +84,7 @@ public String run(TagNode tagNode, JinjavaInterpreter interpreter) {
eagerExecutionResult,
interpreter
);
if (
eagerExecutionResult.getResult().isFullyResolved() &&
interpreter.getContext().isDeferredExecutionMode()
) {
if (eagerExecutionResult.getResult().isFullyResolved() && !triedResolve) {
attemptResolve(tagNode, variables, eagerExecutionResult, interpreter);
}
return buildImage(tagNode, variables, eagerExecutionResult, triple, interpreter);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.google.common.io.Resources;
import com.hubspot.jinjava.interpret.JinjavaInterpreter;
import com.hubspot.jinjava.interpret.JinjavaInterpreter.InterpreterScopeClosable;
import com.hubspot.jinjava.loader.RelativePathResolver;
import com.hubspot.jinjava.mode.DefaultExecutionMode;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
Expand Down Expand Up @@ -147,6 +148,12 @@ public String getFixtureTemplate(String name) {
.getContext()
.getCurrentPathStack()
.push(String.format("%s/%s.jinja", path, name), 0, 0);
interpreter
.getContext()
.put(
RelativePathResolver.CURRENT_PATH_CONTEXT_KEY,
String.format("%s/%s.jinja", path, name)
);
}
return simplify(
Resources.toString(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,7 @@ public void itDefersTripleLayer() {
removeDeferredContextKeys();
context.put("a_val", "a");
// There are some extras due to deferred values copying up the context stack.
context.getDeferredTokens().clear();
assertThat(interpreter.render(result).trim())
.isEqualTo(
interpreter.render(
Expand Down Expand Up @@ -363,6 +364,7 @@ public void itDefersQuadLayer() {
removeDeferredContextKeys();

context.put("a_val", "a");
context.getDeferredTokens().clear();
assertThat(interpreter.render(result).trim()).isEqualTo("12345 cbaabaaba");
}

Expand Down Expand Up @@ -655,6 +657,7 @@ public void itDoesNotSilentlyOverrideVariable() {
"{{ vars.foo }}"
);
interpreter.getContext().put("deferred", "resolved");
context.getDeferredTokens().clear();
assertThat(interpreter.render(result)).isEqualTo("ab");
}

Expand Down Expand Up @@ -687,6 +690,7 @@ public void itDoesNotSilentlyOverrideVariableWithoutAlias() {
);

interpreter.getContext().put("deferred", "resolved");
context.getDeferredTokens().clear();
assertThat(interpreter.render(result)).isEqualTo("ab");
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
Starting current_path: starting path
Intermediate current_path: starting path
Starting current_path: eager/wraps-macro-that-would-change-current-path-in-child-scope/test.jinja
Intermediate current_path: eager/wraps-macro-that-would-change-current-path-in-child-scope/test.jinja
{% for __ignored__ in [0] %}\
{% set __temp_meta_current_path_629250870__,current_path = 'eager/wraps-macro-that-would-change-current-path-in-child-scope/test.jinja', 'eager/wraps-macro-that-would-change-current-path-in-child-scope/dir2/included.jinja' %}\
{% set foo = deferred %}
{{ foo }}\
{% set current_path,__temp_meta_current_path_629250870__ = 'eager/wraps-macro-that-would-change-current-path-in-child-scope/test.jinja', null %}\
{% print foo %}\
{% endfor %}
Ending current_path: starting path
Ending current_path: eager/wraps-macro-that-would-change-current-path-in-child-scope/test.jinja

0 comments on commit bc15907

Please sign in to comment.