Skip to content

Commit

Permalink
fix: correct handling of ./ scan target prefix
Browse files Browse the repository at this point in the history
When the scan target is specified with a `./` prefix, we currently
incorrectly build relative file paths within the project directory.

Also ensure that we correctly handle the cases where a trailing
slash is either present or absent, on a directory path specified
as scan target.
  • Loading branch information
spdawson committed Nov 1, 2022
1 parent 3931e8d commit d925a35
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions pkg/commands/process/balancer/filelist/filelist.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package filelist

import (
"io/fs"
"os"
"path/filepath"
"strings"

Expand All @@ -16,6 +17,19 @@ import (
func Discover(projectPath string, config settings.Config) ([]work.File, error) {
var files []work.File

haveDir, statErr := isDir(projectPath)
if statErr != nil {
return files, statErr
}

if haveDir {
projectPath = strings.TrimPrefix(projectPath, "./")
projectPath = strings.TrimSuffix(projectPath, "/")
if projectPath != "." {
projectPath += "/"
}
}

ignore := fileignore.New(projectPath, config)

err := filepath.WalkDir(projectPath, func(filePath string, d fs.DirEntry, err error) error {
Expand All @@ -31,6 +45,7 @@ func Discover(projectPath string, config settings.Config) ([]work.File, error) {
}

relativePath := strings.TrimPrefix(filePath, projectPath)
relativePath = "/" + relativePath

if ignore.Ignore(projectPath, filePath, d) {
log.Debug().Msgf("skipping file due to file skip rules: %s", relativePath)
Expand All @@ -50,3 +65,12 @@ func Discover(projectPath string, config settings.Config) ([]work.File, error) {

return files, err
}

func isDir(path string) (bool, error) {
fileInfo, err := os.Stat(path)
if err != nil {
return false, err
}

return fileInfo.IsDir(), nil
}

0 comments on commit d925a35

Please sign in to comment.