Skip to content

Commit aedfce6

Browse files
authored
fix one line imports as mage:imports (#204)
* fix one line imports as mage:imports fixes #194
1 parent edea463 commit aedfce6

File tree

4 files changed

+56
-12
lines changed

4 files changed

+56
-12
lines changed

mage/import_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,3 +166,24 @@ func TestMageImportsAliasToNS(t *testing.T) {
166166
t.Fatalf("expected: %q got: %q", expected, actual)
167167
}
168168
}
169+
170+
func TestMageImportsOneLine(t *testing.T) {
171+
stdout := &bytes.Buffer{}
172+
stderr := &bytes.Buffer{}
173+
inv := Invocation{
174+
Dir: "./testdata/mageimport/oneline",
175+
Stdout: stdout,
176+
Stderr: stderr,
177+
Args: []string{"build"},
178+
}
179+
180+
code := Invoke(inv)
181+
if code != 0 {
182+
t.Fatalf("expected to exit with code 0, but got %v, stderr:\n%s", code, stderr)
183+
}
184+
actual := stdout.String()
185+
expected := "build\n"
186+
if actual != expected {
187+
t.Fatalf("expected: %q got: %q", expected, actual)
188+
}
189+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// +build mage
2+
3+
package main
4+
5+
// mage:import
6+
import _ "github.com/magefile/mage/mage/testdata/mageimport/oneline/other"
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package other
2+
3+
import "fmt"
4+
5+
func Build() {
6+
fmt.Println("build")
7+
}

parse/parse.go

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -319,24 +319,34 @@ func setImports(gocmd string, pi *PkgInfo) error {
319319
importNames := map[string]string{}
320320
rootImports := []string{}
321321
for _, f := range pi.AstPkg.Files {
322-
for _, imp := range f.Imports {
323-
name, alias, ok := getImportPath(imp)
324-
if !ok {
322+
for _, d := range f.Decls {
323+
gen, ok := d.(*ast.GenDecl)
324+
if !ok || gen.Tok != token.IMPORT {
325325
continue
326326
}
327-
if alias != "" {
328-
debug.Printf("found %s: %s (%s)", importTag, name, alias)
329-
if importNames[alias] != "" {
330-
return fmt.Errorf("duplicate import alias: %q", alias)
327+
for j := 0; j < len(gen.Specs); j++ {
328+
spec := gen.Specs[j]
329+
impspec := spec.(*ast.ImportSpec)
330+
if len(gen.Specs) == 1 && gen.Lparen == token.NoPos && impspec.Doc == nil {
331+
impspec.Doc = gen.Doc
332+
}
333+
name, alias, ok := getImportPath(impspec)
334+
if !ok {
335+
continue
336+
}
337+
if alias != "" {
338+
debug.Printf("found %s: %s (%s)", importTag, name, alias)
339+
if importNames[alias] != "" {
340+
return fmt.Errorf("duplicate import alias: %q", alias)
341+
}
342+
importNames[alias] = name
343+
} else {
344+
debug.Printf("found %s: %s", importTag, name)
345+
rootImports = append(rootImports, name)
331346
}
332-
importNames[alias] = name
333-
} else {
334-
debug.Printf("found %s: %s", importTag, name)
335-
rootImports = append(rootImports, name)
336347
}
337348
}
338349
}
339-
340350
imports, err := getNamedImports(gocmd, importNames)
341351
if err != nil {
342352
return err

0 commit comments

Comments
 (0)