golang / go

The Go programming language
https://go.dev
BSD 3-Clause "New" or "Revised" License
122.93k stars 17.52k forks source link

go/build: should not arbitrarily resolve conflicting package names #45999

Open bcmills opened 3 years ago

bcmills commented 3 years ago

https://play.golang.org/p/ZnGePWG7Fqs illustrates (at least) two problems with the go/build API:

  1. When ImportDir is called on a directory containing files with conflicting package declarations, the returned build.Package has its Name field set (arbitrarily!) to the first package-name seen in any of the files, and the InvalidGoFiles listed for the package include only the files with package names other than the arbitrary one. That seems wrong to me: if the package has conflicting names, it isn't up to go/build to decide which of those is “correct” — it should report all of the files as erroneous.

  2. The same file is listed twice in InvalidGoFiles.

This issue is about problem (1). (I will fix problem (2) separately, because it seems trivial.)

CC @jayconrod @matloob @rsc

bcmills commented 3 years ago

(I found this while investigating #45827.)

gopherbot commented 3 years ago

Change https://golang.org/cl/317299 mentions this issue: go/build: avoid duplicates in InvalidGoFiles

gopherbot commented 3 years ago

Change https://golang.org/cl/317300 mentions this issue: cmd/go: include packages with InvalidGoFiles when filtering main packages

bcmills commented 3 years ago

Reopening. (Gopherbot erroneously closed this due to misparsing the commit message in CL 317300; filed as #46081.)