Skip to content

Commit bec587e

Browse files
authored
Merge pull request rails#53959 from byroot/template-tracker-recursion
Handle cyclic template dependencies
2 parents 45cb290 + c2ff85e commit bec587e

File tree

5 files changed

+28
-2
lines changed

5 files changed

+28
-2
lines changed

actionview/lib/action_view/digestor.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,12 @@ def dependency_digest(finder, stack)
107107
end.join("-")
108108
end
109109

110-
def to_dep_map
111-
children.any? ? { name => children.map(&:to_dep_map) } : name
110+
def to_dep_map(seen = Set.new.compare_by_identity)
111+
if seen.add?(self)
112+
children.any? ? { name => children.map { |c| c.to_dep_map(seen) } } : name
113+
else # the tree has a cycle
114+
name
115+
end
112116
end
113117
end
114118

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<% if some_condition %>
2+
<%= render partial: "cycle_b" %>
3+
<% end %>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<% if some_other_condition %>
2+
<%= render partial: "cycle_a" %>
3+
<% end %>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<%= render partial: "cycle_a" %>

actionview/test/template/digestor_test.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,21 @@ def test_digest_cache_cleanup_with_recursion
296296
assert_equal first_digest, second_digest
297297
end
298298

299+
def test_digest_cache_with_cycle
300+
expected_deps = [
301+
{
302+
"comments/cycle_a" => [
303+
{
304+
"comments/cycle_b" => [
305+
"comments/cycle_a",
306+
],
307+
},
308+
],
309+
},
310+
]
311+
assert_equal expected_deps, nested_dependencies("comments/cycle")
312+
end
313+
299314
def test_digest_cache_cleanup_with_recursion_and_template_caching_off
300315
disable_resolver_caching do
301316
first_digest = digest("level/_recursion")

0 commit comments

Comments
 (0)