{"record":{"id":"508ab5329cb65924","repo":"puppetlabs/puppet","slug":"the-code-loaded-from-source-ref-produced-mis-ma-508ab5","errorCode":null,"errorMessage":"The code loaded from %{source_ref} produced mis-matched name, expected 'function_%{type_name}', got '%{created_name}'","messagePattern":"The code loaded from %(.+?) produced mis-matched name, expected 'function_%(.+?)', got '%(.+?)'","errorType":"exception","errorClass":"ArgumentError","httpStatus":null,"severity":"error","filePath":"lib/puppet/pops/loader/ruby_legacy_function_instantiator.rb","lineNumber":51,"sourceCode":"    # Avoid reloading the function if already loaded via one of the APIs that trigger 3x function loading\n    # Check if function is already loaded the 3x way (and obviously not the 4x way since we would not be here in the\n    # first place.\n    environment = Puppet.lookup(:current_environment)\n    func_info = Puppet::Parser::Functions.environment_module(environment).get_function_info(typed_name.name.to_sym)\n    if func_info.nil?\n      # This will do the 3x loading and define the \"function_<name>\" and \"real_function_<name>\" methods\n      # in the anonymous module used to hold function definitions.\n      #\n      func_info = eval(ruby_code_string, here, source_ref, 1) # rubocop:disable Security/Eval\n\n      # Validate what was loaded\n      unless func_info.is_a?(Hash)\n        # TRANSLATORS - the word 'newfunction' should not be translated as it is a method name.\n        raise ArgumentError, _(\"Illegal legacy function definition! The code loaded from %{source_ref} did not return the result of calling 'newfunction'. Got '%{klass}'\") % { source_ref: source_ref, klass: func_info.class }\n      end\n\n      unless func_info[:name] == \"function_#{typed_name.name()}\"\n        raise ArgumentError, _(\"The code loaded from %{source_ref} produced mis-matched name, expected 'function_%{type_name}', got '%{created_name}'\") % {\n          source_ref: source_ref, type_name: typed_name.name, created_name: func_info[:name]\n        }\n      end\n    end\n\n    created = Puppet::Functions::Function3x.create_function(typed_name.name(), func_info, loader_for_function)\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,...)","sourceCodeStart":33,"sourceCodeEnd":69,"githubUrl":"https://github.com/puppetlabs/puppet/blob/e227c27540975c25aa22d533a52424a9d2fc886a/lib/puppet/pops/loader/ruby_legacy_function_instantiator.rb#L33-L69","documentation":"Raised by RubyLegacyFunctionInstantiator when the loaded function's info Hash reports a different function name than expected. The check compares func_info[:name] (set by newfunction) against \"function_#{typed_name.name}\", so the symbol passed to newfunction must match the file name: double.rb must call newfunction(:double, ...).","triggerScenarios":"File bar.rb calls newfunction(:baz, ...): the returned Hash's :name is 'function_baz' while the loader expects 'function_bar' (typed name derived from the file name).","commonSituations":"Renaming a legacy function file without updating the newfunction symbol; copy-pasted function files; case differences in the symbol.","solutions":["Make the newfunction symbol match the file name exactly","Rename the file to match the symbol if the symbol is the desired name","Re-check after any rename so file name and symbol stay in sync"],"exampleFix":"# before - lib/puppet/parser/functions/bar.rb\nmodule Puppet::Parser::Functions\n  newfunction(:baz, :type => :rvalue) { |a| a[0] }\nend\n\n# after - lib/puppet/parser/functions/bar.rb\nmodule Puppet::Parser::Functions\n  newfunction(:bar, :type => :rvalue) { |a| a[0] }\nend","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"begin\n  fn = loader.load_typed(typed_name)\nrescue ArgumentError => e\n  raise unless e.message.include?(\"expected 'function_\")\n  warn \"legacy function name mismatch: #{e.message}\"\nend","preventionTips":["Keep the newfunction symbol identical to the file name","Rename file and symbol together","Add a CI check that scans lib/puppet/parser/functions/*.rb and compares the symbol to the basename"],"tags":["puppet","legacy-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"}