Skip to content

Commit a16dcc0

Browse files
SkarlsoBryan C. Mills
authored and
Bryan C. Mills
committed
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 <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Bryan C. Mills <[email protected]>
1 parent 56b8ee2 commit a16dcc0

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

src/cmd/go/internal/load/pkg.go

+14-3
Original file line numberDiff line numberDiff line change
@@ -1935,8 +1935,12 @@ func Packages(args []string) []*Package {
19351935
// cannot be loaded at all.
19361936
// The packages that fail to load will have p.Error != nil.
19371937
func PackagesAndErrors(patterns []string) []*Package {
1938-
if len(patterns) > 0 && strings.HasSuffix(patterns[0], ".go") {
1939-
return []*Package{GoFilesPackage(patterns)}
1938+
if len(patterns) > 0 {
1939+
for _, p := range patterns {
1940+
if strings.HasSuffix(p, ".go") {
1941+
return []*Package{GoFilesPackage(patterns)}
1942+
}
1943+
}
19401944
}
19411945

19421946
matches := ImportPaths(patterns)
@@ -2048,7 +2052,14 @@ func GoFilesPackage(gofiles []string) *Package {
20482052

20492053
for _, f := range gofiles {
20502054
if !strings.HasSuffix(f, ".go") {
2051-
base.Fatalf("named files must be .go files")
2055+
pkg := new(Package)
2056+
pkg.Internal.Local = true
2057+
pkg.Internal.CmdlineFiles = true
2058+
pkg.Name = f
2059+
pkg.Error = &PackageError{
2060+
Err: fmt.Sprintf("named files must be .go files: %s", pkg.Name),
2061+
}
2062+
return pkg
20522063
}
20532064
}
20542065

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
env GO111MODULE=off
2+
3+
# issue 29899: handling files with non-Go extension
4+
go list -e -test -json -- c.c x.go
5+
stdout '"Err": "named files must be .go files: c.c"'
6+
7+
! go list -test -json -- c.c x.go
8+
stderr 'can''t load package: named files must be .go files: c.c'
9+
10+
-- x.go --
11+
package main
12+
-- c.c --
13+
package c

0 commit comments

Comments
 (0)