Skip to content

Commit

Permalink
Build package paths correctly on Windows
Browse files Browse the repository at this point in the history
Addresses vektra#722
  • Loading branch information
kbolino committed Oct 10, 2023
1 parent 9d79f3a commit 8c67080
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"errors"
"fmt"
"os"
"path"
"reflect"
"regexp"
"strings"
Expand Down Expand Up @@ -469,8 +470,6 @@ func isAutoGenerated(path *pathlib.Path) (bool, error) {
func (c *Config) subPackages(
ctx context.Context,
pkgPath string,
pkgConfig *Config,
currentDepth int,
) ([]string, error) {
log := zerolog.Ctx(ctx)

Expand All @@ -482,7 +481,7 @@ func (c *Config) subPackages(
}
pkg := pkgs[0]

if currentDepth == 0 && len(pkg.GoFiles) == 0 {
if len(pkg.GoFiles) == 0 {
log.Error().
Err(ErrNoGoFilesFoundInRoot).
Str("documentation", logging.DocsURL("/notes/#error-no-go-files-found-in-root-search-path")).
Expand All @@ -493,7 +492,7 @@ func (c *Config) subPackages(
searchRoot := representativeFile.Parent()
packageRootName := pathlib.NewPath(pkg.PkgPath)
packageRootPath := searchRoot
subPackages := []string{}
var subPackages []string

walker, err := pathlib.NewWalk(
searchRoot,
Expand All @@ -507,7 +506,7 @@ func (c *Config) subPackages(
}

visitedDirs := map[string]any{}
subdirectoriesWithGoFiles := []*pathlib.Path{}
var subdirectoriesWithGoFiles []*pathlib.Path

// We consider the searchRoot to already be visited because
// we know it's already in the configuration.
Expand Down Expand Up @@ -555,8 +554,13 @@ func (c *Config) subPackages(
log.Err(err).Stringer("root", packageRootPath).Stringer("subRoot", d).Msg("failed to make subroot relative to root")
return nil, fmt.Errorf("failed to make subroot relative to root: %w", err)
}
absolutePackageName := packageRootName.Join(relativeFilesystemPath.Parts()...)
subPackages = append(subPackages, absolutePackageName.String())
partsToJoin := []string{packageRootName.String()}
partsToJoin = append(partsToJoin, relativeFilesystemPath.Parts()...)
// we use path.Join here because the package path is always /-separated
// regardless of os.PathSeparator (i.e. even on Windows)
absolutePackageName := path.Join(partsToJoin...)

subPackages = append(subPackages, absolutePackageName)
}

return subPackages, nil
Expand Down Expand Up @@ -587,7 +591,7 @@ func (c *Config) discoverRecursivePackages(ctx context.Context) error {
pkgLog := log.With().Str("package-path", pkgPath).Logger()
pkgCtx := pkgLog.WithContext(ctx)
pkgLog.Debug().Msg("discovering sub-packages")
subPkgs, err := c.subPackages(pkgCtx, pkgPath, conf, 0)
subPkgs, err := c.subPackages(pkgCtx, pkgPath)
if err != nil {
return fmt.Errorf("failed to get subpackages: %w", err)
}
Expand Down

0 comments on commit 8c67080

Please sign in to comment.