-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Deprecating relative path and forcing full path doesn't appear to be compatible with Go Modules #1102
Comments
Your
If you want the output somewhere else, you'll need to tell |
So, where should it go to work with Go Modules?
…On Thu, Apr 23, 2020, 3:14 PM Damien Neil ***@***.***> wrote:
The protobuf file is generated into the GOPATH, but Go Modules doesn't
look in the GOPATH for the built module.
Your protoc invocation is requesting that output be placed in $GOPATH/src:
//go:generate protoc example.proto --go_out=plugins=grpc:*$GOPATH/src*
If you want the output somewhere else, you'll need to tell protoc where
that is.
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#1102 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ACQDXNFZQIZ7I73IR3SFEWDROC4TJANCNFSM4MPRU4AQ>
.
|
What's the name of your module? Assuming you have
See the release notes for details. |
I'm about to try it out, but will that work for grpc services? In this example, the module would be "somewhere.com/path/to/mine" (as listed in the example.proto go_package option that forcing adoption). Everything I've read in states that using relative paths or leaving off the go_package is being deprecated and I'm trying to figure out how to generate GRPC client and server code using Go Modules once the legacy method is not supported. Simply, today I learned that the way my company builds GRPC services is being deprecated, the closest solution I could find after searching online all day was to do the "--go_out=plugins=grpc:$GOPATH/src" but that method does not work with Go Modules and I cannot find documentation anywhere that describes how to use this solution with Go Modules (unless I create entire repos that host generated code from GRPC files, but that violates "best practices" and adds needless complexity). |
It should for |
I'm afraid I just ran out of time, today. I'll try it out in the morning. Thank you very much! |
This method doesn't seem to work, either. The generated code just appears in the same directory that the "go:generate" command exists and cannot be imported. I also tried moving the full path into the root of the application and it was still not found. Effectively, the file structure ended up looking like: /home/me/code/somewhere.com/path/to/project/somewhere.com/path/to/mine and still doesn't seem to work with Go Modules. In the example code provided (which doesn't use GRPC, but still show the same problem) I changed the code to look like: package main
//go:generate protoc example.proto --go_out=module=somewhere.com/path/to/mine:.
import (
"fmt"
"somewhere.com/path/to/mine"
)
func main() {
v := mine.Empty{}
fmt.Println(v)
} I can confirm that I'm using v1.4.0. (Actually after realizing that I wasn't updated to the latest, the difference is that the "example.pb.go" appears in the root of the project, but is still not imported.) What makes this difficult is in the actual projects, the code generated from the *.proto file either needs to be placed in a different package path per project (which doesn't seem possible with the go_package option (unless that is just a suggestion that is overridden by the --go_out flag)) or needs to be in its own package, but there is consensus that we do not want to commit generated code to a package. Does that make sense? |
Realizing that I was experiencing a version discrepancy between my host machine and my Dockerfile (host was running an older protoc-gen-go that was incorrectly handling 'option go_package=".;mine"' by creating a package "__mine"), as long as the ".;*" syntax continues to be supported, I don't have an issue. If that syntax is going away in the future, then I'm not seeing a path forward. |
This is the first time I have seen: option go_package = ".;mine"; What is the "." in the first segment supposed to mean? |
If you use: option go_package = "mine"; protoc-gen-go warns that it should be ".;mine". My problem arose when I got that warning while building with Docker, then tested on my machine (version <1.4.0) and ended up with packages name "__mine". I have no idea what the syntax means and it makes no sense to me (and I'm not a fan of "magic syntax"), but if it is going to work like that going forward, I can live with it (though it would be nice for that to be documented somewhere online). |
That seems to be a bug with The |
https://go-review.googlesource.com/c/protobuf/+/232338 adjusts the warning provided by |
If the option for a relative path is removed, then I'm back to the original issue for which this was created: I don't see a way to us this with Go Modules. |
Can you explain how full go package paths is incompatible with Go modules? |
You can use the example code and comments above to demonstrate, but in the company I work for (and the company I worked at before this one) we are not allowed to commit generated code. We have a *.proto file that is maintained in a documentation repository that can be used by multiple languages and repositories. We can pull the *.proto service files into our projects and run the code generator on them during our build process. This leaves us two options:
It is possible to NOT use Go Modules and use the method outlined above (Go Modules appear to be the future and appear to be the best method for dependency management) OR to generate code and commit it as a separate go-gettable repository (not allowed at any company I've worked for as it violates many documented best-practices). |
I don't know if you're aware, but the |
Can you give an example protoc line using the example above? I opened this issue because I spent nearly a day searching online and could not find a method or example documented to accomplish this task. Above, I tried this in one code example by including this line: //go:generate protoc example.proto --go_out=module=somewhere.com/path/to/mine:. And it created the *.pb.go file in the local directory, but it couldn't be imported into the application as "somewhere.com/path/to/mine". It appears that I could use the setting |
The If you want to output a |
The "module" flag makes sense to me. The "option go_package" doesn't. The "option go_package" doesn't seem to be used for anything. In the old version, it got stuffed into the package name at the top of the file, but in the new version just seems to be used to validate that the "package x;" matches the |
The
In the old version, |
Makes sense. Thank you for the clarification. |
What version of protobuf and what language are you using?
protoc 3.11.3 using Go protoc-gen-go
What did you do?
main.go
example.proto
Steps to reproduce:
export GO111MODULE=on go generate ./... go run main.go
What did you expect to see?
I expected the program to compile and run. This change has become important because of this warning message from protoc-gen-go:
I can find no way to use a full import path that works with Go Modules.
What did you see instead?
The protobuf file is generated into the GOPATH, but Go Modules doesn't look in the GOPATH for the built module.
Make sure you include information that can help us debug (full error message, exception listing, stack trace, logs).
Anything else we should know about your project / environment?
The text was updated successfully, but these errors were encountered: