Skip to content

Auto-correct Go imports in Code Mode#118

Merged
spachava753 merged 3 commits intomainfrom
feat/auto-correct-imports
Dec 20, 2025
Merged

Auto-correct Go imports in Code Mode#118
spachava753 merged 3 commits intomainfrom
feat/auto-correct-imports

Conversation

@spachava753
Copy link
Copy Markdown
Owner

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

  • Added golang.org/x/tools/imports dependency for programmatic import correction
  • Modified internal/codemode/executor.go:
    • Implemented autoCorrectImports() to process generated code before compilation
    • Added extractImports() to parse import statements from Go source
    • Added formatImportChanges() to generate clear feedback on import modifications
  • Updated internal/codemode/executor_test.go:
    • Added TestExecuteCode_AutoCorrectImports to verify the feature
    • Updated test to check for added/removed package lists in output
  • Fixed test case that was incorrectly expecting missing import errors

Behavior

When imports are auto-corrected, the LLM receives clear feedback:

Note: Imports in run.go were auto-corrected.
  Added: fmt, strings
  Removed: os

Testing

All tests in internal/codemode pass, including the new auto-correction test.

Closes #116

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
@spachava753 spachava753 merged commit a3e433b into main Dec 20, 2025
@spachava753 spachava753 deleted the feat/auto-correct-imports branch April 10, 2026 00:23
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.

[Enhancement]: Auto-correct Go imports in Code Mode using goimports

1 participant