Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle default gopath $HOME/go when env var $GOPATH is not set. #45

Merged
merged 1 commit into from
Jul 30, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion gocomplete/pkgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"go/build"
"io/ioutil"
"os"
"os/user"
"path/filepath"
"strings"

Expand Down Expand Up @@ -71,7 +72,7 @@ func listPackages(dir string) (directories []string) {

func systemDirs(dir string) (directories []string) {
// get all paths from GOPATH environment variable and use their src directory
paths := strings.Split(os.Getenv("GOPATH"), ":")
paths := findGopath()
for i := range paths {
paths[i] = filepath.Join(paths[i], "src")
}
Expand Down Expand Up @@ -106,3 +107,20 @@ func systemDirs(dir string) (directories []string) {
}
return
}

func findGopath() []string {
gopath := os.Getenv("GOPATH")
if gopath == "" {
// By convention
// See rationale at https://github.com/golang/go/issues/17262
usr, err := user.Current()
if err != nil {
return nil
}
usrgo := filepath.Join(usr.HomeDir, "go")
return []string{usrgo}
}
listsep := string([]byte{os.PathListSeparator})
entries := strings.Split(gopath, listsep)
return entries
}