Skip to content

Commit

Permalink
nogo: Add a _default key to be a default config for all Analyzers.
Browse files Browse the repository at this point in the history
This is useful for situations where you want to exclude/include the same directories for all the analyzers. Specifying a config for a particular analyzer will cause the _default config, if it exists, to be discarded for that analyzer (ie, the default config will not be merged).

You can use it like so:

{
	"_default": {
		"exclude_files": {
			"external/": "third_party",
			"proto/": "generated protobuf"
		}
	}
}
  • Loading branch information
DolceTriade committed Nov 15, 2022
1 parent 3d19fb5 commit 3d8c2a1
Showing 1 changed file with 17 additions and 6 deletions.
23 changes: 17 additions & 6 deletions go/tools/builders/nogo_main.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ import (
"golang.org/x/tools/go/gcexportdata"
)

const nogoDefaultConfigName = "_default"

func init() {
if err := analysis.Validate(analyzers); err != nil {
log.Fatal(err)
Expand Down Expand Up @@ -89,7 +91,7 @@ func run(args []string) error {
return fmt.Errorf("errors found by nogo during build-time code analysis:\n%s\n", diagnostics)
}
if *xPath != "" {
if err := ioutil.WriteFile(abs(*xPath), facts, 0666); err != nil {
if err := ioutil.WriteFile(abs(*xPath), facts, 0o666); err != nil {
return fmt.Errorf("error writing facts: %v", err)
}
}
Expand Down Expand Up @@ -390,6 +392,10 @@ func checkAnalysisResults(actions []*action, pkg *goPackage) string {
}
var diagnostics []entry
var errs []error
var defaultConfig *config
if dc, ok := configs[nogoDefaultConfigName]; ok {
defaultConfig = &dc
}
for _, act := range actions {
if act.err != nil {
// Analyzer failed.
Expand All @@ -401,12 +407,17 @@ func checkAnalysisResults(actions []*action, pkg *goPackage) string {
}
config, ok := configs[act.a.Name]
if !ok {
// If the analyzer is not explicitly configured, it emits diagnostics for
// all files.
for _, diag := range act.diagnostics {
diagnostics = append(diagnostics, entry{Diagnostic: diag, Analyzer: act.a})
if defaultConfig != nil {
config = *defaultConfig
// Continue processing using the defaultConfig as config.
} else {
// If the analyzer is not explicitly configured, it emits diagnostics for
// all files.
for _, diag := range act.diagnostics {
diagnostics = append(diagnostics, entry{Diagnostic: diag, Analyzer: act.a})
}
continue
}
continue
}
// Discard diagnostics based on the analyzer configuration.
for _, d := range act.diagnostics {
Expand Down

0 comments on commit 3d8c2a1

Please sign in to comment.