diff --git a/mage/main.go b/mage/main.go index a5f1e2d9..c4ce876c 100644 --- a/mage/main.go +++ b/mage/main.go @@ -101,6 +101,7 @@ func ParseAndRun(dir string, stdout, stderr io.Writer, stdin io.Reader, args []s log.Println(initFile, "created") return 0 } + return Invoke(inv) } @@ -183,6 +184,7 @@ func Invoke(inv Invocation) int { for i := range files { fnames = append(fnames, filepath.Base(files[i])) } + info, err := parse.Package(inv.Dir, fnames) if err != nil { log.Println("Error:", err) @@ -264,24 +266,8 @@ func Magefiles(dir string) ([]string, error) { // Compile uses the go tool to compile the files into an executable at path. func Compile(path string, stdout, stderr io.Writer, gofiles []string) error { - // we explicitly set GOROOT on the command because go build will fail - // without it. - goroot := os.Getenv("GOROOT") - if goroot == "" { - c := exec.Command("go", "env", "GOROOT") - c.Stderr = stderr - b, err := c.Output() - if err != nil { - return fmt.Errorf("failed to get GOROOT from 'go env': %v", err) - } - goroot = strings.TrimSpace(string(b)) - if goroot == "" { - return errors.New("could not determine GOROOT") - } - } - c := exec.Command("go", append([]string{"build", "-o", path}, gofiles...)...) - c.Env = append(os.Environ(), "GOROOT="+goroot) + c.Env = os.Environ() c.Stderr = stderr c.Stdout = stdout err := c.Run() diff --git a/parse/parse.go b/parse/parse.go index c7249edd..85db137e 100644 --- a/parse/parse.go +++ b/parse/parse.go @@ -3,12 +3,14 @@ package parse import ( "fmt" "go/ast" + "go/build" "go/doc" "go/parser" "go/token" "go/types" "log" "os" + "os/exec" "strings" ) @@ -105,7 +107,7 @@ func getPackage(path string, files []string, fset *token.FileSet) (*ast.Package, pkgs, err := parser.ParseDir(fset, path, filter, parser.ParseComments) if err != nil { - return nil, err + return nil, fmt.Errorf("failed to parse directory: %v", err) } for name, pkg := range pkgs { @@ -117,6 +119,21 @@ func getPackage(path string, files []string, fset *token.FileSet) (*ast.Package, } func makeInfo(dir string, fset *token.FileSet, files map[string]*ast.File) (types.Info, error) { + goroot := os.Getenv("GOROOT") + if goroot == "" { + c := exec.Command("go", "env", "GOROOT") + b, err := c.Output() + if err != nil { + return types.Info{}, fmt.Errorf("failed to get GOROOT from 'go env': %v", err) + } + goroot = strings.TrimSpace(string(b)) + if goroot == "" { + return types.Info{}, fmt.Errorf("could not determine GOROOT") + } + } + + build.Default.GOROOT = goroot + cfg := types.Config{ Importer: getImporter(fset), } @@ -133,7 +150,10 @@ func makeInfo(dir string, fset *token.FileSet, files map[string]*ast.File) (type } _, err := cfg.Check(dir, fset, fs, &info) - return info, err + if err != nil { + return info, fmt.Errorf("failed to check types in directory: %v", err) + } + return info, nil } // errorOrVoid filters the list of functions to only those that return only an