maxbrunsfeld / counterfeiter

A tool for generating self-contained, type-safe test doubles in go
MIT License
999 stars 93 forks source link

Missing package in generated fakes with go.mod version at 1.23 #298

Open nginx-nickc opened 2 months ago

nginx-nickc commented 2 months ago

Similar to https://github.com/maxbrunsfeld/counterfeiter/issues/297, seeing different results with go.mod set to 1.22 vs 1.23.

package service

import (
    "context"
    "google.golang.org/protobuf/proto"

)

//go:generate go run github.com/maxbrunsfeld/counterfeiter/v6 -generate

//counterfeiter:generate . Example
type Example interface {
    FindExample(context.Context, proto.Message) ([]string, error)
}

with following go.mod file

module foo

go 1.22.0

toolchain go1.23.1

require google.golang.org/protobuf v1.34.2

require (
    github.com/google/go-cmp v0.6.0 // indirect
    github.com/maxbrunsfeld/counterfeiter/v6 v6.9.0 // indirect
    golang.org/x/mod v0.21.0 // indirect
    golang.org/x/sync v0.8.0 // indirect
    golang.org/x/text v0.18.0 // indirect
    golang.org/x/tools v0.25.0 // indirect
)

getting, works, but not quite correct.

package examplefakes

import (
        "context"
        service "foo"
        "sync"

        "google.golang.org/protobuf/reflect/protoreflect"
)

type FakeExample struct {
        FindExampleStub        func(context.Context, protoreflect.ProtoMessage) ([]string, error)
        findExampleMutex       sync.RWMutex
        findExampleArgsForCall []struct {
                arg1 context.Context
                arg2 protoreflect.ProtoMessage
        }

when change go.mod to

module foo

go 1.23.1

require google.golang.org/protobuf v1.34.2

require (
        github.com/google/go-cmp v0.6.0 // indirect
        github.com/maxbrunsfeld/counterfeiter/v6 v6.9.0 // indirect
        golang.org/x/mod v0.21.0 // indirect
        golang.org/x/sync v0.8.0 // indirect
        golang.org/x/text v0.18.0 // indirect
        golang.org/x/tools v0.25.0 // indirect
)

getting the following, which is causing errors.

package examplefakes

import (
        "context"
        service "foo"
        "sync"
)

type FakeExample struct {
        FindExampleStub        func(context.Context, Message) ([]string, error)
        findExampleMutex       sync.RWMutex
        findExampleArgsForCall []struct {
                arg1 context.Context
                arg2 Message
        }
        findExampleReturns struct {

Looks like possible work around

GODEBUG=gotypesalias=0 go generate ./...

xanderflood commented 1 month ago

https://github.com/maxbrunsfeld/counterfeiter/pull/301 proposed fix!

@maxbrunsfeld feedback (and/or approval to run the test workflows) would be greatly appreciated!

joefitzgerald commented 1 month ago

Please try updating to v6.10.0, which includes #301 (thanks @xanderflood!). Let me know if that resolves the issue fully.