maxbrunsfeld / counterfeiter

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

Missing package in generated fakes using go 1.23.1 #297

Closed luc-gocaspi closed 1 month ago

luc-gocaspi commented 1 month ago

In some cases the the generated fake functions are missing the needed package info for Types when using go 1.23.1. If go 1.22.7 is used everything is working fine.

See the following example:

package service

import (
    "context"
    "go.mongodb.org/mongo-driver/bson"
)

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

//counterfeiter:generate . Example
type Example interface {
    FindExample(ctx context.Context, filter bson.M) ([]string, error)
}

Resulting output using go 1.23.1:

(...)
type FakeExample struct {
    FindExampleStub        func(context.Context, M) ([]string, error)
    findExampleMutex       sync.RWMutex
    findExampleArgsForCall []struct {
        arg1 context.Context
        arg2 M
    }
    findExampleReturns struct {
        result1 []string
        result2 error
    }
    findExampleReturnsOnCall map[int]struct {
        result1 []string
        result2 error
    }
    invocations      map[string][][]interface{}
    invocationsMutex sync.RWMutex
}
(...)

Resulting output using go 1.22.7:

(...)
type FakeExample struct {
    FindExampleStub        func(context.Context, primitive.M) ([]string, error)
    findExampleMutex       sync.RWMutex
    findExampleArgsForCall []struct {
        arg1 context.Context
        arg2 primitive.M
    }
    findExampleReturns struct {
        result1 []string
        result2 error
    }
    findExampleReturnsOnCall map[int]struct {
        result1 []string
        result2 error
    }
    invocations      map[string][][]interface{}
    invocationsMutex sync.RWMutex
}
(...)
thesilentg commented 1 month ago

We are also seeing this with go1.23.1:

import (
    "sync"

    "github.com/aws/aws-sdk-go/aws/request"
    "github.com/aws/aws-sdk-go/service/s3"
    "github.com/aws/aws-sdk-go/service/s3/s3iface"
)

type FakeS3API struct {
    (...)

    // Note the missing "context."  in stub below and missing import for "context" above
    AbortMultipartUploadWithContextStub        func(Context, *s3.AbortMultipartUploadInput, ...request.Option) (*s3.AbortMultipartUploadOutput, error)
    abortMultipartUploadWithContextMutex       sync.RWMutex
    abortMultipartUploadWithContextArgsForCall []struct {
        arg1 Context
        arg2 *s3.AbortMultipartUploadInput
        arg3 []request.Option
    }
        (...)
joefitzgerald commented 1 month ago

I just tagged v6.9.0 of the package. I was able to reproduce the issue with v6.8.1, and the issue is resolved in v.6.9.0 because it includes a newer version of golang.org/x/tools.

xanderflood commented 2 weeks ago

I don't think this bug is resolved by v6.9.0 - @joefitzgerald are you sure you were using go1.23 when you tested it? Using go1.23 and counterfeiter v6.9.0, I still get exactly the same result as described in the bug report above using the input given by the bug reporter.

The root cause seems to have been a breaking change in the stdlib - the AST that's exposed by go/types has changed how it represents alias types, and golang.org/x/tools doesn't contain any change that seems like it would address this. This is from the go 1.23 changelog:

By default, go/types now produces Alias type nodes for type aliases. This behavior can be controlled by the GODEBUG gotypesalias flag. Its default has changed from 0 in Go 1.22 to 1 in Go 1.23.