Skip to content

Commit

Permalink
cmd/gobind: do not compile package just to find package dir
Browse files Browse the repository at this point in the history
Gobind utilizes golang.org/x/tools/go/packages.Load to find
the directory of a package. Configure the load configuration
to just find the list of files. Zero load mode is equivalent
to combining NeedName+NeedFiles+NeedCompiledGoFiles bits.
That is unnecessary, and can increase the chance of load
failures. For example, load with the zero load mode may fail
if all the necessary cgo dependencies aren't available in the
system, but that shouldn't be critical for gobind's use case.

Updates golang/go#56292

Change-Id: Ifaf4f43e9053cf4a43fd657a9a394fc13f611576
Reviewed-on: https://go-review.googlesource.com/c/mobile/+/443935
Reviewed-by: David Chase <drchase@google.com>
Reviewed-by: Bryan Mills <bcmills@google.com>
Reviewed-by: Hajime Hoshi <hajimehoshi@gmail.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Run-TryBot: Hyang-Ah Hana Kim <hyangah@gmail.com>
  • Loading branch information
hyangah authored and hajimehoshi committed Oct 19, 2022
1 parent 51f526d commit 406ed3a
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion cmd/gobind/gen.go
Expand Up @@ -376,10 +376,14 @@ func defaultFileName(lang string, pkg *types.Package) string {
}

func packageDir(path string) (string, error) {
pkgs, err := packages.Load(nil, path)
mode := packages.NeedFiles
pkgs, err := packages.Load(&packages.Config{Mode: mode}, path)
if err != nil {
return "", err
}
if len(pkgs) == 0 || len(pkgs[0].GoFiles) == 0 {
return "", fmt.Errorf("no Go package in %v", path)
}
pkg := pkgs[0]
if len(pkg.Errors) > 0 {
return "", fmt.Errorf("%v", pkg.Errors)
Expand Down

0 comments on commit 406ed3a

Please sign in to comment.