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

import error resulting from /google/api #3500

Closed
tobby-s opened this issue Nov 29, 2024 · 3 comments
Closed

import error resulting from /google/api #3500

tobby-s opened this issue Nov 29, 2024 · 3 comments
Labels
Bug Something isn't working

Comments

@tobby-s
Copy link

tobby-s commented Nov 29, 2024

GitHub repository with your minimal reproducible example (do not leave this field blank or fill out this field with "github.com/bufbuild/buf" or we will automatically close your issue, see the instructions above!)

https://github.com/tobby-s/buf-import-error-example/

Commands

buf generate .

Output

no output, command runs without errors, but the import generated in generated/minimal-example/minimal/example/v1/ex.pb.go has an unusual import on line 10 that doesn't do anything, but prevents the code from building

Expected Output

golang code that can compile

Anything else?

the proto was written by someone other than me (i reduced it to a minimal example), i'm not experienced with using stuff like annotations and have no context on why it was used or whether it's written correctly. i just want to fix the generated code so i can import it and work on integrating with it.

it's entirely possible that there's an issue with how the imports are used, so if there is a fix that involves changing how the proto file is specified i'm open to it (i'd even be happier about it because i wouldn't have to wait for a bugfix+release).

notably, other generated code from other teams that use protoc directlly with the --go_out and --grpc-gateway_out flags don't have this erroneous import.

@tobby-s tobby-s added the Bug Something isn't working label Nov 29, 2024
@doriable
Copy link
Member

doriable commented Dec 2, 2024

Hello, so your problem is that your generated code attempts to import googleapis, and by default, buf generate does not generate code for imports.

You have two options to fix your build:

Option 1: The recommended method is to exclude googleapis from your managed mode options with the disable key. I added that clause there:

# buf.gen.yaml
version: v2
managed:
  enabled: true
  override:
    - file_option: go_package_prefix
      value: github.com/tobby-s/minimal-example
  disable:
    - module: buf.build/googleapis/googleapis
      file_option: go_package_prefix
plugins:
  - local: protoc-gen-go
    out: generated/minimal-example
    opt:
      - paths=source_relative
  - local: protoc-gen-go-grpc
    out: generated/minimal-example
    opt:
      - paths=source_relative
inputs:
  - directory: .

What this is saying is that the googleapis module in your dependencies is excluded from managed mode's go package options, and your generated code will result in an import statement that looks like:

import (
        _ "google.golang.org/genproto/googleapis/api/annotations"

This uses the publicly available googleapis package (https://pkg.go.dev/google.golang.org/genproto/googleapis) that you can then use the go toolchain to manage, etc.

Option 2: If you want to generate the code for googleapis, you can use the --include-imports flag with buf geneate. In this case, you do not want to exclude it from your buf.gen.yaml. This will generate code for googleapis in your generated code:

$ tree generated/minimal-example/
generated/minimal-example/
├── google
│   └── api
│       ├── annotations.pb.go
│       └── http.pb.go
└── minimal
    └── example
        └── v1
            ├── ex.pb.go
            └── ex_grpc.pb.go

And an import statement that now makes sense:

import (
        _ "github.com/tobby-s/minimal-example/generated/minimal-example/google/api"

However, it is recommended that you use the managed package for googleapis.

Also, something to note about your buf.gen.yaml: you set go_package_prefix to github.com/tobby-s/minimal-example, which is the name of your go module. This should be the <go module>/<relative path> to where you are generating your source code, which in this case is github.com/tobby-s/minimal-example/generated/minimal-example.

@tobby-s
Copy link
Author

tobby-s commented Dec 3, 2024

thanks a lot! i tried adding an override: file_option: go_package_prefix; value: google.golang.org/genproto/googleapis/api; path: google earlier and was wondering why that wasn't working. now i see it's because the googleapis imports get overridden by default. i was wondering why the other google.golang.org/grpc imports weren't affected by this, but i roughly get the idea of what's going on now.

@doriable
Copy link
Member

doriable commented Dec 3, 2024

Nice, I'm glad it worked out :)

@doriable doriable closed this as completed Dec 3, 2024
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