Auto-correct Go imports in Code Mode#118
Merged
spachava753 merged 3 commits intomainfrom Dec 20, 2025
Merged
Conversation
Integrate goimports into the code execution workflow to automatically fix missing or unused imports in generated code. This reduces compilation errors and regeneration loops. - Check for goimports availability before execution. - Run goimports -w on the generated run.go file. - Append a notification to the output if imports were modified. - Update tests to reflect the new behavior and ensure existing tests pass. Closes #116
Replace the external dependency on the 'goimports' command-line tool with the 'golang.org/x/tools/imports' library. This ensures that import auto-correction works reliably without requiring the user to install additional tools. - Added 'golang.org/x/tools/imports' dependency. - Refactored 'autoCorrectImports' to use 'imports.Process'. - Removed CLI availability check in tests. - Updated 'executor_test.go' to reflect the new implementation.
Enhance the import auto-correction notification to explicitly list which packages were added and which were removed. This gives the LLM clear feedback on what changes were made.
Example output:
Note: Imports in run.go were auto-corrected.
Added: fmt, strings
Removed: os
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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
Integrates automatic import correction into the Code Mode execution workflow using
golang.org/x/tools/imports. This eliminates the common issue of LLM-generated code failing to compile due to missing or unused imports, reducing regeneration loops and improving user experience.Changes
golang.org/x/tools/importsdependency for programmatic import correctioninternal/codemode/executor.go:autoCorrectImports()to process generated code before compilationextractImports()to parse import statements from Go sourceformatImportChanges()to generate clear feedback on import modificationsinternal/codemode/executor_test.go:TestExecuteCode_AutoCorrectImportsto verify the featureBehavior
When imports are auto-corrected, the LLM receives clear feedback:
Testing
All tests in
internal/codemodepass, including the new auto-correction test.Closes #116