-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
support gcexportdata export files for loading packages instead of `go…
…lang.org/x/tools/go/packages.Load`
- Loading branch information
Showing
14 changed files
with
395 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"go/ast" | ||
"go/build" | ||
"go/parser" | ||
"go/token" | ||
"go/types" | ||
"path/filepath" | ||
"strings" | ||
|
||
"github.com/derision-test/go-mockgen/internal" | ||
) | ||
|
||
type archive struct { | ||
// ImportMap refers to the actual import path to the library this archive represents. | ||
// See https://github.com/bazelbuild/rules_go/blob/a9b312afd2866f4316356b456df1971bff6cd244/go/core.rst#go_library. | ||
ImportMap string | ||
File string | ||
} | ||
|
||
// The following is the format expected by this function: | ||
// | ||
// IMPORTMAP=EXPORT e.g. github.com/foo/bar=bar_export.a | ||
// | ||
// The flag is structured in this format to loosely follow https://sourcegraph.com/github.com/bazelbuild/rules_go@a9b312afd2866f4316356b456df1971bff6cd244/-/blob/go/private/actions/compilepkg.bzl?L22-29; | ||
// however, the IMPORTPATHS section is omitted. There may be future | ||
// work involved in resolving import aliases/vendoring using IMPORTPATHS. | ||
func parseArchive(a string) (archive, error) { | ||
args := strings.Split(a, "=") | ||
if len(args) != 2 { | ||
return archive{}, fmt.Errorf("expected 2 elements, got %d: %v", len(args), a) | ||
} | ||
|
||
return archive{ | ||
ImportMap: args[0], | ||
File: args[1], | ||
}, nil | ||
} | ||
|
||
func PackagesArchive(p loadParams) (packages []*internal.GoPackage, err error) { | ||
fset := token.NewFileSet() | ||
for _, importpath := range p.importPaths { | ||
files := make([]*ast.File, 0, len(p.sources)) | ||
for _, src := range p.sources[importpath] { | ||
if ok, err := build.Default.MatchFile(filepath.Dir(src), filepath.Base(src)); err != nil { | ||
return nil, fmt.Errorf("error checking if file matches constraints: %w", err) | ||
} else if !ok || filepath.Ext(src) == ".s" { | ||
fmt.Printf("skipping %q\n", src) | ||
continue | ||
} | ||
|
||
f, err := parser.ParseFile(fset, src, nil, parser.ParseComments) | ||
if err != nil { | ||
return nil, fmt.Errorf("error parsing %q: %v", src, err) | ||
} | ||
|
||
files = append(files, f) | ||
} | ||
|
||
imp, err := newImporter(fset, p.archives, p.stdlibRoot) | ||
if err != nil { | ||
return nil, err | ||
} | ||
conf := types.Config{Importer: imp, Error: func(err error) { | ||
fmt.Println(err) | ||
}} | ||
typesInfo := &types.Info{ | ||
Types: make(map[ast.Expr]types.TypeAndValue), | ||
Defs: make(map[*ast.Ident]types.Object), | ||
Uses: make(map[*ast.Ident]types.Object), | ||
Implicits: make(map[ast.Node]types.Object), | ||
Selections: make(map[*ast.SelectorExpr]*types.Selection), | ||
Scopes: make(map[ast.Node]*types.Scope), | ||
} | ||
|
||
pkg, err := conf.Check(importpath, fset, files, typesInfo) | ||
if err != nil { | ||
return nil, fmt.Errorf("error building pkg %q: %w", importpath, err) | ||
} | ||
packages = append(packages, &internal.GoPackage{ | ||
PkgPath: pkg.Path(), | ||
CompiledGoFiles: p.sources[importpath], | ||
Syntax: files, | ||
Types: pkg, | ||
TypesInfo: typesInfo, | ||
}) | ||
} | ||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"go/token" | ||
"go/types" | ||
"os" | ||
"strings" | ||
|
||
"golang.org/x/tools/go/gcexportdata" | ||
) | ||
|
||
type importer struct { | ||
importToArchive map[string]string | ||
stdlibRoot string | ||
fset *token.FileSet | ||
imports map[string]*types.Package | ||
} | ||
|
||
func newImporter(fset *token.FileSet, archives []archive, root string) (types.Importer, error) { | ||
imp := &importer{ | ||
importToArchive: make(map[string]string, len(archives)), | ||
fset: fset, | ||
imports: make(map[string]*types.Package), | ||
stdlibRoot: root, | ||
} | ||
|
||
for _, archive := range archives { | ||
imp.importToArchive[archive.ImportMap] = archive.File | ||
} | ||
return imp, nil | ||
} | ||
|
||
func (i *importer) Import(path string) (*types.Package, error) { | ||
if pkg, ok := i.imports[path]; ok && pkg.Complete() { | ||
return pkg, nil | ||
} | ||
|
||
if path == "unsafe" { | ||
// Special case: go/types has pre-defined type information for unsafe. | ||
// See https://github.com/golang/go/issues/13882. | ||
return types.Unsafe, nil | ||
} | ||
|
||
if isStdlibImport(path) { | ||
archiveFile := fmt.Sprintf("%v/%v.a", i.stdlibRoot, path) | ||
return i.readArchive(archiveFile, path) | ||
} | ||
|
||
if archive, ok := i.importToArchive[path]; ok { | ||
return i.readArchive(archive, path) | ||
} | ||
return nil, fmt.Errorf("package %q not found in read archives: please double check dependencies for the go-mockgen bazel rule", path) | ||
} | ||
|
||
func (i *importer) readArchive(archiveFile, path string) (p *types.Package, err error) { | ||
f, err := os.Open(archiveFile) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer func() { f.Close() }() | ||
|
||
r, err := gcexportdata.NewReader(f) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return gcexportdata.Read(r, i.fset, i.imports, path) | ||
} | ||
|
||
func isStdlibImport(path string) bool { | ||
if i := strings.IndexByte(path, '/'); i >= 0 { | ||
path = path[:i] | ||
} | ||
|
||
// If the prefix of the import path contains a ".", it should be considered | ||
// to be a external package (not part of Go standard lib). | ||
return !strings.Contains(path, ".") | ||
} |
Oops, something went wrong.