Skip to content

Commit 2a90056

Browse files
rscgopherbot
authored andcommitted
go/gcexportdata: fix Find for Go modules
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 <[email protected]> TryBot-Result: Gopher Robot <[email protected]> gopls-CI: kokoro <[email protected]> Run-TryBot: Russ Cox <[email protected]> Auto-Submit: Russ Cox <[email protected]>
1 parent 7404bd2 commit 2a90056

File tree

3 files changed

+21
-4
lines changed

3 files changed

+21
-4
lines changed

go/gcexportdata/example_test.go

-2
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ func ExampleRead() {
3030
log.Fatalf("can't find export data for fmt")
3131
}
3232
fmt.Printf("Package path: %s\n", path)
33-
fmt.Printf("Export data: %s\n", filepath.Base(filename))
3433

3534
// Open and read the file.
3635
f, err := os.Open(filename)
@@ -80,7 +79,6 @@ func ExampleRead() {
8079
// Output:
8180
//
8281
// Package path: fmt
83-
// Export data: fmt.a
8482
// Package members: Println found
8583
// Println type: func(a ...any) (n int, err error)
8684
// Println location: $GOROOT/src/fmt/print.go:123:1

go/gcexportdata/gcexportdata.go

+18-2
Original file line numberDiff line numberDiff line change
@@ -22,26 +22,42 @@ package gcexportdata // import "golang.org/x/tools/go/gcexportdata"
2222
import (
2323
"bufio"
2424
"bytes"
25+
"encoding/json"
2526
"fmt"
2627
"go/token"
2728
"go/types"
2829
"io"
2930
"io/ioutil"
31+
"os/exec"
3032

3133
"golang.org/x/tools/go/internal/gcimporter"
3234
)
3335

3436
// Find returns the name of an object (.o) or archive (.a) file
3537
// containing type information for the specified import path,
36-
// using the workspace layout conventions of go/build.
38+
// using the go command.
3739
// If no file was found, an empty filename is returned.
3840
//
3941
// A relative srcDir is interpreted relative to the current working directory.
4042
//
4143
// Find also returns the package's resolved (canonical) import path,
4244
// reflecting the effects of srcDir and vendoring on importPath.
45+
//
46+
// Deprecated: Use the higher-level API in golang.org/x/tools/go/packages,
47+
// which is more efficient.
4348
func Find(importPath, srcDir string) (filename, path string) {
44-
return gcimporter.FindPkg(importPath, srcDir)
49+
cmd := exec.Command("go", "list", "-json", "-export", "--", importPath)
50+
cmd.Dir = srcDir
51+
out, err := cmd.CombinedOutput()
52+
if err != nil {
53+
return "", ""
54+
}
55+
var data struct {
56+
ImportPath string
57+
Export string
58+
}
59+
json.Unmarshal(out, &data)
60+
return data.Export, data.ImportPath
4561
}
4662

4763
// NewReader returns a reader for the export data section of an object

go/gcexportdata/importer.go

+3
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ import (
2222
// version-skew problems described in the documentation of this package,
2323
// or to control the FileSet or access the imports map populated during
2424
// package loading.
25+
//
26+
// Deprecated: Use the higher-level API in golang.org/x/tools/go/packages,
27+
// which is more efficient.
2528
func NewImporter(fset *token.FileSet, imports map[string]*types.Package) types.ImporterFrom {
2629
return importer{fset, imports}
2730
}

0 commit comments

Comments
 (0)