Skip to content

Commit

Permalink
Allow fixing directory structure not matching package paths
Browse files Browse the repository at this point in the history
Following the recent addition of the `directory-package-mismatch` rule,
this PR brings a corresponding `regal fix` remediator as well as a code
action to fix this directly in the editor.

Fixes StyraInc#1025

Signed-off-by: Anders Eknert <anders@styra.com>
  • Loading branch information
anderseknert authored and charlieegan3 committed Sep 3, 2024
1 parent 5900a73 commit c5d8fcb
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 0 deletions.
5 changes: 5 additions & 0 deletions pkg/fixer/fixes/directorypackagemismatch.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ func (d *DirectoryPackageMismatch) Fix(fc *FixCandidate, opts *RuntimeOptions) (

rootPath = filepath.Clean(rootPath)

// We must ensure that any file provided resides under the root path!
if _, err = filepath.Rel(rootPath, fc.Filename); err != nil {
return nil, fmt.Errorf("path not relative to root path: %w", err)
}

newPath := filepath.Join(rootPath, pkgPath, filepath.Base(fc.Filename))

if newPath == fc.Filename {
Expand Down
1 change: 1 addition & 0 deletions pkg/fixer/fixes/fixes.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ type Fix interface {
type RuntimeOptions struct {
// BaseDir is the base directory for the files being fixed. This is often the same as the
// workspace root directory, but not necessarily.
// TODO: use a slice and allow more than one?
BaseDir string
Config *config.Config
Client clients.Identifier
Expand Down
1 change: 1 addition & 0 deletions tmp/unrelated.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
foo

0 comments on commit c5d8fcb

Please sign in to comment.