Repository navigation
fix(cmd/gf): find the api import by path when gen ctrl parses controllers - #4957
Conversation
…lers In merge mode, getApiItemsInDst took the first import whose last path segment equals the api version as the api package of a controller file. An unaliased import such as "github.com/xuri/excelize/v2" placed above the api import of a v2 controller was picked instead, so the existing methods no longer matched the api definitions. A newly added api was then not generated, and with -c the second run failed with "1:1: expected 'package', found 'EOF'". The api import is now chosen by the import paths that getApiItemsInSrc already collects. When no import path matches, the first segment match is kept as before, so controllers of an api version removed from src resolve the same way. Fixes gogf#4954
|
The import selection change looks right for the unaliased case in #4954. Merge is still blocked by CI on this head.
Both assertions are about how many jobs ran before shutdown, so this looks like a timing flake rather than a problem in the genctrl change. That failure also left |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #4957 +/- ##
=======================================
Coverage 84.91% 84.91%
=======================================
Files 892 892
Lines 47052 47052
=======================================
+ Hits 39954 39956 +2
+ Misses 7098 7096 -2
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
|
The earlier comment was about a CI failure on This is still the unaliased-import fix for #4954: merge mode now matches the controller's api import by the source import path, with |
There was a problem hiding this comment.
🟡 Changes recommended
Both deferred cleanup calls ignore gfile.RemoveAll errors, allowing temporary controllers or the generated API fixture to remain after cleanup failures without failing the test.
1 open issue
What changed in this PR
Fixes API import detection in gf gen ctrl merge mode so an unaliased third-party import ending in the same version segment does not hide newly added API methods.
Changes:
- Passes source API items into controller parsing to prefer known API import paths.
- Preserves the existing fallback when no known path matches.
- Adds regression coverage for merge mode with and without
Clear.
| File | Description |
|---|---|
cmd/gf/internal/cmd/genctrl/genctrl.go |
Passes source API items to destination parsing. |
cmd/gf/internal/cmd/genctrl/genctrl_calculate.go |
Prioritizes known API import paths over version-segment matches. |
cmd/gf/internal/cmd/cmd_z_unit_gen_ctrl_test.go |
Tests conflicting unaliased imports with both cleanup settings. |
Review effort: Balanced
Refactor deferred cleanup to assert nil on removal. Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

gf gen ctrl -mpicks the api package of an existing controller file by taking the first import whose last path segment equals the api version. An unaliased import like"github.com/xuri/excelize/v2"above the api import in a v2 controller wins, so the existing methods no longer match the api definitions. A newly added api is then skipped without any error, and with-cthe second run fails with1:1: expected 'package', found 'EOF'. #4940 fixed the aliased form, this is the unaliased one.As the issue suggests,
getApiItemsInDstnow receives the api items from src and prefers the import whose path is one of their import paths. When no path matches, the first segment match is kept as before, so controllers of an api version that was removed from src resolve the same way as on master.Test_Gen_Ctrl_UseMerge_Issue4954follows the #4289 test with the unaliased import from the issue steps, with and withoutClear. On master it fails withEXPECT 0 == 1(noDictTypeAdd) and with the EOF error whenClearis set.Ran:
go test -count=1 -run Test_Gen_Ctrl ./internal/cmd/incmd/gf, all 7 passChecked: the issue steps with a built
gf, a v2 api andexcelize/v2, for-mand-m -cChecked:
golangci-lint run -c .golangci.ymlreports no new issues ingenctrlFixes #4954