Skip to content

Commit

Permalink
Merge pull request #761 from LandonTClipp/file_ordering_gomod
Browse files Browse the repository at this point in the history
Factor out logic to exclude sub-modules
  • Loading branch information
LandonTClipp committed Feb 16, 2024
2 parents 92afb14 + 8d53849 commit f0789fb
Showing 1 changed file with 27 additions and 8 deletions.
35 changes: 27 additions & 8 deletions pkg/config/config.go
Expand Up @@ -528,6 +528,21 @@ func isAutoGenerated(path *pathlib.Path) (bool, error) {
return false, nil
}

func shouldExcludeModule(ctx context.Context, root *pathlib.Path, goModPath *pathlib.Path) (bool, error) {
log := zerolog.Ctx(ctx)
relative, err := goModPath.RelativeTo(root)
if err != nil {
return false, stackerr.NewStackErrf(err, "determining distance from search root")
}

if len(relative.Parts()) != 1 {
log.Debug().Msg("skipping sub-module")
return true, nil
}
log.Debug().Int("parts_len", len(relative.Parts())).Str("parts", fmt.Sprintf("%v", relative.Parts())).Msg("not skipping module as this is the root path")
return false, nil
}

func (c *Config) subPackages(
ctx context.Context,
pkgPath string,
Expand Down Expand Up @@ -587,16 +602,14 @@ func (c *Config) subPackages(
pathLog.Debug().Msg("path contains go.mod file")
// Check if our current depth is 0. We do this to skip sub-modules, but not
// the root module.
relative, err := path.RelativeTo(searchRoot)
shouldExclude, err := shouldExcludeModule(ctx, searchRoot, path)
if err != nil {
return stackerr.NewStackErrf(err, "determining distance from search root")
return err
}

if len(relative.Parts()) != 1 {
pathLog.Debug().Msg("skipping sub-module")
if shouldExclude {
return pathlib.ErrWalkSkipSubtree
}
pathLog.Debug().Int("parts_len", len(relative.Parts())).Str("parts", fmt.Sprintf("%v", relative.Parts())).Msg("not skipping module as this is the root path")
}

_, haveVisitedDir := visitedDirs[path.Parent().String()]
Expand All @@ -615,14 +628,20 @@ func (c *Config) subPackages(
}

pathLog.Debug().Msg("subdirectory has a .go file")
goModExists, err := path.Parent().Join("go.mod").Exists()
goModPath := path.Parent().Join("go.mod")
goModExists, err := goModPath.Exists()
if err != nil {
pathLog.Err(err).Msg("failed to determine if go.mod exists")
return err
}
if goModExists {
pathLog.Debug().Msg("found .go file, but go.mod exists. Skipping.")
return pathlib.ErrWalkSkipSubtree
shouldExclude, err := shouldExcludeModule(ctx, searchRoot, goModPath)
if err != nil {
return err
}
if shouldExclude {
return pathlib.ErrWalkSkipSubtree
}
}
subdirectoriesWithGoFiles = append(subdirectoriesWithGoFiles, path.Parent())
visitedDirs[path.Parent().String()] = nil
Expand Down

0 comments on commit f0789fb

Please sign in to comment.