diff --git a/go/packages/doc.go b/go/packages/doc.go index dc0174d3..7e2c221b 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 86ba7b6f..62b28293 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 00000000..19bf3a2c --- /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 e64ee027..d24ff3b2 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 273bf084..d70518c0 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 (