puppetlabs/puppet · error · ArgumentError
The argument 'code_string' must be a String, got %{type}
Error message
The argument 'code_string' must be a String, got %{type} What it means
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.
Source
Thrown at lib/puppet/pal/compiler.rb:144
#
def evaluate_literal(ast)
catch :not_literal do
return Puppet::Pops::Evaluator::LiteralEvaluator.new().literal(ast)
end
# TRANSLATORS, the 'ast' is the name of a parameter, do not translate
raise ArgumentError, _("The given 'ast' does not represent a literal value")
end
# Parses and validates a puppet language string and returns an instance of Puppet::Pops::Model::Program on success.
# If the content is not valid an error is raised.
#
# @param code_string [String] a puppet language string to parse and validate
# @param source_file [String] an optional reference to a file or other location in angled brackets
# @return [Puppet::Pops::Model::Program] returns a `Program` instance on success
#
def parse_string(code_string, source_file = nil)
unless code_string.is_a?(String)
raise ArgumentError, _("The argument 'code_string' must be a String, got %{type}") % { type: code_string.class }
end
internal_evaluator.parse_string(code_string, source_file)
end
# Parses and validates a puppet language file and returns an instance of Puppet::Pops::Model::Program on success.
# If the content is not valid an error is raised.
#
# @param file [String] a file with puppet language content to parse and validate
# @return [Puppet::Pops::Model::Program] returns a `Program` instance on success
#
def parse_file(file)
unless file.is_a?(String)
raise ArgumentError, _("The argument 'file' must be a String, got %{type}") % { type: file.class }
end
internal_evaluator.parse_file(file)
endView on GitHub (pinned to e227c27540)
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
Example fix
# before compiler.parse_string(options[:code]) # nil -> ArgumentError # after compiler.parse_string(options[:code] || '')
Defensive patterns
Strategy: type-guard
Type guard
def code_string?(value) value.is_a?(String) end raise ArgumentError, 'code_string must be a String' unless code_string?(code)
Prevention
- Remember parse_string has no nil special case; default blank options to ''
- Pass File.read output, never an IO handle
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- The argument 'file' must be a String, got %{type}
- The argument 'puppet_code' must be a String, got %{type}
- Expected an instance of Puppet::SSL::Verifier but was passed
- The given 'ast' does not represent a literal value
- Given data_type value is not a data type, got '%{type}'
AI-assisted analysis of puppetlabs/puppet@e227c27540 (2026-08-21).
Data as JSON: /api/errors/6940a815c4b246c4.
Report an issue: GitHub.