apple/pkl · error · KotlinCodeGeneratorException
Cannot generate Kotlin code for a Pkl standard library…
Error message
Cannot generate Kotlin code for a Pkl standard library module (`${moduleSchema.moduleUri}`). What it means
The Kotlin code generator refuses to generate code for modules whose URI scheme is `pkl` (i.e. Pkl standard library modules like pkl:base). Stdlib modules are provided by the runtime and have no user-side generated counterpart, so KotlinCodeGeneratorException is thrown from the kotlinFile property.
Solutions
- Generate only from your own .pkl files (file:/project: URIs), not pkl: stdlib URIs.
- Filter module list before generation: skip modules whose URI scheme is `pkl`.
- If you need stdlib types in output, reference them from your own module rather than generating stdlib directly.
Example fix
// before
val modules = resolution.allModules // includes pkl:base
// after
modules.filter { it.uri.scheme != "pkl" }.forEach { generate(it) } Defensive patterns
Strategy: validation
Validate before calling
require(moduleSchema.moduleUri.scheme != "pkl") {
"Refusing to codegen stdlib module ${moduleSchema.moduleUri}"
} Type guard
fun isGeneratable(uri: java.net.URI): Boolean = uri.scheme != "pkl"
Try / catch
try {
KotlinCodeGenerator(moduleSchema, options)
} catch (e: KotlinCodeGeneratorException) {
if (e.message?.contains("standard library module") == true) {
logger.warn("Skipping stdlib module ${moduleSchema.moduleUri}")
} else throw e
} Prevention
- Filter resolved modules by URI scheme before generation
- Only pass project/file modules to the codegen API
- Never iterate raw dependency sets (which include pkl: URIs) without filtering
When it happens
Trigger: Passing a module URI such as pkl:base or pkl:math (or a module that imports/evaluates to a stdlib module) to KotlinCodeGenerator / `pkl codegen kotlin`.
Common situations: Scripting the codegen API and iterating over dependency modules including stdlib; accidentally targeting `pkl:` URIs instead of a project file.
Understand the failure class
Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.
Related errors
- Standard library class
- Standard library typealias
- Cannot generate Kotlin enum class for Pkl type alias
- Cannot generate Kotlin enum class for Pkl type alias
- I/O error writing file `$outputFile`. Cause
AI-assisted analysis of apple/pkl@f3efcbfc9b (2026-09-08).
Data as JSON: /api/errors/6e0e16f18cc0ca1b.
Report an issue: GitHub.
Appendix: source
Thrown at pkl-codegen-kotlin/src/main/kotlin/org/pkl/codegen/kotlin/KotlinCodeGenerator.kt:129
}
private val kotlinFileName: String
get() = buildString {
val (packageName, className) = nameMapper.map(moduleSchema.moduleName)
val dirPath = packageName.split('.').joinToString("/", transform = IoUtils::encodePath)
val fileName = IoUtils.encodePath(className)
append("kot")
append("lin/")
if (dirPath.isNotEmpty()) {
append("$dirPath/")
}
append("$fileName.kt")
}
val kotlinFile: String
get() {
if (moduleSchema.moduleUri.scheme == "pkl") {
throw KotlinCodeGeneratorException(
"Cannot generate Kotlin code for a Pkl standard library module (`${moduleSchema.moduleUri}`)."
)
}
val pModuleClass = moduleSchema.moduleClass
val hasModuleProperties = pModuleClass.properties.any { !it.value.isHidden }
val isGenerateModuleClass =
hasModuleProperties || pModuleClass.isOpen || pModuleClass.isAbstract
fun generateCompanionRelatedCode(
builder: TypeSpec.Builder,
isModuleType: Boolean = false,
): TypeSpec.Builder {
// ensure that at most one companion object is generated for this type
val companionObjectBuilder: Lazy<TypeSpec.Builder> = lazy {
TypeSpec.companionObjectBuilder()
}View on GitHub (pinned to f3efcbfc9b)