{"record":{"id":"d13ce5120946d5d7","repo":"apple/pkl","slug":"cannot-generate-kotlin-enum-class-for-pkl-type-ali","errorCode":null,"errorMessage":"Cannot generate Kotlin enum class for Pkl type alias `${typeAlias.displayName}` because string literal type \"$literal\" cannot be converted to a valid enum constant name.","messagePattern":"Cannot generate Kotlin enum class for Pkl type alias `(.+?)` because string literal type \"\\$literal\" cannot be converted to a valid enum constant name\\.","errorType":"exception","errorClass":"KotlinCodeGeneratorException","httpStatus":null,"severity":"error","filePath":"pkl-codegen-kotlin/src/main/kotlin/org/pkl/codegen/kotlin/KotlinCodeGenerator.kt","lineNumber":551,"sourceCode":"        builder.addFunction(generateEqualsMethod()).addFunction(generateHashCodeMethod())\n      }\n\n      return builder\n    }\n\n    return if (superclass == null && !pClass.isAbstract && !pClass.isOpen) generateDataClass()\n    else generateRegularClass()\n  }\n\n  private fun generateEnumTypeSpec(\n    typeAlias: TypeAlias,\n    stringLiterals: Set<String>,\n  ): TypeSpec.Builder {\n    val enumConstantToPklNames =\n      stringLiterals\n        .groupingBy { literal ->\n          CodeGeneratorUtils.toEnumConstantName(literal)\n            ?: throw KotlinCodeGeneratorException(\n              \"Cannot generate Kotlin enum class for Pkl type alias `${typeAlias.displayName}` \" +\n                \"because string literal type \\\"$literal\\\" cannot be converted to a valid enum constant name.\"\n            )\n        }\n        .reduce { enumConstantName, firstLiteral, secondLiteral ->\n          throw KotlinCodeGeneratorException(\n            \"Cannot generate Kotlin enum class for Pkl type alias `${typeAlias.displayName}` \" +\n              \"because string literal types \\\"$firstLiteral\\\" and \\\"$secondLiteral\\\" \" +\n              \"would both be converted to enum constant name `$enumConstantName`.\"\n          )\n        }\n\n    val builder =\n      TypeSpec.enumBuilder(typeAlias.simpleName)\n        .primaryConstructor(\n          FunSpec.constructorBuilder().addParameter(\"value\", String::class).build()\n        )\n        .addProperty(PropertySpec.builder(\"value\", String::class).initializer(\"value\").build())","sourceCodeStart":533,"sourceCodeEnd":569,"githubUrl":"https://github.com/apple/pkl/blob/f3efcbfc9b60d30053b0536d664948d7aa1b8673/pkl-codegen-kotlin/src/main/kotlin/org/pkl/codegen/kotlin/KotlinCodeGenerator.kt#L533-L569","documentation":"When generating a Kotlin enum class for a Pkl string-literal type alias, each literal must map to a valid Kotlin enum constant name via CodeGeneratorUtils.toEnumConstantName. A literal that cannot be converted (e.g. empty or entirely invalid characters) causes this KotlinCodeGeneratorException in generateEnumTypeSpec.","triggerScenarios":"A type alias like `typealias X = \"\"|\"a\"` or literals consisting only of characters that cannot form an identifier, processed while generating an enum from stringLiterals.","commonSituations":"Pkl aliases containing empty string literals, whitespace-only strings, or strings of punctuation/emoji being code-generated to Kotlin.","solutions":["Remove or rename the offending literal in the Pkl type alias so it maps to a valid identifier.","Replace the string-literal alias with a class property typed String if arbitrary values are allowed.","Adjust the literal to start with a letter/underscore and contain identifier-safe characters."],"exampleFix":"// before (Pkl)\ntypealias Mode = \"\" | \"fast\"\n// after\ntypealias Mode = \"default\" | \"fast\"","handlingStrategy":"validation","validationCode":"// Validate each literal maps to a valid identifier before codegen\nfun validKotlinConst(literal: String) =\n  literal.isNotEmpty() &&\n  CodeGeneratorUtils.toEnumConstantName(literal) != null","typeGuard":"fun isEnumCodegenSafe(alias: PType.Alias): Boolean =\n  alias.stringLiterals().all { CodeGeneratorUtils.toEnumConstantName(it) != null }","tryCatchPattern":"try {\n  generator.generate(module)\n} catch (e: KotlinCodeGeneratorException) {\n  if (e.message?.contains(\"cannot be converted to a valid enum constant name\") == true) {\n    logger.error(\"Fix or drop the offending literal: ${e.message}\")\n  } else throw e\n}","preventionTips":["Avoid empty or punctuation-only string literals in enum-like typealiases","Ensure at least one identifier-safe character (letter/underscore) per literal","Add a schema lint that runs toEnumConstantName over all literal aliases"],"tags":["kotlin","codegen","enum","invalid-identifier"],"backgroundTag":"invalid-identifier-format","analyzedSha":"f3efcbfc9b60d30053b0536d664948d7aa1b8673","analyzedAt":"2026-09-08T13:10:45.570Z","contentChangedAt":"2026-09-08T13:10:45.570Z","schemaVersion":2},"datasetVersion":"2026-09-17T15:17:12.973Z"}