google / gnostic

A compiler for APIs described by the OpenAPI Specification with plugins for code generation and other API support tasks.
Apache License 2.0
2.08k stars 244 forks source link

Compile error #235

Open herugen opened 3 years ago

herugen commented 3 years ago

Environment: go version go1.13 linux/amd64 libprotoc 3.14.0

Here follows my operation step:

  1. git clone https://github.com/googleapis/gnostic
  2. cd gnostic
  3. make

And then errors happened:

# github.com/googleapis/gnostic/metrics/vocabulary
metrics/vocabulary/vocabulary.go:71:29: cannot use v (type *gnostic_metrics_v1.Vocabulary) as type protoreflect.ProtoMessage in argument to "google.golang.org/protobuf/proto".Marshal:
        *gnostic_metrics_v1.Vocabulary does not implement protoreflect.ProtoMessage (missing ProtoReflect method)
metrics/vocabulary/vocabulary.go:86:29: cannot use v (type *gnostic_metrics_v1.VocabularyList) as type protoreflect.ProtoMessage in argument to "google.golang.org/protobuf/proto".Marshal:
        *gnostic_metrics_v1.VocabularyList does not implement protoreflect.ProtoMessage (missing ProtoReflect method)
metrics/vocabulary/vocabulary.go:101:29: cannot use v (type *gnostic_metrics_v1.VersionHistory) as type protoreflect.ProtoMessage in argument to "google.golang.org/protobuf/proto".Marshal:
        *gnostic_metrics_v1.VersionHistory does not implement protoreflect.ProtoMessage (missing ProtoReflect method)
metrics/vocabulary/vocabulary.go:212:23: cannot use v (type *gnostic_metrics_v1.Vocabulary) as type protoreflect.ProtoMessage in argument to "google.golang.org/protobuf/proto".Unmarshal:
        *gnostic_metrics_v1.Vocabulary does not implement protoreflect.ProtoMessage (missing ProtoReflect method)
make: *** [all] Error 2 
timburks commented 3 years ago

I can't reproduce this running with libprotoc 3.14.0 and go version go1.15.6 linux/amd64.

Valavanca commented 3 years ago

I have the same problem. libprotoc 3.14.0 go version go1.15.8 linux/amd64

# github.com/googleapis/gnostic/metrics/vocabulary
metrics/vocabulary/vocabulary.go:71:29: cannot use v (type *gnostic_metrics_v1.Vocabulary) as type protoreflect.ProtoMessage in argument to "google.golang.org/protobuf/proto".Marshal:
    *gnostic_metrics_v1.Vocabulary does not implement protoreflect.ProtoMessage (missing ProtoReflect method)
metrics/vocabulary/vocabulary.go:86:29: cannot use v (type *gnostic_metrics_v1.VocabularyList) as type protoreflect.ProtoMessage in argument to "google.golang.org/protobuf/proto".Marshal:
    *gnostic_metrics_v1.VocabularyList does not implement protoreflect.ProtoMessage (missing ProtoReflect method)
metrics/vocabulary/vocabulary.go:101:29: cannot use v (type *gnostic_metrics_v1.VersionHistory) as type protoreflect.ProtoMessage in argument to "google.golang.org/protobuf/proto".Marshal:
    *gnostic_metrics_v1.VersionHistory does not implement protoreflect.ProtoMessage (missing ProtoReflect method)
metrics/vocabulary/vocabulary.go:212:23: cannot use v (type *gnostic_metrics_v1.Vocabulary) as type protoreflect.ProtoMessage in argument to "google.golang.org/protobuf/proto".Unmarshal:
    *gnostic_metrics_v1.Vocabulary does not implement protoreflect.ProtoMessage (missing ProtoReflect method)
make: *** [Makefile:4: all] Error 2
amit-chandak-unskript commented 3 years ago

I see the same issue with

go version go1.16 darwin/amd64
libprotoc 3.15.3
qas commented 3 years ago

I am also seeing a compile error:

libprotoc 3.6.1 (I also tried libprotoc 3.15.5) go version go1.16.2 linux/amd64

root@56cc44da8415:/go/src/github.com/google/gnostic# make
go generate ./...
go get: upgraded github.com/golang/protobuf v1.4.3 => v1.5.1
go get: upgraded google.golang.org/protobuf v1.24.0 => v1.26.0
protoc-gen-go: invalid Go import path "openapiv2" for "openapiv2/OpenAPIv2.proto"

The import path must contain at least one forward slash ('/') character.

See https://developers.google.com/protocol-buffers/docs/reference/go-generated#package for more information.

--go_out: protoc-gen-go: Plugin failed with status code 1.
protoc-gen-go: invalid Go import path "openapiv3" for "openapiv3/OpenAPIv3.proto"

The import path must contain at least one forward slash ('/') character.

See https://developers.google.com/protocol-buffers/docs/reference/go-generated#package for more information.

--go_out: protoc-gen-go: Plugin failed with status code 1.
protoc-gen-go: invalid Go import path "discovery" for "discovery/discovery.proto"

The import path must contain at least one forward slash ('/') character.

See https://developers.google.com/protocol-buffers/docs/reference/go-generated#package for more information.

--go_out: protoc-gen-go: Plugin failed with status code 1.
protoc-gen-go: invalid Go import path "plugins" for "plugins/plugin.proto"

