Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

simplify --fail-on-lint-warning and fix error message #747

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 9 additions & 11 deletions pkg/build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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)
}
}

Expand Down
39 changes: 15 additions & 24 deletions pkg/linter/linter.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package linter

import (
"debug/elf"
"errors"
"fmt"
"io"
"io/fs"
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}

Expand All @@ -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 {
Expand All @@ -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)
Comment on lines -338 to -341
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This eliminates the ability to have individual linters be fatal.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Basically: we need a way to introduce a linter as just a warning, get it clean, and then make it fatal.

If we land this then we can't make anything required without making everything required and it will become a requirement to get linters clean across Wolfi before landing any of its logic, which I don't think we want.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WDYT about having --fail-on-lint-warning=empty,setuidgid,foo,bar which selects which ones will be considered fatal?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That seems fine, but I still think linters should be able to have defaults. This could be through the flag defaults, I have no preference here.

if err := linter.LinterFunc(lctx, fsys); err != nil {
errs = append(errs, err)
}
}
return errors.Join(errs...)

return nil
}
78 changes: 13 additions & 65 deletions pkg/linter/linter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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")
Expand All @@ -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) {
Expand Down Expand Up @@ -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))
}