Give a better error if package_bindings is a file (#221)

The entries under the new `package_bindings` field should be packages,
but it's an easy mistake to put a file path instead (most of the other
fields in `genqlient.yaml` are files). Due to some bizzare behavior from
`go/packages` (described in #220), if you do that you get weird broken
code that gives you no clue what is wrong. Instead, let's guess if what
you gave us looks like a filename, and report a nice error if so.

Test plan: crossed fingers
This commit is contained in:
Ben Kraft
2022-08-15 15:08:15 -07:00
committed by GitHub
parent f600b6e5d3
commit 16a17f2eef
+9
View File
@@ -6,6 +6,7 @@ import (
"go/token"
"os"
"path/filepath"
"strings"
"golang.org/x/tools/go/packages"
"gopkg.in/yaml.v2"
@@ -114,6 +115,14 @@ func (c *Config) ValidateAndFillDefaults(baseDir string) error {
if len(c.PackageBindings) > 0 {
for _, binding := range c.PackageBindings {
if strings.HasSuffix(binding.Package, ".go") {
// total heuristic -- but this is an easy mistake to make and
// results in rather bizarre behavior from go/packages.
return errorf(nil,
"package %v looks like a file, but should be a package-name",
binding.Package)
}
mode := packages.NeedImports | packages.NeedTypes | packages.NeedTypesSizes
pkgs, err := packages.Load(&packages.Config{
Mode: mode,