magefile / mage

a Make/rake-like dev tool using Go
https://magefile.org
Apache License 2.0
4.01k stars 250 forks source link

Tips to avoid annoying linters #139

Open diegobernardes opened 5 years ago

diegobernardes commented 5 years ago

I'm using vscode. This is the first issue:

can't load package: package github.com/diegobernardes/flare: build constraints exclude all Go files in /Users/diego.bernardes/Projects/go/src/github.com/diegobernardes/flare

Ok, this is because the: // +build mage. So i added the build tag config: "go.buildTags": "mage". Problem solved.

And now the second issue:

github.com/diegobernardes/flare\nruntime.main_main·f: function main is undeclared in the main package

This one i don't know how to solve it. And a question, maybe it's better work with the current folder name instead of the package main.

natefinch commented 5 years ago

What linter is complaining about no main function, do you know? Honestly, that seems like a pretty unhelpful linter, since the compiler will tell you that you have no main function.

On Mon, Aug 27, 2018, 5:09 PM Diego Bernardes notifications@github.com wrote:

I'm using vscode. This is the first issue:

can't load package: package github.com/diegobernardes/flare: build constraints exclude all Go files in /Users/diego.bernardes/Projects/go/src/github.com/diegobernardes/flare

Ok, this is because the: // +build mage. So i added the build tag config: "go.buildTags": "mage". Problem solved.

And now the second issue:

github.com/diegobernardes/flare\nruntime.main_main·f http://github.com/diegobernardes/flare%5Cnruntime.main_main%C2%B7f: function main is undeclared in the main package

This one i don't know how to solve it. And a question, maybe it's better work with the current folder name instead of the package main.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/magefile/mage/issues/139, or mute the thread https://github.com/notifications/unsubscribe-auth/ADCcyHLWvrzGLcbJihc3gLCJMtQOj1Jaks5uVHxDgaJpZM4WOpRy .

natefinch commented 5 years ago

Ahh, I figured it out. VSCode defaults to "build on save". Which I personally don't like, so I always turn it off. "go.buildOnSave": "off"

natefinch commented 5 years ago

I'll put this up on the website at some point for posterity.

gsmcwhirter commented 5 years ago

@diegobernardes if you want the build-on-save on, I found that this works:

"go.buildTags": "mage vscode" in the settings, and then create a dummy .go file with

// +build vscode

package main

func main() {}
diegobernardes commented 5 years ago

@gsmcwhirter, yes, that solves 👍

The thing is, the magefile.go requirement to be at package main that is the problem. Why it can't work using only the build tags? I really don't know the interns of the package, so I may be saying something that can't work, but it's really just a question.

It's kinda bad to choose between always having a "error" at the magefile.go and disabling the build on save on the entire project.

natefinch commented 5 years ago

In theory it doesn't have to be package main... But that would be a tricky change. We'd have to generate the main file in a different directory and import the current directory. And we'd have to support current uses that do use package main.

I'll have to think about it some more.

Michael-F-Ellis commented 4 years ago

I'm seeing a problem that seems related to this. When I open my magefile in vscode, there's no linting or intelliSense. Making any change to the file restores both within a couple of seconds and they remain on even after undoing the change (and saving the unchanged file so git can figure out that it's still the same).

The good state persists even if I close and reopen the magefile within the same session, but is lost if I quit vscode and restart.

I just tried adding "go.buildTags": "mage" to my settings.json. No change in behavior. Haven't tried the dummy.go trick yet 'cause it feels, well, hack-ish.

I'm running vscode 1.45.1 on darwin 18.7. Gopls is up to date and I'm linting with revive (have tried it with golang-ci, no help)

Any suggestions?

BTW, thanks for creating and maintaining Mage -- it's the bomb. I say that as someone who's been writing (and cursing) Makefiles since the 80's.

philippgille commented 2 years ago

I don't get any linter errors the following way:

  1. Create magefile with mage -init
    • At this point you don't get IntelliSense etc in VS Code.
  2. Add build tag mage in VS Code Go extension settings ("go.buildTags": "mage")
    • At this point VS Code can complain about the package main if the magefile is in a directory which doesn't have any Go files with a main() function, or if it's in a directory that already has a Go file with a package name other than main
  3. Create directory magefiles and move the magefile to it
    • At this point VS Code can complain about the imports, if you don't have them in your root's go.mod, which you might not want (for example if your project is a library and not an executable, then when other devs import your library you don't want mage and it's dependencies to end up as transitive dependencies their project)
  4. Create a separate Go module for the magefile: go mod init github.com/exampleuser/examplerepo/magefiles
    • At this point, when you're in the root of the repository, VS Code can complain about the nested Go module, because gopls, the language server it uses for Go, doesn't support nested Go modules yet (as of 2022-06-05)
  5. Open a separate VS Code window with the magefiles directory as root for working on the magefile

This way I don't get any linter or build errors. buildOnSave is enabled.

invidian commented 5 days ago

This seems related to issue #344. Would it be possible for mage to not rely on package name main for example? I think that would make Go not complain about lack of main function. Separate go.mod file seems like a viable workaround though, thanks for sharing.