mage icon indicating copy to clipboard operation
mage copied to clipboard

Tips to avoid annoying linters

Open diegobernardes opened this issue 7 years ago • 9 comments

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.

diegobernardes avatar Aug 27 '18 23:08 diegobernardes

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 [email protected] 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 avatar Aug 27 '18 23:08 natefinch

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 avatar Aug 28 '18 05:08 natefinch

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

natefinch avatar Aug 31 '18 19:08 natefinch

@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() {}

gsmcwhirter avatar Sep 07 '18 19:09 gsmcwhirter

@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.

diegobernardes avatar Sep 22 '18 22:09 diegobernardes

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.

natefinch avatar Sep 23 '18 14:09 natefinch

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.

Michael-F-Ellis avatar May 19 '20 18:05 Michael-F-Ellis

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.

philippgille avatar Jun 05 '22 16:06 philippgille