Skip to content

Commit

Permalink
Fix relative path finding (gobuffalo/buffalo#982) (#12)
Browse files Browse the repository at this point in the history
strings.TrimPrefix can give the wrong result with case-insensitive
paths. filepath.Rel usually works better.
  • Loading branch information
egonelbre authored and markbates committed Mar 25, 2018
1 parent 1da02db commit ef60bfc
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions envy.go
Expand Up @@ -184,11 +184,24 @@ func GoPaths() []string {
return strings.Split(gp, ":")
}

func importPath(path string) string {
for _, gopath := range GoPaths() {
srcpath := filepath.Join(gopath, "src")
rel, err := filepath.Rel(srcpath, path)
if err == nil {
return filepath.ToSlash(rel)
}
}

// fallback to trim
rel := strings.TrimPrefix(path, filepath.Join(GoPath(), "src"))
rel = strings.TrimPrefix(rel, string(filepath.Separator))
return filepath.ToSlash(rel)
}

func CurrentPackage() string {
pwd, _ := os.Getwd()
pwd = strings.TrimPrefix(pwd, filepath.Join(GoPath(), "src"))
pwd = strings.TrimPrefix(pwd, string(filepath.Separator))
return filepath.ToSlash(pwd)
return importPath(pwd)
}

func Environ() []string {
Expand Down

0 comments on commit ef60bfc

Please sign in to comment.