]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go: skip "exclude all Go files" error in fmt
authorDaniel Martí <mvdan@mvdan.cc>
Fri, 3 Nov 2017 18:14:37 +0000 (18:14 +0000)
committerRuss Cox <rsc@golang.org>
Sun, 5 Nov 2017 19:28:13 +0000 (19:28 +0000)
Otherwise, one can't run "go fmt" on a directory containing Go files if
none of them are buildable (e.g. because of build tags). This is
counter-intuitive, as fmt will format all Go files anyway.

If we encounter such a load error, ignore it and carry on. All other
load errors, such as when a package can't be found, should still be
shown to the user.

Add a test for the two kinds of load errors. Use fmt -n so that any
changes to the formatting of the files in testdata don't actually get
applied. The load errors still occur with -n, so the test does its job.

Fixes #22183.

Change-Id: I99d0c0cdd29015b6a3f5286a9bbff50757c78e0d
Reviewed-on: https://go-review.googlesource.com/75930
Run-TryBot: Daniel Martí <mvdan@mvdan.cc>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Russ Cox <rsc@golang.org>
src/cmd/dist/deps.go
src/cmd/go/go_test.go
src/cmd/go/internal/fmtcmd/fmt.go

index ffc3b4788c260f58369ca646bfce0c93c9f92c57..294ca22a7067a46fced50870f3a4a81a262ab952 100644 (file)
@@ -167,6 +167,7 @@ var builddeps = map[string][]string{
                "os",            // cmd/go/internal/fmtcmd
                "path/filepath", // cmd/go/internal/fmtcmd
                "runtime",       // cmd/go/internal/fmtcmd
+               "strings",       // cmd/go/internal/fmtcmd
                "sync",          // cmd/go/internal/fmtcmd
        },
 
index 25cc18fa617e616ed2eea2770e9f463ec22447d4..854de7968ffb433a1168df55faedc035381d49f7 100644 (file)
@@ -4906,3 +4906,11 @@ func TestInstallDeps(t *testing.T) {
        tg.run("install", "-i", "p2")
        tg.mustExist(p1)
 }
+
+func TestFmtLoadErrors(t *testing.T) {
+       tg := testgo(t)
+       defer tg.cleanup()
+       tg.setenv("GOPATH", filepath.Join(tg.pwd(), "testdata"))
+       tg.runFail("fmt", "does-not-exist")
+       tg.run("fmt", "-n", "exclude")
+}
index 2ff6dd5356474a07d9b3e7799f932c8ebed982c7..eb96823fa6aa12f1f005ea6da3dbe4f7100aaec1 100644 (file)
@@ -9,6 +9,7 @@ import (
        "os"
        "path/filepath"
        "runtime"
+       "strings"
        "sync"
 
        "cmd/go/internal/base"
@@ -55,7 +56,16 @@ func runFmt(cmd *base.Command, args []string) {
                        }
                }()
        }
-       for _, pkg := range load.Packages(args) {
+       for _, pkg := range load.PackagesAndErrors(args) {
+               if pkg.Error != nil {
+                       if strings.HasPrefix(pkg.Error.Err, "build constraints exclude all Go files") {
+                               // Skip this error, as we will format
+                               // all files regardless.
+                       } else {
+                               base.Errorf("can't load package: %s", pkg.Error)
+                               continue
+                       }
+               }
                // Use pkg.gofiles instead of pkg.Dir so that
                // the command only applies to this package,
                // not to packages in subdirectories.