Skip to content

Commit

Permalink
fix: file paths on windows conflict with the ast escape rune so we ne…
Browse files Browse the repository at this point in the history
…ed to keep paths in the linux style
  • Loading branch information
Dj Gilcrease committed Dec 8, 2020
1 parent b27e0ce commit f1f91b7
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
18 changes: 15 additions & 3 deletions glob.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ import (
"github.com/spf13/afero"
)

const (
runeSeparator = '/'
stringSeparator = string(runeSeparator)
)

// FileSystem is meant to be used with WithFs.
type FileSystem afero.Fs

Expand Down Expand Up @@ -48,10 +53,14 @@ func QuoteMeta(pattern string) string {
return glob.QuoteMeta(pattern)
}

func CleanPath(path string) string {
return filepath.ToSlash(filepath.Clean(path))
}

// Glob returns all files that match the given pattern in the current directory.
func Glob(pattern string, opts ...OptFunc) ([]string, error) {
return doGlob(
strings.TrimPrefix(pattern, "./"),
strings.TrimPrefix(pattern, "." + stringSeparator),
compileOptions(opts),
)
}
Expand All @@ -60,7 +69,7 @@ func doGlob(pattern string, options *globOptions) ([]string, error) { // nolint:
var fs = options.fs
var matches []string

matcher, err := glob.Compile(pattern, filepath.Separator)
matcher, err := glob.Compile(pattern, runeSeparator)
if err != nil {
return matches, fmt.Errorf("compile glob pattern: %w", err)
}
Expand All @@ -76,7 +85,7 @@ func doGlob(pattern string, options *globOptions) ([]string, error) { // nolint:
// glob contains no dynamic matchers so prefix is the file name that
// the glob references directly. When the glob explicitly references
// a single non-existing file, return an error for the user to check.
return []string{}, fmt.Errorf("matching %q: %w", prefix, os.ErrNotExist)
return []string{}, fmt.Errorf(`matching "%s": %w`, prefix, os.ErrNotExist)
}

return []string{}, nil
Expand All @@ -100,6 +109,8 @@ func doGlob(pattern string, options *globOptions) ([]string, error) { // nolint:
return err
}

// The glob ast from github.com/gobwas/glob only works properly with linux paths
path = CleanPath(path)
if !matcher.Match(path) {
return nil
}
Expand Down Expand Up @@ -149,6 +160,7 @@ func filesInDirectory(fs afero.Fs, dir string) ([]string, error) {
if info.IsDir() {
return nil
}
path = CleanPath(path)
files = append(files, path)
return nil
})
Expand Down
9 changes: 5 additions & 4 deletions prefix.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,11 @@ func staticText(node *ast.Node) (text string, ok bool) {
// staticPrefix returns the file path inside the pattern up
// to the first path element that contains a wildcard.
func staticPrefix(pattern string) (string, error) {
parts := strings.Split(pattern, string(filepath.Separator))
parts := strings.Split(pattern, stringSeparator)

prefix := ""
if len(pattern) > 0 && rune(pattern[0]) == filepath.Separator {
prefix = string(filepath.Separator)
if len(pattern) > 0 && rune(pattern[0]) == runeSeparator {
prefix = stringSeparator
}

for _, part := range parts {
Expand All @@ -81,7 +81,8 @@ func staticPrefix(pattern string) (string, error) {
break
}

prefix = filepath.Join(prefix, staticPart)
// The glob ast from github.com/gobwas/glob only works properly with linux paths
prefix = filepath.ToSlash(filepath.Join(prefix, staticPart))
}

if prefix == "" {
Expand Down

0 comments on commit f1f91b7

Please sign in to comment.