puppetlabs/puppet · error · ArgumentError

A required repeated parameter cannot be added after an optio

Error message

A required repeated parameter cannot be added after an optional parameter

What it means

required_repeated_param declares a 1..N positional parameter. Like param, it is rejected when an optional parameter was already declared (@min != @max), because a mandatory argument cannot follow an optional one. The repeated parameter may only appear last, or just before a block parameter.

Source

Thrown at lib/puppet/functions.rb:465

    def repeated_param(type, name)
      internal_param(type, name, true)
      @max = :default
    end
    alias optional_repeated_param repeated_param

    # Defines a repeated positional parameter with _type_ and _name_ that may occur 1 to "infinite" number of times.
    # It may only appear last or just before a block parameter.
    #
    # @param type [String] The type specification for the parameter.
    # @param name [Symbol] The name of the parameter. This is primarily used
    #   for error message output and does not have to match an implementation
    #   method parameter.
    # @return [Void]
    #
    # @api public
    def required_repeated_param(type, name)
      internal_param(type, name, true)
      raise ArgumentError, _('A required repeated parameter cannot be added after an optional parameter') if @min != @max

      @min += 1
      @max = :default
    end

    # Defines one required block parameter that may appear last. If type and name is missing the
    # default type is "Callable", and the name is "block". If only one
    # parameter is given, then that is the name and the type is "Callable".
    #
    # @api public
    def block_param(*type_and_name)
      case type_and_name.size
      when 0
        type = @all_callables
        name = :block
      when 1
        type = @all_callables
        name = type_and_name[0]

View on GitHub (pinned to e227c27540)

Solutions

  1. Change the trailing capture to optional_repeated_param if the values may be absent (0..N is legal after an optional).
  2. Or make every preceding parameter required (param) so nothing optional precedes the repeated one.
  3. Alternatively split into two dispatch blocks: one for the optional tail, one for the variadic shape.

Example fix

# before
 dispatch :run do
   optional_param 'String', :prefix
   required_repeated_param 'String', :rest   # raises
 end

# after: use the optional repeated form after an optional parameter
 dispatch :run do
   optional_param 'String', :prefix
   optional_repeated_param 'String', :rest
 end
Defensive patterns

Strategy: validation

Validate before calling

# CI grep: required_repeated_param must not follow an optional_param in one dispatch
! grep -Pzo 'dispatch[^}]*optional_param[^}]*\n[^}]*required_repeated_param' lib/puppet/functions/**/*.rb

Prevention

When it happens

Trigger: dispatch :m do optional_param 'String', :b; required_repeated_param 'String', :rest end. The mandatory repeated parameter after the optional :b raises at definition time.

Common situations: Converting a trailing optional parameter list into a variadic capture while an older optional_param stays in place; signature growth over time.

Related errors


AI-assisted analysis of puppetlabs/puppet@e227c27540 (2026-08-21). Data as JSON: /api/errors/59971ada0da9fb23. Report an issue: GitHub.