{"record":{"id":"54fa8f829a5941d6","repo":"puppetlabs/puppet","slug":"the-code-loaded-from-source-ref-produced-mis-ma-54fa8f","errorCode":null,"errorMessage":"The code loaded from %{source_ref} produced mis-matched name, expected '%{type_name}', got %{created_name}","messagePattern":"The code loaded from %(.+?) produced mis-matched name, expected '%(.+?)', got %(.+?)","errorType":"exception","errorClass":"ArgumentError","httpStatus":null,"severity":"error","filePath":"lib/puppet/pops/loader/ruby_function_instantiator.rb","lineNumber":30,"sourceCode":"  # @param source_ref [URI, String] a reference to the source / origin of the ruby code to evaluate\n  # @param ruby_code_string [String] ruby code in a string\n  #\n  # @return [Puppet::Pops::Functions.Function] - an instantiated function with global scope closure associated with the given loader\n  #\n  def self.create(loader, typed_name, source_ref, ruby_code_string)\n    unless ruby_code_string.is_a?(String) && ruby_code_string =~ /Puppet::Functions\\.create_function/\n      raise ArgumentError, _(\"The code loaded from %{source_ref} does not seem to be a Puppet 4x API function - no create_function call.\") % { source_ref: source_ref }\n    end\n\n    # make the private loader available in a binding to allow it to be passed on\n    loader_for_function = loader.private_loader\n    here = get_binding(loader_for_function)\n    created = eval(ruby_code_string, here, source_ref, 1) # rubocop:disable Security/Eval\n    unless created.is_a?(Class)\n      raise ArgumentError, _(\"The code loaded from %{source_ref} did not produce a Function class when evaluated. Got '%{klass}'\") % { source_ref: source_ref, klass: created.class }\n    end\n    unless created.name.to_s == typed_name.name()\n      raise ArgumentError, _(\"The code loaded from %{source_ref} produced mis-matched name, expected '%{type_name}', got %{created_name}\") % { source_ref: source_ref, type_name: typed_name.name, created_name: created.name }\n    end\n\n    # create the function instance - it needs closure (scope), and loader (i.e. where it should start searching for things\n    # when calling functions etc.\n    # It should be bound to global scope\n\n    # Sets closure scope to nil, to let it be picked up at runtime from Puppet.lookup(:global_scope)\n    # If function definition used the loader from the binding to create a new loader, that loader wins\n    created.new(nil, loader_for_function)\n  end\n\n  # Produces a binding where the given loader is bound as a local variable (loader_injected_arg). This variable can be used in loaded\n  # ruby code - e.g. to call Puppet::Function.create_loaded_function(:name, loader,...)\n  #\n  def self.get_binding(loader_injected_arg)\n    binding\n  end\n  private_class_method :get_binding","sourceCodeStart":12,"sourceCodeEnd":48,"githubUrl":"https://github.com/puppetlabs/puppet/blob/e227c27540975c25aa22d533a52424a9d2fc886a/lib/puppet/pops/loader/ruby_function_instantiator.rb#L12-L48","documentation":"Raised by RubyFunctionInstantiator when the class produced by eval has a name different from the typed name. The symbol passed to Puppet::Functions.create_function must exactly match (case-sensitive == on to_s) the function name the loader binds, which is derived from the module layout and file path (e.g. mymod::double in lib/puppet/functions/mymod/double.rb).","triggerScenarios":"`created.name.to_s != typed_name.name`: e.g. create_function(:'mymod::foo') in a file loaded as mymod::bar, or a missing module namespace segment (defining :double where :mymod::double is expected).","commonSituations":"Renaming a function in code but not the file (or vice versa); omitting the module namespace during 3x-to-4x migration; copy-pasted functions with stale names.","solutions":["Match the create_function symbol exactly to the namespaced name derived from the file path","Rename the file or the symbol so both agree","Remember this check is case-sensitive, unlike the data-type instantiator's casecmp"],"exampleFix":"# before - lib/puppet/functions/mymod/double.rb\nPuppet::Functions.create_function(:'mymod::twice') do ... end\n\n# after\nPuppet::Functions.create_function(:'mymod::double') do ... end","handlingStrategy":"try-catch","validationCode":"m = ruby_source.match(/Puppet::Functions\\.create_function\\(\\s*:?'([^']+)'|Puppet::Functions\\.create_function\\(\\s*:?\"([^\"]+)\"/)\nexpected = namespaced_name_from_path(path)\nwarn \"name mismatch: file says #{m && (m[1] || m[2])}, path says #{expected}\" if m && (m[1] || m[2]) != expected","typeGuard":null,"tryCatchPattern":"begin\n  fn = loader.load_typed(typed_name)\nrescue ArgumentError => e\n  raise unless e.message.include?('mis-matched name')\n  warn \"function name mismatch: #{e.message}\"\nend","preventionTips":["Namespace 4x function symbols as 'module::name' matching the file path","Rename file and symbol together","Watch case: this comparison is case-sensitive"],"tags":["puppet","function","naming","ruby","loader"],"backgroundTag":"puppet-definition-name-mismatch","analyzedSha":"e227c27540975c25aa22d533a52424a9d2fc886a","analyzedAt":"2026-08-21T20:49:46.650Z","schemaVersion":2},"datasetVersion":"2026-08-21T23:17:16.201Z"}