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>
diff --git a/src/cmd/go/internal/load/pkg.go b/src/cmd/go/internal/load/pkg.go
index 6d3a297..68acb96 100644
--- a/src/cmd/go/internal/load/pkg.go
+++ b/src/cmd/go/internal/load/pkg.go
@@ -1935,8 +1935,12 @@
// 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 @@
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
}
}
diff --git a/src/cmd/go/testdata/script/list_test_non_go_files.txt b/src/cmd/go/testdata/script/list_test_non_go_files.txt
new file mode 100644
index 0000000..16b98f4
--- /dev/null
+++ b/src/cmd/go/testdata/script/list_test_non_go_files.txt
@@ -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