Skip to content
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

Search in parent directories if mage files are not found #469

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
68 changes: 43 additions & 25 deletions mage/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -319,44 +319,62 @@ func Invoke(inv Invocation) int {
if inv.Dir == "" {
inv.Dir = dotDirectory
}
inv.Dir, _ = filepath.Abs(inv.Dir)
if inv.WorkDir == "" {
inv.WorkDir = inv.Dir
}
magefilesDir := filepath.Join(inv.Dir, MagefilesDirName)
// . will be default unless we find a mage folder.
mfSt, err := os.Stat(magefilesDir)
if err == nil {
if mfSt.IsDir() {
stderrBuf := &bytes.Buffer{}
originalDir := inv.Dir
inv.Dir = magefilesDir // preemptive assignment
// TODO: Remove this fallback and the above Magefiles invocation when the bw compatibility is removed.
files, err := Magefiles(originalDir, inv.GOOS, inv.GOARCH, inv.GoCmd, stderrBuf, false, inv.Debug)
if err == nil {
if len(files) != 0 {
errlog.Println("[WARNING] You have both a magefiles directory and mage files in the " +
"current directory, in future versions the files will be ignored in favor of the directory")
inv.Dir = originalDir
var (
files []string
err error
errMsg string
)

for len(files) == 0 && len(inv.Dir) > 1 {
magefilesDir := filepath.Join(inv.Dir, MagefilesDirName)
// . will be default unless we find a mage folder.
mfSt, err := os.Stat(magefilesDir)
if err == nil {
if mfSt.IsDir() {
stderrBuf := &bytes.Buffer{}
originalDir := inv.Dir
inv.Dir = magefilesDir // preemptive assignment
// TODO: Remove this fallback and the above Magefiles invocation when the bw compatibility is removed.
files, err := Magefiles(originalDir, inv.GOOS, inv.GOARCH, inv.GoCmd, stderrBuf, false, inv.Debug)
if err == nil {
if len(files) != 0 {
errlog.Println("[WARNING] You have both a magefiles directory and mage files in the " +
"current directory, in future versions the files will be ignored in favor of the directory")
inv.Dir = originalDir
}
}
}
}
}

if inv.CacheDir == "" {
inv.CacheDir = mg.CacheDir()
}
files, err = Magefiles(inv.Dir, inv.GOOS, inv.GOARCH, inv.GoCmd, inv.Stderr, inv.UsesMagefiles(), inv.Debug)
if err != nil {
errMsg = fmt.Sprintf("Error determining list of magefiles: %s", err)
inv.Dir = filepath.Dir(inv.Dir)
files = []string{}
continue
}

files, err := Magefiles(inv.Dir, inv.GOOS, inv.GOARCH, inv.GoCmd, inv.Stderr, inv.UsesMagefiles(), inv.Debug)
if err != nil {
errlog.Println("Error determining list of magefiles:", err)
return 1
if len(files) == 0 {
errMsg = "No .go files marked with the mage build tag in this directory or any parent directory."
inv.Dir = filepath.Dir(inv.Dir)
files = []string{}
continue
}
}

if len(files) == 0 {
errlog.Println("No .go files marked with the mage build tag in this directory.")
errlog.Println(errMsg)
return 1
}
debug.Printf("found magefiles: %s", strings.Join(files, ", "))

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

exePath := inv.CompileOut
if inv.CompileOut == "" {
exePath, err = ExeName(inv.GoCmd, inv.CacheDir, files)
Expand Down
48 changes: 48 additions & 0 deletions mage/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,54 @@ func TestMagefilesFolder(t *testing.T) {
}
}

func TestFindFilesInParents(t *testing.T) {
resetTerm()
wd, err := os.Getwd()
t.Log(wd)
if err != nil {
t.Fatalf("finding current working directory: %v", err)
}
if err := os.Chdir("testdata/with_magefiles_folder/sub_folder"); err != nil {
t.Fatalf("changing to magefolders tests data: %v", err)
}
// restore previous state
defer os.Chdir(wd)

stderr := &bytes.Buffer{}
stdout := &bytes.Buffer{}
inv := Invocation{
Dir: "",
Stdout: stdout,
Stderr: stderr,
List: true,
}
// get magefiles even when in subfolder
code := Invoke(inv)
if code != 0 {
t.Errorf("expected to exit with code 0, but got %v, stderr: %s", code, stderr)
}
expected := "Targets:\n build \n"
actual := stdout.String()
if actual != expected {
t.Fatalf("expected %q but got %q", expected, actual)
}
stdout.Truncate(0) // clear buffer for next test case

// ignore parent magefiles when subfolder has magefiles
if err := os.Chdir("sub_folder2"); err != nil {
t.Fatalf("changing to magefolders tests data: %v", err)
}
code = Invoke(inv)
if code != 0 {
t.Errorf("expected to exit with code 0, but got %v, stderr: %s", code, stderr)
}
expected = "Targets:\n subBuild \n"
actual = stdout.String()
if actual != expected {
t.Fatalf("expected %q but got %q", expected, actual)
}
}

func TestMagefilesFolderMixedWithMagefiles(t *testing.T) {
resetTerm()
wd, err := os.Getwd()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
//go:build mage
// +build mage

package main

func foo() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
//go:build mage
// +build mage

package main

func SubBuild() {}