Skip to content

fix(cmd/gf): find the api import by path when gen ctrl parses controllers - #4957

Merged
hailaz merged 3 commits into
gogf:masterfrom
nk-batman21:fix/genctrl-unaliased-version-import
Nov 9, 2026
Merged

hailaz merged 3 commits into
gogf:masterfrom
nk-batman21:fix/genctrl-unaliased-version-import

Conversation

@nk-batman21

Copy link
Copy Markdown
Contributor

gf gen ctrl -m picks 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 -c the second run fails with 1:1: expected 'package', found 'EOF'. #4940 fixed the aliased form, this is the unaliased one.

As the issue suggests, getApiItemsInDst now 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_Issue4954 follows the #4289 test with the unaliased import from the issue steps, with and without Clear. On master it fails with EXPECT 0 == 1 (no DictTypeAdd) and with the EOF error when Clear is set.

Ran: go test -count=1 -run Test_Gen_Ctrl ./internal/cmd/ in cmd/gf, all 7 pass
Checked: the issue steps with a built gf, a v2 api and excelize/v2, for -m and -m -c
Checked: golangci-lint run -c .golangci.yml reports no new issues in genctrl

Fixes #4954

…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
@gqcn

gqcn commented Nov 2, 2026

Copy link
Copy Markdown
Member

The import selection change looks right for the unaliased case in #4954. getApiItemsInDst now prefers an import whose path is one of the source api imports, and still keeps the first version-segment match when nothing in the file is in that set. Test_Gen_Ctrl_UseMerge_Issue4954 covers both -m and -m -c.

Merge is still blocked by CI on this head. code-test (1.26, core) failed in github.com/gogf/gf/v2/os/gcron, which this patch does not touch:

  • TestCron_JobWaiter (os/gcron/gcron_z_unit_test.go:364): EXPECT 1 == 2
  • TestCron_JobWaiterNonBlocking (os/gcron/gcron_z_unit_test.go:407): EXPECT 1 == 2

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 code-test (1.25, core), code-test (1.27, core), code-test (1.27, drivers-1), and code-test (1.27, drivers-2) cancelled. Rerunning GoFrame Main CI should be enough to get a clean result. I have not added bot-approved.

@codecov

codecov Bot commented Nov 9, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 84.91%. Comparing base (e226709) to head (8c6a328).

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     
Flag Coverage Δ
go-1.27-core 72.64% <ø> (+<0.01%) ⬆️
go-1.27-drivers-1 27.65% <ø> (ø)
go-1.27-drivers-2 27.40% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@gqcn

gqcn commented Nov 9, 2026

Copy link
Copy Markdown
Member

The earlier comment was about a CI failure on 01913142 in os/gcron (TestCron_JobWaiter, TestCron_JobWaiterNonBlocking). That job does not exercise this patch, and the checks on the current head 411f125 are green.

This is still the unaliased-import fix for #4954: merge mode now matches the controller's api import by the source import path, with Test_Gen_Ctrl_UseMerge_Issue4954 covering both merge and clear. I don't see anything else that should hold it up. Adding bot-approved.

@gqcn gqcn added the bot-approved Approved by gf-pr-review label Nov 9, 2026
@hailaz
hailaz requested a balanced review from Copilot November 9, 2026 03:20

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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>
@hailaz
hailaz merged commit df697b6 into gogf:master Nov 9, 2026
17 of 23 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bot-approved Approved by gf-pr-review

Projects

None yet

Development

Successfully merging this pull request may close these issues.

cmd/gf: gen ctrl merge mode takes an unaliased import ending in the api version as the api package

5 participants