Skip to content

Commit

Permalink
go/gcexportdata: fix Find for Go modules
Browse files Browse the repository at this point in the history
Find needs to invoke the go command to find the package export data.
It cannot rely on GOPATH-based file location heuristics.
This has the nice side effect of automatically compiling the code,
removing the possibility of stale export data.

Ideally Find would use go/packages, but go/packages imports
this package for the export data parser (not for Find itself),
so we have to make do with an explicit go command invocation.

Marked both Find and NewImporter deprecated: using go/packages
will be faster for nearly all use cases, because it can gather info
about multiple packages in a single go command invocation.
They are essentially unused anyway.

Removed the file name print from the example because
the file may be in the cache, in which case it will not be
named "fmt.a".

Change-Id: I7940c90e230b22df9dcbfc8103a69a2d18df3bb0
Reviewed-on: https://go-review.googlesource.com/c/tools/+/310515
Reviewed-by: Alan Donovan <adonovan@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
gopls-CI: kokoro <noreply+kokoro@google.com>
Run-TryBot: Russ Cox <rsc@golang.org>
Auto-Submit: Russ Cox <rsc@golang.org>
  • Loading branch information
rsc authored and gopherbot committed Jun 28, 2022
1 parent 7404bd2 commit 2a90056
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 4 deletions.
2 changes: 0 additions & 2 deletions go/gcexportdata/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ func ExampleRead() {
log.Fatalf("can't find export data for fmt")
}
fmt.Printf("Package path: %s\n", path)
fmt.Printf("Export data: %s\n", filepath.Base(filename))

// Open and read the file.
f, err := os.Open(filename)
Expand Down Expand Up @@ -80,7 +79,6 @@ func ExampleRead() {
// Output:
//
// Package path: fmt
// Export data: fmt.a
// Package members: Println found
// Println type: func(a ...any) (n int, err error)
// Println location: $GOROOT/src/fmt/print.go:123:1
Expand Down
20 changes: 18 additions & 2 deletions go/gcexportdata/gcexportdata.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,26 +22,42 @@ package gcexportdata // import "golang.org/x/tools/go/gcexportdata"
import (
"bufio"
"bytes"
"encoding/json"
"fmt"
"go/token"
"go/types"
"io"
"io/ioutil"
"os/exec"

"golang.org/x/tools/go/internal/gcimporter"
)

// Find returns the name of an object (.o) or archive (.a) file
// containing type information for the specified import path,
// using the workspace layout conventions of go/build.
// using the go command.
// If no file was found, an empty filename is returned.
//
// A relative srcDir is interpreted relative to the current working directory.
//
// Find also returns the package's resolved (canonical) import path,
// reflecting the effects of srcDir and vendoring on importPath.
//
// Deprecated: Use the higher-level API in golang.org/x/tools/go/packages,
// which is more efficient.
func Find(importPath, srcDir string) (filename, path string) {
return gcimporter.FindPkg(importPath, srcDir)
cmd := exec.Command("go", "list", "-json", "-export", "--", importPath)
cmd.Dir = srcDir
out, err := cmd.CombinedOutput()
if err != nil {
return "", ""
}
var data struct {
ImportPath string
Export string
}
json.Unmarshal(out, &data)
return data.Export, data.ImportPath
}

// NewReader returns a reader for the export data section of an object
Expand Down
3 changes: 3 additions & 0 deletions go/gcexportdata/importer.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ import (
// version-skew problems described in the documentation of this package,
// or to control the FileSet or access the imports map populated during
// package loading.
//
// Deprecated: Use the higher-level API in golang.org/x/tools/go/packages,
// which is more efficient.
func NewImporter(fset *token.FileSet, imports map[string]*types.Package) types.ImporterFrom {
return importer{fset, imports}
}
Expand Down

0 comments on commit 2a90056

Please sign in to comment.