Improve Cleanup Function Naming in Generated Code #423
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
This PR refines the naming of cleanup functions in generated code to enhance clarity and correctness. Previously, Wire generated cleanup functions with generic names (
cleanup
,cleanup2
), which made it difficult to immediately understand which cleanup function corresponded to which provided value.This change updates Wire’s code generation logic to assign cleanup function names based on the provider’s return value.
Before and After
Currently, Wire generates code like this:
With this change, Wire will instead generate:
Rationale
Improved Readability & Comprehension
The previous naming (
cleanup
,cleanup2
) was ambiguous and required mentally mapping cleanup functions back to their respective resources.The new naming explicitly links cleanup functions to the values they manage (
fooCleanup
,barCleanup
), making the code self-explanatory.Better Maintainability of Generated Code
When debugging or modifying generated code, engineers can now immediately understand which cleanup function corresponds to which provider.
This makes troubleshooting easier when dealing with complex dependency graphs.
Implementation Details
This PR only changes a single line in production code (
internal/wire/wire.go:676
),which was updated to use the name provided value within the cleanup name.
It uses
internal/wire.disambiguate()
to prevent naming collisions.Testing & Compatibility