Skip to content

Commit

Permalink
filter mageimport files (#281)
Browse files Browse the repository at this point in the history
When calling mage import, we weren't prefiltering the list of go
files, so we'd get files with build tags and test files and such.
What we want is just the normal go files for building the library.
Using go list returns us that, so use that.
  • Loading branch information
natefinch authored Jan 14, 2020
1 parent 1e0dbb8 commit 0c7e3de
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 6 deletions.
33 changes: 27 additions & 6 deletions parse/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ func getNamedImports(gocmd string, pkgs map[string]string) ([]*Import, error) {
return imports, nil
}

// getImport returns the metadata about a package that has been mage:import'ed.
func getImport(gocmd, importpath, alias string) (*Import, error) {
out, err := internal.OutputDebug(gocmd, "list", "-f", "{{.Dir}}||{{.Name}}", importpath)
if err != nil {
Expand All @@ -241,7 +242,17 @@ func getImport(gocmd, importpath, alias string) (*Import, error) {
}
dir, name := parts[0], parts[1]
debug.Printf("parsing imported package %q from dir %q", importpath, dir)
info, err := Package(dir, nil)

// we use go list to get the list of files, since go/parser doesn't differentiate between
// go files with build tags etc, and go list does. This prevents weird problems if you
// have more than one package in a folder because of build tags.
out, err = internal.OutputDebug(gocmd, "list", "-f", `{{join .GoFiles "||"}}`, importpath)
if err != nil {
return nil, err
}
files := strings.Split(out, "||")

info, err := Package(dir, files)
if err != nil {
return nil, err
}
Expand All @@ -253,6 +264,7 @@ func getImport(gocmd, importpath, alias string) (*Import, error) {
return &Import{Alias: alias, Name: name, Path: importpath, Info: *info}, nil
}

// Import represents the data about a mage:import package
type Import struct {
Alias string
Name string
Expand Down Expand Up @@ -653,7 +665,7 @@ func getFunction(exp ast.Expr, pi *PkgInfo) (*Function, error) {
return nil, fmt.Errorf("unknown package for function %q", exp)
}

// getPackage returns the non-test package at the given path.
// getPackage returns the importable package at the given path.
func getPackage(path string, files []string, fset *token.FileSet) (*ast.Package, error) {
var filter func(f os.FileInfo) bool
if len(files) > 0 {
Expand All @@ -672,12 +684,21 @@ func getPackage(path string, files []string, fset *token.FileSet) (*ast.Package,
return nil, fmt.Errorf("failed to parse directory: %v", err)
}

for name, pkg := range pkgs {
if !strings.HasSuffix(name, "_test") {
return pkg, nil
switch len(pkgs) {
case 1:
var pkg *ast.Package
for _, pkg = range pkgs {
}
return pkg, nil
case 0:
return nil, fmt.Errorf("no importable packages found in %s", path)
default:
var names []string
for name := range pkgs {
names = append(names, name)
}
return nil, fmt.Errorf("multiple packages found in %s: %v", path, strings.Join(names, ", "))
}
return nil, fmt.Errorf("no non-test packages found in %s", path)
}

func hasContextParam(ft *ast.FuncType) bool {
Expand Down
18 changes: 18 additions & 0 deletions parse/parse_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
package parse

import (
"log"
"os"
"reflect"
"testing"

"github.com/magefile/mage/internal"
)

func init() {
internal.SetDebug(log.New(os.Stdout, "", 0))
}

func TestParse(t *testing.T) {
info, err := PrimaryPackage("go", "./testdata", []string{"func.go", "command.go", "alias.go", "repeating_synopsis.go", "subcommands.go"})
if err != nil {
Expand Down Expand Up @@ -94,3 +102,13 @@ func TestParse(t *testing.T) {
}
}
}

func TestGetImportSelf(t *testing.T) {
imp, err := getImport("go", "github.com/magefile/mage/parse/testdata/importself", "")
if err != nil {
t.Fatal(err)
}
if imp.Info.AstPkg.Name != "importself" {
t.Fatalf("expected package importself, got %v", imp.Info.AstPkg.Name)
}
}
14 changes: 14 additions & 0 deletions parse/testdata/importself/magefile.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//+build mage

package main

import (
"fmt"

// mage:import
_ "github.com/magefile/mage/parse/testdata/importself"
)

func Build(){
fmt.Println("built")
}
7 changes: 7 additions & 0 deletions parse/testdata/importself/targets.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package importself

import "fmt"

func Stuff() {
fmt.Println("stuff")
}

0 comments on commit 0c7e3de

Please sign in to comment.