diff options
| author | Gergely Brautigam <skarlso777@gmail.com> | 2019-03-09 13:28:50 +0100 |
|---|---|---|
| committer | Bryan C. Mills <bcmills@google.com> | 2019-04-16 18:20:43 +0000 |
| commit | a16dcc00526dbb5ff411004c987a4182a8d68e7c (patch) | |
| tree | 668353457c6d87be8825565ccf2d6bf02be2c90a /src | |
| parent | 56b8ee23986c48ec63a0411f12b3fcaad61d6c06 (diff) | |
| download | go-a16dcc00526dbb5ff411004c987a4182a8d68e7c.tar.xz | |
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>
Diffstat (limited to 'src')
| -rw-r--r-- | src/cmd/go/internal/load/pkg.go | 17 | ||||
| -rw-r--r-- | src/cmd/go/testdata/script/list_test_non_go_files.txt | 13 |
2 files changed, 27 insertions, 3 deletions
diff --git a/src/cmd/go/internal/load/pkg.go b/src/cmd/go/internal/load/pkg.go index 6d3a2972a1..68acb96a80 100644 --- a/src/cmd/go/internal/load/pkg.go +++ b/src/cmd/go/internal/load/pkg.go @@ -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 index 0000000000..16b98f4a37 --- /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 |
