{"record":{"id":"88813eeb7ac0cc83","repo":"puppetlabs/puppet","slug":"a-required-parameter-cannot-be-added-after-an-opti","errorCode":null,"errorMessage":"A required parameter cannot be added after an optional parameter","messagePattern":"A required parameter cannot be added after an optional parameter","errorType":"exception","errorClass":"ArgumentError","httpStatus":null,"severity":"error","filePath":"lib/puppet/functions.rb","lineNumber":415,"sourceCode":"    # @api private\n    def initialize(dispatcher, all_callables, loader)\n      @all_callables = all_callables\n      @dispatcher = dispatcher\n      @loader = loader\n    end\n\n    # Defines a required positional parameter with _type_ and _name_.\n    #\n    # @param type [String] The type specification for the parameter.\n    # @param name [Symbol] The name of the parameter. This is primarily used\n    #   for error message output and does not have to match an implementation\n    #   method parameter.\n    # @return [Void]\n    #\n    # @api public\n    def param(type, name)\n      internal_param(type, name)\n      raise ArgumentError, _('A required parameter cannot be added after an optional parameter') if @min != @max\n\n      @min += 1\n      @max += 1\n    end\n    alias required_param param\n\n    # Defines an optional positional parameter with _type_ and _name_.\n    # May not be followed by a required parameter.\n    #\n    # @param type [String] The type specification for the parameter.\n    # @param name [Symbol] The name of the parameter. This is primarily used\n    #   for error message output and does not have to match an implementation\n    #   method parameter.\n    # @return [Void]\n    #\n    # @api public\n    def optional_param(type, name)\n      internal_param(type, name)","sourceCodeStart":397,"sourceCodeEnd":433,"githubUrl":"https://github.com/puppetlabs/puppet/blob/e227c27540975c25aa22d533a52424a9d2fc886a/lib/puppet/functions.rb#L397-L433","documentation":"In a dispatch DSL block, param declares a required positional parameter. The builder tracks optionality with @min and @max; when @min != @max an optional parameter was already declared. A required parameter after an optional one is impossible to bind at call time, so Puppet rejects it at definition time with this ArgumentError.","triggerScenarios":"dispatch :m do optional_param 'String', :b; param 'String', :a end. The required :a is declared after the optional :b, so the raise fires.","commonSituations":"Adding a new mandatory argument to a signature that already ends with optional arguments; merges or edits that reorder param blocks mechanically.","solutions":["Reorder the declarations: all param entries first, then optional_param entries.","If the new argument must be last and mandatory, promote the earlier optional_param to param as well (note: breaking change for callers).","If both call shapes must stay supported, declare two separate dispatch blocks with different arities instead."],"exampleFix":"# before\n dispatch :run do\n   optional_param 'String', :suffix\n   param 'String', :base       # raises: required after optional\n end\n\n# after: required parameters come first\n dispatch :run do\n   param 'String', :base\n   optional_param 'String', :suffix\n end","handlingStrategy":"validation","validationCode":"# Review-time ordering rule: required params first, then optional, then repeated, then block\n# CI grep: fail if an optional_param line appears before a param line in one dispatch\n! grep -Pzo 'dispatch[^}]*optional_param[^}]*\\n[^}]*\\sparam ' lib/puppet/functions/**/*.rb","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Memorize the legal order: param, optional_param, repeated, block_param.","When adding a mandatory argument, re-check the whole block, not just the new line.","Use two dispatch blocks to support old and new call shapes instead of reordering optionals."],"tags":["puppet","functions","dispatch","parameter-order","dsl"],"backgroundTag":"parameter-order-invalid","analyzedSha":"e227c27540975c25aa22d533a52424a9d2fc886a","analyzedAt":"2026-08-21T20:49:46.650Z","schemaVersion":2},"datasetVersion":"2026-08-21T23:17:16.201Z"}