{"record":{"id":"422f8ad71cc78544","repo":"puppetlabs/puppet","slug":"the-argument-puppet-code-must-be-a-string-got","errorCode":null,"errorMessage":"The argument 'puppet_code' must be a String, got %{type}","messagePattern":"The argument 'puppet_code' must be a String, got %(.+?)","errorType":"exception","errorClass":"ArgumentError","httpStatus":null,"severity":"error","filePath":"lib/puppet/pal/compiler.rb","lineNumber":89,"sourceCode":"    end\n\n    # Evaluates a string of puppet language code in top scope.\n    # A \"source_file\" reference to a source can be given - if not an actual file name, by convention the name should\n    # be bracketed with < > to indicate it is something symbolic; for example `<commandline>` if the string was given on the\n    # command line.\n    #\n    # If the given `puppet_code` is `nil` or an empty string, `nil` is returned, otherwise the result of evaluating the\n    # puppet language string. The given string must form a complete and valid expression/statement as an error is raised\n    # otherwise. That is, it is not possible to divide a compound expression by line and evaluate each line individually.\n    #\n    # @param puppet_code [String, nil] the puppet language code to evaluate, must be a complete expression/statement\n    # @param source_file [String, nil] an optional reference to a source (a file or symbolic name/location)\n    # @return [Object] what the `puppet_code` evaluates to\n    #\n    def evaluate_string(puppet_code, source_file = nil)\n      return nil if puppet_code.nil? || puppet_code == ''\n      unless puppet_code.is_a?(String)\n        raise ArgumentError, _(\"The argument 'puppet_code' must be a String, got %{type}\") % { type: puppet_code.class }\n      end\n\n      evaluate(parse_string(puppet_code, source_file))\n    end\n\n    # Evaluates a puppet language file in top scope.\n    # The file must exist and contain valid puppet language code or an error is raised.\n    #\n    # @param file [Path, String] an absolute path to a file with puppet language code, must exist\n    # @return [Object] what the last evaluated expression in the file evaluated to\n    #\n    def evaluate_file(file)\n      evaluate(parse_file(file))\n    end\n\n    # Evaluates an AST obtained from `parse_string` or `parse_file` in topscope.\n    # If the ast is a `Puppet::Pops::Model::Program` (what is returned from the `parse` methods, any definitions\n    # in the program (that is, any function, plan, etc. that is defined will be made available for use).","sourceCodeStart":71,"sourceCodeEnd":107,"githubUrl":"https://github.com/puppetlabs/puppet/blob/e227c27540975c25aa22d533a52424a9d2fc886a/lib/puppet/pal/compiler.rb#L71-L107","documentation":"PAL's Compiler#evaluate_string evaluates a snippet of Puppet language code in top scope. nil and '' short-circuit to nil, but any other non-String value (Symbol, Integer, Array, Hash) raises ArgumentError naming the actual class. The check exists so the failure happens at the API boundary instead of deep inside the parser with a confusing message.","triggerScenarios":"evaluate_string(:'include foo') (a Symbol from keyword-style code), evaluate_string(42), or passing a variable that was never converted to source text, such as an options-hash value fetched with a typo'd key or a nested structure from YAML instead of the snippet string.","commonSituations":"Rake tasks and rspec-puppet-style helpers that build PAL calls dynamically from config; data-driven tooling where the snippet comes from YAML and is accidentally a list of lines or a symbol.","solutions":["Coerce before calling: evaluate_string(puppet_code.to_s)","Fix the producer so the value really is the Puppet source string","Skip the call for nil/empty (already supported) and raise your own descriptive error for other types"],"exampleFix":"# before: Symbol passed instead of source text\ncompiler.evaluate_string(options[:snippet])   # :include_foo\n\n# after\ncompiler.evaluate_string(options[:snippet].to_s)","handlingStrategy":"type-guard","validationCode":null,"typeGuard":"def puppet_code?(value)\n  value.is_a?(String)\nend\n\nraise ArgumentError, 'puppet_code must be a String' unless puppet_code?(code)","tryCatchPattern":null,"preventionTips":["Call .to_s on data-driven snippets at the PAL boundary","Skip evaluate_string for nil/empty values (the method already handles them) instead of guessing"],"tags":["pal","type-check","argument","puppet-language"],"backgroundTag":"wrong-argument-type","analyzedSha":"e227c27540975c25aa22d533a52424a9d2fc886a","analyzedAt":"2026-08-21T20:49:46.650Z","schemaVersion":2},"datasetVersion":"2026-08-22T04:17:13.399Z"}