]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go: build: print import errors when invoked on files
authorKyle Lemons <kyle@kylelemons.net>
Mon, 6 Feb 2012 03:10:03 +0000 (14:10 +1100)
committerRob Pike <r@golang.org>
Mon, 6 Feb 2012 03:10:03 +0000 (14:10 +1100)
      This fix makes the goFilesPackage helper function print the errors from
      package imports and exit similar to how the packagesForBuild function does.

      Without this change, when invoking "go build *.go" with, for example,
      an old import path, the following stack trace is generated:

      panic: runtime error: invalid memory address or nil pointer dereference

      goroutine 1 [running]:
      go/build.(*Tree).PkgDir(...)
              /opt/go/src/pkg/go/build/path.go:52 +0xfb
      main.(*builder).action(...)
              /opt/go/src/cmd/go/build.go:327 +0xb8
      main.(*builder).action(...)
              /opt/go/src/cmd/go/build.go:335 +0x208
      main.runBuild(...)
              /opt/go/src/cmd/go/build.go:129 +0x386
      main.main()
              /opt/go/src/cmd/go/main.go:126 +0x2d8

Fixes #2865.

R=rsc, dvyukov, r
CC=golang-dev
https://golang.org/cl/5624052

src/cmd/go/build.go

index bad37df21c5cc48eee59e8560fb8b30277147909..f6f8de56061e89b3d164df5b6acf28315c59cdf4 100644 (file)
@@ -304,6 +304,17 @@ func goFilesPackage(gofiles []string, target string) *Package {
        if pkg.Error != nil {
                fatalf("%s", pkg.Error)
        }
+       printed := map[error]bool{}
+       for _, err := range pkg.DepsErrors {
+               // Since these are errors in dependencies,
+               // the same error might show up multiple times,
+               // once in each package that depends on it.
+               // Only print each once.
+               if !printed[err] {
+                       printed[err] = true
+                       errorf("%s", err)
+               }
+       }
        if target != "" {
                pkg.target = target
        } else if pkg.Name == "main" {
@@ -312,6 +323,7 @@ func goFilesPackage(gofiles []string, target string) *Package {
                pkg.target = pkg.Name + ".a"
        }
        pkg.ImportPath = "_/" + pkg.target
+       exitIfErrors()
        return pkg
 }