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: shadow false positive variable/type declaration in package block #18018

Closed
meomap opened this issue Nov 22, 2016 · 5 comments

Comments

Projects
None yet
4 participants
@meomap
Copy link

commented Nov 22, 2016

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

go version go1.7.3 darwin/amd64

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

GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/meomap/testlinter"
GORACE=""
GOROOT="/usr/local/go"
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"
CC="clang"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/6f/tq_z_9xs71vg9wlv5yg8g_lm0000gn/T/go-build230941075=/tmp/go-build -gno-record-gcc-switches -fno-common"
CXX="clang++"
CGO_ENABLED="1"

What did you do?

I run go tool vet --shadow on top repository of 3 packages, each has a main.go file:

// `a/main.go`
package main

var foo error

func main() {
}
// `b/main.go`
package main

import (
        "errors"
        "fmt"
)

func main() {
        foo := errors.New("meomeo")
        fmt.Println(foo)
}
// `c/main.go`
package main

import "errors"

type foo error

func newFoo(msg string) foo {
        return foo(errors.New(msg))
}

func main() {
}

vet command

$ go tool vet --shadow a/main.go b/main.go c/main.go 

What did you expect to see?

no vet error, same output as

$ go tool vet --shadow .

What did you see instead?

b/main.go:9: declaration of "foo" shadows declaration at a/main.go:3

but when I run same vet on a/main.go and b/main.go only, no vet error

$ go tool vet --shadow a/main.go b/main.go 

or change order of arguments input, no vet error

$ go tool vet --shadow c/main.go b/main.go a/main.go
@mvdan

This comment has been minimized.

Copy link
Member

commented Nov 22, 2016

Note that go help packages says:

As a special case, if the package list is a list of .go files from a
single directory, the command is applied to a single synthesized
package made up of exactly those files, ignoring any build constraints
in those files and ignoring any other files in the directory.

This behaviour is still confusing and perhaps a bug, but I would also say that you are not using the tool properly by specifying go files from different packages. That merges them into a single package. Have you tried ./...?

@mvdan

This comment has been minimized.

Copy link
Member

commented Nov 22, 2016

I just realised that it says from a single directory. I wonder if that means that it treats all the files as if they were from the same directory, or if it does a separate run for each different directory. I'm betting on the former.

@robpike

This comment has been minimized.

Copy link
Contributor

commented Nov 22, 2016

There is no expectation that any of the go tools will work properly when presented with a single directory containing multiple packages.

@robpike robpike closed this Nov 22, 2016

@meomap

This comment has been minimized.

Copy link
Author

commented Nov 23, 2016

@mvdan actually go vet can run with . only. Anyway thanks for sharing that package usage note

@mvdan

This comment has been minimized.

Copy link
Member

commented Nov 23, 2016

@meomap like any other go foo tool, go vet ./... works just fine here. Make sure your GOPATH is properly set up and you're in it.

@golang golang locked and limited conversation to collaborators Nov 23, 2017

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
You can’t perform that action at this time.