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

How to generate right Import paths #257

Open
ashishnegi opened this issue Feb 1, 2017 · 10 comments
Open

How to generate right Import paths #257

ashishnegi opened this issue Feb 1, 2017 · 10 comments
Labels

Comments

@ashishnegi
Copy link

Hi Thanks for the project. I have having problems with getting correct import paths.
I have following directory structure

root/dproto/a.proto
root/abc/b.proto

b.proto's has import for a.proto with line
import "dproto/a.proto";

In root directory, i run
protoc --gofast_out=plugins=grpc,import_prefix=github.com/myrepo/repo:. abc/b.proto
I get no errors and b.pb.go is generated with following imports :

import proto "github.com/myrepo/repo/github.com/golang/protobuf/proto"
import fmt "fmt"
import math "math"
import a "github.com/myrepo/repo/dproto"

import io "github.com/myrepo/repo/io"
import fmt "github.com/myrepo/repo/fmt"

I want the import_prefix for only the import path i have given in b.proto.
I also found that projects like etcd listed on main page , use same kind of imports across directories.
How can i solve this problem ? Am i using wrong arguments ?

Version information :

protoc --version
libprotoc 3.2.0
@awalterschulze
Copy link
Member

Does this look like it would solve your issue golang/protobuf#63 ?

@theobat
Copy link

theobat commented Feb 1, 2017

Trouble is this parameter is -seemingly- not doing anything without this PR: golang/protobuf#64
(at least on my end)

@ashishnegi
Copy link
Author

ashishnegi commented Feb 1, 2017

Well, only way is to use M option, which as others have felt too, is very difficult to maintain.
Is it possible to get this fix (or should i say a feature) in gogo ?

@awalterschulze
Copy link
Member

I would really want to know why they aren't merging this pull request golang/protobuf#64
before adding another import path "feature".
I find these import paths by far the most confusing thing in this project.

@alecthomas
Copy link

I can appreciate your desire to remain compatible with upstream, but it seems to me that they are largely uninterested in (or don't have the bandwidth for) extra features, usability fixes, or even in some cases bug fixes. Just looking through the open issues and pull requests shows very little activity and a large number of unacknowledged issues and pull-requests. The last PR was a bug fix 2 days ago, but prior to that the last merged PR was from November last year.

In particular, as you say, the way paths are mapped is very clunky (to put it mildly). They've acknowledged this for years, but no movement has been made to rectify it. My particular pet peeve is golang/protobuf#39, which is now almost two years old.

Maybe it's time to consider diverging?

@awalterschulze
Copy link
Member

Is merging golang/protobuf#64 enough?

@awalterschulze
Copy link
Member

hello?

@neild
Copy link

neild commented Feb 15, 2018

The complaint about import_prefix applying to too many paths is #181, but I think import_prefix is a distraction here. The right answer is to include a go_package option in each of the source files specifying its import path.

# a.proto
syntax = "proto2";
package a;
option go_package = "github.com/neild/foo/a";
import "b.proto";
message MessageA {
  optional b.MessageB field = 1;
}
syntax = "proto2";
package b;
option go_package = "github.com/neild/foo/b";
message MessageB {}
protoc --go_out=$GOPATH/src a.proto

And the imports in a.pb.go are now correct:

import proto "github.com/golang/protobuf/proto"
import fmt "fmt"
import math "math"
import b "github.com/neild/foo/b"

Specifying the import path will influence the generated filenames. In the common case (no vendoring), --go_out=$GOPATH/src should work; in some other cases, you might need to generate into a temporary directory and copy the generated file(s) to the desired location. I just filed #515 to possibly improve control over generated filenames.

@awalterschulze
Copy link
Member

@alecthomas @ashishnegi @theobat What do you think about neild's comment?

@k1ng440
Copy link

k1ng440 commented Mar 24, 2019

this still has not been solved yet?

karlek added a commit to lapsang-boys/pippi that referenced this issue Sep 25, 2019
Solved by adding go_package option to proto as referenced in this
comment: gogo/protobuf#257 (comment)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

6 participants