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

-d dir contains magefiles stop with "No .go files marked with the mag…" #447 #448

Merged
Merged
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
45 changes: 20 additions & 25 deletions mage/main.go
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
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 Build() {}