Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

running generate with package_bindings causes an error (@main) #220

Closed
penina-nyt opened this issue Aug 15, 2022 · 5 comments
Closed

running generate with package_bindings causes an error (@main) #220

penina-nyt opened this issue Aug 15, 2022 · 5 comments
Labels
bug Something isn't working

Comments

@penina-nyt
Copy link

Describe the bug
received the following error trying to generate code

genqlient internal error: failed to gofmt code:
	16:21: expected ';', found '-' (and 5 more errors)
	16:21: expected ';', found '-' (and 5 more errors)
	16:21: expected ';', found '-' (and 5 more errors)
	16:21: expected ';', found '-' (and 5 more errors)
	16:21: expected ';', found '-' (and 5 more errors)
	16:21: expected ';', found '-' (and 5 more errors)---source code---

To Reproduce
in genqlient.yaml, specify the path to your models.

package_bindings:
- package: path/to/models.go

run go run github.com/Khan/genqlient@main with a defined query file.

Expected behavior
Should not fail and should successfully generate the file reusing the project's existing models

genqlient version
github.com/Khan/genqlient@main

Additional context
Here's a snipped from what was printed on the command line:

   1 | // Code generated by github.com/Khan/genqlient, DO NOT EDIT.
   2 | 
   3 | package client
   4 | 
   5 | import (
   6 | 	"command-line-arguments"
   7 | 	"context"
   8 | 	"github.com/Khan/genqlient/graphql"
   9 | )
  10 | 
  11 | 
  12 | 
  13 | 
  14 | // Type is returned by Query on success.
  15 | type Type struct {
  16 | 	Field command-line-arguments.Field `json:"field"`
> 17 | }
@penina-nyt penina-nyt added the bug Something isn't working label Aug 15, 2022
@benjaminjkraft
Copy link
Collaborator

Weird! Something seems to think that your models are in a package named command-line-arguments. Does that string by chance appear anywhere in your genqlient config or models file? (It doesn't seem to appear in the genqlient source.) If not, are you able to post a bit more of your config or ideally a sample config and models file that reproduces the issue?

@penina-nyt
Copy link
Author

@benjaminjkraft it doesn't appear anywhere in the source code, but i was able to find a reference to that string in a vendor file - it seems to be coming from go itself

https://github.com/golang/tools/blob/master/go/packages/golist.go#L344-L361

here's my .yaml file if that's helpful

# Default genqlient config; for full documentation see:
# https://github.com/Khan/genqlient/blob/main/docs/genqlient.yaml
schema:
- dir/schema/**/*.graphql
- dir/schema/*.graphql
operations:
- pkg/client/queries/query_name.graphql
generated: pkg/client/generated/generated.go
package: client
package_bindings:
- package: path/to/model.go

@benjaminjkraft
Copy link
Collaborator

Whoa, weird, nice find!

It took me a moment to notice but I think the problem here is that the value for package: is supposed to be a package-path, not a local path to a file. (Then the go/packages API is I guess assuming you want to compile that file as a standalone package and that somehow works but results in a bogus package-path.)

That is, it should be:

package_bindings:
- package: github.com/yourorg/yourrepo/path/to

Does it work if you change it like that? (And if that's the issue, is there somewhere in the genqlient documentation you got that from, or that we could be clearer where you would have seen it? We should definitely look for this and give a better error, too!)

@penina-nyt
Copy link
Author

fixed with using the correct package specification! thanks so much - i had initially specified the path to the file rather than the package itself within the github path, which led to the models file being regenerated, which is what led me to switch to using the local file path.

thanks so much for your help!

benjaminjkraft added a commit that referenced this issue Aug 15, 2022
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
@benjaminjkraft
Copy link
Collaborator

Great! #221 should give a better error here for the future. Let us know if you run into any other problems with package_bindings as it's brand new!

benjaminjkraft added a commit that referenced this issue Aug 15, 2022
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants