easyjson icon indicating copy to clipboard operation
easyjson copied to clipboard

easyjson requires GOPATH ended with "src"

Open ksimka opened this issue 4 years ago • 1 comments

Here https://github.com/mailru/easyjson/blob/c120ca7ced6051261161ce15e8f1542a4b2567fc/parser/pkgpath.go#L122 we have getPkgPathFromGOPATH function

func getPkgPathFromGOPATH(fname string, isDir bool) (string, error) {
	gopath := os.Getenv("GOPATH")
	if gopath == "" {
		gopath = build.Default.GOPATH
	}

	for _, p := range strings.Split(gopath, string(filepath.ListSeparator)) {
		prefix := filepath.Join(p, "src") + string(filepath.Separator)
		rel, err := filepath.Rel(prefix, fname)
		if err == nil && !strings.HasPrefix(rel, ".."+string(filepath.Separator)) {
			if !isDir {
				return path.Dir(filePathToPackagePath(rel)), nil
			} else {
				return path.Clean(filePathToPackagePath(rel)), nil
			}
		}
	}

	return "", fmt.Errorf("file '%v' is not in GOPATH '%v'", fname, gopath)
}

As you can see, it searches for path that ends with "src". So I have one question and one notice:

  • is it kind of golang agreement that I'm not aware of that GOPATH must end with "src"?
  • if GOPATH doesn't have "src" easyjson returns an error that looks like "file '/my/go/path/some_file.go' is not in GOPATH '/my/go/path'" which is very confusing

I personally don't use GOPATH for a long time (since modules become a thing), but I faced with this issues when tried to run easyjson in some CI on autogenerated files (yes, I do easyjson autogeneration after another autogeneration).

ksimka avatar Nov 01 '21 16:11 ksimka

I am suffering because of this as well while trying to package nextcloud-spreed-signaling; which uses easyjson to generate some files.

It would be good if this got improved.

renich avatar Apr 29 '24 17:04 renich