{"record":{"id":"42ff4cb9f01a4423","repo":"apple/pkl","slug":"could-not-find-a-source-set-for-code-generator","errorCode":null,"errorMessage":"Could not find a source set for code generator '${name}'. Either apply a JVM plugin (e.g. 'java') or set the sourceSet property explicitly.","messagePattern":"Could not find a source set for code generator '(.+?)'\\. Either apply a JVM plugin \\(e\\.g\\. 'java'\\) or set the sourceSet property explicitly\\.","errorType":"exception","errorClass":"GradleException","httpStatus":null,"severity":"error","filePath":"pkl-gradle/src/main/java/org/pkl/gradle/PklPlugin.java","lineNumber":435,"sourceCode":"                                .getSourceDirectories()\n                                .filter(\n                                    f ->\n                                        !f.getAbsolutePath()\n                                            .startsWith(\n                                                specOutputDir.getAsFile().getAbsolutePath()))\n                                .getFiles()));\n  }\n\n  private void configureCodeGenSpecSourceDirectories(\n      Project project,\n      CodeGenSpec spec,\n      String languageName,\n      Function<? super SourceSet, ? extends Optional<SourceDirectorySet>>\n          extractSourceDirectorySet) {\n    var task = project.getTasks().named(spec.getName(), CodeGenTask.class);\n    var sourceSet = spec.getSourceSet().getOrNull();\n    if (sourceSet == null) {\n      throw new GradleException(\n          \"Could not find a source set for code generator '\"\n              + spec.getName()\n              + \"'. Either apply a JVM plugin (e.g. 'java') or set the sourceSet property explicitly.\");\n    }\n    extractSourceDirectorySet\n        .apply(sourceSet)\n        .ifPresentOrElse(\n            dirSet -> dirSet.srcDir(task.flatMap(t -> t.getOutputDir().dir(languageName))),\n            () ->\n                project\n                    .getLogger()\n                    .debug(\n                        \"Source directory set for language {} is not available, \"\n                            + \"will not add task {} as its dependency\",\n                        languageName,\n                        task.getName()));\n    sourceSet.getResources().srcDir(task.flatMap(t -> t.getOutputDir().dir(\"resources\")));\n  }","sourceCodeStart":417,"sourceCodeEnd":453,"githubUrl":"https://github.com/apple/pkl/blob/f3efcbfc9b60d30053b0536d664948d7aa1b8673/pkl-gradle/src/main/java/org/pkl/gradle/PklPlugin.java#L417-L453","documentation":"When configuring code-generation tasks, each CodeGen spec must resolve to a Gradle SourceSet. If the spec's sourceSet property is unset and no JVM plugin (java/kotlin) has been applied to the project, there is no default source set to resolve, and the plugin throws GradleException. It exists to tell users they must either apply a JVM plugin or set sourceSet explicitly.","triggerScenarios":"Applying pkl-codegen in a project (or subproject) without the `java` or `kotlin` plugin, leaving codegen { generator { ... } } blocks without a sourceSet = ... assignment; running configureJavaCodeGenTasks/configureKotlinCodeGenTasks over such a spec.","commonSituations":"Pkl codegen added to a non-JVM module like a pure Pkl or docs subproject; a multi-project build where the JVM plugin is applied at the root but not in the subproject containing the codegen block; migrating from an older plugin version with different defaults.","solutions":["Apply a JVM plugin to the same project: add `java` (or `kotlin`) to its plugins block.","Set the source set explicitly in the codegen block: `sourceSet = sourceSets.main` (or `sourceSets.test`).","Check you applied the codegen config in the correct subproject, not the root.","If the module is intentionally non-JVM, remove the codegen block and invoke codegen via the CLI instead."],"exampleFix":"// before (build.gradle.kts)\npkl { codegen { generators { register(\"pklGen\") { ... } } } }\n// after\nplugins { `java` }\npkl { codegen { generators { register(\"pklGen\") { sourceSet = sourceSets.main; ... } } } }","handlingStrategy":"validation","validationCode":"val codegen = project.extensions.getByType<PklExtension>().codegen\ncodegen.generators.forEach { require(it.sourceSet.isPresent || project.plugins.hasPlugin(JavaPlugin::class.java)) { \"Apply 'java' or set sourceSet for generator ${it.name}\" } }","typeGuard":"fun Project.hasDefaultSourceSet() = plugins.hasPlugin(JavaPlugin::class.java) || plugins.hasPlugin(KotlinPluginWrapper::class.java)","tryCatchPattern":"try { pkl.configure(...) } catch (GradleException e) { if (e.message?.contains('source set')) logger.error('Apply java/kotlin plugin or set sourceSet on generator ' + name); throw e }","preventionTips":["Apply java/kotlin plugin in every subproject using pkl codegen","Always set sourceSet explicitly rather than relying on defaults","Verify with a failing-fast configuration check in CI","Keep codegen blocks only in JVM modules"],"tags":["gradle","configuration","codegen","source-set"],"backgroundTag":"missing-required-config-field","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"}