Skip to content

Preserve the current Arbre element when within(nil) raises - #757

Open
javierjulio wants to merge 1 commit into
masterfrom
fix-context-element-buffer
Open

javierjulio wants to merge 1 commit into
masterfrom
fix-context-element-buffer

Conversation

@javierjulio

Copy link
Copy Markdown
Member

within(nil) raised ArgumentError before it pushed an element, but cleanup still popped the active element. If the caller rescued the error, later tags could attach to the wrong parent or fail to build.

Validate the element → push it → run the block → pop it on exit

The regression spec confirms that within(nil) raises and that a new tag still attaches to the original parent. It fails on master and passes with the fix. All 181 specs passed on Rails 8.0 and 8.1.

Low risk and easy to revert. The fix changes only buffer cleanup after argument validation fails.

@javierjulio javierjulio self-assigned this Nov 30, 2026
@codecov

codecov Bot commented Nov 30, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (7ba87c0) to head (61e344c).

Additional details and impacted files
@@            Coverage Diff            @@
##            master      #757   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           17        17           
  Lines          459       459           
=========================================
  Hits           459       459           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@javierjulio
javierjulio force-pushed the fix-context-element-buffer branch 2 times, most recently from 80c26ab to 5336ca0 Compare December 3, 2026 01:04
Context#with_current_arbre_element (aliased as #within) validates its
argument and raises ArgumentError when given nil. The raise sat inside
the method body, which is covered by the method-level `ensure`, so the
`ensure` still ran and popped the element buffer even though nothing had
been pushed.

That pop removed an element the caller still owned. At the top level it
removed the context itself, leaving current_arbre_element as nil, so any
further building after rescuing the error failed with a confusing
NoMethodError on nil instead of adding to the context. Inside a nested
block it silently dropped the enclosing element, so later tags were
attached to the wrong parent.

Validate the argument before pushing, and wrap only the yield in
begin/ensure so the buffer is popped only when something was pushed.

This is safe and non-breaking:

- With a non-nil element the behavior is unchanged: the element is
  pushed, the block runs, and the element is popped even if the block
  raises.
- With nil the same ArgumentError is raised with the same message. The
  only difference is that the buffer is no longer corrupted, which no
  caller could have relied on, since it left the context unusable.
- The public method signature, the #within alias and the return value
  (the block's result) are unchanged.

Add a spec that fails without this change: after rescuing the error the
current element is still the context, and new tags are added to it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@javierjulio
javierjulio force-pushed the fix-context-element-buffer branch from 5336ca0 to 61e344c Compare December 3, 2026 01:09

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants