{"record":{"id":"83568b1f785b088e","repo":"puppetlabs/puppet","slug":"the-given-ast-does-not-represent-a-literal-value","errorCode":null,"errorMessage":"The given 'ast' does not represent a literal value","messagePattern":"The given 'ast' does not represent a literal value","errorType":"exception","errorClass":"ArgumentError","httpStatus":null,"severity":"error","filePath":"lib/puppet/pal/compiler.rb","lineNumber":132,"sourceCode":"        loaders = Puppet.lookup(:loaders)\n        loaders.instantiate_definitions(ast, loaders.public_environment_loader)\n      end\n      internal_evaluator.evaluate(topscope, ast)\n    end\n\n    # Produces a literal value if the AST obtained from `parse_string` or `parse_file` does not require any actual evaluation.\n    # This method is useful if obtaining an AST that represents literal values; string, integer, float, boolean, regexp, array, hash;\n    # for example from having read this from the command line or as values in some file.\n    #\n    # @param ast [Puppet::Pops::Model::PopsObject] typically the returned `Program` from the parse methods, but can be any `Expression`\n    # @returns [Object] whatever the literal value the ast evaluates to\n    #\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.","sourceCodeStart":114,"sourceCodeEnd":150,"githubUrl":"https://github.com/puppetlabs/puppet/blob/e227c27540975c25aa22d533a52424a9d2fc886a/lib/puppet/pal/compiler.rb#L114-L150","documentation":"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.","triggerScenarios":"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.","commonSituations":"Tooling (parsers, IDE helpers, task runners) reading defaults from manifests where values reference variables; developers confusing evaluate_literal with evaluate/evaluate_string.","solutions":["If the code needs evaluation, use evaluate_string or evaluate inside a configured compiler instead","Restructure the source so the value is literal (a quoted string, number, or literal list) rather than computed","Catch ArgumentError and fall back to full evaluation when literal extraction is not possible","Keep parameter defaults in data files (YAML/JSON literals) if tooling must extract them statically"],"exampleFix":"# before: expression requires evaluation\nast = compiler.parse_string('$port = 8080; $port + 1')\nval = compiler.evaluate_literal(ast)            # raises\n\n# after: evaluate for real\nval = compiler.evaluate_string('$port = 8080; $port + 1')   # => 8081","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"begin\n  value = compiler.evaluate_literal(ast)\nrescue ArgumentError\n  # AST needs real evaluation (variables, calls, interpolation)\n  value = compiler.evaluate_string(source)\nend","preventionTips":["Use evaluate_literal only for trusted literal data (defaults from data files)","Document in your tooling which AST shapes are literal-safe"],"tags":["pal","ast","literal","evaluation"],"backgroundTag":"expression-not-literal","analyzedSha":"e227c27540975c25aa22d533a52424a9d2fc886a","analyzedAt":"2026-08-21T20:49:46.650Z","schemaVersion":2},"datasetVersion":"2026-08-21T23:17:16.201Z"}