Skip to content

Commit

Permalink
expand: work around incorrect globbing error
Browse files Browse the repository at this point in the history
v3, currently in the master.v3 branch, doesn't have this bug as the
globbing code that calls ReadDir was refactored. This older code
performs two ReadDir calls, and one of them is run even if the current
potential glob path is not a dir.

This can be a problem if one performs a glob like "*/foo" and the file
"bar" exists; we should skip it, instead of erroring because it's not a
directory.

Fix that in a simple way in v2, and add a regression test.

Fixes #347.
  • Loading branch information
mvdan committed Jan 15, 2019
1 parent a2465f3 commit 932bd44
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
9 changes: 8 additions & 1 deletion expand/expand.go
Original file line number Diff line number Diff line change
Expand Up @@ -709,7 +709,14 @@ func (cfg *Config) globDir(base, dir string, rx *regexp.Regexp, matches []string
}
infos, err := cfg.ReadDir(filepath.Join(base, dir))
if err != nil {
return nil, err
// Ignore the error, as this might be a file instead of a
// directory. v3 refactored globbing to only use one ReadDir
// call per directory instead of two, so it knows to skip this
// kind of path at the ReadDir call of its parent.
// Instead of backporting that complex rewrite into v2, just
// work around the edge case here. We might ignore other kinds
// of errors, but at least we don't fail on a correct glob.
return matches, nil
}
for _, info := range infos {
name := info.Name()
Expand Down
4 changes: 4 additions & 0 deletions interp/interp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2087,6 +2087,10 @@ set +o pipefail
"mkdir x-d1 x-d2; touch x-f; echo ././x-*/// | sed -e 's@\\\\@/@g'",
"././x-d1/ ././x-d2/\n",
},
{
"mkdir -p x-d1/a x-d2/b; touch x-f; echo x-*/* | sed -e 's@\\\\@/@g'",
"x-d1/a x-d2/b\n",
},
{
"mkdir x-d; touch x-f; test -d $PWD/x-*/",
"",
Expand Down

0 comments on commit 932bd44

Please sign in to comment.