Skip to content

Commit

Permalink
fix goroot (#74)
Browse files Browse the repository at this point in the history
  • Loading branch information
natefinch authored Oct 17, 2017
1 parent 5cc3565 commit 2f97430
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 19 deletions.
20 changes: 3 additions & 17 deletions mage/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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()
Expand Down
24 changes: 22 additions & 2 deletions parse/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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 {
Expand All @@ -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),
}
Expand All @@ -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
Expand Down

0 comments on commit 2f97430

Please sign in to comment.