Skip to content

Commit 10e51f8

Browse files
feat(debug): add debug flag and docs for dumping vet rule variables (#2521)
* feat(debug): add debug flag and docs for dumping vet rule variables * feat(debug): dump vet rule env as JSON
1 parent e3c657d commit 10e51f8

File tree

4 files changed

+26
-0
lines changed

4 files changed

+26
-0
lines changed

docs/reference/environment-variables.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,13 @@ return an error.
126126

127127
`SQLCDEBUG=processplugins=0`
128128

129+
### dumpvetenv
130+
131+
The `dumpvetenv` command prints the variables available to a `sqlc vet` rule
132+
during evaluation.
133+
134+
`SQLCDEBUG=dumpvetenv=1`
135+
129136
### dumpexplain
130137

131138
The `dumpexplain` command prints the JSON-formatted result from running

internal/cmd/vet.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,11 @@ func (c *checker) checkSQL(ctx context.Context, s config.SQL) error {
397397
evalMap["mysql"] = engineOutput.MySQL
398398
}
399399

400+
if debug.Debug.DumpVetEnv {
401+
fmt.Printf("vars for rule '%s' evaluating against query '%s':\n", name, query.Name)
402+
debug.DumpAsJSON(evalMap)
403+
}
404+
400405
out, _, err := (*rule.Program).Eval(evalMap)
401406
if err != nil {
402407
return err

internal/debug/dump.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package debug
22

33
import (
4+
"encoding/json"
5+
"fmt"
46
"os"
57

68
"github.com/davecgh/go-spew/spew"
@@ -23,3 +25,10 @@ func Dump(n ...interface{}) {
2325
spew.Dump(n)
2426
}
2527
}
28+
29+
func DumpAsJSON(a any) {
30+
if Active {
31+
out, _ := json.MarshalIndent(a, "", " ")
32+
fmt.Printf("%s\n", out)
33+
}
34+
}

internal/opts/debug.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import (
1212
// dumpcatalog: setting dumpcatalog=1 will print the parsed database schema
1313
// trace: setting trace=<path> will output a trace
1414
// processplugins: setting processplugins=0 will disable process-based plugins
15+
// dumpvetenv: setting dumpvetenv=1 will print the variables available to
16+
// a vet rule during evaluation
1517
// dumpexplain: setting dumpexplain=1 will print the JSON-formatted output
1618
// from executing EXPLAIN ... on a query during vet rule evaluation
1719

@@ -20,6 +22,7 @@ type Debug struct {
2022
DumpCatalog bool
2123
Trace string
2224
ProcessPlugins bool
25+
DumpVetEnv bool
2326
DumpExplain bool
2427
}
2528

@@ -50,6 +53,8 @@ func DebugFromString(val string) Debug {
5053
}
5154
case pair == "processplugins=0":
5255
d.ProcessPlugins = false
56+
case pair == "dumpvetenv=1":
57+
d.DumpVetEnv = true
5358
case pair == "dumpexplain=1":
5459
d.DumpExplain = true
5560
}

0 commit comments

Comments
 (0)