aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlan Donovan <adonovan@google.com>2018-07-13 11:49:34 -0400
committerAlan Donovan <adonovan@google.com>2018-07-13 17:38:21 +0000
commit9d3ae49c73ce10a6e25424c99b6cf42439ebbd88 (patch)
tree9a4f2a2896545a1a518b856fb8ff690e4c37c811
parent2087f8c10712366cfc2f4fcb1bf99eeef61ab21e (diff)
downloadgolang-x-tools-9d3ae49c73ce10a6e25424c99b6cf42439ebbd88.tar.gz
go/packages: report a distinguished ErrGoTooOld error for older go commands
We should add support for older go commands using the multiple-calls approach of earlier drafts of go/packages. Also, tag tests for go1.11 to make 1.10 builder happy. Change-Id: Ia04979528af25cbcd4b4fa5b21cb91d014d530c1 Reviewed-on: https://go-review.googlesource.com/123756 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
-rw-r--r--go/packages/doc.go3
-rw-r--r--go/packages/golist.go12
-rw-r--r--go/packages/packages110_test.go21
-rw-r--r--go/packages/packages_test.go8
-rw-r--r--go/packages/stdlib_test.go2
5 files changed, 44 insertions, 2 deletions
diff --git a/go/packages/doc.go b/go/packages/doc.go
index dc0174d38..7e2c221b4 100644
--- a/go/packages/doc.go
+++ b/go/packages/doc.go
@@ -13,6 +13,9 @@ recursively loading dependencies from source code.
THIS INTERFACE IS EXPERIMENTAL AND IS LIKELY TO CHANGE.
+This package currently requires a go1.11 version of go list;
+its functions will return a GoTooOldError for older toolchains.
+
This package is intended to replace golang.org/x/tools/go/loader.
It provides a simpler interface to the same functionality and serves
as a foundation for analysis tools that work with 'go build',
diff --git a/go/packages/golist.go b/go/packages/golist.go
index 86ba7b6f4..62b282938 100644
--- a/go/packages/golist.go
+++ b/go/packages/golist.go
@@ -1,3 +1,7 @@
+// Copyright 2018 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
package packages
// This file defines the "go list" implementation of the Packages metadata query.
@@ -13,6 +17,12 @@ import (
"strings"
)
+// A GoTooOldError indicates that the go command predates the Go
+// 1.11 features needed by this package. This error is a stopgap measure
+// until the necessary features can be emulated in terms of an older go
+// command, at which point this error will no longer be used.
+type GoTooOldError struct{ error }
+
// golistPackages uses the "go list" command to expand the
// pattern words and return metadata for the specified packages.
func golistPackages(ctx context.Context, gopath string, cgo, export bool, words []string) ([]*Package, error) {
@@ -201,7 +211,7 @@ func golist(ctx context.Context, gopath string, cgo, export bool, args []string)
// Old go list?
if strings.Contains(fmt.Sprint(cmd.Stderr), "flag provided but not defined") {
- return nil, fmt.Errorf("unsupported version of go list: %s: %s", exitErr, cmd.Stderr)
+ return nil, GoTooOldError{fmt.Errorf("unsupported version of go list: %s: %s", exitErr, cmd.Stderr)}
}
// Export mode entails a build.
diff --git a/go/packages/packages110_test.go b/go/packages/packages110_test.go
new file mode 100644
index 000000000..19bf3a2c9
--- /dev/null
+++ b/go/packages/packages110_test.go
@@ -0,0 +1,21 @@
+// Copyright 2018 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+// +build !go1.11
+
+package packages_test
+
+import (
+ "testing"
+
+ "golang.org/x/tools/go/packages"
+)
+
+func TestGoIsTooOld(t *testing.T) {
+ _, err := packages.Metadata(nil, "errors")
+
+ if _, ok := err.(packages.GoTooOldError); !ok {
+ t.Fatalf("using go/packages with pre-Go 1.11 go: err=%v, want ErrGoTooOld", err)
+ }
+}
diff --git a/go/packages/packages_test.go b/go/packages/packages_test.go
index e64ee027a..d24ff3b25 100644
--- a/go/packages/packages_test.go
+++ b/go/packages/packages_test.go
@@ -1,3 +1,9 @@
+// Copyright 2018 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+// +build go1.11
+
package packages_test
import (
@@ -44,7 +50,7 @@ func TestMetadataImportGraph(t *testing.T) {
"src/a/a.go": `package a; const A = 1`,
"src/b/b.go": `package b; import ("a"; _ "errors"); var B = a.A`,
"src/c/c.go": `package c; import (_ "b"; _ "unsafe")`,
- "src/c/c2.go": "//+build ignore\n\n" + `package c; import _ "fmt"`,
+ "src/c/c2.go": "// +build ignore\n\n" + `package c; import _ "fmt"`,
"src/subdir/d/d.go": `package d`,
"src/subdir/d/d_test.go": `package d; import _ "math/bits"`,
"src/subdir/d/x_test.go": `package d_test; import _ "subdir/d"`, // TODO(adonovan): test bad import here
diff --git a/go/packages/stdlib_test.go b/go/packages/stdlib_test.go
index 273bf0845..d70518c00 100644
--- a/go/packages/stdlib_test.go
+++ b/go/packages/stdlib_test.go
@@ -2,6 +2,8 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
+// +build go1.11
+
package packages_test
import (