Shopify / liquid

Liquid markup language. Safe, customer facing template language for flexible web apps.
https://shopify.github.io/liquid/
MIT License
11.13k stars 1.39k forks source link

parse_for_liquid_tag errors without line numbers #1734

Open jg-rp opened 1 year ago

jg-rp commented 1 year ago

While adding some nested liquid tag test cases to the golden liquid test suite, I was getting errors like this:

  1) Error:
Golden::liquid.golden.liquid_tag#test_0001_bare liquid tag in liquid tag:
NoMethodError: undefined method `-' for nil:NilClass

            parse_context.line_number -= 1
                                      ^
    /home/james/.rbenv/versions/3.1.4/lib/ruby/gems/3.1.0/bundler/gems/liquid-5e92b3a89a11/lib/liquid/block_body.rb:50:in `parse_for_liquid_tag'
    /home/james/.rbenv/versions/3.1.4/lib/ruby/gems/3.1.0/bundler/gems/liquid-5e92b3a89a11/lib/liquid/block_body.rb:111:in `parse_liquid_tag'
    /home/james/.rbenv/versions/3.1.4/lib/ruby/gems/3.1.0/bundler/gems/liquid-5e92b3a89a11/lib/liquid/block_body.rb:145:in `parse_for_document'
    /home/james/.rbenv/versions/3.1.4/lib/ruby/gems/3.1.0/bundler/gems/liquid-5e92b3a89a11/lib/liquid/block_body.rb:29:in `parse'
    /home/james/.rbenv/versions/3.1.4/lib/ruby/gems/3.1.0/bundler/gems/liquid-5e92b3a89a11/lib/liquid/document.rb:55:in `parse_body'
    /home/james/.rbenv/versions/3.1.4/lib/ruby/gems/3.1.0/bundler/gems/liquid-5e92b3a89a11/lib/liquid/document.rb:23:in `parse'
    /home/james/.rbenv/versions/3.1.4/lib/ruby/gems/3.1.0/bundler/gems/liquid-5e92b3a89a11/lib/liquid/document.rb:7:in `parse'
    /home/james/.rbenv/versions/3.1.4/lib/ruby/gems/3.1.0/bundler/gems/liquid-5e92b3a89a11/lib/liquid/template.rb:111:in `parse'
    /home/james/.rbenv/versions/3.1.4/lib/ruby/gems/3.1.0/bundler/gems/liquid-5e92b3a89a11/lib/liquid/template.rb:97:in `parse'
    golden_liquid.rb:25:in `block (5 levels) in <main>'

Adding line_numbers: true to Liquid::Template.parse solved the problem.

This is can be reproduced with the following:

require 'liquid'

template = <<~LIQUID
{%- liquid
    liquid
    echo 'foo'
-%}
LIQUID

Liquid::Template.parse(template).render!()
john-goncalves-hc commented 8 months ago

I stumbled upon the same issue, any news on this?

john-goncalves-hc commented 8 months ago

@jg-rp did a quick search on liquid gem's tests for "line_number" and it turns out we have to Liquid::Template.parse(template, line_numbers: true).render!()!

Hope it helps 👋