diff --git a/docs/reference/environment-variables.md b/docs/reference/environment-variables.md index 535231c1e9..185807168c 100644 --- a/docs/reference/environment-variables.md +++ b/docs/reference/environment-variables.md @@ -126,6 +126,13 @@ return an error. `SQLCDEBUG=processplugins=0` +### dumpvetenv + +The `dumpvetenv` command prints the variables available to a `sqlc vet` rule +during evaluation. + +`SQLCDEBUG=dumpvetenv=1` + ### dumpexplain The `dumpexplain` command prints the JSON-formatted result from running diff --git a/internal/cmd/vet.go b/internal/cmd/vet.go index 0fe8c0047c..15927a3bbb 100644 --- a/internal/cmd/vet.go +++ b/internal/cmd/vet.go @@ -397,6 +397,11 @@ func (c *checker) checkSQL(ctx context.Context, s config.SQL) error { evalMap["mysql"] = engineOutput.MySQL } + if debug.Debug.DumpVetEnv { + fmt.Printf("vars for rule '%s' evaluating against query '%s':\n", name, query.Name) + debug.DumpAsJSON(evalMap) + } + out, _, err := (*rule.Program).Eval(evalMap) if err != nil { return err diff --git a/internal/debug/dump.go b/internal/debug/dump.go index d2e25bab7c..a0d30d95cf 100644 --- a/internal/debug/dump.go +++ b/internal/debug/dump.go @@ -1,6 +1,8 @@ package debug import ( + "encoding/json" + "fmt" "os" "github.com/davecgh/go-spew/spew" @@ -23,3 +25,10 @@ func Dump(n ...interface{}) { spew.Dump(n) } } + +func DumpAsJSON(a any) { + if Active { + out, _ := json.MarshalIndent(a, "", " ") + fmt.Printf("%s\n", out) + } +} \ No newline at end of file diff --git a/internal/opts/debug.go b/internal/opts/debug.go index 9fbb0a9742..94cda87863 100644 --- a/internal/opts/debug.go +++ b/internal/opts/debug.go @@ -12,6 +12,8 @@ import ( // dumpcatalog: setting dumpcatalog=1 will print the parsed database schema // trace: setting trace= will output a trace // processplugins: setting processplugins=0 will disable process-based plugins +// dumpvetenv: setting dumpvetenv=1 will print the variables available to +// a vet rule during evaluation // dumpexplain: setting dumpexplain=1 will print the JSON-formatted output // from executing EXPLAIN ... on a query during vet rule evaluation @@ -20,6 +22,7 @@ type Debug struct { DumpCatalog bool Trace string ProcessPlugins bool + DumpVetEnv bool DumpExplain bool } @@ -50,6 +53,8 @@ func DebugFromString(val string) Debug { } case pair == "processplugins=0": d.ProcessPlugins = false + case pair == "dumpvetenv=1": + d.DumpVetEnv = true case pair == "dumpexplain=1": d.DumpExplain = true }