]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go: print each import error only once
authorHaraldNordgren <haraldnordgren@gmail.com>
Sat, 6 Jan 2018 20:30:42 +0000 (21:30 +0100)
committerIan Lance Taylor <iant@golang.org>
Tue, 27 Mar 2018 14:07:14 +0000 (14:07 +0000)
This change prevents import errors from being printed multiple times.
Creating a bare-bones package 'p' with only one file importing itself
and running 'go build p', the current implementation gives this error
message:

can't load package: import cycle not allowed
package p
imports p
import cycle not allowed
package p
imports p

With this change we will show the message only once.

Updates #23295

Change-Id: I653b34c1c06c279f3df514f12ec0b89745a7e64a
Reviewed-on: https://go-review.googlesource.com/86535
Reviewed-by: Harald Nordgren <haraldnordgren@gmail.com>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>

src/cmd/go/go_test.go
src/cmd/go/internal/load/pkg.go
src/cmd/go/testdata/importcycle/src/selfimport/selfimport.go [new file with mode: 0644]

index eef6309a5df4072ae0b52a16441e306eb789e724..b5200335ad257e4738dce3995e3c1721f61d3e9a 100644 (file)
@@ -1359,6 +1359,22 @@ func TestImportCommentConflict(t *testing.T) {
        tg.grepStderr("found import comments", "go build did not mention comment conflict")
 }
 
+func TestImportCycle(t *testing.T) {
+       tg := testgo(t)
+       defer tg.cleanup()
+       tg.parallel()
+       tg.setenv("GOPATH", filepath.Join(tg.pwd(), "testdata/importcycle"))
+       tg.runFail("build", "selfimport")
+
+       count := tg.grepCountBoth("import cycle not allowed")
+       if count == 0 {
+               t.Fatal("go build did not mention cyclical import")
+       }
+       if count > 1 {
+               t.Fatal("go build mentioned import cycle more than once")
+       }
+}
+
 // cmd/go: custom import path checking should not apply to Go packages without import comment.
 func TestIssue10952(t *testing.T) {
        testenv.MustHaveExternalNetwork(t)
index b006d4137c61282cf4c9cd9bdc8c6e66457c6392..0b82fc9f41fa89162c8b3fd107ac1aecbf80ca07 100644 (file)
@@ -1473,6 +1473,7 @@ func PackagesForBuild(args []string) []*Package {
        for _, pkg := range pkgs {
                if pkg.Error != nil {
                        base.Errorf("can't load package: %s", pkg.Error)
+                       printed[pkg.Error] = true
                }
                for _, err := range pkg.DepsErrors {
                        // Since these are errors in dependencies,
diff --git a/src/cmd/go/testdata/importcycle/src/selfimport/selfimport.go b/src/cmd/go/testdata/importcycle/src/selfimport/selfimport.go
new file mode 100644 (file)
index 0000000..dc63c4b
--- /dev/null
@@ -0,0 +1,3 @@
+package selfimport
+
+import "selfimport"