From e2deaf768c79efcea69caee681adb938b6d2f898 Mon Sep 17 00:00:00 2001 From: knqyf263 Date: Thu, 31 Oct 2024 14:54:26 +0400 Subject: [PATCH 1/2] chore: add a new rule for errors.Join Signed-off-by: knqyf263 --- misc/lint/rules.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/misc/lint/rules.go b/misc/lint/rules.go index e477175a244f..4f1b098535ec 100644 --- a/misc/lint/rules.go +++ b/misc/lint/rules.go @@ -20,3 +20,13 @@ func initializeMaps(m dsl.Matcher) { Suggest(`make(map[$key]$value)`). Report(`replace '$$' with 'make(map[$key]$value)`) } + +// While errors.Join from standard library can combine multiple errors, +// we use hashicorp/go-multierror for more user-friendly error outputs. +func errorsJoin(m dsl.Matcher) { + m.Match(`errors.Join($x...)`). + Report("use github.com/hashicorp/go-multierror.Append instead of errors.Join.") + + m.Match(`errors.Join($*args)`). + Report("use github.com/hashicorp/go-multierror.Append instead of errors.Join.") +} From 078d01c43f5504e8d68b9e73796c7272aadab473 Mon Sep 17 00:00:00 2001 From: knqyf263 Date: Thu, 31 Oct 2024 15:13:57 +0400 Subject: [PATCH 2/2] fix: use multierror.Append Signed-off-by: knqyf263 --- pkg/iac/scanners/cloudformation/parser/parser.go | 11 +++++------ pkg/iac/terraform/block.go | 15 ++++++++------- pkg/report/writer.go | 4 ++-- pkg/vex/repo/repo.go | 3 ++- 4 files changed, 17 insertions(+), 16 deletions(-) diff --git a/pkg/iac/scanners/cloudformation/parser/parser.go b/pkg/iac/scanners/cloudformation/parser/parser.go index 40d0035f6cd2..696dfcf16349 100644 --- a/pkg/iac/scanners/cloudformation/parser/parser.go +++ b/pkg/iac/scanners/cloudformation/parser/parser.go @@ -3,13 +3,13 @@ package parser import ( "context" "encoding/json" - "errors" "fmt" "io" "io/fs" "path/filepath" "strings" + "github.com/hashicorp/go-multierror" "github.com/liamg/jfather" "gopkg.in/yaml.v3" @@ -171,18 +171,17 @@ func (p *Parser) parseParams() error { params := make(Parameters) - var errs []error - + var errs error for _, path := range p.parameterFiles { if parameters, err := p.parseParametersFile(path); err != nil { - errs = append(errs, err) + errs = multierror.Append(errs, err) } else { params.Merge(parameters) } } - if len(errs) != 0 { - return errors.Join(errs...) + if errs != nil { + return errs } params.Merge(p.parameters) diff --git a/pkg/iac/terraform/block.go b/pkg/iac/terraform/block.go index dd6d8446c124..348f938d4559 100644 --- a/pkg/iac/terraform/block.go +++ b/pkg/iac/terraform/block.go @@ -8,6 +8,7 @@ import ( "strings" "github.com/google/uuid" + "github.com/hashicorp/go-multierror" "github.com/hashicorp/hcl/v2" "github.com/hashicorp/hcl/v2/hclsyntax" "github.com/zclconf/go-cty/cty" @@ -600,14 +601,14 @@ func (b *Block) IsNotNil() bool { func (b *Block) ExpandBlock() error { var ( expanded []*Block - errs []error + errs error ) for _, child := range b.childBlocks { if child.Type() == "dynamic" { blocks, err := child.expandDynamic() if err != nil { - errs = append(errs, err) + errs = multierror.Append(errs, err) continue } expanded = append(expanded, blocks...) @@ -618,7 +619,7 @@ func (b *Block) ExpandBlock() error { b.injectBlock(block) } - return errors.Join(errs...) + return errs } func (b *Block) expandDynamic() ([]*Block, error) { @@ -638,7 +639,7 @@ func (b *Block) expandDynamic() ([]*Block, error) { var ( expanded []*Block - errs []error + errs error ) forEachVal.ForEachElement(func(key, val cty.Value) (stop bool) { @@ -648,7 +649,7 @@ func (b *Block) expandDynamic() ([]*Block, error) { iteratorName, err := b.iteratorName(realBlockType) if err != nil { - errs = append(errs, err) + errs = multierror.Append(errs, err) return } @@ -664,7 +665,7 @@ func (b *Block) expandDynamic() ([]*Block, error) { inherited.hclBlock.Labels = []string{} inherited.hclBlock.Type = realBlockType if err := inherited.ExpandBlock(); err != nil { - errs = append(errs, err) + errs = multierror.Append(errs, err) return } expanded = append(expanded, inherited) @@ -676,7 +677,7 @@ func (b *Block) expandDynamic() ([]*Block, error) { b.markExpanded() } - return expanded, errors.Join(errs...) + return expanded, errs } func (b *Block) validateForEach() (cty.Value, error) { diff --git a/pkg/report/writer.go b/pkg/report/writer.go index a5b7ae231599..f25d579d66ef 100644 --- a/pkg/report/writer.go +++ b/pkg/report/writer.go @@ -2,10 +2,10 @@ package report import ( "context" - "errors" "io" "strings" + "github.com/hashicorp/go-multierror" "golang.org/x/xerrors" cr "github.com/aquasecurity/trivy/pkg/compliance/report" @@ -32,7 +32,7 @@ func Write(ctx context.Context, report types.Report, option flag.Options) (err e } defer func() { if cerr := cleanup(); cerr != nil { - err = errors.Join(err, cerr) + err = multierror.Append(err, cerr) } }() diff --git a/pkg/vex/repo/repo.go b/pkg/vex/repo/repo.go index 433b8224c20e..b9637c229ee9 100644 --- a/pkg/vex/repo/repo.go +++ b/pkg/vex/repo/repo.go @@ -11,6 +11,7 @@ import ( "time" "github.com/hashicorp/go-getter" + "github.com/hashicorp/go-multierror" "github.com/samber/lo" "golang.org/x/xerrors" @@ -256,7 +257,7 @@ func (r *Repository) download(ctx context.Context, ver Version, dst string, opts etag = etags[loc.URL] // Keep the old ETag // Update last updated time so that Trivy will not try to download the same URL soon case err != nil: - errs = errors.Join(errs, err) + errs = multierror.Append(errs, err) continue // Try the next location default: // Successfully downloaded