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

protoc-gen-go: import_path flag doesn't do anything #63

Closed
mwitkow opened this issue Aug 14, 2015 · 12 comments
Closed

protoc-gen-go: import_path flag doesn't do anything #63

mwitkow opened this issue Aug 14, 2015 · 12 comments

Comments

@mwitkow
Copy link

mwitkow commented Aug 14, 2015

We'd like to add a go get like prefix to all of our generated protobuf packages.

The option in generator.go called PackageImportPath seems like it should do what we want:

PackageImportPath string            // Go import path of the package we're generating code for

However, that value is never used in the codebase.

The ImportPrefix, unfortunately, prefixes all imports, including the full go get paths internal to protobufs.

@mwitkow
Copy link
Author

mwitkow commented Aug 14, 2015

PR with a proposed fix ready. We checked and it seems to be doing exactly as advertised :)

@dsymonds
Copy link
Contributor

What do you mean it isn't used? It's used in the defaultGoPackage method.

But if you want to control the import path on a per-input basis, that's what the M parameter is for. It populates ImportMap and has full flexibility.

@mwitkow
Copy link
Author

mwitkow commented Aug 17, 2015

Apologies, I haven't made the connection between defaultGoPackage and the written go lines, partially because it's not clear what it's meant to do.

I thought that when I specify protoc ... --go_out=plugins=grpc,import_path=mygit.com/autogen:/tmp/dest (using import_path as the way to specify PackageImportPath) , I'd get the following:

import proto "github.com/golang/protobuf/proto"

// discarding unused import google_api1 "mygit.com/autogen/google/api"
import google_protobuf1 "mygit.com/autogen/google/protobuf"
import base1 "mygit.com/autogen/base"

(where in a file generated from base/serverstatus.proto)

Instead I get:

import proto "github.com/golang/protobuf/proto"

// discarding unused import google_api1 "google/api"
import google_protobuf1 "google/protobuf"
import base1 "base"

If, I use import_prefix=mygit.com/autogen, I get:

import proto "mygit.com/autogen/github.com/golang/protobuf/proto"

// discarding unused import google_api1 "mygit.com/autogen/google/api"
import google_protobuf1 "mygit.com/autogen/google/protobuf"
import base1 "mygit.com/autogen/base"

So basically, there's no way to be able to say: prefix all the Go import paths that are an output of protoc. This is really useful when you use the go get convention for imports.
We're working around that by using import_prefix and then seding the file to remove the prefix from github.com/golang/protobuf/proto. How can we achieve this without such ugly hacks?

@dsymonds
Copy link
Contributor

The import_prefix parameter is for when the whole code base is effectively vendored. If you only have a subset of packages that need some sort of prefix, use the M parameter, which permits you to specify an arbitrary mapping from proto file to import path. But maybe post-processing is simpler for you.

@mwitkow
Copy link
Author

mwitkow commented Sep 5, 2015

Currently we use import_prefix for all and sed the standard proto dependencies out.

Since we use the "single repo" GOPATH approach (with vendoring done through subtrees), it would be incredibly useful for us to not have to hack our way around postprocessing and have a flag that would add a Go import "base protobuf" prefix. I tried the M parameter, but since we have a big tree of protobufs, it quickly became a nightmare to maintain.

@drombosky
Copy link

I also have the same problem as @mwitkow-io. We currently are using protobufs with Python but are looking to make the move to Go for some of our services.

Something like #64 would fix our problem immediately. Using the mapping parameter is possible, but a large pain for both existing and new message packages.

Regarding vendoring, having this package do it's own vendoring seems counterproductive given various 3rd party vendoring tools and the GO15VENDOREXPERIMENT flag in 1.5.

@mwitkow
Copy link
Author

mwitkow commented Oct 28, 2015

@dsymonds any comment on maybe introducing a different import_path parameter that would address this usecase?

@zeroZshadow
Copy link

Im currently fighting with this issue as well, however mwitkow's PR does not seems to change anything?
protoc.exe -I . --go_out=import_path=test/:pb *.proto
One of the protofiles imports another package in the root directory, which results in import pb "."

@sean-q
Copy link

sean-q commented Nov 20, 2015

Same problem here - I would like a way to change the import_prefex for the pb.go files I am generating, but leave the "github.com/golang/protobuf/proto" import alone. import_prefix changes both, which is not what I would like.

@idevz
Copy link

idevz commented Dec 5, 2016

I also have the same problem, any update here?

@jacovieASAPP
Copy link

Is there any resolution to this issue?

@dsnet dsnet changed the title import_path parameter doesn't do anything protoc-gen-go: import_path flag doesn't do anything Feb 14, 2018
@neild
Copy link
Contributor

neild commented Feb 15, 2018

The title of this issue is clearly incorrect--import_path sets the package name of generated files if they don't contain a go_package option. Admittedly, I don't think it's useful, since you can and should set the go_package option instead. (I'm guessing it predates go_package?)

I think the underlying problem described here was addressed by #139, which permits specifying the import path for files in the go_package option.

There are a couple additional comments here about import_prefix being too broad, which is a separate issue and I believe covered more specifically by #181. I'm going to close this in favor of that issue.

@neild neild closed this as completed Feb 15, 2018
@golang golang locked as resolved and limited conversation to collaborators Jun 25, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants