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: redeclaration errors when GOFLAGS includes a tag for a different GOOS #64179

Open
powellnorma opened this issue Nov 15, 2023 · 12 comments
Labels
NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@powellnorma
Copy link

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

go1.21.4 linux/amd64

Does this issue reproduce with the latest release?

Yes

What did you do?

main.go:

package main

import "fmt"

func main() {
	fmt.Printf("Hello %s\n")
}
$ go vet main.go  # works fine
# command-line-arguments
./main.go:8:2: fmt.Printf format %s reads arg #1, but call has 0 args

$ GOFLAGS=-tags=windows go vet main.go  # internal warnings, but nothing for main.go
# internal/goos
/usr/local/go/src/internal/goos/zgoos_windows.go:7:7: GOOS redeclared in this block
	/usr/local/go/src/internal/goos/zgoos_linux.go:7:7: other declaration of GOOS
/usr/local/go/src/internal/goos/zgoos_windows.go:9:7: IsAix redeclared in this block
	/usr/local/go/src/internal/goos/zgoos_linux.go:9:7: other declaration of IsAix
/usr/local/go/src/internal/goos/zgoos_windows.go:10:7: IsAndroid redeclared in this block
	/usr/local/go/src/internal/goos/zgoos_linux.go:10:7: other declaration of IsAndroid
/usr/local/go/src/internal/goos/zgoos_windows.go:11:7: IsDarwin redeclared in this block
	/usr/local/go/src/internal/goos/zgoos_linux.go:11:7: other declaration of IsDarwin
/usr/local/go/src/internal/goos/zgoos_windows.go:12:7: IsDragonfly redeclared in this block
	/usr/local/go/src/internal/goos/zgoos_linux.go:12:7: other declaration of IsDragonfly
/usr/local/go/src/internal/goos/zgoos_windows.go:13:7: IsFreebsd redeclared in this block
	/usr/local/go/src/internal/goos/zgoos_linux.go:13:7: other declaration of IsFreebsd
/usr/local/go/src/internal/goos/zgoos_windows.go:14:7: IsHurd redeclared in this block
	/usr/local/go/src/internal/goos/zgoos_linux.go:14:7: other declaration of IsHurd
/usr/local/go/src/internal/goos/zgoos_windows.go:15:7: IsIllumos redeclared in this block
	/usr/local/go/src/internal/goos/zgoos_linux.go:15:7: other declaration of IsIllumos
/usr/local/go/src/internal/goos/zgoos_windows.go:16:7: IsIos redeclared in this block
	/usr/local/go/src/internal/goos/zgoos_linux.go:16:7: other declaration of IsIos
/usr/local/go/src/internal/goos/zgoos_windows.go:17:7: IsJs redeclared in this block
	/usr/local/go/src/internal/goos/zgoos_linux.go:17:7: other declaration of IsJs
/usr/local/go/src/internal/goos/zgoos_windows.go:17:7: too many errors

What did you expect to see?

The same output as before (when no GOFLAGS was used)

@powellnorma
Copy link
Author

Just found this one: #45488 - Are there any workarounds?

@bcmills
Copy link
Contributor

bcmills commented Nov 15, 2023

@powellnorma, don't set tags in GOFLAGS that overlap with GOOS tags? 😅

@bcmills bcmills changed the title cmd/vet: Doesn't work when GOFLAGS is used cmd/vet: redeclaration errors when GOFLAGS includes a tag for a different GOOS Nov 15, 2023
@bcmills
Copy link
Contributor

bcmills commented Nov 15, 2023

I'm not sure that this is really a bug in cmd/vet per se. GOFLAGS=-tags=windows specifies that files with //go:build windows should be included in packages, but your GOOS is set to linux, so vet is trying to analyze packages loaded from files for two different (and mutually exclusive) configurations.

(CC @matloob @timothy-king per https://dev.golang.org/owners)

@bcmills bcmills added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Nov 15, 2023
@bcmills bcmills added this to the Backlog milestone Nov 15, 2023
@powellnorma
Copy link
Author

don't set tags in GOFLAGS that overlap with GOOS tags?

Ok that helps, thank you - This works as expected: GOOS=windows go vet main.go

Fyi I didn't have a specific reason to not use GOOS, I just copied it from #29202 (comment)

Perhaps a warning could be displayed when tags are used that don't match the GOOS?

@raghvenders
Copy link
Contributor

@bcmills @powellnorma - I have encountered the same issue yesterday go vet -tags=linux # internal/goos
and the same issue happens when you do go vet -tags=arm64 # internal/goarch

..\..\golang\go\src\internal\goarch\goarch_arm64.go:8:2: _ArchFamily redeclared in this block
        ..\..\golang\go\src\internal\goarch\goarch_amd64.go:8:2: other declaration of _ArchFamily
..\..\golang\go\src\internal\goarch\goarch_arm64.go:9:2: _DefaultPhysPageSize redeclared in this block
        ..\..\golang\go\src\internal\goarch\goarch_amd64.go:9:2: other declaration of _DefaultPhysPageSize
..\..\golang\go\src\internal\goarch\goarch_arm64.go:10:2: _PCQuantum redeclared in this block
        ..\..\golang\go\src\internal\goarch\goarch_amd64.go:10:2: other declaration of _PCQuantum
..\..\golang\go\src\internal\goarch\goarch_arm64.go:11:2: _MinFrameSize redeclared in this block
        ..\..\golang\go\src\internal\goarch\goarch_amd64.go:11:2: other declaration of _MinFrameSize
..\..\golang\go\src\internal\goarch\goarch_arm64.go:12:2: _StackAlign redeclared in this block
        ..\..\golang\go\src\internal\goarch\goarch_amd64.go:12:2: other declaration of _StackAlign
..\..\golang\go\src\internal\goarch\zgoarch_arm64.go:7:7: GOARCH redeclared in this block
        ..\..\golang\go\src\internal\goarch\zgoarch_amd64.go:7:7: other declaration of GOARCH
..\..\golang\go\src\internal\goarch\zgoarch_arm64.go:9:7: Is386 redeclared in this block
        ..\..\golang\go\src\internal\goarch\zgoarch_amd64.go:9:7: other declaration of Is386
..\..\golang\go\src\internal\goarch\zgoarch_arm64.go:10:7: IsAmd64 redeclared in this block
        ..\..\golang\go\src\internal\goarch\zgoarch_amd64.go:10:7: other declaration of IsAmd64
..\..\golang\go\src\internal\goarch\zgoarch_arm64.go:11:7: IsAmd64p32 redeclared in this block
        ..\..\golang\go\src\internal\goarch\zgoarch_amd64.go:11:7: other declaration of IsAmd64p32
..\..\golang\go\src\internal\goarch\zgoarch_arm64.go:12:7: IsArm redeclared in this block
        ..\..\golang\go\src\internal\goarch\zgoarch_amd64.go:12:7: other declaration of IsArm
..\..\golang\go\src\internal\goarch\zgoarch_arm64.go:12:7: too many errors

@matloob @timothy-king - I tried to run vet_test.go -> TestTags -> (c *Cmd) CombinedOutput() -> c.Stderr. is this stack data pushed into error?

@timothy-king
Copy link
Contributor

go vet -tags=arm64 # internal/goarch

GOARCH=arm64 gotip vet should work as a short term solution. A longer term solution will take some investigation.

@raghvenders
Copy link
Contributor

Thanks @timothy-king . I have done investigation and started a fix for that. It is not just for vet , impacts for build too. So testing more scenarios and here is the CL - https://go-review.googlesource.com/c/go/+/543675

@gopherbot
Copy link

Change https://go.dev/cl/543675 mentions this issue: cmd/vet: Fix for not including GOOS/GOARCH or both go files based on

@raghvenders
Copy link
Contributor

@matloob

Issue Description :
When we give build flags with tags as one or more of the already existing Go variable values like GOOS and GOARCH,
go command process tries to include the file with the tag name like file_.go along with respective platform based files from internal/goos or internal/goarch.
This leads to conflicting constants in those go files which are generated by go generate.

For Example :

go vet -tags=linux main.go in darwin.

In this case, since we are doing that in darwin - internal/goos/zgoos_darwin.go is included and the command susequently includes the file mentioned in the tag //go:build linux, 
which is nothing but zgoos_linux.go. Both of these files have common available global contants. That gives the error message in the build process  - _**redeclared in this block**_ 

This issue is not limited to just GOOS for a combination of OS. This can happen to multiple combinations across OS platform. The same issue is possible for using 
GOARCH variable as a build tags for different architectures.

Uknown: This is pretty much possible for other Go variable which may need to be investigated.

Impact: The same issue happens in go build process and leads to build failure.
The error message is unclear and doesn't point towards the fix.

Fix Approaches:

  1. Identify the conflicting tags against Go variable tags and error out notifying the user to fix it - This is pretty safe approach.

  2. Fixing it for higher level for go vet and go build command (Have separate fixes by ignoring the conflicting tags) and safely run the command - Independent handling and flexibility to change(Feels like duplicate effort).

3. Fixing at build.go as to ignore the conflicting tag files(Based on OS/ARCH currently) in all associated build process- This needs careful testing as almost all go command uses this file and common fix for all. (Preferred approach) . We can grab known OS or ARCH from a common file which can be used here for ignoring respective tags.

@matloob
Copy link
Contributor

matloob commented Dec 11, 2023

I agree with @bcmills that this doesn't seem to be a bug.

@raghvenders
Copy link
Contributor

raghvenders commented Dec 11, 2023

It is expected not to include both os files under internal/goos for windows and linux. But it does(Pls correct my understanding). This happens for go build too.
So we have to have a way to ignore conflicting env variable values and the same used as flag. This is what I am thinking.

@bcmills - your thoughts.

@bcmills
Copy link
Contributor

bcmills commented Dec 11, 2023

@raghvenders, we do not expect Go users to set -tags=windows explicitly, since that tag is reserved to mean GOOS=windows.

(There may be some tools — such as static analyzers — that set it explicitly in go list to get additional files, but those tools do not expect to also run the Go compiler in that configuration.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

6 participants