Go: cmd/vet: Consider reverting tag conflict for embedded fields

Created on 28 Feb 2019  路  16Comments  路  Source: golang/go

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

$ go version
go version go1.12 linux/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="/home/pschultz/.cache/go-build"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/pschultz/go"
GOPROXY=""
GORACE=""
GOROOT="/usr/local/go"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
CC="gcc"
CXX="g++"
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 -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build788821326=/tmp/go-build -gno-record-gcc-switches"

What did you do?

Run go vet on existing code base, containing code such as this: https://play.golang.org/p/HnkPcNUu84J

What did you expect to see?

go vet to either succeed, or to report only real problems.

What did you see instead?

go vet fails due to intended shadowing of embedded fields with json tags.

25593 changed vet to also report repeated tags from embedded types.

We use this technique intentionally to create slightly varying JSON representation for struct types, typically when returning large lists of a particular type. Here is a simplified example (playground):

package main

import (
    "encoding/json"
    "fmt"
)

type Book struct {
    Author Author `json:"author"`
    Title  string `json:"title"`
}

type Author struct {
    Name      string `json:"name"`
    Biography string `json:"biography"`
}

func main() {
    b := Book{
        Title: "Foo",
        Author: Author{
            Name:      "John Doe",
            Biography: "Lorem Ipsum",
        },
    }

    // When returning a list of books, we don't want to include the whole
    // Author struct for each book.
    type BookSummary struct {
        Book            // Include all Book fields by default,
        Author struct { // but override the "author" field with a smaller model
            Name string `json:"name"`
            // All other fields omitted
        } `json:"author"`
    }

    var s BookSummary
    s.Book = b
    s.Author.Name = b.Author.Name

    j, _ := json.MarshalIndent(s, "", "  ")
    fmt.Println(string(j))
}

With go vet now rejecting this code, the only viable alternative I can see is to redefine the Book type completely:

type BookSummary struct {
    Title  string `json:"title"`
    Author struct {
        Name string `json:"name"`
    } `json:"author"`
    // Many more fields
}

This way, however, the JSON representation of BookSummary will not be updated automatically when Book fields are added or changed. We would also have to assign each field individually, since Book and BookSummary don't have the same underlying type. Realistically, this forces us to do some kind of code generation, which is clearly much more trouble than what worked with Go 1.11.

Other possible fixes are:

  • Not run go vet anymore
  • Marshal the Book value, then unmarshal into a map, then modify the map, and then marshal again.
  • Add the MarshalJSON method and some fields that control how that method behaves.

None of these are appealing.

Please consider this as a valid use-case for repeated json tags on embedded fields and revert 4b439e41e2cdd78e0eeed05942c93364c5d99b6b.

FrozenDueToAge NeedsFix

Most helpful comment

@nightlyone I'm not sure if this is a documented and intended feature. But like @alandonovan mentioned, it seems like a useful feature to me. And I imagine some users already depend on it, as pointed out by the bugs filed against vet.

Let's use #30846 to keep track of the proper fix for Go 1.13.

All 16 comments

This is why we ask that you try out betas and RCs - this change to vet has been shipped in all 1.12 releases, going all the way back to beta1 :)

I admit I haven't considered this kind of false positive, but it would be a shame to simply throw away the added check. @alandonovan @robpike @powerman any thoughts?

Another option is to revert this particular vet enhancement in 1.12.1, and keep it in master to decide what to do before the 1.13 release.

Well, we had same bug in our old implementation (small test helper function based on reflect). It was easy enough to fix it: iterate over struct fields from last to first, add seen map to skip already seen field names, pass current seen map to same (recursive) function in case of Anonymous struct field or pass empty map in case of non-Anonymous struct field.

I don't know exact Go policy about such non-security fixes in patch releases - if policy forbid this then such a fix should go in 1.13 while 1.12.1 should revert added check. But fix is fairly obvious (I suppose go vet should require similar change in algorithm), so maybe it makes sense to allow it for 1.12.1.

This does seem like a valid use of field shadowing with no easy alternative. Daniel, do you have any real-world examples of true-positive reports of this checker that we could use to try to identify a better heuristic? Otherwise we may have to disable the field shadowing check for now.

There are many ways in which adding an annotation or comment mechanism would simplify the task of writing checkers with high precision and recall, but we have historically avoided doing so.

@alandonovan I think there are some true positives which we do want to keep as part of the check. In particular, when the duplicates happen at the same level, so neither "wins" and overrides the other. For example:

https://play.golang.org/p/tcTVhSJ2F2b

In that program, neither field is encoded, even though both are promoted normally. Both start showing up if either json tag is changed. I don't think this could ever be a false positive; if one really wants to skip nested fields, they can add json:"-" to the anonymous struct fields, or they can override both with a third field in the parent which isn't encoded.

I don't think the fix would be trivial, though. And given that the check isn't super important, and we've gotten one false positive wrong already, I think we should revert in 1.12.1. I can submit the non-revert fix to master for 1.13.

@alandonovan what would be the simplest way to apply the revert? The original change was in cmd/vet, and the code has been refactored and moved to another repo, so a clean revert won't work. In order of preference, I would:

  • Add a return right after if field.Anonymous() and remove the relevant tests, disabling the enhancement
  • Manually port the revert to cmd/vendor/x/tools
  • Revert in x/tools, pull only that change to cmd/vendor, and un-revert in x/tools

I forgot that x/tools has a 1.12 branch; we can revert the commit(s) there, and pull the latest 1.12 branch changes into Go's 1.12 branch.

Change https://golang.org/cl/164659 mentions this issue: [release-branch.go1.12] go/analysis: disable embedded struct tag check

Is this behaviour described in the bug an supported use case that should continue to work or a hack that happens to work?

Because if this behaviour is supported, then it seems subtle enough to warrant an example. This would lock down this behaviour both in vet as well as in the encoding/json package.

Closed by merging aa82965741a9fecd12b026fbb3d3c6ed3231b8f8 to release-branch.go1.12.

@nightlyone I'm not sure if this is a documented and intended feature. But like @alandonovan mentioned, it seems like a useful feature to me. And I imagine some users already depend on it, as pointed out by the bugs filed against vet.

Let's use #30846 to keep track of the proper fix for Go 1.13.

@mvdan, go vet is still failing for the same reason in 1.12.1. Looks like the cmd vendor tree hasn't been updated.

@pschultz you're absolutely right. Why was this issue closed? Nothing about this CL was merged into Go's 1.12 release branch. And https://go-review.googlesource.com/c/tools/+/164659/ simply updated this issue, it didn't close it.

Well... let's try again for 1.12.2.

@mvdan, is this still on your radar?

See https://github.com/golang/go/issues/30399#issuecomment-483652435; I did the relevant backports in the tools repo over a month ago. This just needs someone to update the vendored copy of x/tools before doing a 1.12.x release. I'm not needed for that, nor do I know when Go 1.12.5 will happen. I honestly don't know why this trivial amount of work has been getting pushed back since Go 1.12.2.

Change https://golang.org/cl/174519 mentions this issue: [release-branch.go1.12] cmd/vendor/golang.org/x/tools/go/analysis: update from release-branch.go1.12

Change https://golang.org/cl/174520 mentions this issue: [release-branch.go1.12] cmd/vet: add tests for point-release issues

Closing as the cherry-picks have landed.

Was this page helpful?
0 / 5 - 0 ratings