gojuno / minimock

Powerful mock generation tool for Go programming language
MIT License
633 stars 38 forks source link

"atomic redeclared as imported package name" build error #27

Closed msoap closed 5 years ago

msoap commented 5 years ago

Hi, great tool:), but is not working with interfaces with .../atomic types like "go.uber.org/atomic".

For example for interface:

import "go.uber.org/atomic"

type At interface {
    Method(*atomic.Int64)
}

minimock generates mock:

package tt

// DO NOT EDIT!
// The code below was generated with http://github.com/gojuno/minimock (dev)

//go:generate minimock -i github.com/BrightLocal/tt.At -o ./at_mock_test.go

import (
    "sync/atomic"
    "time"

    "go.uber.org/atomic"

    "github.com/gojuno/minimock"
)
...

which produce build error:

/at_mock_test.go:12:2: atomic redeclared as imported package name
    previous declaration at ./at_mock_test.go:9:2
deadok22 commented 5 years ago

Just hit that, too.

@msoap as a workaround you can use a named import like this:

import uberatomic "go.uber.org/atomic"
hexdigest commented 5 years ago

@msoap glad to see you again and thank you for raising the issue. @deadok22 thanks for the workaround

I'll fix it soon.

hexdigest commented 5 years ago

@msoap @deadok22 please check if the latest 2.1.3 version works for you

deadok22 commented 5 years ago

Thanks for jumping onto this @hexdigest. Unfortunately, the fix doesn't work for me.

Here's how to reproduce the issue:

While reproducing the issue I've noticed another issue: -o option is broken for empty directories - filed #28

hexdigest commented 5 years ago

@deadok22

These are different issues actually. In your case both packages "github.com/gbrlsnchs/jwt/v3" and "example.com/example/i27/jwt" have same identifier which is "jwt". What minimock does is it includes all imports from the original file into the destination file and hope that goimports will fix imports which is not happening here because packages have same id and there there are references to "jwt" id in the generated code.

I'll think what I can do here. As a workaround you can try using an alias for "github.com/gbrlsnchs/jwt/v3" in the source file, i.e. jwtoken "github.com/gbrlsnchs/jwt/v3"

deadok22 commented 5 years ago

I must have misunderstood the original ticket then. I thought it was about having import "go.uber.org/atomic" in a package named atomic, but now I see it's about the mock-related "sync/atomic" usages. Thanks for clarifying this!

The workaround I suggested earlier works in this case, too - that's exactly what I was doing when I first saw this ticket.

As for the proper fix, I guess implementing some logic for generating aliases would be the only way to go...

hexdigest commented 5 years ago

@deadok22 please check if version 2.1.4 works for you #28 should be fixed in this release either.

deadok22 commented 5 years ago

Thanks @hexdigest! 2.1.4 works fine for me, albeit it's a bit to aggressive in introducing aliases: in the example I shared above there is only one "jwt" import in the mock destination package, so aliasing is not required. Improving this in the future might be a good idea.

msoap commented 5 years ago

@hexdigest thank you, now it works fine.