Skip to content

Commit 402e63c

Browse files
natefinchmojzesh
authored andcommitted
swallow error on no non-magefiles (magefile#265)
* swallow error on no non-magefiles This fixes magefile#262. go list will error out if there are no go files. So we ignore that specific error.
1 parent 59e7d08 commit 402e63c

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"
@@ -339,13 +340,17 @@ func Invoke(inv Invocation) int {
339340
if inv.HashFast {
340341
debug.Println("user has set MAGEFILE_HASHFAST, so we'll ignore GOCACHE")
341342
} else {
342-
if s, err := internal.OutputDebug(inv.GoCmd, "env", "GOCACHE"); err == nil {
343-
// if GOCACHE exists, always rebuild, so we catch transitive
344-
// dependencies that have changed.
345-
if s != "" {
346-
debug.Println("go build cache exists, will ignore any compiled binary")
347-
useCache = true
348-
}
343+
s, err := internal.OutputDebug(inv.GoCmd, "env", "GOCACHE")
344+
if err != nil {
345+
errlog.Printf("failed to run %s env GOCACHE: %s", inv.GoCmd, err)
346+
return 1
347+
}
348+
349+
// if GOCACHE exists, always rebuild, so we catch transitive
350+
// dependencies that have changed.
351+
if s != "" {
352+
debug.Println("go build cache exists, will ignore any compiled binary")
353+
useCache = true
349354
}
350355
}
351356

@@ -442,17 +447,22 @@ func Magefiles(magePath, goos, goarch, goCmd string, stderr io.Writer, isDebug b
442447
}
443448

444449
debug.Println("getting all non-mage files in", magePath)
450+
445451
// // first, grab all the files with no build tags specified.. this is actually
446452
// // our exclude list of things without the mage build tag.
447453
cmd := exec.Command(goCmd, "list", "-e", "-f", `{{join .GoFiles "||"}}`)
448454
cmd.Env = env
449-
if isDebug {
450-
cmd.Stderr = stderr
451-
}
455+
buf := &bytes.Buffer{}
456+
cmd.Stderr = buf
452457
cmd.Dir = magePath
453458
b, err := cmd.Output()
454459
if err != nil {
455-
return fail(fmt.Errorf("failed to list non-mage gofiles: %v", err))
460+
stderr := buf.String()
461+
// if the error is "cannot find module", that can mean that there's no
462+
// non-mage files, which is fine, so ignore it.
463+
if !strings.Contains(stderr, "cannot find module for path") {
464+
return fail(fmt.Errorf("failed to list non-mage gofiles: %v: %s", err, stderr))
465+
}
456466
}
457467
list := strings.TrimSpace(string(b))
458468
debug.Println("found non-mage files", list)
@@ -467,13 +477,11 @@ func Magefiles(magePath, goos, goarch, goCmd string, stderr io.Writer, isDebug b
467477
cmd = exec.Command(goCmd, "list", "-tags=mage", "-e", "-f", `{{join .GoFiles "||"}}`)
468478
cmd.Env = env
469479

470-
if isDebug {
471-
cmd.Stderr = stderr
472-
}
480+
buf.Reset()
473481
cmd.Dir = magePath
474482
b, err = cmd.Output()
475483
if err != nil {
476-
return fail(fmt.Errorf("failed to list mage gofiles: %v", err))
484+
return fail(fmt.Errorf("failed to list mage gofiles: %v: %s", err, buf.Bytes()))
477485
}
478486

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

0 commit comments

Comments
 (0)