Skip to content

Comments

Ensure whitespace-only if branches still execute assign side effects#923

Merged
sebastienros merged 2 commits intomainfrom
copilot/fix-iswhitespaceorcommentonly-bug
Feb 16, 2026
Merged

Ensure whitespace-only if branches still execute assign side effects#923
sebastienros merged 2 commits intomainfrom
copilot/fix-iswhitespaceorcommentonly-bug

Conversation

Copy link
Contributor

Copilot AI commented Feb 16, 2026

IfStatement could short-circuit elsif/else branches marked IsWhitespaceOrCommentOnly, which skipped execution of non-output statements (notably assign). This caused branch-local state (e.g. media_list_to_show) to remain unset even when the branch condition matched.

  • Execution semantics in IfStatement

    • Removed early returns in the false-condition path that skipped matched elsif/else branches when IsWhitespaceOrCommentOnly == true.
    • Matched branches now always run through their own WriteToAsync, preserving existing whitespace suppression while executing side effects.
  • Regression coverage

    • Added focused tests in IfStatementTests for:
      • assign inside a whitespace-only else branch
      • assign inside a whitespace-only elsif branch
    • Both assert that context state is updated after branch execution.
  • Behavioral impact

    • No change to rendered output behavior for whitespace/comment-only branches.
    • Restores expected side-effect behavior for stateful tags inside those branches.
{% if false %}
{% elsif true %}
  {% assign x = 'value' %}
{% else %}
  {% assign x = 'other' %}
{% endif %}
{{ x }}  {# now resolves to "value" instead of remaining undefined #}
Original prompt

This section details on the original issue you should resolve

<issue_title>Bug: Marking assign IsWhitespaceOrCommentOnly sometimes prevents the assignment from execution</issue_title>
<issue_description>This is caused by #909
At least when the assign is the only statement in the else branch of an if statement, the assignment will not be executed, as the whole else branch will be marked IsWhitespaceOrCommentOnly and therefore later the statements that actually produce output will render nothing, as there is no input evaluated. It might not be the only case with IsWhitespaceOrCommentOnly, but the one I've encountered

Example from Horizon's card-gallery.liquid

    {% liquid
      # Check if there are generic media that are not variant images

      # [ ]
      assign variant_images = product.images | where: 'attached_to_variant?', true | map: 'src'
      # 2
      assign generic_media_size = all_media.size | minus: variant_images.size
      # 0
      assign combined_listing_count = product.options_with_values | map: 'values' | map: 'product_url' | compact | size

      # false
      if combined_listing_count > 0 or has_applied_colour_filter 
        ...
      else
        # should be executed, but actually not, so it's null
        assign media_list_to_show = all_media 
      endif
      ...
     
      # empty
      capture slides
        for media in media_list_to_show
          capture slideshow_children
            render 'product-media', media: media, sizes: sizes, loading: loading, preview_image_only: true
          endcapture
          ...
          render 'slideshow-slide', slide_id: media.id, index: forloop.index, hidden: hidden, class: class, attributes: attributes, children: slideshow_children, media_fit: media_fit
      endcapture 
    %}
    <a>
      {% render 'slideshow', 
        # renders empty
        ref: 'slideshow',
        initial_slide: 0,
        slides: slides,
        slide_count: all_media.size,
        show_arrows: show_arrows,
        attributes: slideshow_attributes
      %}
    </a>
```</issue_description>

## Comments on the Issue (you are @copilot in this section)

<comments>
</comments>

💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Co-authored-by: sebastienros <1165805+sebastienros@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix bug preventing assignment execution due to IsWhitespaceOrCommentOnly Ensure whitespace-only if branches still execute assign side effects Feb 16, 2026
Copilot AI requested a review from sebastienros February 16, 2026 17:34
@sebastienros sebastienros marked this pull request as ready for review February 16, 2026 18:03
@sebastienros sebastienros merged commit 0d949db into main Feb 16, 2026
3 checks passed
@sebastienros sebastienros deleted the copilot/fix-iswhitespaceorcommentonly-bug branch February 16, 2026 18:04
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.

Bug: Marking assign IsWhitespaceOrCommentOnly sometimes prevents the assignment from execution

2 participants