mage icon indicating copy to clipboard operation
mage copied to clipboard

Unexpected Behavior When Trying to Run Magefile with Import in Different Directory

Open scottames opened this issue 2 years ago • 5 comments

In attempting to setup one git project that has a magefile & sub-packages imported with the // mage:import tag and then running mage (with the -d /path/to/central/mage/project/ and the -w .) outside the project the following error is thrown. When running mage in the project's directory things work as expected.

Note: it does work if the import is removed from the magefile.

# mage -d /path/to/central/mage/project/ -w . target
Error parsing magefiles: error running "go list -f {{.Dir}}||{{.Name}} github.com/redacted/package": exit status 1
no required module provides package github.com/redacted/package: go.mod file not found in current
directory or any parent directory; see 'go help modules'

Magefile:

//+build mage

package main

import (
	"fmt"

	// mage:import
	_ "github.com/redacted/package"
)

func Target() {
	fmt.Println("testing...")
}

scottames avatar Aug 30 '21 19:08 scottames

hmm, this is an interesting problem. So the magefile is in a repo that has a go.mod I presume? I think the problem is that mage is trying to build the code by running the go tool in the current directory, but the current directory isn't in a module, so it doesn't know how to find other packages to import them.

A fix could be to always run the go tool to compile the code in the directory where the magefile lives.... that's probably doable.

natefinch avatar Sep 24 '21 02:09 natefinch

So the magefile is in a repo that has a go.mod I presume?

That is correct.

A fix could be to always run the go tool to compile the code in the directory where the magefile lives.... that's probably doable.

That sounds like a good direction.

scottames avatar Sep 26 '21 16:09 scottames

I jumped into a subdirectory with a angular project today and added a task for the developers in the scripts: {} section. I wanted them to still be able to benefit from the parent directory mage files.

I setup with "mage": "mage -w ../ -d ../" and I believe it worked great. Mage supports specifying the working directory and the magefile directory as parameters already.

The magefile.go in the parent directory has the mage import targets (for remote pull), and then it imports from a nested directory all the local tasks. If I put the import for mage discovery in the nested magefiles/myrepo/main.go I don't think it finds them, I have to put the imports in the {projectdir}/magefile.go.

magefile-parent-to-magefile-dir.go has an example (this file would be the one you put in your project root directory pointing to the magefiles directory.

It's late so maybe I missed something, but would that information help?

sheldonhull avatar Oct 02 '21 03:10 sheldonhull

@sheldonhull - thanks for digging in. You are correct in that if the magefile (and the related go.mod/sum) is in a parent directory from the directory being run in, it works as you would expect. But when the magefile (and the related go.mod/sum) is not in a parent directory, but a different path altogether the result is the above.

For example:

  • /path/a/magefile run from /path/a/b/ will work like a dream
  • /path/a/magefile run from /path/z/ will fail

Does that make sense?

That being said, this behavior could be completely expected as it's a bit out of the scope of how mage is intended to be used. However, GNU Make does support this behavior.

scottames avatar Oct 02 '21 15:10 scottames

I have very similar problem, which you could see here: https://github.com/cardil/kn-plugin-event/commit/869069cbb4313fcae1e0492829d4f0ead5b9e952 (numerous vendor changes)

In this PR I've moved Magefile.go to build/ sub directory, added additional go.mod file for mage stuff. I also change mage.go runner to point to that dir by default. I'm seeing following:

$ ./mage                
Error parsing magefiles: error running "go list -f {{.Dir}}||{{.Name}} github.com/wavesoftware/go-magetasks": exit status 1
cannot find package "." in:
	/home/ksuszyns/git/kn-event/vendor/github.com/wavesoftware/go-magetasks
exit status 1

$ mage                  
No .go files marked with the mage build tag in this directory.

$ mage -d build         
Error parsing magefiles: error running "go list -f {{.Dir}}||{{.Name}} github.com/wavesoftware/go-magetasks": exit status 1
cannot find package "." in:
	/home/ksuszyns/git/kn-event/vendor/github.com/wavesoftware/go-magetasks

cardil avatar Oct 15 '21 22:10 cardil