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

cmd/vet: stdmethods check gets confused if run on a package named "xml" #29750

Open
jacoelho opened this issue Jan 15, 2019 · 2 comments
Open

cmd/vet: stdmethods check gets confused if run on a package named "xml" #29750

jacoelho opened this issue Jan 15, 2019 · 2 comments
Labels
Milestone

Comments

@jacoelho
Copy link

@jacoelho jacoelho commented Jan 15, 2019

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

$ go version
go version go1.11.4 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
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/jc/Library/Caches/go-build"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/jc/go"
GOPROXY=""
GORACE=""
GOROOT="/usr/local/Cellar/go/1.11.4/libexec"
GOTMPDIR=""
GOTOOLDIR="/usr/local/Cellar/go/1.11.4/libexec/pkg/tool/darwin_amd64"
GCCGO="gccgo"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/pf/_mdvc42j193c4tf6kgfm80gcss_jqd/T/go-build565539251=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

For example, if I have the following package:

package xml

import (
	"encoding/xml"
	"io"
)

type XMLMap map[string]string

type XMLMapEntry struct {
	XMLName xml.Name
	Value   string `xml:",chardata"`
}

// UnmarshalXML 
func (m *XMLMap) UnmarshalXML(dec *xml.Decoder, start xml.StartElement) error {
	v := XMLMap{}

	for {
		var e XMLMapEntry

		err := dec.Decode(&e)
		if err == io.EOF {
			break
		}

		if err != nil {
			return err
		}

		v[e.XMLName.Local] = e.Value
	}

	*m = v
	return nil
}

What did you expect to see?

go vet ./...
< no output>

What did you see instead?

$ go vet ./...
./xml.go:16: method UnmarshalXML(dec *xml.Decoder, start xml.StartElement) error should have signature UnmarshalXML(*xml.Decoder, xml.StartElement) error

even if I try to alias encoding/xml import I still get same error.

If instead I have the same code, but rename package:

package something
...
<same code as before>
go vet ./...
< no output>

go vet doesn't seem to handle correctly if code is in package also named xml.

@mvdan
Copy link
Member

@mvdan mvdan commented Jan 15, 2019

Interesting. Like you say, this sounds like a bug in vet relating to how it identifies or compares types.

@mvdan mvdan added the NeedsFix label Jan 15, 2019
@mvdan mvdan changed the title cmd/vet: xml.Marshaler/Unmarshaler check cmd/vet: stdmethods check gets confused if run on a package named "xml" Jan 15, 2019
@mvdan mvdan added this to the Go1.13 milestone Jan 15, 2019
@andybons andybons modified the milestones: Go1.13, Go1.14 Jul 8, 2019
@rsc rsc modified the milestones: Go1.14, Backlog Oct 9, 2019
@jpopadak
Copy link

@jpopadak jpopadak commented Nov 30, 2020

I have a very similar issue. I have a package named github.com/mywork/myservice/encoding/json and I have my own Unmarshaler interface.

// Marshaler allows a type to force how it is encoded, given it knows how do the JSON Schema encoding and return
// its schemaID.
type Marshaler interface {
	// MarshalJSON knows the schemaID and how to encode itself in JSON format.
	MarshalJSON() (encodedBytes []byte, schemaID int, err error)
}

I get the following.

go vet ./...
# github.com/mywork/myservice/encoding/json
encoding/json/encode.go:17:2: method MarshalJSON() (encodedBytes []byte, schemaID int, err error) should have signature MarshalJSON() ([]byte, error)

For my code, I require a schemaID to be returned. We have many different types (Avro, JSON, plaintext, Protobuf) and each of them have their own Marshaler. They all exist within our project github.com/mywork/myservice/encoding/* package. The error is not thrown for the other types, assuming because they have (not std lib) MarshalAvro and MarshalProto methods.

Is there any workaround for this? The only one I currently see is to disable the -stdmethods on go vet.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
5 participants