Commit a16dcc00 authored by Gergely Brautigam's avatar Gergely Brautigam Committed by Bryan C. Mills

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: default avatarBryan C. Mills <bcmills@google.com>
parent 56b8ee23
......@@ -1935,8 +1935,12 @@ func Packages(args []string) []*Package {
// cannot be loaded at all.
// The packages that fail to load will have p.Error != nil.
func PackagesAndErrors(patterns []string) []*Package {
if len(patterns) > 0 && strings.HasSuffix(patterns[0], ".go") {
return []*Package{GoFilesPackage(patterns)}
if len(patterns) > 0 {
for _, p := range patterns {
if strings.HasSuffix(p, ".go") {
return []*Package{GoFilesPackage(patterns)}
}
}
}
matches := ImportPaths(patterns)
......@@ -2048,7 +2052,14 @@ func GoFilesPackage(gofiles []string) *Package {
for _, f := range gofiles {
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
}
}
......
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
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment