Commit ec32eee
fix(codegen): no spurious lib/pq import for MySQL with sqlc.narg + sqlc.slice (#3783)
Problem
-------
A MySQL query that references the same named parameter as both a regular
argument (e.g. `sqlc.narg('x') IS NULL`) and a `sqlc.slice('x')`
expansion produced Go code that imports `github.com/lib/pq` even
though the import is unused — the runtime path expands the slice
in-place via the `/*SLICE:...*/?` placeholder and never calls
`pq.Array`.
Root cause
----------
The two references generate two `goColumn` entries with the same
name and type; only the slice variant carries the `IsSqlcSlice` flag.
The non-slice duplicate field still satisfies the `[]T && !sqlc.slice`
condition in `sliceScan()` inside `internal/codegen/golang/imports.go`,
which causes `lib/pq` to be added to the import set. The struct
itself is rendered through `UniqueFields` so only one field is
emitted, but the import-bookkeeping pass sees both.
Fix
---
In `sliceScan()`, collect the names of all fields carrying
`IsSqlcSlice` first, then skip any field whose name matches one of
those when deciding whether to require `lib/pq`. The duplicate is
already handled by the slice-expansion code path and does not need
`pq.Array`.
Tests
-----
- New endtoend fixture `mysql_slice_narg_no_pq_3783/mysql` mirrors
the issue's repro; golden files confirm no `lib/pq` import in the
generated code.
- Full `internal/endtoend` TestReplay suite passes.
- `go test ./internal/codegen/... ./internal/compiler/... ./internal/sql/...` clean.
What does NOT change
--------------------
- Postgres / pgx output is unaffected (the field-name filter only
suppresses lib/pq when a sqlc.slice with the same name exists, and
pgx never went through the lib/pq branch anyway).
- MySQL queries that use `sqlc.slice` alone, or a plain `[]T`
argument, are unchanged.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>1 parent a3b0cfd commit ec32eee
7 files changed
Lines changed: 158 additions & 0 deletions
File tree
- internal
- codegen/golang
- endtoend/testdata/mysql_slice_narg_no_pq_3783/mysql
- go
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
376 | 376 | | |
377 | 377 | | |
378 | 378 | | |
| 379 | + | |
| 380 | + | |
| 381 | + | |
| 382 | + | |
| 383 | + | |
| 384 | + | |
| 385 | + | |
| 386 | + | |
| 387 | + | |
379 | 388 | | |
| 389 | + | |
| 390 | + | |
| 391 | + | |
| 392 | + | |
| 393 | + | |
| 394 | + | |
| 395 | + | |
| 396 | + | |
380 | 397 | | |
381 | 398 | | |
382 | 399 | | |
| |||
Lines changed: 31 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 15 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 68 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 10 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
Lines changed: 5 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
Lines changed: 12 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
0 commit comments