-
Notifications
You must be signed in to change notification settings - Fork 8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
support gcexportdata export files for loading packages instead of golang.org/x/tools/go/packages.Load
#50
Open
Strum355
wants to merge
1
commit into
derision-test:main
Choose a base branch
from
Strum355:nsc/gcexportdata
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -43,14 +43,18 @@ func parseAndValidateOptions() ([]*generation.Options, error) { | |||||
|
||||||
func parseOptions() ([]*generation.Options, error) { | ||||||
if len(os.Args) == 1 { | ||||||
return parseManifest() | ||||||
return parseManifest("") | ||||||
} | ||||||
|
||||||
opts, err := parseFlags() | ||||||
if err != nil { | ||||||
return nil, err | ||||||
} | ||||||
|
||||||
if opts.ManifestDir != "" { | ||||||
return parseManifest(opts.ManifestDir) | ||||||
} | ||||||
|
||||||
return []*generation.Options{opts}, nil | ||||||
} | ||||||
|
||||||
|
@@ -67,7 +71,7 @@ func parseFlags() (*generation.Options, error) { | |||||
app := kingpin.New(consts.Name, consts.Description).Version(consts.Version) | ||||||
app.UsageWriter(os.Stdout) | ||||||
|
||||||
app.Arg("path", "The import paths used to search for eligible interfaces").Required().StringsVar(&opts.PackageOptions[0].ImportPaths) | ||||||
app.Arg("path", "The import paths used to search for eligible interfaces").StringsVar(&opts.PackageOptions[0].ImportPaths) | ||||||
app.Flag("package", "The name of the generated package. It will be inferred from the output options by default.").Short('p').StringVar(&opts.ContentOptions.PkgName) | ||||||
app.Flag("interfaces", "A list of target interfaces to generate defined in the given the import paths.").Short('i').StringsVar(&opts.PackageOptions[0].Interfaces) | ||||||
app.Flag("exclude", "A list of interfaces to exclude from generation. Mocks for all other exported interfaces defined in the given import paths are generated.").Short('e').StringsVar(&opts.PackageOptions[0].Exclude) | ||||||
|
@@ -82,6 +86,7 @@ func parseFlags() (*generation.Options, error) { | |||||
app.Flag("for-test", "Append _test suffix to generated package names and file names.").Default("false").BoolVar(&opts.OutputOptions.ForTest) | ||||||
app.Flag("file-prefix", "Content that is written at the top of each generated file.").StringVar(&opts.ContentOptions.FilePrefix) | ||||||
app.Flag("build-constraints", "Build constraints that are added to each generated file.").StringVar(&opts.ContentOptions.BuildConstraints) | ||||||
app.Flag("manifest-dir", "Dir in which to search for the root mockgen.yaml file in. All other flags are ignored if this is set, and config is taken from the manifest file(s).").StringVar(&opts.ManifestDir) | ||||||
|
||||||
if _, err := app.Parse(os.Args[1:]); err != nil { | ||||||
return nil, err | ||||||
|
@@ -90,8 +95,8 @@ func parseFlags() (*generation.Options, error) { | |||||
return opts, nil | ||||||
} | ||||||
|
||||||
func parseManifest() ([]*generation.Options, error) { | ||||||
payload, err := readManifest() | ||||||
func parseManifest(manifestDir string) ([]*generation.Options, error) { | ||||||
payload, err := readManifest(manifestDir) | ||||||
if err != nil { | ||||||
return nil, err | ||||||
} | ||||||
|
@@ -126,6 +131,10 @@ func parseManifest() ([]*generation.Options, error) { | |||||
opts.ForTest = true | ||||||
} | ||||||
|
||||||
if len(opts.Paths) > 0 && len(opts.Archives) > 0 { | ||||||
return nil, fmt.Errorf("multiple import paths and archives are mutually exclusive") | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Is this more correct? They're both >0, not >1. |
||||||
} | ||||||
|
||||||
// Canonicalization | ||||||
paths := opts.Paths | ||||||
if opts.Path != "" { | ||||||
|
@@ -139,11 +148,15 @@ func parseManifest() ([]*generation.Options, error) { | |||||
|
||||||
var packageOptions []generation.PackageOptions | ||||||
if len(opts.Sources) > 0 { | ||||||
if len(opts.Paths) > 0 || len(opts.Interfaces) > 0 { | ||||||
if len(opts.Paths) > 0 || len(opts.Interfaces) > 0 || opts.Path != "" { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this necessary? I thought we rolled in the single path into the slice somewhere. |
||||||
return nil, fmt.Errorf("sources and path/paths/interfaces are mutually exclusive") | ||||||
} | ||||||
|
||||||
for _, source := range opts.Sources { | ||||||
if len(source.Paths) > 0 && len(opts.Archives) > 0 { | ||||||
return nil, fmt.Errorf("multiple import paths and archives are mutually exclusive") | ||||||
} | ||||||
|
||||||
// Canonicalization | ||||||
paths := source.Paths | ||||||
if source.Path != "" { | ||||||
|
@@ -155,6 +168,9 @@ func parseManifest() ([]*generation.Options, error) { | |||||
Interfaces: source.Interfaces, | ||||||
Exclude: source.Exclude, | ||||||
Prefix: source.Prefix, | ||||||
Archives: opts.Archives, | ||||||
SourceFiles: source.SourceFiles, | ||||||
StdlibRoot: payload.StdlibRoot, | ||||||
}) | ||||||
} | ||||||
} else { | ||||||
|
@@ -163,6 +179,9 @@ func parseManifest() ([]*generation.Options, error) { | |||||
Interfaces: opts.Interfaces, | ||||||
Exclude: opts.Exclude, | ||||||
Prefix: opts.Prefix, | ||||||
Archives: opts.Archives, | ||||||
SourceFiles: opts.SourceFiles, | ||||||
StdlibRoot: payload.StdlibRoot, | ||||||
}) | ||||||
} | ||||||
|
||||||
|
@@ -203,13 +222,17 @@ type yamlPayload struct { | |||||
ForTest bool `yaml:"for-test"` | ||||||
FilePrefix string `yaml:"file-prefix"` | ||||||
|
||||||
StdlibRoot string `yaml:"stdlib-root"` | ||||||
|
||||||
Mocks []yamlMock `yaml:"mocks"` | ||||||
} | ||||||
|
||||||
type yamlMock struct { | ||||||
Path string `yaml:"path"` | ||||||
Paths []string `yaml:"paths"` | ||||||
Sources []yamlSource `yaml:"sources"` | ||||||
SourceFiles []string `yaml:"source-files"` | ||||||
Archives []string `yaml:"archives"` | ||||||
Package string `yaml:"package"` | ||||||
Interfaces []string `yaml:"interfaces"` | ||||||
Exclude []string `yaml:"exclude"` | ||||||
|
@@ -226,15 +249,16 @@ type yamlMock struct { | |||||
} | ||||||
|
||||||
type yamlSource struct { | ||||||
Path string `yaml:"path"` | ||||||
Paths []string `yaml:"paths"` | ||||||
Interfaces []string `yaml:"interfaces"` | ||||||
Exclude []string `yaml:"exclude"` | ||||||
Prefix string `yaml:"prefix"` | ||||||
Path string `yaml:"path"` | ||||||
Paths []string `yaml:"paths"` | ||||||
Interfaces []string `yaml:"interfaces"` | ||||||
Exclude []string `yaml:"exclude"` | ||||||
Prefix string `yaml:"prefix"` | ||||||
SourceFiles []string `yaml:"source-files"` | ||||||
} | ||||||
|
||||||
func readManifest() (yamlPayload, error) { | ||||||
contents, err := os.ReadFile("mockgen.yaml") | ||||||
func readManifest(manifestDir string) (yamlPayload, error) { | ||||||
contents, err := os.ReadFile(filepath.Join(manifestDir, "mockgen.yaml")) | ||||||
if err != nil { | ||||||
return yamlPayload{}, err | ||||||
} | ||||||
|
@@ -245,7 +269,7 @@ func readManifest() (yamlPayload, error) { | |||||
} | ||||||
|
||||||
for _, path := range payload.IncludeConfigPaths { | ||||||
payload, err = readIncludeConfig(payload, path) | ||||||
payload, err = readIncludeConfig(payload, filepath.Join(manifestDir, path)) | ||||||
if err != nil { | ||||||
return yamlPayload{}, err | ||||||
} | ||||||
|
@@ -307,6 +331,10 @@ var goIdentifierPattern = regexp.MustCompile("^[A-Za-z]([A-Za-z0-9_]*)?$") | |||||
|
||||||
func validateOptions(opts *generation.Options) (bool, error) { | ||||||
for _, packageOpts := range opts.PackageOptions { | ||||||
if len(packageOpts.ImportPaths) == 0 { | ||||||
return false, fmt.Errorf("missing interface source import paths") | ||||||
} | ||||||
|
||||||
if len(packageOpts.Interfaces) != 0 && len(packageOpts.Exclude) != 0 { | ||||||
return false, fmt.Errorf("interface lists and exclude lists are mutually exclusive") | ||||||
} | ||||||
|
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This change should be covered later by https://github.com/derision-test/go-mockgen/pull/50/files#diff-bec68f6a8e7a9ffdba1264a1140ed04a177aade22c6e516c9d47cd36cd6e1656R334-R336.
It has to be non-required so that --manifest-dir can be passed in isolation