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

Improve Cleanup Function Naming in Generated Code #423

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

frederik-jatzkowski
Copy link

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:

func injectBaz() (Baz, func(), error) {
	foo, cleanup := provideFoo()
	bar, cleanup2, err := provideBar(foo)
	if err != nil {
		cleanup()
		return 0, nil, err
	}
	baz, err := provideBaz(bar)
	if err != nil {
		cleanup2()
		cleanup()
		return 0, nil, err
	}
	return baz, func() {
		cleanup2()
		cleanup()
	}, nil
}

With this change, Wire will instead generate:

func injectBaz() (Baz, func(), error) {
	foo, fooCleanup := provideFoo()
	bar, barCleanup, err := provideBar(foo)
	if err != nil {
		fooCleanup()
		return 0, nil, err
	}
	baz, err := provideBaz(bar)
	if err != nil {
		barCleanup()
		fooCleanup()
		return 0, nil, err
	}
	return baz, func() {
		barCleanup()
		fooCleanup()
	}, nil
}

Rationale

  1. 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.

  2. 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

  • There is no functional change to how Wire resolves dependencies or manages cleanup execution—only a naming improvement in the generated output.
  • The test suite was accordingly modified.
  • The test suite now also tests naming conflicts of cleanup functions.
  • No impact on existing generated code behavior.
  • This will not break any users since function names in generated code are not part of a stable API contract. However, there will be a diff in the generated output if users upgrade to a version containing this change.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant