puppetlabs/puppet · error · ArgumentError

The given 'ast' does not represent a literal value

Error message

The given 'ast' does not represent a literal value

What it means

Compiler#evaluate_literal uses Puppet::Pops::Evaluator::LiteralEvaluator to extract a compile-time constant from an AST (from parse_string/parse_file) without running a compiler. If the tree contains anything requiring evaluation (variables, function calls, interpolations, arithmetic), the evaluator throws :not_literal and the method raises ArgumentError. It is a fast path for literal data, not a general evaluator.

Source

Thrown at lib/puppet/pal/compiler.rb:132

        loaders = Puppet.lookup(:loaders)
        loaders.instantiate_definitions(ast, loaders.public_environment_loader)
      end
      internal_evaluator.evaluate(topscope, ast)
    end

    # Produces a literal value if the AST obtained from `parse_string` or `parse_file` does not require any actual evaluation.
    # This method is useful if obtaining an AST that represents literal values; string, integer, float, boolean, regexp, array, hash;
    # for example from having read this from the command line or as values in some file.
    #
    # @param ast [Puppet::Pops::Model::PopsObject] typically the returned `Program` from the parse methods, but can be any `Expression`
    # @returns [Object] whatever the literal value the ast evaluates to
    #
    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.

View on GitHub (pinned to e227c27540)

Solutions

  1. If the code needs evaluation, use evaluate_string or evaluate inside a configured compiler instead
  2. Restructure the source so the value is literal (a quoted string, number, or literal list) rather than computed
  3. Catch ArgumentError and fall back to full evaluation when literal extraction is not possible
  4. Keep parameter defaults in data files (YAML/JSON literals) if tooling must extract them statically

Example fix

# before: expression requires evaluation
ast = compiler.parse_string('$port = 8080; $port + 1')
val = compiler.evaluate_literal(ast)            # raises

# after: evaluate for real
val = compiler.evaluate_string('$port = 8080; $port + 1')   # => 8081
Defensive patterns

Strategy: try-catch

Try / catch

begin
  value = compiler.evaluate_literal(ast)
rescue ArgumentError
  # AST needs real evaluation (variables, calls, interpolation)
  value = compiler.evaluate_string(source)
end

Prevention

When it happens

Trigger: evaluate_literal(parse_string('$port + 1')) (VariableExpression is not literal); parsing '"${a} b"' (interpolation); feeding a Program containing function calls or resource statements and expecting a value back.

Common situations: Tooling (parsers, IDE helpers, task runners) reading defaults from manifests where values reference variables; developers confusing evaluate_literal with evaluate/evaluate_string.

Related errors


AI-assisted analysis of puppetlabs/puppet@e227c27540 (2026-08-21). Data as JSON: /api/errors/83568b1f785b088e. Report an issue: GitHub.