From 7fbe89f60da342dc2870277795492f310e38b8c5 Mon Sep 17 00:00:00 2001 From: Benjamin Pflanz Date: Thu, 21 Apr 2016 09:13:48 -0400 Subject: [PATCH] Use static parsing instead of requiring a binary. Using go/parser requires a built binary of the code to be enum'ed to exist. This leads to two problems: 1. The binary may be very out of date with the code, for example, when pulling into an existing repo that was last built locally some time ago, running jsonenums on newly added files will fail; 2. If the binary has never been built, but the code already expects the output of jsonenums, it will be impossible to generate the enums, making it impossible to build the binary. In response to a similar issue, Alan Donovan of the Go team suggested that instead generating tools should be using (the not-yet stable) x/tools/go/loader package to parse the actual Go code itself rather than the compilation output: https://github.com/golang/go/issues/11415 This change uses the suggested approach to break the bootstrapping dependency cycle. --- jsonenums.go | 13 ++++++++---- parser/parser.go | 53 +++++++++++++++--------------------------------- server/server.go | 2 +- 3 files changed, 26 insertions(+), 42 deletions(-) diff --git a/jsonenums.go b/jsonenums.go index 4635852..51d780c 100644 --- a/jsonenums.go +++ b/jsonenums.go @@ -75,7 +75,7 @@ import ( "path/filepath" "strings" - "github.com/thought-machine/jsonenums/parser" + "github.com/adfin/jsonenums/parser" ) var ( @@ -92,14 +92,19 @@ func main() { types := strings.Split(*typeNames, ",") // Only one directory at a time can be processed, and the default is ".". - dir := "." + in_dir := "." if args := flag.Args(); len(args) == 1 { - dir = args[0] + in_dir = args[0] } else if len(args) > 1 { log.Fatalf("only one directory at a time") } + dir, err := filepath.Abs(in_dir) + if err != nil { + log.Fatalf("Unable to determine absolute filepath for requested path %s: %v", + in_dir, err) + } - pkg, err := parser.ParsePackage(dir, *outputPrefix, *outputSuffix+".go") + pkg, err := parser.ParsePackage(dir) if err != nil { log.Fatalf("parsing package: %v", err) } diff --git a/parser/parser.go b/parser/parser.go index ee1111c..26c524b 100644 --- a/parser/parser.go +++ b/parser/parser.go @@ -10,15 +10,14 @@ import ( "fmt" "go/ast" "go/build" - "go/parser" + "go/constant" "go/token" + "go/types" "log" "path/filepath" "strings" - "go/constant" - "go/importer" - "go/types" + "golang.org/x/tools/go/loader" ) // A Package contains all the information related to a parsed package. @@ -30,45 +29,25 @@ type Package struct { } // ParsePackage parses the package in the given directory and returns it. -func ParsePackage(directory, skipPrefix, skipSuffix string) (*Package, error) { - pkgDir, err := build.Default.ImportDir(directory, 0) +func ParsePackage(directory string) (*Package, error) { + relDir, err := filepath.Rel(filepath.Join(build.Default.GOPATH, "src"), directory) if err != nil { - return nil, fmt.Errorf("cannot process directory %s: %s", directory, err) + return nil, fmt.Errorf("provided directory not under GOPATH (%s): %v", + build.Default.GOPATH, err) } - var files []*ast.File - fs := token.NewFileSet() - for _, name := range pkgDir.GoFiles { - if !strings.HasSuffix(name, ".go") || - (skipSuffix != "" && strings.HasPrefix(name, skipPrefix) && - strings.HasSuffix(name, skipSuffix)) { - continue - } - if directory != "." { - name = filepath.Join(directory, name) - } - f, err := parser.ParseFile(fs, name, nil, 0) - if err != nil { - return nil, fmt.Errorf("parsing file %v: %v", name, err) - } - files = append(files, f) - } - if len(files) == 0 { - return nil, fmt.Errorf("%s: no buildable Go files", directory) - } - - // type-check the package - defs := make(map[*ast.Ident]types.Object) - config := types.Config{FakeImportC: true, Importer: importer.Default()} - info := &types.Info{Defs: defs} - if _, err := config.Check(directory, fs, files, info); err != nil { - return nil, fmt.Errorf("type-checking package: %v", err) + loaderConf := loader.Config{TypeChecker: types.Config{FakeImportC: true}} + loaderConf.Import(relDir) + program, err := loaderConf.Load() + if err != nil { + return nil, fmt.Errorf("Couldn't load package: %v", err) } + pkgInfo := program.Package(relDir) return &Package{ - Name: files[0].Name.Name, - files: files, - defs: defs, + Name: pkgInfo.Pkg.Name(), + files: pkgInfo.Files, + defs: pkgInfo.Defs, }, nil } diff --git a/server/server.go b/server/server.go index 2110205..c15810b 100644 --- a/server/server.go +++ b/server/server.go @@ -21,7 +21,7 @@ import ( "go/format" - "github.com/thought-machine/jsonenums/parser" + "github.com/adfin/jsonenums/parser" ) func init() {