From 7a52a5096408d23c699215d07dd3f6c94c8ec7c9 Mon Sep 17 00:00:00 2001 From: zhangwenjian Date: Sat, 19 Sep 2026 20:48:58 +0800 Subject: [PATCH] =?UTF-8?q?fix=F0=9F=90=9B:=20dedupe=20FK=20imports=20by?= =?UTF-8?q?=20target=20table=20and=20filter=20by=20actual=20use?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Two columns pointing at the same foreign table -- owner and approver both selecting from the same users table, say -- each triggered their own `import { listX } from ...` / `import type { X } from ...` line, which is a duplicate ES module import once both fire: TS2300. Nothing here ever asked whether a target table had already been imported by an earlier column, because nothing tracked target tables at all -- only source columns, and "one FK-configured column" was never the same thing as "one distinct target table". The same import was also gated on the column having FkTableName set, not on $fkUsed -- the condition the previous fix already applies to the const declarations that read the import. A column carrying FK metadata but reaching no query, list or insert-select branch imported a module nothing in the file references. text/template has no set to check membership in, so the dedup is a nested range: a column only imports its target if no earlier, equally-used column already claimed the same FkTableNameClass. $fkUsed is recomputed for both the outer and the inner column rather than factored out, since text/template has no way to carry a per-column value computed in one range into a second, later range over the same data. Verified with a fixture carrying three columns pointing at the same target table -- one read only from search, one only from the list, one from neither -- rendered through the real template.Execute and checked against a throwaway go-admin-ui worktree (deleted afterwards): before this change, TS2300 fired six times (the function and the type, three times over); after, exactly one import of each, and the unused third column contributes neither. Re-ran the previous rounds' fixtures alongside it; all stayed green. pnpm type-check and pnpm lint both zero error, on Node 24.11.0. --- template/v4/vue.go.template | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/template/v4/vue.go.template b/template/v4/vue.go.template index 503fdc2a..789ab16b 100644 --- a/template/v4/vue.go.template +++ b/template/v4/vue.go.template @@ -273,10 +273,31 @@ import { add{{.ClassName}}, del{{.ClassName}}, get{{.ClassName}}, list{{.ClassName}}, update{{.ClassName}} } from '@/api/{{.PackageName}}/{{.MLTBName}}' import type { {{.ClassName}}, {{.ClassName}}Query } from '@/api/{{.PackageName}}/{{.MLTBName}}' -{{- range .Columns}} -{{- if ne .FkTableName ""}} -import { list{{.FkTableNameClass}} } from '@/api/{{$package}}/{{.FkTableNamePackage}}' -import type { {{.FkTableNameClass}} } from '@/api/{{$package}}/{{.FkTableNamePackage}}' +{{- /* + Two columns pointing at the same foreign table must not import it twice -- + "one FK-configured column" was never the same thing as "one distinct target + table", and gen.go has no concept of a table's FK targets being unique. + text/template has no set to check membership in, so the dedup is a nested + range: a column only imports its target if no earlier, equally-used column + already claimed the same FkTableNameClass. $fkUsed is repeated here (it also + guards the const declarations above) because a column with FkTableName set + but reaching none of them -- unqueried, unlisted, not an insert select -- + has nothing that would use the import either. +*/ -}} +{{- range $i, $col := .Columns}} +{{- $fkUsed := and (ne $col.FkTableName "") (or (eq $col.IsQuery "1") (eq $col.IsList "1") (and (eq $col.IsInsert "1") (eq $col.HtmlType "select"))) -}} +{{- if $fkUsed}} +{{- $alreadyImported := false -}} +{{- range $j, $prior := $.Columns}} +{{- if lt $j $i}} +{{- $priorUsed := and (ne $prior.FkTableName "") (or (eq $prior.IsQuery "1") (eq $prior.IsList "1") (and (eq $prior.IsInsert "1") (eq $prior.HtmlType "select"))) -}} +{{- if and $priorUsed (eq $prior.FkTableNameClass $col.FkTableNameClass) }}{{$alreadyImported = true}}{{end -}} +{{- end}} +{{- end}} +{{- if not $alreadyImported}} +import { list{{$col.FkTableNameClass}} } from '@/api/{{$package}}/{{$col.FkTableNamePackage}}' +import type { {{$col.FkTableNameClass}} } from '@/api/{{$package}}/{{$col.FkTableNamePackage}}' +{{- end}} {{- end}} {{- end}} {{- /*