{"record":{"id":"7673e179d37eeb79","repo":"puppetlabs/puppet","slug":"attempt-to-redefine-method-method-with-block","errorCode":null,"errorMessage":"Attempt to redefine method %{method} with block","messagePattern":"Attempt to redefine method %(.+?) with block","errorType":"exception","errorClass":"ArgumentError","httpStatus":null,"severity":"error","filePath":"lib/puppet/property.rb","lineNumber":172,"sourceCode":"  #   to use?\n  # @option options [Symbol] :invalidate_refreshes Indicates a change on this property should invalidate and\n  #   remove any scheduled refreshes (from notify or subscribe) targeted at the same resource. For example, if\n  #   a change in this property takes into account any changes that a scheduled refresh would have performed,\n  #   then the scheduled refresh would be deleted.\n  # @option options [Object] any Any other option is treated as a call to a setter having the given\n  #   option name (e.g. `:required_features` calls `required_features=` with the option's value as an\n  #   argument).\n  #\n  # @dsl type\n  # @api public\n  def self.newvalue(name, options = {}, &block)\n    value = value_collection.newvalue(name, options, &block)\n\n    unless value.method.nil?\n      method = value.method.to_sym\n      if value.block\n        if instance_methods(false).include?(method)\n          raise ArgumentError, _(\"Attempt to redefine method %{method} with block\") % { method: method }\n        end\n\n        define_method(method, &value.block)\n      else\n        # Let the method be an alias for calling the providers setter unless we already have this method\n        alias_method(method, :call_provider) unless method_defined?(method)\n      end\n    end\n    value\n  end\n\n  # Calls the provider setter method for this property with the given value as argument.\n  # @return [Object] what the provider returns when calling a setter for this property's name\n  # @raise [Puppet::Error] when the provider can not handle this property.\n  # @see #set\n  # @api private\n  #\n  def call_provider(value)","sourceCodeStart":154,"sourceCodeEnd":190,"githubUrl":"https://github.com/puppetlabs/puppet/blob/e227c27540975c25aa22d533a52424a9d2fc886a/lib/puppet/property.rb#L154-L190","documentation":"newvalue(:name, :method => :sym, &block) optionally defines an instance method :sym on the property class, implemented by the given block; without a block :sym is aliased to call_provider. Puppet refuses to overwrite an already-defined instance method with a block (ArgumentError 'Attempt to redefine method') because silent replacement would hide bugs. Puppet::Property::Ensure itself defines :create and :destroy this way, so those names are classic collisions.","triggerScenarios":"A property declares newvalue(:present, :method => :create) { ... } while :create already exists - inherited from Property::Ensure or defined earlier in the same class; the same value is registered twice, the second time with :method pointing at a method the first registration created.","commonSituations":"Custom ensure-like properties copying the ensure idiom; module upgrades that add methods colliding with existing newvalue methods; inheriting from core property classes.","solutions":["Pick a method name that is not already defined (e.g. :make_present instead of :create).","Drop the :method option so the value aliases to call_provider, and implement the behavior provider-side instead.","Drop the block and define the method yourself (define_method) where you control collisions explicitly."],"exampleFix":"# before (custom type)\nnewvalue(:present, :method => :create) do\n  provider.create      # ArgumentError: 'create' already defined by Puppet::Property::Ensure\nend\n\n# after\nnewvalue(:present, :method => :make_present) do\n  provider.create\nend","handlingStrategy":"validation","validationCode":"method_sym = :make_present\nraise ArgumentError, \"method :#{method_sym} already defined\" if instance_methods(false).include?(method_sym)\nnewvalue(:present, :method => method_sym) { provider.create }","typeGuard":"def free_value_method?(klass, sym)\n  !klass.instance_methods(false).include?(sym) && !klass.method_defined?(sym)\nend","tryCatchPattern":null,"preventionTips":["Avoid :method names already used by core properties (:create, :destroy from Puppet::Property::Ensure).","Register each value once; check the property class for existing methods before wiring blocks.","Search the whole hierarchy (method_defined?) before defining methods through newvalue."],"tags":["puppet","custom-type","method-redefinition","type-dsl"],"backgroundTag":"method-name-collision","analyzedSha":"e227c27540975c25aa22d533a52424a9d2fc886a","analyzedAt":"2026-08-21T20:49:46.650Z","schemaVersion":2},"datasetVersion":"2026-08-21T23:17:16.201Z"}