gowrap icon indicating copy to clipboard operation
gowrap copied to clipboard

"Redeclared as the imported package name" error when main module and one of dependencies have the same last part

Open zcolleen opened this issue 1 year ago • 2 comments

Hey @hexdigest! This issue is related to https://github.com/gojuno/minimock/issues/87 and about this line of code https://github.com/hexdigest/gowrap/blob/master/generator/generator.go#L211 . If the main module last part and one of dependencies last part are the same, in resulting generated code we will have <import_name> redeclared as the imported package name error.

This code was added here https://github.com/hexdigest/gowrap/pull/23#issuecomment-696729926 , there is also said and fixed some issues from the original PR in commit message. Could you please provide some more context, what were those issues?

zcolleen avatar May 15 '24 22:05 zcolleen

Hello! This is my first time describing a bug on github, if something is unclear to you, please ask additional questions!

I have a dependency in the "order" package on a package with the same name, it looks something like this:

├── order
│     └── impl.go
├── some_package
│     └── order
│         └── ifaces.go

When generating mocks for interfaces in ifaces.go, an unused import of the some_package/order package appears in the generated code. Moreover, this problem only appears if mocks are output to another directory (for example, some_packages/order/mocks). If mocks are created in the same directory, then everything is fine with imports.

import (
	"internal/usecase/order" // this shouldn't be here, because it's unused + it's redeclared package now
	"sync"
	"time"

	"context"
	"internal/models/usecase/order"
	mm_atomic "sync/atomic"
	mm_time "time"

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

Skullduggery-sys avatar Jun 23 '24 20:06 Skullduggery-sys

Today I hit the exact same issue as well. It can be worked around by using an alias for the package, that is imported, but this is clearly not the preferred way to handle this.

breml avatar Aug 21 '24 12:08 breml

Hey guys, oped pr, that fixes this issue. Everybody are welcome to review

zcolleen avatar Aug 25 '24 11:08 zcolleen