]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go: report non-Go files as package error
authorGergely Brautigam <skarlso777@gmail.com>
Sat, 9 Mar 2019 12:28:50 +0000 (13:28 +0100)
committerBryan C. Mills <bcmills@google.com>
Tue, 16 Apr 2019 18:20:43 +0000 (18:20 +0000)
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>
src/cmd/go/internal/load/pkg.go
src/cmd/go/testdata/script/list_test_non_go_files.txt [new file with mode: 0644]

index 6d3a2972a1e1207acb0f367514cc5b7132ae9da5..68acb96a809d89a5ce22ecebf8da2d78de4cc291 100644 (file)
@@ -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
                }
        }
 
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 (file)
index 0000000..16b98f4
--- /dev/null
@@ -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