{"record":{"id":"6e0e16f18cc0ca1b","repo":"apple/pkl","slug":"cannot-generate-kotlin-code-for-a-pkl-standard-lib","errorCode":null,"errorMessage":"Cannot generate Kotlin code for a Pkl standard library module (`${moduleSchema.moduleUri}`).","messagePattern":"Cannot generate Kotlin code for a Pkl standard library module \\(`(.+?)`\\)\\.","errorType":"exception","errorClass":"KotlinCodeGeneratorException","httpStatus":null,"severity":"error","filePath":"pkl-codegen-kotlin/src/main/kotlin/org/pkl/codegen/kotlin/KotlinCodeGenerator.kt","lineNumber":129,"sourceCode":"    }\n\n  private val kotlinFileName: String\n    get() = buildString {\n      val (packageName, className) = nameMapper.map(moduleSchema.moduleName)\n      val dirPath = packageName.split('.').joinToString(\"/\", transform = IoUtils::encodePath)\n      val fileName = IoUtils.encodePath(className)\n      append(\"kot\")\n      append(\"lin/\")\n      if (dirPath.isNotEmpty()) {\n        append(\"$dirPath/\")\n      }\n      append(\"$fileName.kt\")\n    }\n\n  val kotlinFile: String\n    get() {\n      if (moduleSchema.moduleUri.scheme == \"pkl\") {\n        throw KotlinCodeGeneratorException(\n          \"Cannot generate Kotlin code for a Pkl standard library module (`${moduleSchema.moduleUri}`).\"\n        )\n      }\n\n      val pModuleClass = moduleSchema.moduleClass\n\n      val hasModuleProperties = pModuleClass.properties.any { !it.value.isHidden }\n      val isGenerateModuleClass =\n        hasModuleProperties || pModuleClass.isOpen || pModuleClass.isAbstract\n\n      fun generateCompanionRelatedCode(\n        builder: TypeSpec.Builder,\n        isModuleType: Boolean = false,\n      ): TypeSpec.Builder {\n        // ensure that at most one companion object is generated for this type\n        val companionObjectBuilder: Lazy<TypeSpec.Builder> = lazy {\n          TypeSpec.companionObjectBuilder()\n        }","sourceCodeStart":111,"sourceCodeEnd":147,"githubUrl":"https://github.com/apple/pkl/blob/f3efcbfc9b60d30053b0536d664948d7aa1b8673/pkl-codegen-kotlin/src/main/kotlin/org/pkl/codegen/kotlin/KotlinCodeGenerator.kt#L111-L147","documentation":"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.","triggerScenarios":"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`.","commonSituations":"Scripting the codegen API and iterating over dependency modules including stdlib; accidentally targeting `pkl:` URIs instead of a project file.","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."],"exampleFix":"// before\nval modules = resolution.allModules // includes pkl:base\n// after\nmodules.filter { it.uri.scheme != \"pkl\" }.forEach { generate(it) }","handlingStrategy":"validation","validationCode":"require(moduleSchema.moduleUri.scheme != \"pkl\") {\n  \"Refusing to codegen stdlib module ${moduleSchema.moduleUri}\"\n}","typeGuard":"fun isGeneratable(uri: java.net.URI): Boolean = uri.scheme != \"pkl\"","tryCatchPattern":"try {\n  KotlinCodeGenerator(moduleSchema, options)\n} catch (e: KotlinCodeGeneratorException) {\n  if (e.message?.contains(\"standard library module\") == true) {\n    logger.warn(\"Skipping stdlib module ${moduleSchema.moduleUri}\")\n  } else throw e\n}","preventionTips":["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"],"tags":["kotlin","codegen","stdlib","unsupported-target"],"backgroundTag":"unsupported-operation","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"}