Skip to content

Commit 1b8774a

Browse files
authoredFeb 23, 2023
-d dir contains magefiles stop with "No .go files marked with the mage build tag..." (#447) (#448)
1 parent a920604 commit 1b8774a

File tree

4 files changed

+54
-25
lines changed

4 files changed

+54
-25
lines changed
 

‎mage/main.go

+20-25
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ const MagefilesDirName = "magefiles"
127127

128128
// UsesMagefiles returns true if we are getting our mage files from a magefiles directory.
129129
func (i Invocation) UsesMagefiles() bool {
130-
return i.Dir == MagefilesDirName
130+
return filepath.Base(i.Dir) == MagefilesDirName
131131
}
132132

133133
// ParseAndRun parses the command line, and then compiles and runs the mage
@@ -316,34 +316,29 @@ func Invoke(inv Invocation) int {
316316
if inv.GoCmd == "" {
317317
inv.GoCmd = "go"
318318
}
319-
var noDir bool
320319
if inv.Dir == "" {
321-
noDir = true
322320
inv.Dir = dotDirectory
323-
// . will be default unless we find a mage folder.
324-
mfSt, err := os.Stat(MagefilesDirName)
325-
if err == nil {
326-
if mfSt.IsDir() {
327-
stderrBuf := &bytes.Buffer{}
328-
inv.Dir = MagefilesDirName // preemptive assignment
329-
// TODO: Remove this fallback and the above Magefiles invocation when the bw compatibility is removed.
330-
files, err := Magefiles(dotDirectory, inv.GOOS, inv.GOARCH, inv.GoCmd, stderrBuf, false, inv.Debug)
331-
if err == nil {
332-
if len(files) != 0 {
333-
errlog.Println("[WARNING] You have both a magefiles directory and mage files in the " +
334-
"current directory, in future versions the files will be ignored in favor of the directory")
335-
inv.Dir = dotDirectory
336-
}
337-
}
338-
}
339-
}
340321
}
341-
342322
if inv.WorkDir == "" {
343-
if noDir {
344-
inv.WorkDir = dotDirectory
345-
} else {
346-
inv.WorkDir = inv.Dir
323+
inv.WorkDir = inv.Dir
324+
}
325+
magefilesDir := filepath.Join(inv.Dir, MagefilesDirName)
326+
// . will be default unless we find a mage folder.
327+
mfSt, err := os.Stat(magefilesDir)
328+
if err == nil {
329+
if mfSt.IsDir() {
330+
stderrBuf := &bytes.Buffer{}
331+
originalDir := inv.Dir
332+
inv.Dir = magefilesDir // preemptive assignment
333+
// TODO: Remove this fallback and the above Magefiles invocation when the bw compatibility is removed.
334+
files, err := Magefiles(originalDir, inv.GOOS, inv.GOARCH, inv.GoCmd, stderrBuf, false, inv.Debug)
335+
if err == nil {
336+
if len(files) != 0 {
337+
errlog.Println("[WARNING] You have both a magefiles directory and mage files in the " +
338+
"current directory, in future versions the files will be ignored in favor of the directory")
339+
inv.Dir = originalDir
340+
}
341+
}
347342
}
348343
}
349344

‎mage/main_test.go

+22
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,28 @@ func TestMixedTaggingMagefilesFolder(t *testing.T) {
477477
}
478478
}
479479

480+
func TestSetDirWithMagefilesFolder(t *testing.T) {
481+
resetTerm()
482+
483+
stderr := &bytes.Buffer{}
484+
stdout := &bytes.Buffer{}
485+
inv := Invocation{
486+
Dir: "testdata/setdir_with_magefiles_folder",
487+
Stdout: stdout,
488+
Stderr: stderr,
489+
List: true,
490+
}
491+
code := Invoke(inv)
492+
if code != 0 {
493+
t.Errorf("expected to exit with code 0, but got %v, stderr: %s", code, stderr)
494+
}
495+
expected := "Targets:\n build \n"
496+
actual := stdout.String()
497+
if actual != expected {
498+
t.Fatalf("expected %q but got %q", expected, actual)
499+
}
500+
}
501+
480502
func TestGoRun(t *testing.T) {
481503
c := exec.Command("go", "run", "main.go")
482504
c.Dir = "./testdata"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
//go:build mage
2+
// +build mage
3+
4+
package main
5+
6+
func foo() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
//go:build mage
2+
// +build mage
3+
4+
package main
5+
6+
func Build() {}

0 commit comments

Comments
 (0)
Please sign in to comment.