[go] cmd/go: skip "exclude all Go files" error in fmt

73 views
Skip to first unread message

Daniel Martí (Gerrit)

unread,
Nov 3, 2017, 2:23:10 PM11/3/17
to Ian Lance Taylor, goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

Daniel Martí has uploaded this change for review.

View Change

cmd/go: skip "exclude all Go files" error in fmt

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
---
M src/cmd/go/go_test.go
M src/cmd/go/internal/fmtcmd/fmt.go
2 files changed, 19 insertions(+), 1 deletion(-)

diff --git a/src/cmd/go/go_test.go b/src/cmd/go/go_test.go
index a4f6452..b593bcc 100644
--- a/src/cmd/go/go_test.go
+++ b/src/cmd/go/go_test.go
@@ -4686,3 +4686,11 @@
t.Fatalf("bad output from compressed go binary:\ngot %q; want %q", out, "hello upx")
}
}
+
+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")
+}
diff --git a/src/cmd/go/internal/fmtcmd/fmt.go b/src/cmd/go/internal/fmtcmd/fmt.go
index 2ff6dd5..eb96823 100644
--- a/src/cmd/go/internal/fmtcmd/fmt.go
+++ b/src/cmd/go/internal/fmtcmd/fmt.go
@@ -9,6 +9,7 @@
"os"
"path/filepath"
"runtime"
+ "strings"
"sync"

"cmd/go/internal/base"
@@ -55,7 +56,16 @@
}
}()
}
- 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.

To view, visit change 75930. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: go
Gerrit-Branch: master
Gerrit-MessageType: newchange
Gerrit-Change-Id: I99d0c0cdd29015b6a3f5286a9bbff50757c78e0d
Gerrit-Change-Number: 75930
Gerrit-PatchSet: 1
Gerrit-Owner: Daniel Martí <mv...@mvdan.cc>
Gerrit-Reviewer: Daniel Martí <mv...@mvdan.cc>

Daniel Martí (Gerrit)

unread,
Nov 3, 2017, 2:24:09 PM11/3/17
to goph...@pubsubhelper.golang.org, Russ Cox, Ian Lance Taylor, golang-co...@googlegroups.com

Whoops, I intended to update the existing CL but forgot to carry over the Change-Id. Will abandon the old one.

