Skip to content

Export AddImportFix and improve GCI-aware import insertion - #30

Merged
mmorel-35 merged 3 commits into
http-constfrom
copilot/improve-importfix-method-visibility
Mar 16, 2026
Merged

Export AddImportFix and improve GCI-aware import insertion#30
mmorel-35 merged 3 commits into
http-constfrom
copilot/improve-importfix-method-visibility

Conversation

Copilot AI commented Mar 16, 2026

Copy link
Copy Markdown

addImportFix was package-private, forcing every new checker (negated-assert, graceful-teardown, …) to either duplicate it or add export_test.go workarounds. Import insertion also blindly appended non-stdlib packages before ), which breaks GCI group ordering when a file has blank-line-separated import groups.

Changes

  • helpers_import.goaddImportFixAddImportFix (exported); importInsertEdit now scans non-stdlib specs in file order and inserts before the first spec whose path is lexicographically greater, instead of appending at the end:
// Before: assert always appended last — ends up after require (and after local groups)
import (
    "testing"

    "github.com/stretchr/testify/require"
    "github.com/stretchr/testify/assert" // ← wrong position
)

// After: assert inserted before require (a < r); blank-line GCI groups preserved
import (
    "testing"

    "github.com/stretchr/testify/assert" // ← correct
    "github.com/stretchr/testify/require"
)
  • helpers_http.gohttpNetPkgName updated to call AddImportFix.
  • helpers_import_test.go — added unit tests for AddImportFix: already-imported (no-op edit), GCI ordering, GCI group boundary preservation across blank lines, and blank-import (ok=false).

📍 Connect Copilot coding agent with Jira, Azure Boards or Linear to delegate work to Copilot in one click without leaving your project management tool.

- Export addImportFix → AddImportFix so all checkers can share it without
  duplication (negated-assert, graceful-teardown, etc.)
- Improve importInsertEdit: scan non-stdlib specs in file order and insert
  before the first entry whose path is lexicographically greater, so that
  go/format produces alphabetically sorted, GCI-compliant import groups
- Use safe type assertion in the new scan loop (imp, ok := spec.(*ast.ImportSpec))
- Update httpNetPkgName to call AddImportFix
- Add unit tests: AlreadyImported, GCIOrdering, GCIOrderingPreservesGroup,
  BlankImported

Co-authored-by: mmorel-35 <6032561+mmorel-35@users.noreply.github.com>
Copilot AI changed the title [WIP] Refactor importfix and challenge methods for better visibility Export AddImportFix and improve GCI-aware import insertion Mar 16, 2026
Copilot AI requested a review from mmorel-35 March 16, 2026 10:02
@mmorel-35

Copy link
Copy Markdown
Owner

@copilot ,
Don't forget to pass golangci-lint v2.4.0 and tests

Co-authored-by: mmorel-35 <6032561+mmorel-35@users.noreply.github.com>

Copilot AI commented Mar 16, 2026

Copy link
Copy Markdown
Author

@copilot ,
Don't forget to pass golangci-lint v2.4.0 and tests

Fixed in fb0557a. The helpers_import_test.go import block was missing tab indentation (a heredoc artifact from the initial write), which caused the gci formatter to report an error. Applied golangci-lint run --fix to auto-correct the formatting — golangci-lint run ./... now reports 0 issues and all tests pass.

@mmorel-35
mmorel-35 marked this pull request as ready for review March 16, 2026 10:06
@mmorel-35
mmorel-35 merged commit f6cd8f1 into http-const Mar 16, 2026
@mmorel-35
mmorel-35 deleted the copilot/improve-importfix-method-visibility branch March 16, 2026 10:07
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.

2 participants