diff --git a/pkg/build/build.go b/pkg/build/build.go index 3c8fa716..f0b9aaa0 100644 --- a/pkg/build/build.go +++ b/pkg/build/build.go @@ -1132,6 +1132,7 @@ func (b *Build) BuildPackage(ctx context.Context) error { b.Logger.Printf("retrieved and wrote post-build workspace to: %s", b.WorkspaceDir) // perform package linting + var errs []error for _, lt := range linterQueue { b.Logger.Printf("running package linters for %s", lt.pkgName) @@ -1140,18 +1141,15 @@ func (b *Build) BuildPackage(ctx context.Context) error { lctx := linter.NewLinterContext(lt.pkgName, fsys) linters := lt.checks.GetLinters() - var innerErr error - err = lctx.LintPackageFs(fsys, func(err error) { - if b.FailOnLintWarning { - innerErr = err - } else { - b.Logger.Warnf("WARNING: %v", err) - } - }, linters) - if err != nil { + if err := lctx.LintPackageFs(fsys, linters); err != nil { + errs = append(errs, err) + } + } + if err := errors.Join(errs...); err != nil { + if b.FailOnLintWarning { return fmt.Errorf("package linter error: %w", err) - } else if innerErr != nil { - return fmt.Errorf("package linter warning: %w", err) + } else { + b.Logger.Warnf("WARNING: %v", err) } } diff --git a/pkg/linter/linter.go b/pkg/linter/linter.go index 482b4125..7113bc68 100644 --- a/pkg/linter/linter.go +++ b/pkg/linter/linter.go @@ -16,6 +16,7 @@ package linter import ( "debug/elf" + "errors" "fmt" "io" "io/fs" @@ -262,7 +263,8 @@ func strippedLinter(lctx LinterContext, path string, d fs.DirEntry) error { func emptyPostLinter(_ LinterContext, fsys fs.FS) error { foundfile := false - walkCb := func(path string, _ fs.DirEntry, err error) error { + + if err := fs.WalkDir(fsys, ".", func(path string, _ fs.DirEntry, err error) error { if err != nil { return err } @@ -274,10 +276,7 @@ func emptyPostLinter(_ LinterContext, fsys fs.FS) error { foundfile = true return fs.SkipAll - } - - err := fs.WalkDir(fsys, ".", walkCb) - if err != nil { + }); err != nil { return err } @@ -289,18 +288,19 @@ func emptyPostLinter(_ LinterContext, fsys fs.FS) error { return fmt.Errorf("Package is empty but no-provides is not set") } -func (lctx LinterContext) LintPackageFs(fsys fs.FS, warn func(error), linters []string) error { +func (lctx LinterContext) LintPackageFs(fsys fs.FS, linters []string) error { // If this is a compat package, do nothing. if isCompatPackageRegex.MatchString(lctx.pkgname) { return nil } postLinters := []string{} - walkCb := func(path string, d fs.DirEntry, err error) error { + if err := fs.WalkDir(fsys, ".", func(path string, d fs.DirEntry, err error) error { if err != nil { return fmt.Errorf("Error traversing tree at %s: %w", path, err) } + var errs []error for _, linterName := range linters { linter, present := linterMap[linterName] if !present { @@ -314,33 +314,24 @@ func (lctx LinterContext) LintPackageFs(fsys fs.FS, warn func(error), linters [] continue } - err = linter.LinterFunc(lctx, path, d) - if err != nil { - if linter.FailOnError { - return fmt.Errorf("Linter %s failed at path %q: %w; suggest: %s", linterName, path, err, linter.Explain) - } - warn(err) + if err := linter.LinterFunc(lctx, path, d); err != nil { + errs = append(errs, err) } } - - return nil - } - - if err := fs.WalkDir(fsys, ".", walkCb); err != nil { + return errors.Join(errs...) + }); err != nil { return err } // Run post-walking linters + var errs []error for _, linterName := range postLinters { linter := postLinterMap[linterName] - err := linter.LinterFunc(lctx, fsys) - if err != nil { - if linter.FailOnError { - return fmt.Errorf("Linter %s failed; suggest: %s", linterName, linter.Explain) - } - warn(err) + if err := linter.LinterFunc(lctx, fsys); err != nil { + errs = append(errs, err) } } + return errors.Join(errs...) return nil } diff --git a/pkg/linter/linter_test.go b/pkg/linter/linter_test.go index ec014b85..8d610551 100644 --- a/pkg/linter/linter_test.go +++ b/pkg/linter/linter_test.go @@ -46,11 +46,7 @@ func Test_emptyLinter(t *testing.T) { assert.Equal(t, linters, []string{"empty"}) fsys := os.DirFS(dir) lctx := NewLinterContext(cfg.Package.Name, fsys) - called := false - assert.NoError(t, lctx.LintPackageFs(fsys, func(err error) { - called = true - }, linters)) - assert.True(t, called) + assert.Error(t, lctx.LintPackageFs(fsys, linters)) } func Test_usrLocalLinter(t *testing.T) { @@ -79,11 +75,7 @@ func Test_usrLocalLinter(t *testing.T) { assert.Equal(t, linters, []string{"usrlocal"}) fsys := os.DirFS(dir) lctx := NewLinterContext(cfg.Package.Name, fsys) - called := false - assert.NoError(t, lctx.LintPackageFs(fsys, func(err error) { - called = true - }, linters)) - assert.True(t, called) + assert.Error(t, lctx.LintPackageFs(fsys, linters)) } func Test_varEmptyLinter(t *testing.T) { @@ -113,11 +105,7 @@ func Test_varEmptyLinter(t *testing.T) { assert.Equal(t, linters, []string{"varempty"}) fsys := os.DirFS(dir) lctx := NewLinterContext(cfg.Package.Name, fsys) - called := false - assert.NoError(t, lctx.LintPackageFs(fsys, func(err error) { - called = true - }, linters)) - assert.True(t, called) + assert.Error(t, lctx.LintPackageFs(fsys, linters)) } func Test_devLinter(t *testing.T) { @@ -147,11 +135,7 @@ func Test_devLinter(t *testing.T) { assert.Equal(t, linters, []string{"dev"}) fsys := os.DirFS(dir) lctx := NewLinterContext(cfg.Package.Name, fsys) - called := false - assert.NoError(t, lctx.LintPackageFs(fsys, func(err error) { - called = true - }, linters)) - assert.True(t, called) + assert.Error(t, lctx.LintPackageFs(fsys, linters)) } func Test_optLinter(t *testing.T) { @@ -181,11 +165,7 @@ func Test_optLinter(t *testing.T) { assert.Equal(t, linters, []string{"opt"}) fsys := os.DirFS(dir) lctx := NewLinterContext(cfg.Package.Name, fsys) - called := false - assert.NoError(t, lctx.LintPackageFs(fsys, func(err error) { - called = true - }, linters)) - assert.True(t, called) + assert.Error(t, lctx.LintPackageFs(fsys, linters)) } func Test_srvLinter(t *testing.T) { @@ -215,11 +195,7 @@ func Test_srvLinter(t *testing.T) { assert.Equal(t, linters, []string{"srv"}) fsys := os.DirFS(dir) lctx := NewLinterContext(cfg.Package.Name, fsys) - called := false - assert.NoError(t, lctx.LintPackageFs(fsys, func(err error) { - called = true - }, linters)) - assert.True(t, called) + assert.Error(t, lctx.LintPackageFs(fsys, linters)) } func Test_tempDirLinter(t *testing.T) { @@ -261,11 +237,7 @@ func Test_tempDirLinter(t *testing.T) { _, err = os.Create(filename) assert.NoError(t, err) lctx := NewLinterContext(cfg.Package.Name, fsys) - called := false - assert.NoError(t, lctx.LintPackageFs(fsys, func(err error) { - called = true - }, linters)) - assert.True(t, called) + assert.Error(t, lctx.LintPackageFs(fsys, linters)) os.Remove(filename) // Test /var/tmp check @@ -320,11 +292,7 @@ func Test_setUidGidLinter(t *testing.T) { assert.Equal(t, linters, []string{"setuidgid"}) fsys := os.DirFS(dir) lctx := NewLinterContext(cfg.Package.Name, fsys) - called := false - assert.NoError(t, lctx.LintPackageFs(fsys, func(err error) { - called = true - }, linters)) - assert.True(t, called) + assert.Error(t, lctx.LintPackageFs(fsys, linters)) } func Test_worldWriteLinter(t *testing.T) { @@ -353,11 +321,7 @@ func Test_worldWriteLinter(t *testing.T) { assert.Equal(t, linters, []string{"worldwrite"}) fsys := os.DirFS(dir) lctx := NewLinterContext(cfg.Package.Name, fsys) - called := false - assert.NoError(t, lctx.LintPackageFs(fsys, func(err error) { - called = true - }, linters)) - assert.False(t, called) + assert.NoError(t, lctx.LintPackageFs(fsys, linters)) // Create test file filePath := filepath.Join(usrLocalDirPath, "test.txt") @@ -369,33 +333,21 @@ func Test_worldWriteLinter(t *testing.T) { assert.NoError(t, err) // Linter should not trigger - called = false - assert.NoError(t, lctx.LintPackageFs(fsys, func(err error) { - called = true - }, linters)) - assert.False(t, called) + assert.NoError(t, lctx.LintPackageFs(fsys, linters)) // Set writeable bit (but not executable bit) err = os.Chmod(filePath, 0776) assert.NoError(t, err) // Linter should trigger - called = false - assert.NoError(t, lctx.LintPackageFs(fsys, func(err error) { - called = true - }, linters)) - assert.True(t, called) + assert.Error(t, lctx.LintPackageFs(fsys, linters)) // Set writeable and executable bit err = os.Chmod(filePath, 0777) assert.NoError(t, err) // Linter should trigger - called = false - assert.NoError(t, lctx.LintPackageFs(fsys, func(err error) { - called = true - }, linters)) - assert.True(t, called) + assert.Error(t, lctx.LintPackageFs(fsys, linters)) } func Test_disableDefaultLinter(t *testing.T) { @@ -426,9 +378,5 @@ func Test_disableDefaultLinter(t *testing.T) { linters := cfg.Package.Checks.GetLinters() fsys := os.DirFS(dir) lctx := NewLinterContext(cfg.Package.Name, fsys) - called := false - assert.NoError(t, lctx.LintPackageFs(fsys, func(err error) { - called = true - }, linters)) - assert.False(t, called) + assert.NoError(t, lctx.LintPackageFs(fsys, linters)) }