{"record":{"id":"6940a815c4b246c4","repo":"puppetlabs/puppet","slug":"the-argument-code-string-must-be-a-string-got","errorCode":null,"errorMessage":"The argument 'code_string' must be a String, got %{type}","messagePattern":"The argument 'code_string' must be a String, got %(.+?)","errorType":"exception","errorClass":"ArgumentError","httpStatus":null,"severity":"error","filePath":"lib/puppet/pal/compiler.rb","lineNumber":144,"sourceCode":"    #\n    def evaluate_literal(ast)\n      catch :not_literal do\n        return Puppet::Pops::Evaluator::LiteralEvaluator.new().literal(ast)\n      end\n      # TRANSLATORS, the 'ast' is the name of a parameter, do not translate\n      raise ArgumentError, _(\"The given 'ast' does not represent a literal value\")\n    end\n\n    # Parses and validates a puppet language string and returns an instance of Puppet::Pops::Model::Program on success.\n    # If the content is not valid an error is raised.\n    #\n    # @param code_string [String] a puppet language string to parse and validate\n    # @param source_file [String] an optional reference to a file or other location in angled brackets\n    # @return [Puppet::Pops::Model::Program] returns a `Program` instance on success\n    #\n    def parse_string(code_string, source_file = nil)\n      unless code_string.is_a?(String)\n        raise ArgumentError, _(\"The argument 'code_string' must be a String, got %{type}\") % { type: code_string.class }\n      end\n\n      internal_evaluator.parse_string(code_string, source_file)\n    end\n\n    # Parses and validates a puppet language file and returns an instance of Puppet::Pops::Model::Program on success.\n    # If the content is not valid an error is raised.\n    #\n    # @param file [String] a file with puppet language content to parse and validate\n    # @return [Puppet::Pops::Model::Program] returns a `Program` instance on success\n    #\n    def parse_file(file)\n      unless file.is_a?(String)\n        raise ArgumentError, _(\"The argument 'file' must be a String, got %{type}\") % { type: file.class }\n      end\n\n      internal_evaluator.parse_file(file)\n    end","sourceCodeStart":126,"sourceCodeEnd":162,"githubUrl":"https://github.com/puppetlabs/puppet/blob/e227c27540975c25aa22d533a52424a9d2fc886a/lib/puppet/pal/compiler.rb#L126-L162","documentation":"Compiler#parse_string parses and validates Puppet language source but first requires the argument to be a String. Unlike evaluate_string there is no nil special case here: parse_string(nil) raises ArgumentError with 'got NilClass'. The message reports the actual class, making the mismatch obvious.","triggerScenarios":"parse_string(nil) from an unset option key; parse_string(:sym) from keyword-style code; passing a File or IO object instead of its contents.","commonSituations":"Optional config keys feeding the parser without defaults; reading a file but passing the handle instead of File.read output; Symbols leaking from options hashes into PAL calls.","solutions":["Read the source fully first (File.read) and pass the resulting String","Default nil options to '' or skip the parse when the value is blank","Coerce Symbols intentionally with to_s before parsing"],"exampleFix":"# before\ncompiler.parse_string(options[:code])   # nil -> ArgumentError\n\n# after\ncompiler.parse_string(options[:code] || '')","handlingStrategy":"type-guard","validationCode":null,"typeGuard":"def code_string?(value)\n  value.is_a?(String)\nend\n\nraise ArgumentError, 'code_string must be a String' unless code_string?(code)","tryCatchPattern":null,"preventionTips":["Remember parse_string has no nil special case; default blank options to ''","Pass File.read output, never an IO handle"],"tags":["pal","type-check","argument","parse"],"backgroundTag":"wrong-argument-type","analyzedSha":"e227c27540975c25aa22d533a52424a9d2fc886a","analyzedAt":"2026-08-21T20:49:46.650Z","schemaVersion":2},"datasetVersion":"2026-08-21T23:17:16.201Z"}