Skip to content
New issue

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

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

Already on GitHub? # to your account

cmd/fix: Filter files loaded for fixing #762

Merged
merged 1 commit into from
May 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion cmd/fix.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,13 @@ func fix(args []string, params *fixCommandParams) error {
f := fixer.NewFixer()
f.RegisterFixes(fixes.NewDefaultFixes()...)

fileProvider := fileprovider.NewFSFileProvider(args...)
ignore := userConfig.Ignore.Files

if len(params.ignoreFiles.v) > 0 {
ignore = params.ignoreFiles.v
}

fileProvider := fileprovider.NewFSFileProvider(ignore, args...)

fixReport, err := f.Fix(ctx, &l, fileProvider)
if err != nil {
Expand Down
6 changes: 6 additions & 0 deletions e2e/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -751,6 +751,12 @@ allow if {
t.Fatalf("failed to write main.rego: %v", err)
}

unrelatedFileContents := []byte(`foobar`)
err = os.WriteFile(filepath.Join(td, "unrelated.txt"), unrelatedFileContents, 0o644)
if err != nil {
t.Fatalf("failed to write unrelated.txt: %v", err)
}

err = regal(&stdout, &stderr)("fix", td)

// 0 exit status is expected as all violations should have been fixed
Expand Down
32 changes: 10 additions & 22 deletions pkg/fixer/fileprovider/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,45 +3,33 @@ package fileprovider
import (
"fmt"
"os"
"path/filepath"

"github.com/open-policy-agent/opa/ast"

"github.com/styrainc/regal/internal/parse"
"github.com/styrainc/regal/pkg/config"
"github.com/styrainc/regal/pkg/rules"
)

type FSFileProvider struct {
roots []string
roots []string
ignore []string
}

func NewFSFileProvider(roots ...string) *FSFileProvider {
func NewFSFileProvider(ignore []string, roots ...string) *FSFileProvider {
return &FSFileProvider{
roots: roots,
roots: roots,
ignore: ignore,
}
}

func (p *FSFileProvider) ListFiles() ([]string, error) {
var files []string

for _, root := range p.roots {
err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}

if !info.IsDir() {
files = append(files, path)
}

return nil
})
if err != nil {
return nil, fmt.Errorf("failed to list files: %w", err)
}
filtered, err := config.FilterIgnoredPaths(p.roots, p.ignore, true, "")
if err != nil {
return nil, fmt.Errorf("failed to filter ignored paths: %w", err)
}

return files, nil
return filtered, nil
}

func (*FSFileProvider) GetFile(file string) ([]byte, error) {
Expand Down
4 changes: 4 additions & 0 deletions pkg/fixer/fixes/useassignmentoperator.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ func (*UseAssignmentOperator) Fix(fc *FixCandidate, opts *RuntimeOptions) ([]Fix

line := lines[loc.Row-1]

if loc.Col-1 < 0 || loc.Col-1 >= len(line) {
Copy link
Member Author

Choose a reason for hiding this comment

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

I ran into this running fix on a large rego codebase where another fix had shifted the contents for that run.

continue
}

// unexpected character at location column, skipping
if line[loc.Col-1] != byte('=') {
continue
Expand Down