Skip to content

Traverse upwards for magefiles #290

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion mage/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,23 +298,41 @@ func Invoke(inv Invocation) int {
if inv.GoCmd == "" {
inv.GoCmd = "go"
}

inv.Dir = filepath.Clean(inv.Dir)
if inv.Dir == "" {
inv.Dir = "."
}
resolved, err := filepath.Abs(inv.Dir)
if err != nil {
errlog.Println("Cannot resolve absolute path from dir:", err)
} else {
inv.Dir = resolved
}

if inv.WorkDir == "" {
inv.WorkDir = inv.Dir
}
if inv.CacheDir == "" {
inv.CacheDir = mg.CacheDir()
}

FINDFILES:
files, err := Magefiles(inv.Dir, inv.GOOS, inv.GOARCH, inv.GoCmd, inv.Stderr, inv.Debug)
if err != nil {
errlog.Println("Error determining list of magefiles:", err)
return 1
}

if len(files) == 0 {
if strings.HasSuffix(inv.Dir, "/") {
debug.Println("Reached root searching for magefiles")
} else {
inv.Dir = filepath.Dir(inv.Dir)
debug.Println("No .go files marked with the mage build tag in this directory")
debug.Println("Moving to parent to find magefiles")
goto FINDFILES
}

Comment on lines +319 to +335
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd recommend against using the label in favor of pulling this all out into a function + function call here. A recursion is far easier to reason about than this

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's like bigfoot. First goto I've seen in years 😆 (not saying it doesn't have it's uses, just rare to see it in the wild!)

errlog.Println("No .go files marked with the mage build tag in this directory.")
return 1
}
Expand Down
5 changes: 4 additions & 1 deletion site/content/howitworks/_index.en.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ unchanged, the same compiled binary will be reused next time, to avoid the
generation overhead. As of Mage 1.3.0, the version of Go used to compile the
binary is also used in the hash.

When no magefiles are found in the current directory, Mage traverses upwards
through the path attempting to find any magefiles.

## Binary Cache

Compiled magefile binaries are stored in $HOME/.magefile. This location can be
Expand All @@ -22,4 +25,4 @@ customized by setting the MAGEFILE_CACHE environment variable.
Mage itself requires no dependencies to run. However, because it is compiling go
code, you must have a valid go environment set up on your machine. Mage is
compatible with any go 1.7+ environment (earlier versions may work but are not
tested).
tested).