View Change

    To view, visit change 75930. To unsubscribe, or for help writing mail filters, visit settings.

    Gerrit-Project: go
    Gerrit-Branch: master
    Gerrit-MessageType: comment
    Gerrit-Change-Id: I99d0c0cdd29015b6a3f5286a9bbff50757c78e0d
    Gerrit-Change-Number: 75930
    Gerrit-PatchSet: 1
    Gerrit-Owner: Daniel Martí <mv...@mvdan.cc>
    Gerrit-Reviewer: Daniel Martí <mv...@mvdan.cc>
    Gerrit-Reviewer: Ian Lance Taylor <ia...@golang.org>
    Gerrit-Reviewer: Russ Cox <r...@golang.org>
    Gerrit-Comment-Date: Fri, 03 Nov 2017 18:24:04 +0000
    Gerrit-HasComments: No
    Gerrit-HasLabels: No

    Gobot Gobot (Gerrit)

    unread,
    Nov 3, 2017, 2:24:36 PM11/3/17
    to Daniel Martí, goph...@pubsubhelper.golang.org, Russ Cox, Ian Lance Taylor, golang-co...@googlegroups.com

    TryBots beginning. Status page: https://farmer.golang.org/try?commit=ed023776

    View Change

      To view, visit change 75930. To unsubscribe, or for help writing mail filters, visit settings.

      Gerrit-Project: go
      Gerrit-Branch: master
      Gerrit-MessageType: comment
      Gerrit-Change-Id: I99d0c0cdd29015b6a3f5286a9bbff50757c78e0d
      Gerrit-Change-Number: 75930
      Gerrit-PatchSet: 1
      Gerrit-Owner: Daniel Martí <mv...@mvdan.cc>
      Gerrit-Reviewer: Daniel Martí <mv...@mvdan.cc>
      Gerrit-Reviewer: Ian Lance Taylor <ia...@golang.org>
      Gerrit-Reviewer: Russ Cox <r...@golang.org>
      Gerrit-CC: Gobot Gobot <go...@golang.org>
      Gerrit-Comment-Date: Fri, 03 Nov 2017 18:24:33 +0000
      Gerrit-HasComments: No
      Gerrit-HasLabels: No

      Gobot Gobot (Gerrit)

      unread,
      Nov 3, 2017, 2:28:18 PM11/3/17
      to Daniel Martí, goph...@pubsubhelper.golang.org, Russ Cox, Ian Lance Taylor, golang-co...@googlegroups.com

      Build is still in progress...
      This change failed on darwin-amd64-10_11:
      See https://storage.googleapis.com/go-build-log/ed023776/darwin-amd64-10_11_09a94e21.log

      Consult https://build.golang.org/ to see whether it's a new failure. Other builds still in progress; subsequent failure notices suppressed until final report.

      View Change

        To view, visit change 75930. To unsubscribe, or for help writing mail filters, visit settings.

        Gerrit-Project: go
        Gerrit-Branch: master
        Gerrit-MessageType: comment
        Gerrit-Change-Id: I99d0c0cdd29015b6a3f5286a9bbff50757c78e0d
        Gerrit-Change-Number: 75930
        Gerrit-PatchSet: 1
        Gerrit-Owner: Daniel Martí <mv...@mvdan.cc>
        Gerrit-Reviewer: Daniel Martí <mv...@mvdan.cc>
        Gerrit-Reviewer: Ian Lance Taylor <ia...@golang.org>
        Gerrit-Reviewer: Russ Cox <r...@golang.org>
        Gerrit-CC: Gobot Gobot <go...@golang.org>
        Gerrit-Comment-Date: Fri, 03 Nov 2017 18:28:16 +0000
        Gerrit-HasComments: No
        Gerrit-HasLabels: No

        Daniel Martí (Gerrit)

        unread,
        Nov 3, 2017, 2:30:29 PM11/3/17
        to goph...@pubsubhelper.golang.org, Gobot Gobot, Russ Cox, Ian Lance Taylor, golang-co...@googlegroups.com

        I see that adding deps to cmd/go isn't trivial. Should I update via mkdeps, or should I find a way to not add strings as a dependency?

        View Change

          To view, visit change 75930. To unsubscribe, or for help writing mail filters, visit settings.

          Gerrit-Project: go
          Gerrit-Branch: master
          Gerrit-MessageType: comment
          Gerrit-Change-Id: I99d0c0cdd29015b6a3f5286a9bbff50757c78e0d
          Gerrit-Change-Number: 75930
          Gerrit-PatchSet: 1
          Gerrit-Owner: Daniel Martí <mv...@mvdan.cc>
          Gerrit-Reviewer: Daniel Martí <mv...@mvdan.cc>
          Gerrit-Reviewer: Ian Lance Taylor <ia...@golang.org>
          Gerrit-Reviewer: Russ Cox <r...@golang.org>
          Gerrit-CC: Gobot Gobot <go...@golang.org>
          Gerrit-Comment-Date: Fri, 03 Nov 2017 18:30:24 +0000
          Gerrit-HasComments: No
          Gerrit-HasLabels: No

          Gobot Gobot (Gerrit)

          unread,
          Nov 3, 2017, 2:40:33 PM11/3/17
          to Daniel Martí, goph...@pubsubhelper.golang.org, Russ Cox, Ian Lance Taylor, golang-co...@googlegroups.com

          6 of 21 TryBots failed:
          Failed on darwin-amd64-10_11: https://storage.googleapis.com/go-build-log/ed023776/darwin-amd64-10_11_09a94e21.log
          Failed on linux-amd64: https://storage.googleapis.com/go-build-log/ed023776/linux-amd64_5166f8bf.log
          Failed on linux-386: https://storage.googleapis.com/go-build-log/ed023776/linux-386_271f425a.log
          Failed on freebsd-amd64-110: https://storage.googleapis.com/go-build-log/ed023776/freebsd-amd64-110_dc5e6ca4.log
          Failed on openbsd-amd64-60: https://storage.googleapis.com/go-build-log/ed023776/openbsd-amd64-60_d6bc89b0.log
          Failed on linux-arm: https://storage.googleapis.com/go-build-log/ed023776/linux-arm_5065dca5.log

          Consult https://build.golang.org/ to see whether they are new failures.

          Patch set 1:TryBot-Result -1

          View Change

            To view, visit change 75930. To unsubscribe, or for help writing mail filters, visit settings.

            Gerrit-Project: go
            Gerrit-Branch: master
            Gerrit-MessageType: comment
            Gerrit-Change-Id: I99d0c0cdd29015b6a3f5286a9bbff50757c78e0d
            Gerrit-Change-Number: 75930
            Gerrit-PatchSet: 1
            Gerrit-Owner: Daniel Martí <mv...@mvdan.cc>
            Gerrit-Reviewer: Daniel Martí <mv...@mvdan.cc>
            Gerrit-Reviewer: Gobot Gobot <go...@golang.org>
            Gerrit-Reviewer: Ian Lance Taylor <ia...@golang.org>
            Gerrit-Reviewer: Russ Cox <r...@golang.org>
            Gerrit-Comment-Date: Fri, 03 Nov 2017 18:40:30 +0000
            Gerrit-HasComments: No
            Gerrit-HasLabels: Yes

            Ian Lance Taylor (Gerrit)

            unread,
            Nov 3, 2017, 2:53:05 PM11/3/17
            to Daniel Martí, goph...@pubsubhelper.golang.org, Gobot Gobot, Russ Cox, golang-co...@googlegroups.com

            This change failed on darwin-amd64-10_11:
            See https://storage.googleapis.com/go-build-log/ed023776/darwin-amd64-10_11_09a94e21.log

            I see that adding deps to cmd/go isn't trivial. Should I update via mkdeps, or should I find a way to not add strings as a dependency?

            Just run mkdeps. There are other dependencies on strings underneath cmd/go, so this is not a problem.

            View Change

              To view, visit change 75930. To unsubscribe, or for help writing mail filters, visit settings.

              Gerrit-Project: go
              Gerrit-Branch: master
              Gerrit-MessageType: comment
              Gerrit-Change-Id: I99d0c0cdd29015b6a3f5286a9bbff50757c78e0d
              Gerrit-Change-Number: 75930
              Gerrit-PatchSet: 1
              Gerrit-Owner: Daniel Martí <mv...@mvdan.cc>
              Gerrit-Reviewer: Daniel Martí <mv...@mvdan.cc>
              Gerrit-Reviewer: Gobot Gobot <go...@golang.org>
              Gerrit-Reviewer: Ian Lance Taylor <ia...@golang.org>
              Gerrit-Reviewer: Russ Cox <r...@golang.org>
              Gerrit-Comment-Date: Fri, 03 Nov 2017 18:53:01 +0000
              Gerrit-HasComments: No
              Gerrit-HasLabels: No

              Daniel Martí (Gerrit)

              unread,
              Nov 4, 2017, 6:19:23 PM11/4/17
              to Russ Cox, Ian Lance Taylor, Gobot Gobot, goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

              Daniel Martí uploaded patch set #2 to this change.

              View Change

              cmd/go: skip "exclude all Go files" error in fmt

              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
              ---
              M src/cmd/dist/deps.go
              M src/cmd/go/go_test.go
              M src/cmd/go/internal/fmtcmd/fmt.go
              3 files changed, 20 insertions(+), 1 deletion(-)

              To view, visit change 75930. To unsubscribe, or for help writing mail filters, visit settings.

              Gerrit-Project: go
              Gerrit-Branch: master
              Gerrit-MessageType: newpatchset
              Gerrit-Change-Id: I99d0c0cdd29015b6a3f5286a9bbff50757c78e0d
              Gerrit-Change-Number: 75930
              Gerrit-PatchSet: 2
              Gerrit-Owner: Daniel Martí <mv...@mvdan.cc>
              Gerrit-Reviewer: Daniel Martí <mv...@mvdan.cc>

              Gobot Gobot (Gerrit)

              unread,
              Nov 4, 2017, 6:19:37 PM11/4/17
              to Daniel Martí, goph...@pubsubhelper.golang.org, Ian Lance Taylor, Russ Cox, golang-co...@googlegroups.com

              TryBots beginning. Status page: https://farmer.golang.org/try?commit=8fa26293

              View Change

                To view, visit change 75930. To unsubscribe, or for help writing mail filters, visit settings.

                Gerrit-Project: go
                Gerrit-Branch: master
                Gerrit-MessageType: comment
                Gerrit-Change-Id: I99d0c0cdd29015b6a3f5286a9bbff50757c78e0d
                Gerrit-Change-Number: 75930
                Gerrit-PatchSet: 2
                Gerrit-Owner: Daniel Martí <mv...@mvdan.cc>
                Gerrit-Reviewer: Daniel Martí <mv...@mvdan.cc>
                Gerrit-Reviewer: Gobot Gobot <go...@golang.org>
                Gerrit-Reviewer: Ian Lance Taylor <ia...@golang.org>
                Gerrit-Reviewer: Russ Cox <r...@golang.org>
                Gerrit-Comment-Date: Sat, 04 Nov 2017 22:19:34 +0000
                Gerrit-HasComments: No
                Gerrit-HasLabels: No

                Daniel Martí (Gerrit)

                unread,
                Nov 4, 2017, 6:19:42 PM11/4/17
                to goph...@pubsubhelper.golang.org, Gobot Gobot, Ian Lance Taylor, Russ Cox, golang-co...@googlegroups.com

                Just run mkdeps. There are other dependencies on strings underneath cmd/go, so this is not a problem.

                Thanks - done and rebased.

                View Change

                  To view, visit change 75930. To unsubscribe, or for help writing mail filters, visit settings.

                  Gerrit-Project: go
                  Gerrit-Branch: master
                  Gerrit-MessageType: comment
                  Gerrit-Change-Id: I99d0c0cdd29015b6a3f5286a9bbff50757c78e0d
                  Gerrit-Change-Number: 75930
                  Gerrit-PatchSet: 2
                  Gerrit-Owner: Daniel Martí <mv...@mvdan.cc>
                  Gerrit-Reviewer: Daniel Martí <mv...@mvdan.cc>
                  Gerrit-Reviewer: Gobot Gobot <go...@golang.org>
                  Gerrit-Reviewer: Ian Lance Taylor <ia...@golang.org>
                  Gerrit-Reviewer: Russ Cox <r...@golang.org>
                  Gerrit-Comment-Date: Sat, 04 Nov 2017 22:19:39 +0000
                  Gerrit-HasComments: No
                  Gerrit-HasLabels: No

                  Gobot Gobot (Gerrit)

                  unread,
                  Nov 4, 2017, 7:15:48 PM11/4/17
                  to Daniel Martí, goph...@pubsubhelper.golang.org, Ian Lance Taylor, Russ Cox, golang-co...@googlegroups.com

                  TryBots are happy.

                  Patch set 2:TryBot-Result +1

                  View Change

                    To view, visit change 75930. To unsubscribe, or for help writing mail filters, visit settings.

                    Gerrit-Project: go
                    Gerrit-Branch: master
                    Gerrit-MessageType: comment
                    Gerrit-Change-Id: I99d0c0cdd29015b6a3f5286a9bbff50757c78e0d
                    Gerrit-Change-Number: 75930
                    Gerrit-PatchSet: 2
                    Gerrit-Owner: Daniel Martí <mv...@mvdan.cc>
                    Gerrit-Reviewer: Daniel Martí <mv...@mvdan.cc>
                    Gerrit-Reviewer: Gobot Gobot <go...@golang.org>
                    Gerrit-Reviewer: Ian Lance Taylor <ia...@golang.org>
                    Gerrit-Reviewer: Russ Cox <r...@golang.org>
                    Gerrit-Comment-Date: Sat, 04 Nov 2017 23:15:45 +0000
                    Gerrit-HasComments: No
                    Gerrit-HasLabels: Yes

                    Russ Cox (Gerrit)

                    unread,
                    Nov 5, 2017, 2:28:12 PM11/5/17
                    to Daniel Martí, goph...@pubsubhelper.golang.org, Russ Cox, Gobot Gobot, Ian Lance Taylor, golang-co...@googlegroups.com

                    Patch set 2:Code-Review +2

                    View Change

                      To view, visit change 75930. To unsubscribe, or for help writing mail filters, visit settings.

                      Gerrit-Project: go
                      Gerrit-Branch: master
                      Gerrit-MessageType: comment
                      Gerrit-Change-Id: I99d0c0cdd29015b6a3f5286a9bbff50757c78e0d
                      Gerrit-Change-Number: 75930
                      Gerrit-PatchSet: 2
                      Gerrit-Owner: Daniel Martí <mv...@mvdan.cc>
                      Gerrit-Reviewer: Daniel Martí <mv...@mvdan.cc>
                      Gerrit-Reviewer: Gobot Gobot <go...@golang.org>
                      Gerrit-Reviewer: Ian Lance Taylor <ia...@golang.org>
                      Gerrit-Reviewer: Russ Cox <r...@golang.org>
                      Gerrit-Comment-Date: Sun, 05 Nov 2017 19:28:09 +0000
                      Gerrit-HasComments: No
                      Gerrit-HasLabels: Yes

                      Russ Cox (Gerrit)

                      unread,
                      Nov 5, 2017, 2:28:17 PM11/5/17
                      to Russ Cox, Daniel Martí, goph...@pubsubhelper.golang.org, golang-...@googlegroups.com, Gobot Gobot, Ian Lance Taylor, golang-co...@googlegroups.com

                      Russ Cox merged this change.

                      View Change

                      Approvals: Russ Cox: Looks good to me, approved Daniel Martí: Run TryBots Gobot Gobot: TryBots succeeded
                      cmd/go: skip "exclude all Go files" error in fmt

                      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í <mv...@mvdan.cc>
                      TryBot-Result: Gobot Gobot <go...@golang.org>
                      Reviewed-by: Russ Cox <r...@golang.org>

                      ---
                      M src/cmd/dist/deps.go
                      M src/cmd/go/go_test.go
                      M src/cmd/go/internal/fmtcmd/fmt.go
                      3 files changed, 20 insertions(+), 1 deletion(-)

                      diff --git a/src/cmd/dist/deps.go b/src/cmd/dist/deps.go
                      index ffc3b47..294ca22 100644
                      --- a/src/cmd/dist/deps.go
                      +++ b/src/cmd/dist/deps.go
                      @@ -167,6 +167,7 @@
                      "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
                      },

                      diff --git a/src/cmd/go/go_test.go b/src/cmd/go/go_test.go
                      index 25cc18f..854de79 100644
                      --- a/src/cmd/go/go_test.go
                      +++ b/src/cmd/go/go_test.go
                      @@ -4906,3 +4906,11 @@
                      tg.run("install", "-i", "p2")
                      tg.mustExist(p1)
                      Gerrit-MessageType: merged
                      Gerrit-Change-Id: I99d0c0cdd29015b6a3f5286a9bbff50757c78e0d
                      Gerrit-Change-Number: 75930
                      Gerrit-PatchSet: 3
                      Gerrit-Owner: Daniel Martí <mv...@mvdan.cc>
                      Gerrit-Reviewer: Daniel Martí <mv...@mvdan.cc>
                      Reply all
                      Reply to author
                      Forward
                      0 new messages