-
Notifications
You must be signed in to change notification settings - Fork 810
Fix to avoid printing import aliases when not needed #341
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
Conversation
It required a minor change in the grpc package, but otherwise the tests passed (surprisingly).
|
||
func (g *Generator) PrintImport(alias, pkg string) { | ||
statement := "import " + alias + " " + strconv.Quote(pkg) | ||
if alias == pkg[strings.LastIndex(pkg, "/")+1:] { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suspect this won't always work, since the packageName can differ from the packagePath.
// Convert dots to underscores before finding a unique alias. | ||
pkg = strings.Map(badToUnderscore, pkg) | ||
// Only make pkg names unique if they are non-standard lib | ||
if f != nil { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice trick here :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The comment looks incorrect though, judging by the method comment which says:
If f is nil, it's a builtin package like "proto" and has no file descriptor.
So it's not that the package is in the stdlib, it's that it is "a builtin package", whatever that means.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This comment is still wrong (this code only makes the names unique if they are not builtin).
However, this makes me think that this comment is just not useful. Perhaps instead of explaining what this code is doing (which is pretty clear just from reading the code and the method comment), explain why it is doing it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about this:
// Ensure that package names across multiple imports are unique, unless they
// are builtin or standard lib packages, in which case they should not conflict.
I also wonder about a case where someone is importing github.com/gogo/protobuf/proto and github.com/golang/protobuf/proto |
When we have thought about these cases, then hopefully we are at a place where we can ask a gogoprotobuf user to test this branch their system. |
I ran the tests in import proto "github.com/gogo/protobuf/proto"
import golang_proto "github.com/golang/protobuf/proto" Of course, there could be other issues that I've overlooked. Let me know if there is any specific tests beyond this I should run. I've also checked a few other places and it seems that some duplicate imports of the same package under different aliases are eliminated, e.g. in About the Travis CI build failing, I assume it is caused by some diff check of the current output against previous output. I was unable to find a specific test failure or error message in the log. (Perhaps if I committed also the regenerated (I'm sorry if my unfamiliarity with the dev process is causing this. Please advise on how to do things properly, if I'm doing it wrong; maybe there is a contributor guide that I should read?) |
Those proto imports are beautiful :) On Travis you should be able to click on details and see the printout. Your doing perfectly great :) I think this is ready for a first outside tester. After that I think it would be great to add a little test to gogoprotobuf, where multiple imports end in the same suffix, just like |
@tamird , can I ask you a favor. Can you test this branch on your codebase? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tested against CockroachDB, looks good (with the changes I suggested). Here's the diff: https://gist.github.com/bb8f751131a1b7c4c43ab48b5065ce5f.
// Convert dots to underscores before finding a unique alias. | ||
pkg = strings.Map(badToUnderscore, pkg) | ||
// Only make pkg names unique if they are non-standard lib | ||
if f != nil { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The comment looks incorrect though, judging by the method comment which says:
If f is nil, it's a builtin package like "proto" and has no file descriptor.
So it's not that the package is in the stdlib, it's that it is "a builtin package", whatever that means.
protoc-gen-gogo/grpc/grpc.go
Outdated
g.P(")") | ||
g.P() | ||
grpcImports := []string{ | ||
path.Join(g.gen.ImportPrefix, contextPkgPath), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this bit is incorrect - there is no need for this path.Join
- the imports
object already takes care of the import prefix. In CockroachDB, this causes the import prefix to appear twice.
I meant to put this comment in the main thread I wonder if a package like sortkeys is a f!=nil package and if so I wonder what happens when I also try to import a proto file with package name sortkeys. Maybe it is time to start adding a test like this one? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
9 commits seems excessive, though.
Yeah, I guess they should be squashed into a single commit at some point when it is ready to be merged, but I'm not sure how legible the commits will be anyway with all the generated noise. Anyway, I planning to take a stab at adding the tests that @awalterschulze requested; these last few commits are just to bring my fork up to dated with the master branch. |
The importduplicate test tests if the fix to avoid long import names is still able to detect duplicate import names.
@awalterschulze I've prepared a new test to check that my proposed patch still works when a proto files uses a generated package (from another proto file) that conflicts with the name of an internal package (in this case, |
I discovered a problem, if the proto package name is import proto "github.com/gogo/protobuf/proto" My fix won't detect this. I'm thinking about a fix. |
I am really positive about this change.
I can squash the commits when I merge.
Also github makes it easy to review the generated code. I don't see it as
noise.
Good luck with the final issue
…On Wed, 8 Nov 2017, 04:43 Hein Meling, ***@***.***> wrote:
I discovered a problem, if the proto package name is proto, and there is
also an import like this:
import proto "github.com/gogo/protobuf/proto"
My fix won't detect this. I'm thinking about a fix.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#341 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/ABvsLSWWfaATGLIOoLjXw4clE1aJGSf5ks5s0SNRgaJpZM4PudnO>
.
|
I've tried a wide range of things to make the imports more readable, including trying to incorporate #316, but that fails with my new tests in Anyway, I've resolved the issues I discovered yesterday, and so this should be ready to be merged. PS: If you import a type whose name overlap with the package name of the proto file, then that type gets named import types1 "github.com/gogo/protobuf/types" This is strictly not necessary, as Go allows importing |
I think thats fair. |
Womp womp, no squash |
My bad. |
Sorry |
It required a minor change in the
grpc
package, but otherwise the tests passed (surprisingly). This PR solves my own use case in issue #338.PS: I realize this may potentially be an invasive change, and so should be tested widely on use cases outside the main repo.