From ebe1664d2789cd4ea0ded0eccb9067c729378cc5 Mon Sep 17 00:00:00 2001 From: Russ Cox Date: Thu, 1 Mar 2012 12:12:09 -0500 Subject: go/build: replace FindTree, ScanDir, Tree, DirInfo with Import, Package This is an API change, but one I have been promising would happen when it was clear what the go command needed. This is basically a complete replacement of what used to be here. build.Tree is gone. build.DirInfo is expanded and now called build.Package. build.FindTree is now build.Import(package, srcDir, build.FindOnly). The returned *Package contains information that FindTree returned, but applicable only to a single package. build.ScanDir is now build.ImportDir. build.FindTree+build.ScanDir is now build.Import. The new Import API allows specifying the source directory, in order to resolve local imports (import "./foo") and also allows scanning of packages outside of $GOPATH. They will come back with less information in the Package, but they will still work. The old go/build API exposed both too much and too little. This API is much closer to what the go command needs, and it works well enough in the other places where it is used. Path is gone, so it can no longer be misused. (Fixes issue 2749.) This CL updates clients of go/build other than the go command. The go command changes are in a separate CL, to be submitted at the same time. R=golang-dev, r, alex.brainman, adg CC=golang-dev https://golang.org/cl/5713043 --- src/cmd/api/goapi.go | 23 ++++++++--------------- src/cmd/api/goapi_test.go | 3 +-- src/cmd/godoc/godoc.go | 28 ++++++++++++++++------------ src/cmd/godoc/main.go | 6 +++--- 4 files changed, 28 insertions(+), 32 deletions(-) (limited to 'src/cmd') diff --git a/src/cmd/api/goapi.go b/src/cmd/api/goapi.go index ee0f92328e..fe9c862f4f 100644 --- a/src/cmd/api/goapi.go +++ b/src/cmd/api/goapi.go @@ -74,16 +74,10 @@ func main() { pkgs = strings.Fields(string(stds)) } - tree, _, err := build.FindTree("os") // some known package - if err != nil { - log.Fatalf("failed to find tree: %v", err) - } - var featureCtx = make(map[string]map[string]bool) // feature -> context name -> true for _, context := range contexts { w := NewWalker() w.context = context - w.tree = tree for _, pkg := range pkgs { w.wantedPkg[pkg] = true @@ -95,7 +89,7 @@ func main() { strings.HasPrefix(pkg, "old/") { continue } - if !tree.HasSrc(pkg) { + if fi, err := os.Stat(filepath.Join(w.root, pkg)); err != nil || !fi.IsDir() { log.Fatalf("no source in tree for package %q", pkg) } w.WalkPackage(pkg) @@ -165,7 +159,7 @@ type pkgSymbol struct { type Walker struct { context *build.Context - tree *build.Tree + root string fset *token.FileSet scope []string features map[string]bool // set @@ -191,6 +185,7 @@ func NewWalker() *Walker { selectorFullPkg: make(map[string]string), wantedPkg: make(map[string]bool), prevConstType: make(map[pkgSymbol]string), + root: filepath.Join(build.Default.GOROOT, "src/pkg"), } } @@ -252,15 +247,13 @@ func (w *Walker) WalkPackage(name string) { defer func() { w.packageState[name] = loaded }() - dir := filepath.Join(w.tree.SrcDir(), filepath.FromSlash(name)) + dir := filepath.Join(w.root, filepath.FromSlash(name)) - var info *build.DirInfo - var err error - if ctx := w.context; ctx != nil { - info, err = ctx.ScanDir(dir) - } else { - info, err = build.ScanDir(dir) + ctxt := w.context + if ctxt == nil { + ctxt = &build.Default } + info, err := ctxt.ImportDir(dir, 0) if err != nil { if strings.Contains(err.Error(), "no Go source files") { return diff --git a/src/cmd/api/goapi_test.go b/src/cmd/api/goapi_test.go index dbbec46b0d..c7cc601b1a 100644 --- a/src/cmd/api/goapi_test.go +++ b/src/cmd/api/goapi_test.go @@ -7,7 +7,6 @@ package main import ( "flag" "fmt" - "go/build" "io/ioutil" "os" "path/filepath" @@ -36,7 +35,7 @@ func TestGolden(t *testing.T) { w := NewWalker() w.wantedPkg[fi.Name()] = true - w.tree = &build.Tree{Path: "testdata", Goroot: true} + w.root = "testdata/src/pkg" goldenFile := filepath.Join("testdata", "src", "pkg", fi.Name(), "golden.txt") w.WalkPackage(fi.Name()) diff --git a/src/cmd/godoc/godoc.go b/src/cmd/godoc/godoc.go index 1f212a0bcd..e5f7a73d4f 100644 --- a/src/cmd/godoc/godoc.go +++ b/src/cmd/godoc/godoc.go @@ -15,6 +15,7 @@ import ( "go/printer" "go/token" "io" + "io/ioutil" "log" "net/http" "net/url" @@ -90,11 +91,11 @@ var ( func initHandlers() { paths := filepath.SplitList(*pkgPath) - for _, t := range build.Path { - if t.Goroot { - continue + gorootSrc := filepath.Join(build.Default.GOROOT, "src", "pkg") + for _, p := range build.Default.SrcDirs() { + if p != gorootSrc { + paths = append(paths, p) } - paths = append(paths, t.SrcDir()) } fsMap.Init(paths) @@ -1002,11 +1003,13 @@ func fsReadDir(dir string) ([]os.FileInfo, error) { return fs.ReadDir(dir) } -// fsReadFile implements ReadFile for the go/build package. -func fsReadFile(dir, name string) (path string, data []byte, err error) { - path = filepath.Join(dir, name) - data, err = ReadFile(fs, path) - return +// fsOpenFile implements OpenFile for the go/build package. +func fsOpenFile(name string) (r io.ReadCloser, err error) { + data, err := ReadFile(fs, name) + if err != nil { + return nil, err + } + return ioutil.NopCloser(bytes.NewReader(data)), nil } func inList(name string, list []string) bool { @@ -1039,10 +1042,11 @@ func (h *httpHandler) getPageInfo(abspath, relpath, pkgname string, mode PageInf // To use different pair, such as if we allowed the user // to choose, set ctxt.GOOS and ctxt.GOARCH before // calling ctxt.ScanDir. - ctxt := build.DefaultContext + ctxt := build.Default + ctxt.IsAbsPath = path.IsAbs ctxt.ReadDir = fsReadDir - ctxt.ReadFile = fsReadFile - dir, err := ctxt.ScanDir(abspath) + ctxt.OpenFile = fsOpenFile + dir, err := ctxt.ImportDir(abspath, 0) if err == nil { pkgFiles = append(dir.GoFiles, dir.CgoFiles...) } diff --git a/src/cmd/godoc/main.go b/src/cmd/godoc/main.go index ee905bb7a0..5f42105393 100644 --- a/src/cmd/godoc/main.go +++ b/src/cmd/godoc/main.go @@ -388,9 +388,9 @@ func main() { } relpath := path abspath := path - if t, pkg, err := build.FindTree(path); err == nil { - relpath = pkg - abspath = filepath.Join(t.SrcDir(), pkg) + if bp, _ := build.Import(path, "", build.FindOnly); bp.Dir != "" && bp.ImportPath != "" { + relpath = bp.ImportPath + abspath = bp.Dir } else if !filepath.IsAbs(path) { abspath = absolutePath(path, pkgHandler.fsRoot) } else { -- cgit v1.3