Skip to content

Commit

Permalink
-d dir contains magefiles stop with "No .go files marked with the mag…
Browse files Browse the repository at this point in the history
…e build tag..." (#447) (#448)
  • Loading branch information
turutcrane committed Feb 23, 2023
1 parent a920604 commit 1b8774a
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 25 deletions.
45 changes: 20 additions & 25 deletions mage/main.go
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
}
}
}

Expand Down
22 changes: 22 additions & 0 deletions mage/main_test.go
Expand Up @@ -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"
Expand Down
@@ -0,0 +1,6 @@
//go:build mage
// +build mage

package main

func foo() {}
@@ -0,0 +1,6 @@
//go:build mage
// +build mage

package main

func Build() {}

0 comments on commit 1b8774a

Please sign in to comment.