diff --git a/cmd/fix.go b/cmd/fix.go index 523fafee..318a4de5 100644 --- a/cmd/fix.go +++ b/cmd/fix.go @@ -277,6 +277,23 @@ func fix(args []string, params *fixCommandParams) error { ignore = params.ignoreFiles.v } + // create a list of absolute paths, these will be used for the file from + // this point in order to be able to use the roots for format reporting. + absArgs := make([]string, len(args)) + + for i, arg := range args { + if filepath.IsAbs(arg) { + absArgs[i] = arg + + continue + } + + absArgs[i], err = filepath.Abs(arg) + if err != nil { + return fmt.Errorf("failed to get absolute path for %s: %w", arg, err) + } + } + filtered, err := config.FilterIgnoredPaths(args, ignore, true, "") if err != nil { return fmt.Errorf("failed to filter ignored paths: %w", err) diff --git a/e2e/cli_test.go b/e2e/cli_test.go index 80bd0158..d6715e47 100644 --- a/e2e/cli_test.go +++ b/e2e/cli_test.go @@ -779,6 +779,7 @@ func TestFix(t *testing.T) { td := t.TempDir() initialState := map[string]string{ + ".regal/config.yaml": "", // needed to find the root in the right place "foo/main.rego": `package wow import rego.v1 @@ -821,13 +822,13 @@ test_allow { mustWriteToFile(t, filepath.Join(td, file), string(content)) } - err := regal(&stdout, &stderr)("fix", td) + err := regal(&stdout, &stderr)("fix", filepath.Join(td, "foo"), filepath.Join(td, "bar")) // 0 exit status is expected as all violations should have been fixed expectExitCode(t, err, 0, &stdout, &stderr) exp := fmt.Sprintf(`8 fixes applied: -In project root: %s +In project root: %[1]s bar/main.rego -> wow/foo-bar/baz/main.rego: - directory-package-mismatch diff --git a/pkg/config/config.go b/pkg/config/config.go index a2f409f1..d83968d4 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -675,22 +675,30 @@ func (rule *Rule) mapToConfig(result any) error { } func GetPotentialRoots(paths ...string) ([]string, error) { + var err error + dirMap := make(map[string]struct{}) - for _, path := range paths { - abs, err := filepath.Abs(path) - if err != nil { - return nil, fmt.Errorf("failed to get absolute path for %s: %w", path, err) + absDirPaths := make([]string, len(paths)) + + for i, path := range paths { + abs := path + + if !filepath.IsAbs(abs) { + abs, err = filepath.Abs(path) + if err != nil { + return nil, fmt.Errorf("failed to get absolute path for %s: %w", path, err) + } } if isDir(abs) { - dirMap[abs] = struct{}{} + absDirPaths[i] = abs } else { - dirMap[filepath.Dir(abs)] = struct{}{} + absDirPaths[i] = filepath.Dir(abs) } } - for _, dir := range util.Keys(dirMap) { + for _, dir := range absDirPaths { brds, err := FindBundleRootDirectories(dir) if err != nil { return nil, fmt.Errorf("failed to find bundle root directories in %s: %w", dir, err) @@ -701,7 +709,12 @@ func GetPotentialRoots(paths ...string) ([]string, error) { } } - return util.Keys(dirMap), nil + foundRoots := util.Keys(dirMap) + if len(foundRoots) == 0 { + return absDirPaths, nil + } + + return foundRoots, nil } func isDir(path string) bool {