Skip to content

Commit 1c36bf7

Browse files
authored
swallow error on no non-magefiles (#265)
* swallow error on no non-magefiles This fixes #262. go list will error out if there are no go files. So we ignore that specific error.
1 parent e1fda1a commit 1c36bf7

File tree

2 files changed

+24
-15
lines changed

2 files changed

+24
-15
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ branches:
99
# library, I'm not going to worry about older versions for now.
1010
go:
1111
- tip
12+
- 1.13.x
1213
- 1.12.x
1314
- 1.11.x
1415
- 1.10.x

mage/main.go

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package mage
22

33
import (
4+
"bytes"
45
"crypto/sha1"
56
"errors"
67
"flag"
@@ -325,13 +326,17 @@ func Invoke(inv Invocation) int {
325326
if inv.HashFast {
326327
debug.Println("user has set MAGEFILE_HASHFAST, so we'll ignore GOCACHE")
327328
} else {
328-
if s, err := internal.OutputDebug(inv.GoCmd, "env", "GOCACHE"); err == nil {
329-
// if GOCACHE exists, always rebuild, so we catch transitive
330-
// dependencies that have changed.
331-
if s != "" {
332-
debug.Println("go build cache exists, will ignore any compiled binary")
333-
useCache = true
334-
}
329+
s, err := internal.OutputDebug(inv.GoCmd, "env", "GOCACHE")
330+
if err != nil {
331+
errlog.Printf("failed to run %s env GOCACHE: %s", inv.GoCmd, err)
332+
return 1
333+
}
334+
335+
// if GOCACHE exists, always rebuild, so we catch transitive
336+
// dependencies that have changed.
337+
if s != "" {
338+
debug.Println("go build cache exists, will ignore any compiled binary")
339+
useCache = true
335340
}
336341
}
337342

@@ -428,17 +433,22 @@ func Magefiles(magePath, goos, goarch, goCmd string, stderr io.Writer, isDebug b
428433
}
429434

430435
debug.Println("getting all non-mage files in", magePath)
436+
431437
// // first, grab all the files with no build tags specified.. this is actually
432438
// // our exclude list of things without the mage build tag.
433439
cmd := exec.Command(goCmd, "list", "-e", "-f", `{{join .GoFiles "||"}}`)
434440
cmd.Env = env
435-
if isDebug {
436-
cmd.Stderr = stderr
437-
}
441+
buf := &bytes.Buffer{}
442+
cmd.Stderr = buf
438443
cmd.Dir = magePath
439444
b, err := cmd.Output()
440445
if err != nil {
441-
return fail(fmt.Errorf("failed to list non-mage gofiles: %v", err))
446+
stderr := buf.String()
447+
// if the error is "cannot find module", that can mean that there's no
448+
// non-mage files, which is fine, so ignore it.
449+
if !strings.Contains(stderr, "cannot find module for path") {
450+
return fail(fmt.Errorf("failed to list non-mage gofiles: %v: %s", err, stderr))
451+
}
442452
}
443453
list := strings.TrimSpace(string(b))
444454
debug.Println("found non-mage files", list)
@@ -453,13 +463,11 @@ func Magefiles(magePath, goos, goarch, goCmd string, stderr io.Writer, isDebug b
453463
cmd = exec.Command(goCmd, "list", "-tags=mage", "-e", "-f", `{{join .GoFiles "||"}}`)
454464
cmd.Env = env
455465

456-
if isDebug {
457-
cmd.Stderr = stderr
458-
}
466+
buf.Reset()
459467
cmd.Dir = magePath
460468
b, err = cmd.Output()
461469
if err != nil {
462-
return fail(fmt.Errorf("failed to list mage gofiles: %v", err))
470+
return fail(fmt.Errorf("failed to list mage gofiles: %v: %s", err, buf.Bytes()))
463471
}
464472

465473
list = strings.TrimSpace(string(b))

0 commit comments

Comments
 (0)