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

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

Closed
msoap opened this issue Apr 15, 2019 · 9 comments
Closed

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

msoap opened this issue Apr 15, 2019 · 9 comments

Comments

@msoap
Copy link
Contributor

msoap commented Apr 15, 2019

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

Just hit that, too.

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

import uberatomic "go.uber.org/atomic"

@hexdigest
Copy link
Collaborator

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

I'll fix it soon.

@hexdigest
Copy link
Collaborator

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

@deadok22
Copy link

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

Here's how to reproduce the issue:

  • Download i27.tar.gz
  • Make sure Go modules is enabled:
$ env | grep GO
GO111MODULE=on
GOROOT=/usr/local/go
  • Run go generate:
/tmp$ tar fx i27.tar.gz 
/tmp$ cd i27/
/tmp/i27$ go generate ./...
minimock: ./jwttest/token_issuer_mock.go
/tmp/i27$ cat ./jwt/jwttest/token_issuer_mock.go  | grep v3
	"github.com/gbrlsnchs/jwt/v3"
  • github.com/gbrlsnchs/jwt/v3 should not be imported

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

@hexdigest hexdigest reopened this Apr 23, 2019
@hexdigest
Copy link
Collaborator

@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
Copy link

deadok22 commented Apr 23, 2019

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
Copy link
Collaborator

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

@deadok22
Copy link

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

msoap commented Apr 27, 2019

@hexdigest thank you, now it works fine.

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

No branches or pull requests

3 participants