diff --git a/go/buildutil/buildutil_go15.go b/go/buildutil/buildutil_go15.go deleted file mode 100644 index d1640b14..00000000 --- a/go/buildutil/buildutil_go15.go +++ /dev/null @@ -1,14 +0,0 @@ -// Copyright 2015 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.6 - -package buildutil - -import "go/build" - -// AllowVendor is a synonym for zero. -// It allows applications to refer to the go/build.AllowVendor -// feature whether or not it is supported. -const AllowVendor build.ImportMode = 0 diff --git a/go/buildutil/buildutil_go16.go b/go/buildutil/buildutil_go16.go deleted file mode 100644 index c5703d72..00000000 --- a/go/buildutil/buildutil_go16.go +++ /dev/null @@ -1,14 +0,0 @@ -// Copyright 2015 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.6 - -package buildutil - -import "go/build" - -// AllowVendor is a synonym for go/build.AllowVendor. -// It allows applications to refer to the AllowVendor -// feature whether or not it is supported. -const AllowVendor build.ImportMode = build.AllowVendor diff --git a/go/buildutil/go16_test.go b/go/buildutil/go16_test.go new file mode 100644 index 00000000..7bb889ed --- /dev/null +++ b/go/buildutil/go16_test.go @@ -0,0 +1,14 @@ +// Copyright 2014 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. + +// Incomplete source tree on Android. + +// +build !android +// +build go1.6 + +package buildutil_test + +func init() { + go16 = true +} diff --git a/go/buildutil/util.go b/go/buildutil/util.go index c4bdd29f..0e093fc0 100644 --- a/go/buildutil/util.go +++ b/go/buildutil/util.go @@ -90,15 +90,6 @@ func dirHasPrefix(dir, prefix string) bool { return len(dir) >= len(prefix) && strings.EqualFold(dir[:len(prefix)], prefix) } -// StripVendor removes the "vendor" segment and all preceding ones -// from a slash-segmented path. (See go/build.AllowVendor.) -func StripVendor(path string) string { - if i := strings.LastIndex(path, "/vendor/"); i >= 0 { - return path[i+len("/vendor/"):] - } - return strings.TrimPrefix(path, "vendor/") -} - // -- Effective methods of file system interface ------------------------- // (go/build.Context defines these as methods, but does not export them.) diff --git a/go/buildutil/util_test.go b/go/buildutil/util_test.go index ee35403c..295e2918 100644 --- a/go/buildutil/util_test.go +++ b/go/buildutil/util_test.go @@ -18,6 +18,8 @@ import ( "golang.org/x/tools/go/buildutil" ) +var go16 bool // Go version >= go1.6 + func TestContainingPackage(t *testing.T) { // unvirtualized: goroot := runtime.GOROOT() @@ -31,7 +33,7 @@ func TestContainingPackage(t *testing.T) { "golang.org/x/tools/go/buildutil"}, } // TODO(adonovan): simplify after Go 1.6. - if buildutil.AllowVendor != 0 { + if go16 { tests = append(tests, [2]string{ gopath + "/src/vendor/golang.org/x/net/http2/hpack/hpack.go", "vendor/golang.org/x/net/http2/hpack", @@ -51,22 +53,3 @@ func TestContainingPackage(t *testing.T) { // TODO(adonovan): test on virtualized GOPATH too. } - -func TestStripVendor(t *testing.T) { - for _, test := range []struct { - path, want string - }{ - {"", ""}, - {"a", "a"}, - {"a/b", "a/b"}, - {"a/vendor/b", "b"}, - {"a/b/vendor/c/d", "c/d"}, - {"vendor/a/b", "a/b"}, - {"a/vendor", "a/vendor"}, - } { - if got := buildutil.StripVendor(test.path); got != test.want { - t.Errorf("StripVendor(%q) = %q, want %q", - test.path, got, test.want) - } - } -} diff --git a/go/loader/go16.go b/go/loader/go16.go new file mode 100644 index 00000000..c0ed50f4 --- /dev/null +++ b/go/loader/go16.go @@ -0,0 +1,13 @@ +// Copyright 2013 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.6 + +package loader + +import "go/build" + +func init() { + ignoreVendor = build.IgnoreVendor +} diff --git a/go/loader/go16_test.go b/go/loader/go16_test.go new file mode 100644 index 00000000..71f89b2d --- /dev/null +++ b/go/loader/go16_test.go @@ -0,0 +1,12 @@ +// Copyright 2013 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.6 +// +build !android + +package loader_test + +func init() { + go16 = true +} diff --git a/go/loader/loader.go b/go/loader/loader.go index 41d5957d..66713525 100644 --- a/go/loader/loader.go +++ b/go/loader/loader.go @@ -23,9 +23,10 @@ import ( "time" "golang.org/x/tools/go/ast/astutil" - "golang.org/x/tools/go/buildutil" ) +var ignoreVendor build.ImportMode + const trace = false // show timing info for type-checking // Config specifies the configuration for loading a whole program from @@ -503,7 +504,7 @@ func (conf *Config) Load() (*Program, error) { // Load the initially imported packages and their dependencies, // in parallel. // No vendor check on packages imported from the command line. - infos, importErrors := imp.importAll("", conf.Cwd, conf.ImportPkgs, 0) + infos, importErrors := imp.importAll("", conf.Cwd, conf.ImportPkgs, ignoreVendor) for _, ie := range importErrors { conf.TypeChecker.Error(ie.err) // failed to create package errpkgs = append(errpkgs, ie.path) @@ -521,7 +522,7 @@ func (conf *Config) Load() (*Program, error) { } // No vendor check on packages imported from command line. - bp, err := imp.findPackage(importPath, conf.Cwd, 0) + bp, err := imp.findPackage(importPath, conf.Cwd, ignoreVendor) if err != nil { // Package not found, or can't even parse package declaration. // Already reported by previous loop; ignore it. @@ -768,7 +769,7 @@ func (imp *importer) doImport(from *PackageInfo, to string) (*types.Package, err from.Pkg.Path()) } - bp, err := imp.findPackage(to, from.dir, buildutil.AllowVendor) + bp, err := imp.findPackage(to, from.dir, 0) if err != nil { return nil, err } @@ -966,7 +967,7 @@ func (imp *importer) addFiles(info *PackageInfo, files []*ast.File, cycleCheck b } // TODO(adonovan): opt: make the caller do scanImports. // Callers with a build.Package can skip it. - imp.importAll(fromPath, info.dir, scanImports(files), buildutil.AllowVendor) + imp.importAll(fromPath, info.dir, scanImports(files), 0) if trace { fmt.Fprintf(os.Stderr, "%s: start %q (%d)\n", diff --git a/go/loader/loader14.go b/go/loader/loader14.go index ba71a67a..836ab92a 100644 --- a/go/loader/loader14.go +++ b/go/loader/loader14.go @@ -22,7 +22,6 @@ import ( "time" "golang.org/x/tools/go/ast/astutil" - "golang.org/x/tools/go/buildutil" "golang.org/x/tools/go/types" ) @@ -502,7 +501,6 @@ func (conf *Config) Load() (*Program, error) { // Load the initially imported packages and their dependencies, // in parallel. - // No vendor check on packages imported from the command line. infos, importErrors := imp.importAll("", conf.Cwd, conf.ImportPkgs, 0) for _, ie := range importErrors { conf.TypeChecker.Error(ie.err) // failed to create package @@ -520,7 +518,6 @@ func (conf *Config) Load() (*Program, error) { continue } - // No vendor check on packages imported from command line. bp, err := imp.findPackage(importPath, conf.Cwd, 0) if err != nil { // Package not found, or can't even parse package declaration. @@ -768,7 +765,7 @@ func (imp *importer) doImport(from *PackageInfo, to string) (*types.Package, err from.Pkg.Path()) } - bp, err := imp.findPackage(to, from.dir, buildutil.AllowVendor) + bp, err := imp.findPackage(to, from.dir, 0) if err != nil { return nil, err } @@ -966,7 +963,7 @@ func (imp *importer) addFiles(info *PackageInfo, files []*ast.File, cycleCheck b } // TODO(adonovan): opt: make the caller do scanImports. // Callers with a build.Package can skip it. - imp.importAll(fromPath, info.dir, scanImports(files), buildutil.AllowVendor) + imp.importAll(fromPath, info.dir, scanImports(files), 0) if trace { fmt.Fprintf(os.Stderr, "%s: start %q (%d)\n", diff --git a/go/loader/loader_test.go b/go/loader/loader_test.go index 10e8bec8..197a66f1 100644 --- a/go/loader/loader_test.go +++ b/go/loader/loader_test.go @@ -24,6 +24,8 @@ import ( "golang.org/x/tools/go/loader" ) +var go16 bool // Go version >= go1.6 + // TestFromArgs checks that conf.FromArgs populates conf correctly. // It does no I/O. func TestFromArgs(t *testing.T) { @@ -396,7 +398,7 @@ func TestCwd(t *testing.T) { } func TestLoad_vendor(t *testing.T) { - if buildutil.AllowVendor == 0 { + if !go16 { // Vendoring requires Go 1.6. // TODO(adonovan): delete in due course. t.Skip() @@ -434,7 +436,7 @@ func TestLoad_vendor(t *testing.T) { } func TestVendorCwd(t *testing.T) { - if buildutil.AllowVendor == 0 { + if !go16 { // Vendoring requires Go 1.6. // TODO(adonovan): delete in due course. t.Skip() diff --git a/refactor/importgraph/go16_test.go b/refactor/importgraph/go16_test.go new file mode 100644 index 00000000..4c0c6939 --- /dev/null +++ b/refactor/importgraph/go16_test.go @@ -0,0 +1,12 @@ +// Copyright 2015 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 !android +// +build go1.6 + +package importgraph_test + +func init() { + go16 = true +} diff --git a/refactor/importgraph/graph.go b/refactor/importgraph/graph.go index 7afb1807..1fc07811 100644 --- a/refactor/importgraph/graph.go +++ b/refactor/importgraph/graph.go @@ -82,7 +82,7 @@ func Build(ctxt *build.Context) (forward, reverse Graph, errors map[string]error defer wg.Done() sema <- 1 - bp, err := ctxt.Import(path, "", buildutil.AllowVendor) + bp, err := ctxt.Import(path, "", 0) <-sema if err != nil { @@ -105,25 +105,22 @@ func Build(ctxt *build.Context) (forward, reverse Graph, errors map[string]error // 840ms: nonblocking cache with duplicate suppression // 340ms: original code (no vendor check) // TODO(adonovan): optimize, somehow. - absolutize := func(path string) string { return path } - if buildutil.AllowVendor != 0 { - memo := make(map[string]string) - absolutize = func(path string) string { - canon, ok := memo[path] - if !ok { - sema <- 1 - bp2, _ := ctxt.Import(path, bp.Dir, build.FindOnly|buildutil.AllowVendor) - <-sema + memo := make(map[string]string) + absolutize := func(path string) string { + canon, ok := memo[path] + if !ok { + sema <- 1 + bp2, _ := ctxt.Import(path, bp.Dir, build.FindOnly) + <-sema - if bp2 != nil { - canon = bp2.ImportPath - } else { - canon = path - } - memo[path] = canon + if bp2 != nil { + canon = bp2.ImportPath + } else { + canon = path } - return canon + memo[path] = canon } + return canon } if bp != nil { diff --git a/refactor/importgraph/graph_test.go b/refactor/importgraph/graph_test.go index 595ecdb6..21c845d3 100644 --- a/refactor/importgraph/graph_test.go +++ b/refactor/importgraph/graph_test.go @@ -15,7 +15,6 @@ import ( "strings" "testing" - "golang.org/x/tools/go/buildutil" "golang.org/x/tools/refactor/importgraph" _ "crypto/hmac" // just for test, below @@ -23,6 +22,8 @@ import ( const this = "golang.org/x/tools/refactor/importgraph" +var go16 bool // Go version >= go1.6 + func TestBuild(t *testing.T) { forward, reverse, errors := importgraph.Build(&build.Default) @@ -49,7 +50,7 @@ func TestBuild(t *testing.T) { } // Test vendor packages appear under their absolute names. - if buildutil.AllowVendor != 0 { // hack: Go 1.6+ only + if go16 { // hack: Go 1.6+ only if !forward["net/http"]["vendor/golang.org/x/net/http2/hpack"] { t.Errorf("forward[net/http] does not include vendor/golang.org/x/net/http2/hpack: %v", strings.Replace(fmt.Sprint(forward["net/http"]), ":true", "", -1))