The import path must contain at least one forward slash ('/') character.

See https://developers.google.com/protocol-buffers/docs/reference/go-generated#package for more information.

--go_out: protoc-gen-go: Plugin failed with status code 1.
protoc-gen-go: invalid Go import path "extensions" for "extensions/extension.proto"

The import path must contain at least one forward slash ('/') character.

See https://developers.google.com/protocol-buffers/docs/reference/go-generated#package for more information.

--go_out: protoc-gen-go: Plugin failed with status code 1.
protoc-gen-go: invalid Go import path "surface" for "surface/surface.proto"

The import path must contain at least one forward slash ('/') character.

See https://developers.google.com/protocol-buffers/docs/reference/go-generated#package for more information.

--go_out: protoc-gen-go: Plugin failed with status code 1.
protoc-gen-go: invalid Go import path "metrics" for "metrics/vocabulary.proto"

The import path must contain at least one forward slash ('/') character.

See https://developers.google.com/protocol-buffers/docs/reference/go-generated#package for more information.

--go_out: protoc-gen-go: Plugin failed with status code 1.
protoc-gen-go: invalid Go import path "metrics" for "metrics/complexity.proto"

The import path must contain at least one forward slash ('/') character.

See https://developers.google.com/protocol-buffers/docs/reference/go-generated#package for more information.

--go_out: protoc-gen-go: Plugin failed with status code 1.
gnostic.go:15: running "./COMPILE-PROTOS.sh": exit status 1
make: *** [Makefile:3: all] Error 1
root@56cc44da8415:/go/src/github.com/google/gnostic# 
adarah commented 3 years ago

I'm having the same issue as the guy above me. After digging around a bit, it seems to be caused by this change: https://github.com/protocolbuffers/protobuf-go/commit/8b366e880709d9be636fad37fdab80990e7effc9#diff-399aacc23da934164976b6fb41cbb5cff5df743f45210d007800504022127b86

Should I open a separate issue?

sbooeshaghi commented 3 years ago

After running make I also got the same error as @qas

Environment go version go1.16.3 darwin/amd64 libprotoc 3.15.8

sbooeshaghi commented 3 years ago

Ok I think I figured out the problem, thanks to @Adarah for pointing me to the commit.

When protogen.go is run, it executes impPath, pkgName := splitImportPathAndPackageName(value) which splits the go_package variable in the .proto file. The problem is that the Go import path is expected to contain a forward slash. To fix this, every instance of go_package should be modified in the following way

// The Go package name.
option go_package = "openapiv3;openapi_v3";

to

// The Go package name.
option go_package = "openapiv3/;openapi_v3";

for the following files

After making these changes and running make, I got that there was a similar error for gnostic/extensions/sample/generated/gnostic-x-sampleone/proto/x-sampleone.proto

So I made the following change in that file from

// The Go package path.
option go_package = ".;sampleone";

to

// The Go package path.
option go_package = "./;sampleone";

Then when I did that and ran make again I got the following error:

go generate ./...
go get ./...
# github.com/googleapis/gnostic/extensions/sample/generated/gnostic-x-sampleone/proto
extensions/sample/generated/gnostic-x-sampleone/proto/x-sampleone.go:33:58: undefined: Book
extensions/sample/generated/gnostic-x-sampleone/proto/x-sampleone.go:35:8: undefined: Book
extensions/sample/generated/gnostic-x-sampleone/proto/x-sampleone.go:81:59: undefined: Shelf
extensions/sample/generated/gnostic-x-sampleone/proto/x-sampleone.go:83:8: undefined: Shelf
extensions/sample/generated/gnostic-x-sampleone/proto/x-sampleone.go:129:10: undefined: Book
extensions/sample/generated/gnostic-x-sampleone/proto/x-sampleone.go:135:10: undefined: Shelf
extensions/sample/generated/gnostic-x-sampleone/proto/x-sampleone.go:141:10: undefined: Book
extensions/sample/generated/gnostic-x-sampleone/proto/x-sampleone.go:156:10: undefined: Shelf
make: *** [all] Error 2

There seems to be a problem when generating this sample .proto file where the go_package path is written improperly. I have very limited experience with go but from a brief browse of the source code my guess is that it has something to do with writing the package path.

sbooeshaghi commented 3 years ago

Ok I think the problem is here:

https://github.com/google/gnostic/blob/ed308846fc77db7667936d27d7ac1fb278b74216/generate-gnostic/main.go#L87

and here:

https://github.com/google/gnostic/blob/ed308846fc77db7667936d27d7ac1fb278b74216/generate-gnostic/generate-extension.go#L257

The following code for the ProtoOption should be changed from

        ProtoOption{
            Name:    "go_package",
            Value:   directoryName + ";" + packageName, // <-- this line here
            Comment: "// The Go package name.",
        },

to

        ProtoOption{
            Name:    "go_package",
            Value:   directoryName + "/;" + packageName, // <-- this line here
            Comment: "// The Go package name.",
        },

or an equivalent change should be made to ensure that the directoryName variable has a trailing /.

sbooeshaghi commented 3 years ago

To conclude, make runs without errors if I

  1. Delete previous installation of generate-gnostic* in my ~/go/bin path
  2. Change all of the .proto files listed in my comment above and
  3. Modify the value of ProtoOption to include a trailing slash after directoryName