Java codegen: clean output directory before generating to prevent orphan accumulation#1623
Conversation
…rphan accumulation ## Summary Add a clean step to `java/scripts/codegen/java.ts` that removes the entire generated output directory before writing new files. This ensures that when upstream schema types are renamed or removed, the previously-generated `.java` files are automatically deleted rather than silently accumulating as orphans. ## Changes - **`java/scripts/codegen/java.ts`** — At the start of `main()`, recursively delete `src/generated/java/com/github/copilot/generated/` and recreate the empty directory before invoking any generators. - **`java/src/generated/java/`** — 204 orphan files removed (types no longer emitted by the current schema). These accumulated over multiple `@github/copilot` releases as types were renamed or removed upstream. - **`GeneratedRpcRecordsCoverageTest.java`** — Removed test methods that exercised deleted generated types (`SessionWorkspaceCreateFileParams`, `SessionModeGetResult`, `SessionModeSetResult`, `DiscoveredMcpServerSource`, and 16 others). - **`GeneratedRpcApiCoverageTest.java`** — Removed test method for the deleted `SessionAgentDeselectResult` type. ## Why this works going forward The CI workflow (`java-codegen-check.yml`) runs codegen and then checks `git status --porcelain`. Because codegen now cleans before writing, any future orphans will appear as deletions in the diff and the check will fail — preventing orphan accumulation without any additional workflow changes. Fixes #1619
Cross-SDK Consistency Review ✅All 207 changed files are in Why the clean-before-generate pattern is Java-specificThe orphan accumulation problem exists only in the Java codegen because Java conventionally writes one file per class. The other SDK codegens all write to a fixed set of monolithic files (always overwritten, so they cannot accumulate orphans):
The fix (clean the output directory before generating) is correctly scoped to Java. No equivalent change is needed in any other SDK.
|
|
I'm very sorry about this mistake, this PR is ready for review @stephentoub . |
There was a problem hiding this comment.
Pull request overview
This PR improves the Java SDK’s code generation workflow by cleaning the Java generated-sources output directory before regenerating from the upstream JSON schemas, preventing stale/orphaned .java files from accumulating when schema types are renamed or removed (fixes #1619).
Changes:
- Add a “clean generated output dir” step at the start of
java/scripts/codegen/java.tsso regeneration reflects the current schema exactly (including deletions). - Remove previously orphaned generated Java types that are no longer emitted by the current
@github/copilotschemas. - Update Java generated-type coverage tests to stop referencing deleted generated types.
Show a summary per file
| File | Description |
|---|---|
| java/scripts/codegen/java.ts | Adds a pre-generation clean step for src/generated/java/com/github/copilot/generated to prevent orphan accumulation. |
| java/src/test/java/com/github/copilot/generated/rpc/GeneratedRpcApiCoverageTest.java | Removes a coverage test that exercised a generated type that is no longer emitted. |
| java/src/generated/java/com/github/copilot/generated/** | Regenerated output: removes stale orphaned generated types no longer present in the schema. |
| java/src/generated/java/com/github/copilot/generated/rpc/** | Regenerated output: removes stale orphaned generated RPC types no longer present in the schema. |
Copilot's findings
- Files reviewed: 1/207 changed files
- Comments generated: 1
| // Clean the generated output directory to remove orphaned files from previous runs | ||
| const generatedOutputDir = path.join(REPO_ROOT, "src/generated/java/com/github/copilot/generated"); | ||
| console.log(`🧹 Cleaning output directory: ${generatedOutputDir}`); | ||
| await fs.rm(generatedOutputDir, { recursive: true, force: true }); | ||
| await fs.mkdir(generatedOutputDir, { recursive: true }); |
Summary
Add a clean step to
java/scripts/codegen/java.tsthat removes the entire generated output directory before writing new files. This ensures that when upstream schema types are renamed or removed, the previously-generated.javafiles are automatically deleted rather than silently accumulating as orphans.Changes
java/scripts/codegen/java.ts— At the start ofmain(), recursively deletesrc/generated/java/com/github/copilot/generated/and recreate the empty directory before invoking any generators.java/src/generated/java/— 204 orphan files removed (types no longer emitted by the current schema). These accumulated over multiple@github/copilotreleases as types were renamed or removed upstream.GeneratedRpcRecordsCoverageTest.java— Removed test methods that exercised deleted generated types (SessionWorkspaceCreateFileParams,SessionModeGetResult,SessionModeSetResult,DiscoveredMcpServerSource, and 16 others).GeneratedRpcApiCoverageTest.java— Removed test method for the deletedSessionAgentDeselectResulttype.Why this works going forward
The CI workflow (
java-codegen-check.yml) runs codegen and then checksgit status --porcelain. Because codegen now cleans before writing, any future orphans will appear as deletions in the diff and the check will fail — preventing orphan accumulation without any additional workflow changes.Fixes #1619