Skip to content

Commit

Permalink
checkers: add hidden ruleguard debug handle (#1134)
Browse files Browse the repository at this point in the history
With GOCRITIC_RULEGUARD_DEBUG=1 we can troubleshoot
what went wrong during the ruleguard rules loading.
  • Loading branch information
quasilyte committed Oct 19, 2021
1 parent 78f672b commit 252273b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
12 changes: 11 additions & 1 deletion checkers/checkers.go
Expand Up @@ -34,6 +34,8 @@ func init() {

var buildContext *build.Context

ruleguardDebug := os.Getenv("GOCRITIC_RULEGUARD_DEBUG") != ""

// First we create an Engine to parse all rules.
// We need it to get the structured info about our rules
// that will be used to generate checkers.
Expand All @@ -46,7 +48,11 @@ func init() {
buildContext = rootEngine.BuildContext

loadContext := &ruleguard.LoadContext{
Fset: fset,
Fset: fset,
DebugImports: ruleguardDebug,
DebugPrint: func(s string) {
fmt.Println("debug:", s)
},
}
if err := rootEngine.LoadFromIR(loadContext, filename, rulesdata.PrecompiledRules); err != nil {
panic(fmt.Sprintf("load embedded ruleguard rules: %v", err))
Expand Down Expand Up @@ -74,6 +80,10 @@ func init() {
GroupFilter: func(name string) bool {
return name == g.Name
},
DebugImports: ruleguardDebug,
DebugPrint: func(s string) {
fmt.Println("debug:", s)
},
}
engine := ruleguard.NewEngine()
engine.BuildContext = buildContext
Expand Down
8 changes: 7 additions & 1 deletion checkers/ruleguard_checker.go
Expand Up @@ -124,8 +124,14 @@ func newRuleguardChecker(info *linter.CheckerInfo, ctx *linter.CheckerContext) (
fset := token.NewFileSet()
filePatterns := strings.Split(rulesFlag, ",")

ruleguardDebug := os.Getenv("GOCRITIC_RULEGUARD_DEBUG") != ""

loadContext := &ruleguard.LoadContext{
Fset: fset,
Fset: fset,
DebugImports: ruleguardDebug,
DebugPrint: func(s string) {
fmt.Println("debug:", s)
},
}

loaded := 0
Expand Down

0 comments on commit 252273b

Please sign in to comment.