11package mage
22
33import (
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