diff --git a/mage/main.go b/mage/main.go index 0a50da6d..0062bd35 100644 --- a/mage/main.go +++ b/mage/main.go @@ -127,7 +127,7 @@ const MagefilesDirName = "magefiles" // UsesMagefiles returns true if we are getting our mage files from a magefiles directory. func (i Invocation) UsesMagefiles() bool { - return i.Dir == MagefilesDirName + return filepath.Base(i.Dir) == MagefilesDirName } // ParseAndRun parses the command line, and then compiles and runs the mage @@ -316,34 +316,29 @@ func Invoke(inv Invocation) int { if inv.GoCmd == "" { inv.GoCmd = "go" } - var noDir bool if inv.Dir == "" { - noDir = true inv.Dir = dotDirectory - // . will be default unless we find a mage folder. - mfSt, err := os.Stat(MagefilesDirName) - if err == nil { - if mfSt.IsDir() { - stderrBuf := &bytes.Buffer{} - inv.Dir = MagefilesDirName // preemptive assignment - // TODO: Remove this fallback and the above Magefiles invocation when the bw compatibility is removed. - files, err := Magefiles(dotDirectory, 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 = dotDirectory - } - } - } - } } - if inv.WorkDir == "" { - if noDir { - inv.WorkDir = dotDirectory - } else { - inv.WorkDir = inv.Dir + 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 + } + } } } diff --git a/mage/main_test.go b/mage/main_test.go index 4ea4d0b1..b722d842 100644 --- a/mage/main_test.go +++ b/mage/main_test.go @@ -477,6 +477,28 @@ func TestMixedTaggingMagefilesFolder(t *testing.T) { } } +func TestSetDirWithMagefilesFolder(t *testing.T) { + resetTerm() + + stderr := &bytes.Buffer{} + stdout := &bytes.Buffer{} + inv := Invocation{ + Dir: "testdata/setdir_with_magefiles_folder", + Stdout: stdout, + Stderr: stderr, + List: true, + } + 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) + } +} + func TestGoRun(t *testing.T) { c := exec.Command("go", "run", "main.go") c.Dir = "./testdata" diff --git a/mage/testdata/setdir_with_magefiles_folder/magefiles/mage_helpers.go b/mage/testdata/setdir_with_magefiles_folder/magefiles/mage_helpers.go new file mode 100644 index 00000000..e4b8872f --- /dev/null +++ b/mage/testdata/setdir_with_magefiles_folder/magefiles/mage_helpers.go @@ -0,0 +1,6 @@ +//go:build mage +// +build mage + +package main + +func foo() {} diff --git a/mage/testdata/setdir_with_magefiles_folder/magefiles/magefile.go b/mage/testdata/setdir_with_magefiles_folder/magefiles/magefile.go new file mode 100644 index 00000000..63584509 --- /dev/null +++ b/mage/testdata/setdir_with_magefiles_folder/magefiles/magefile.go @@ -0,0 +1,6 @@ +//go:build mage +// +build mage + +package main + +func Build() {}