Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unnecessary file content validation in CodeWriter #5923

Open
1 task
rpash opened this issue Mar 4, 2025 · 0 comments
Open
1 task

Unnecessary file content validation in CodeWriter #5923

rpash opened this issue Mar 4, 2025 · 0 comments
Labels
bug This issue is a bug. needs-triage This issue or PR still needs to be triaged.

Comments

@rpash
Copy link

rpash commented Mar 4, 2025

Describe the bug

Description
The CodeWriter class currently includes content validation here that prevents overwriting files with different content. This appears to be an anti-pattern that goes against the fundamental nature of code generation.

Why this is problematic
Code generation, by definition, should be:

  1. Deterministic - Same input should produce same output
  2. Repeatable - Should be able to run multiple times without side effects
  3. Idempotent - The final state should be determined by the input model, not the previous state

The current content validation

  • Assumes generated files should never change without manual cleanup
  • Creates unnecessary coupling between previous and new generation runs
  • Forces developers to manually clean directories between generations
  • Adds complexity to build processes that should be straightforward

Proposed Change
Remove the content validation entirely. Code generation should simply:

  • Generate the new files
  • Write them to the specified location
  • Overwrite any existing files

This would

  • Simplify the code
  • Remove an unnecessary validation
  • Make the generation process more reliable
  • Follow standard code generation patterns

The original intent might have been to catch configuration errors, but this should be handled through proper input validation and clear error messages about the input model, not by validating output files against their previous state.

Regression Issue

  • Select this option if this issue appears to be a regression.

Expected Behavior

When running code generation:

  1. The generator should process the Smithy model and generate the corresponding Java code
  2. Any existing files in the output directory should be overwritten with the newly generated content
  3. The process should complete successfully regardless of what files existed before
  4. The final state of the generated code should depend only on the current input model, not on previous generations

Current Behavior

When running code generation, if a file already exists in the output directory, the generator:

  1. Reads the existing file
  2. Compares its content with the newly generated content
  3. Throws an IllegalStateException if they don't match:
java.lang.RuntimeException: Failed to generate code. Exception message: java.lang.IllegalStateException: Attempted to clobber existing file (...) with a new file that has different content. This may indicate forgetting to clean up old generated files before running the generator?

This forces users to manually delete generated files between runs, complicating build processes and development workflows.

Reproduction Steps

Given some codegen setup and a clean build environment

  1. Run the codegen task
  2. Change the input mode
  3. Run the codegen task again

On step 3 the codegen task fails with an IllegalStateException

Possible Solution

Remove the content validation entirely. Or at least make it configurable and default to on if there are backwards compatibility concerns (but it is quite the smell if a project relies on this check).

Additional Information/Context

No response

AWS Java SDK version used

2.30.32

JDK version used

21

Operating System and version

MacOS 15

@rpash rpash added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Mar 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug This issue is a bug. needs-triage This issue or PR still needs to be triaged.
Projects
None yet
Development

No branches or pull requests

1 participant