Shopify/liquid · error · Liquid::SyntaxError

Bare bracket access is not allowed. Use #

Error message

Bare bracket access is not allowed. Use #{Expression::SELF}['...'] instead

What it means

In strict2 parser mode (@reject_bare_brackets), Liquid refuses to parse a bare [ opening square bracket when starting an expression. Developers must write the explicit self reference Expression::SELF ('this') before bracket access, e.g. this['key'], because a bare bracket is ambiguous in strict mode.

Solutions

  1. Prefix bracket access with the self expression: replace ['key'] with this['key']
  2. Wrap dynamic keys in a variable and access via dot or bracketed self access
  3. Use the non-strict parser if the legacy syntax must be preserved

Example fix

<!-- before -->
{% assign x = ['a', 'b'] %}
<!-- after -->
{% assign x = this['a', 'b'] %} or use this[0]['a'] style access
Defensive patterns

Strategy: validation

Validate before calling

def bare_bracket_free?(tpl)
  !tpl.match?(/\{\{\s*\[/) && !tpl.match?(/\{%[^%]*\s\[/)
end

Try / catch

begin
  Liquid::Template.parse(tpl)
rescue Liquid::SyntaxError => e
  raise unless e.message.include?('Bare bracket access')
  # auto-fix or surface guidance to use this['...']
end

Prevention

When it happens

Trigger: Parsing a template expression that begins with [ ... ] (e.g. {{ ['key'] }} or tag markup like {% for item in ['a','b'] %}) while the parser was constructed with reject_bare_brackets: true (strict2).

Common situations: Upgrading to Shopify Liquid versions that enable strict bracket rules; themes/linters enforcing strict2; code that relied on shorthand bracket variable access.

Related errors


AI-assisted analysis of Shopify/liquid@807d45a6b3 (2026-09-08). Data as JSON: /api/errors/0f04bd439d066b33. Report an issue: GitHub.

Appendix: source

Thrown at lib/liquid/parser.rb:58

      @p += 1
      token[1]
    end

    def look(type, ahead = 0)
      tok = @tokens[@p + ahead]
      return false unless tok
      tok[0] == type
    end

    def expression
      token = @tokens[@p]
      case token[0]
      when :id
        str = consume
        str << variable_lookups
      when :open_square
        if @reject_bare_brackets
          raise SyntaxError, "Bare bracket access is not allowed. Use #{Expression::SELF}['...'] instead"
        end
        str = consume.dup
        str << expression
        str << consume(:close_square)
        str << variable_lookups
      when :string, :number
        consume
      when :open_round
        consume
        first = expression
        consume(:dotdot)
        last = expression
        consume(:close_round)
        "(#{first}..#{last})"
      else
        raise SyntaxError, "#{token} is not a valid expression"
      end
    end

View on GitHub (pinned to 807d45a6b3)