Skip to content

SQLC Config Discovery

gogenfilter discovers SQLC configuration files and extracts output directories, so you don’t have to hardcode paths. It supports both v1 and v2 config formats, including plugin-based codegen and JSON output.

  • sqlc.yaml and sqlc.yml
  • Version 1 (version: "1") and Version 2 (version: "2")
version: "2"
sql:
- engine: "postgresql"
schema: "queries/"
queries: "queries/"
gen:
go:
package: "db"
out: "gen/db" # Go output directory
json:
out: "gen/json" # JSON output directory
codegen:
- plugin: "go-structs"
out: "gen/structs" # Plugin output directory

All three output directory sources are extracted:

SourceField
Go codegensql[].gen.go.out
JSON outputsql[].gen.json.out
Plugin codegensql[].codegen[].out
version: "1"
packages:
- name: "db"
path: "gen/db" # output directory
engine: "postgresql"
schema: "queries/"
queries: "queries/"

V1 configs use packages[].path as the output directory, which is automatically mapped.

FindSQLCConfigs searches the provided paths for sqlc.yaml / sqlc.yml. It also searches parent directories (up to 10 levels up) for configs that may live above the query directory.

configs, err := gogenfilter.FindSQLCConfigs([]string{"./..."})
if err != nil {
log.Fatal(err)
}
for path, root := range configs {
fmt.Printf("Config: %s (root: %s)\n", path, root)
}

FindSQLCConfigsFS is the fs.FS-compatible version. It does not search parent directories — use this for testing or custom filesystems:

configs, err := gogenfilter.FindSQLCConfigsFS(fsys, []string{"."})

GetSQLOutputDirs reads config files and extracts output directories:

dirs, err := gogenfilter.GetSQLOutputDirs([]string{"."})
if err != nil {
log.Fatal(err)
}
for _, dir := range dirs {
fmt.Printf("Output: %s\n", dir)
}
dirs, err := gogenfilter.GetSQLOutputDirsFS(fsys, []string{"."})

Combine config discovery with include patterns for precise scoping:

dirs, _ := gogenfilter.GetSQLOutputDirs([]string{"."})
includePatterns := make([]string, len(dirs))
for i, dir := range dirs {
includePatterns[i] = dir + "/**"
}
opts, err := gogenfilter.WithFilterOptions(gogenfilter.FilterSQLC)
if err != nil {
panic(err)
}
includeConfig := gogenfilter.WithIncludePatterns(includePatterns...)
f, err := gogenfilter.NewFilter(opts, includeConfig)
if err != nil {
panic(err)
}

The parser uses github.com/go-faster/yaml — a 2–3x faster, actively maintained fork of go-yaml/yaml. The config version is detected first, then dispatched to v1 or v2 parsing logic. Unsupported versions return a parse error. See the Known Limitations page for documented tradeoffs.