a couple of fixes to prevent crashing if package has no files (e.g., go.mod not set properly)

This commit is contained in:
Randall C. O'Reilly 2021-08-31 21:30:13 -07:00
parent 5b7e6c3b07
commit 9b66a3f22d
2 changed files with 8 additions and 3 deletions

View File

@ -74,12 +74,12 @@ func gopyRunCmdBuild(cmdr *commander.Command, args []string) error {
return fmt.Errorf("gopy-gen: go build / load of package failed with path=%q: %v", path, err)
}
pkg, err := parsePackage(bpkg)
if cfg.Name == "" {
cfg.Name = pkg.Name()
}
if err != nil {
return err
}
if cfg.Name == "" {
cfg.Name = pkg.Name()
}
}
return runBuild("build", cfg)
}

5
gen.go
View File

@ -122,6 +122,11 @@ func loadPackage(path string, buildFirst bool) (*packages.Package, error) {
}
func parsePackage(bpkg *packages.Package) (*bind.Package, error) {
if len(bpkg.GoFiles) == 0 {
err := fmt.Errorf("gopy: no files in package %q", bpkg.PkgPath)
fmt.Println(err)
return nil, err
}
dir, _ := filepath.Split(bpkg.GoFiles[0])
p := bpkg.Types