mirror of https://github.com/golang/go.git
cmd/go: report non-Go files as package error
This change modifies cmd/go/list to format the error correctly in case -e flag is set. It also fixes a bug where the package loader was only ever checking the first pattern if it had the go extension. This caused and error when a file without .go extension was not the first argument. Fixes #29899 Change-Id: I029bf4465ad4ad054434b8337c1d2a59369783da Reviewed-on: https://go-review.googlesource.com/c/go/+/166398 Run-TryBot: Bryan C. Mills <bcmills@google.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Bryan C. Mills <bcmills@google.com>
This commit is contained in:
parent
56b8ee2398
commit
a16dcc0052
|
|
@ -1935,9 +1935,13 @@ func Packages(args []string) []*Package {
|
||||||
// cannot be loaded at all.
|
// cannot be loaded at all.
|
||||||
// The packages that fail to load will have p.Error != nil.
|
// The packages that fail to load will have p.Error != nil.
|
||||||
func PackagesAndErrors(patterns []string) []*Package {
|
func PackagesAndErrors(patterns []string) []*Package {
|
||||||
if len(patterns) > 0 && strings.HasSuffix(patterns[0], ".go") {
|
if len(patterns) > 0 {
|
||||||
|
for _, p := range patterns {
|
||||||
|
if strings.HasSuffix(p, ".go") {
|
||||||
return []*Package{GoFilesPackage(patterns)}
|
return []*Package{GoFilesPackage(patterns)}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
matches := ImportPaths(patterns)
|
matches := ImportPaths(patterns)
|
||||||
var (
|
var (
|
||||||
|
|
@ -2048,7 +2052,14 @@ func GoFilesPackage(gofiles []string) *Package {
|
||||||
|
|
||||||
for _, f := range gofiles {
|
for _, f := range gofiles {
|
||||||
if !strings.HasSuffix(f, ".go") {
|
if !strings.HasSuffix(f, ".go") {
|
||||||
base.Fatalf("named files must be .go files")
|
pkg := new(Package)
|
||||||
|
pkg.Internal.Local = true
|
||||||
|
pkg.Internal.CmdlineFiles = true
|
||||||
|
pkg.Name = f
|
||||||
|
pkg.Error = &PackageError{
|
||||||
|
Err: fmt.Sprintf("named files must be .go files: %s", pkg.Name),
|
||||||
|
}
|
||||||
|
return pkg
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,13 @@
|
||||||
|
env GO111MODULE=off
|
||||||
|
|
||||||
|
# issue 29899: handling files with non-Go extension
|
||||||
|
go list -e -test -json -- c.c x.go
|
||||||
|
stdout '"Err": "named files must be .go files: c.c"'
|
||||||
|
|
||||||
|
! go list -test -json -- c.c x.go
|
||||||
|
stderr 'can''t load package: named files must be .go files: c.c'
|
||||||
|
|
||||||
|
-- x.go --
|
||||||
|
package main
|
||||||
|
-- c.c --
|
||||||
|
package c
|
||||||
Loading…
Reference in New Issue