google / wire

Compile-time Dependency Injection for Go
Apache License 2.0
13.14k stars 625 forks source link

Missed receiver in generated inject method #371

Open px3303 opened 2 years ago

px3303 commented 2 years ago

What version of Go are you using (go version)?

$ go version
go version go1.17.2 darwin/amd64

Does this issue reproduce with the latest release?

yes

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
GO111MODULE="on"
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/simbapeng/Library/Caches/go-build"
GOENV="/Users/simbapeng/Library/Application Support/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOINSECURE=""
GOMODCACHE="/Users/simbapeng/go/pkg/mod"
GONOPROXY="git.trustasia.cn/*"
GONOSUMDB="git.trustasia.cn/*"
GOOS="darwin"
GOPATH="/Users/simbapeng/go"
GOPRIVATE="git.trustasia.cn/*"
GOPROXY="https://goproxy.cn,direct"
GOROOT="/usr/local/Cellar/go/1.17.2/libexec"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/Cellar/go/1.17.2/libexec/pkg/tool/darwin_amd64"
GOVCS=""
GOVERSION="go1.17.2"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/Users/simbapeng/Project/gendemo/go.mod"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -arch x86_64 -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/t0/tw9t0q2j7_z5lvxgdx_4chtm0000gn/T/go-build3502256362=/tmp/go-build -gno-record-gcc-switches -fno-common"
GOROOT/bin/go version: go version go1.17.2 darwin/amd64
GOROOT/bin/go tool compile -V: compile version go1.17.2
uname -v: Darwin Kernel Version 21.6.0: Sat Jun 18 17:07:25 PDT 2022; root:xnu-8020.140.41~1/RELEASE_X86_64
ProductName:    macOS
ProductVersion: 12.5
BuildVersion:   21G72
lldb --version: lldb-1316.0.9.46
Apple Swift version 5.6.1 (swiftlang-5.6.0.323.66 clang-1316.0.20.12)

What did you do?

//go:build wireinject
// +build wireinject

package main

import (
    "io"
    "os"

    "github.com/google/wire"
)

type foo struct{}
type bar struct{}

func (foo) injectWriter() io.Writer {
    wire.Build(wire.InterfaceValue(new(io.Writer), os.Stdout))
    return nil
}

func (bar) injectWriter() io.Writer {
    wire.Build(wire.InterfaceValue(new(io.Writer), os.Stderr))
    return nil
}

What did you expect to see?

package main

import (
    "io"
    "os"
)

// Injectors from main.go:

func (foo) injectWriter() io.Writer {
    writer := _wireFileValue
    return writer
}

var (
    _wireFileValue = os.Stdout
)

func (bar) injectWriter() io.Writer {
    writer := _wireOsFileValue
    return writer
}

var (
    _wireOsFileValue = os.Stderr
)

// main.go:

type foo struct{}

type bar struct{}

What did you see instead?

Wire generates broken code. It generates function without receiver.

package main

import (
    "io"
    "os"
)

// Injectors from main.go:

func injectWriter() io.Writer {
    writer := _wireFileValue
    return writer
}

var (
    _wireFileValue = os.Stdout
)

func injectWriter() io.Writer {
    writer := _wireOsFileValue
    return writer
}

var (
    _wireOsFileValue = os.Stderr
)

// main.go:

type foo struct{}

type bar struct{}