Skip to content

Commit 16a17f2

Browse files
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
1 parent f600b6e commit 16a17f2

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

generate/config.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"go/token"
77
"os"
88
"path/filepath"
9+
"strings"
910

1011
"golang.org/x/tools/go/packages"
1112
"gopkg.in/yaml.v2"
@@ -114,6 +115,14 @@ func (c *Config) ValidateAndFillDefaults(baseDir string) error {
114115

115116
if len(c.PackageBindings) > 0 {
116117
for _, binding := range c.PackageBindings {
118+
if strings.HasSuffix(binding.Package, ".go") {
119+
// total heuristic -- but this is an easy mistake to make and
120+
// results in rather bizarre behavior from go/packages.
121+
return errorf(nil,
122+
"package %v looks like a file, but should be a package-name",
123+
binding.Package)
124+
}
125+
117126
mode := packages.NeedImports | packages.NeedTypes | packages.NeedTypesSizes
118127
pkgs, err := packages.Load(&packages.Config{
119128
Mode: mode,

0 commit comments

Comments
 (0)