Skip to content

Commit

Permalink
Give a better error if package_bindings is a file (#221)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
benjaminjkraft authored Aug 15, 2022
1 parent f600b6e commit 16a17f2
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions generate/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"go/token"
"os"
"path/filepath"
"strings"

"golang.org/x/tools/go/packages"
"gopkg.in/yaml.v2"
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 16a17f2

Please sign in to comment.