Skip to content

Conversation

meling
Copy link
Contributor

@meling meling commented Oct 5, 2017

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.

meling added 2 commits October 4, 2017 18:25
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:] {
Copy link
Member

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 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice trick here :)

Copy link
Contributor

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.

Copy link
Contributor

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?

Copy link
Contributor Author

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.

@awalterschulze
Copy link
Member

I also wonder about a case where someone is importing github.com/gogo/protobuf/proto and github.com/golang/protobuf/proto
Which is what happens when someone uses the goproto_registration plugin.

@awalterschulze
Copy link
Member

awalterschulze commented Oct 5, 2017

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.

@meling
Copy link
Contributor Author

meling commented Oct 5, 2017

I ran the tests in github.com/gogo/protobuf/test/registration locally, and the following import statements are produced:

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 vanity/test/faster/vanity.pb.go.

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 .pb.go files etc. this problem would go away?)

(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?)

@awalterschulze
Copy link
Member

Those proto imports are beautiful :)

On Travis you should be able to click on details and see the printout.
But it is as you suspect, it is simply a diff that is failing, that wants the regenerated code to be commited. Simply calling make in the project root should fix this.

Your doing perfectly great :)
But adding a contributing guidelines doc is maybe not a bad idea. I just haven't found one in the wild which I thought was a good read yet, but I am open to suggestions and would like to know where you are struggling.
From my end though, you are doing 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 proto, but without requiring more dependencies.

@awalterschulze
Copy link
Member

awalterschulze commented Oct 6, 2017

@tamird , can I ask you a favor. Can you test this branch on your codebase?
The tests that are failing is simply generated code that is not committed yet.

Copy link
Contributor

@tamird tamird left a 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 {
Copy link
Contributor

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.

g.P(")")
g.P()
grpcImports := []string{
path.Join(g.gen.ImportPrefix, contextPkgPath),
Copy link
Contributor

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.

@awalterschulze
Copy link
Member

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?

Copy link
Contributor

@tamird tamird left a 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.

@meling
Copy link
Contributor Author

meling commented Nov 8, 2017

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.
@meling
Copy link
Contributor Author

meling commented Nov 8, 2017

@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, sortkeys as suggested earlier). Let me know if you want something more in addition to this.

@meling
Copy link
Contributor Author

meling commented Nov 8, 2017

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.

@awalterschulze
Copy link
Member

awalterschulze commented Nov 8, 2017 via email

It seems that PR gogo#316 is only sound if you ensure that the
package name does not overlap with a builtin type, such as proto.
Hence, this commit reverts my previous attempt to incorporate gogo#316.
@meling
Copy link
Contributor Author

meling commented Nov 9, 2017

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 test/importduplicate. Hence, unless you think my tests are unreasonable (importing from a sub package proto/proto.proto), I think you can close #316.

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 type1. For example, in test package types, there will be an import like this:

import types1 "github.com/gogo/protobuf/types"

This is strictly not necessary, as Go allows importing types even though the package is also types. However, fixing that issue is beyond the need for my other project, and it does not appear trivial to fix.

@awalterschulze
Copy link
Member

I think thats fair.
Really great work :)
Thank you very much. I really appreciate it.

@tamird
Copy link
Contributor

tamird commented Nov 9, 2017

Womp womp, no squash

@awalterschulze
Copy link
Member

My bad.

@awalterschulze
Copy link
Member

Sorry

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants