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 4e6618d
Showing 1 changed file with 27 additions and 6 deletions.
33 changes: 27 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 @@ -380,6 +382,16 @@ func (g *goPackage) String() string {
return g.types.Path()
}

func mergeMap(a, b map[string]string) (out map[string]string) {
for k, v := range a {
out[k] = v
}
for k, v := range b {
out[k] = v
}
return out
}

// checkAnalysisResults checks the analysis diagnostics in the given actions
// and returns a string containing all the diagnostics that should be printed
// to the build log.
Expand All @@ -390,6 +402,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 +417,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 4e6618d

Please sign in to comment.