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
x/tools/gopls: spurious errors reported with go1.17 and mage #49104
Comments
Note you've set |
If I delete that setting, I get the following warning:
|
Thanks for reporting. We want to understand this, and will investigate with your reproducer. It is however a busy time on the Go team, so this might take a couple weeks. |
+1 happens on my project as well magefile starts with:
but I still get:
|
A couple week turned into a couple months I'm afraid, due to Go 1.18 busyness. Putting this in the v0.8.0 release, though see below about our options for improving this. Looking at it again, I'm not sure what is desired here. The diagnostics of the form For most build tags, this is the correct behavior: gopls can't always guess the combinations of build tags that lead to a valid package, and wants to avoid presenting spurious errors by guessing wrong. However, for some build tags (such as "ignore"), there is an implicit convention that the files should be considered part of a single-file package. For mage users: is it always the case that magefiles should type-check as a single package? If we had a gopls setting like |
FWIW, I have a project with multiple folders containing multiple magefiles. All my magefiles have no build tags other than As mentioned in my original post, |
Vet has the same limitation as gopls. Suppose I put an arbitrary vet error into your magefile -- say a bad string(int) conversion
With the first command, vet does not find the error in So as you can see, vet has the same limitation that without more information, it doesn't know what files are intended to constitute a package. I am guessing that mage has some convention around how to build a package out of magefiles. What I'm proposing is a rule that would allow users to encode the convention of "files guarded by this build tag should always be considered a standalone package", but I don't know enough about mage to determine if this is sufficiently expressive. |
So, I think a lot of the problem here is likely due to how Mage works when you have mage files interleaved with non-mage files. Mage does some tricks to only build the files with the The problem comes with the fact that Mage needs the code in magefiles to be in Also, because Mage generates the code for This is something the Mage team is trying to address. We just released mage v1.13, which adds support for putting your magefiles in a subdirectory, so they don't have to be in the same folder as other go code, and you don't need the mage build tag, and won't ever have two different packages in the same directory. |
Note that we just release v1.13 of Mage, which adds support for moving your magefiles out of the root directory and into a subdirectory, so you don't need them interleaved with other go files, and don't need the build tag anymore (the old way is still supported, but causes problems with gopls as you've noticed). See here for details: https://magefile.org/blog/2022/03/release-v1.13.0/ |
I get a similar error if I merely rename a Go source file, such as iDictionary.go to idictionary.go. There is no iDictionary.go file, but if I double-click the error, VS Code opens the idictionary.go as iDictionary.go. I also get lots of weird mangled reformating / undoing code changes if I press Ctrl-S on the idictionary.go file. |
Hi folks, I recently added a feature to gopls (not yet released) such that gopls can detect certain files as "standalone main files", meaning that gopls recognizes that they are intended to be standalone packages, such as with To use this feature, you will first need to install the prerelease version of gopls:
Then you will need to configure your editor to set the new Please reply here with any feedback, as I believe this new feature should resolve this issue. Thank you! |
Ok, closing this as I believe it should be addressed by the new setting. Please comment or re-open if this isn't working for you. |
What version of Go, VS Code & VS Code Go extension are you using?
Version Information
go version
to get version of Go from the VS Code integrated terminal.gopls -v version
to get version of Gopls from the VS Code integrated terminal.golang.org/x/tools/gopls@v0.7.3 h1:Lru57ht8vtDMouRskFC085VAjBAZRAISd/lwvwOOV0Q=
github.com/BurntSushi/toml@v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
github.com/google/go-cmp@v0.5.6 h1:BKbKCqvP6I+rmFHt06ZmyQtvB8xAkWdhFyr0ZUNZcxQ=
github.com/sergi/go-diff@v1.1.0 h1:we8PVUC3FE2uYfodKH/nBHMSetSfHDR6scGdBi+erh0=
golang.org/x/mod@v0.4.2 h1:Gz96sIWK3OalVv/I/qNygP42zyoKp3xptRVCWRFEBvo=
golang.org/x/sync@v0.0.0-20210220032951-036812b2e83c h1:5KslGYwFpkhGh+Q16bwMP3cOontH8FOep7tGV86Y7SQ=
golang.org/x/sys@v0.0.0-20210809222454-d867a43fc93e h1:WUoyKPm6nCo1BnNUvPGnFG3T5DUVem42yDJZZ4CNxMA=
golang.org/x/text@v0.3.6 h1:aRYxNxv6iGQlyVaZmk6ZgYEDa+Jg18DxebPSrd6bg1M=
golang.org/x/tools@v0.1.8-0.20211014194737-fc98fb2abd48 h1:hk7xRoeg0CG1nRLsd5BZLDUgVpA9bnKylGk1p2/BPH0=
golang.org/x/xerrors@v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=
honnef.co/go/tools@v0.2.0 h1:ws8AfbgTX3oIczLPNPCu5166oBg9ST2vNs0rcht+mDE=
mvdan.cc/gofumpt@v0.1.1 h1:bi/1aS/5W00E2ny5q65w9SnKpWEF/UIOqDYBILpo9rA=
mvdan.cc/xurls/v2@v2.3.0 h1:59Olnbt67UKpxF1EwVBopJvkSUBmgtb468E4GVWIZ1I=
code -v
orcode-insiders -v
to get version of VS Code or VS Code Insiders.6cba118ac49a1b88332f312a8f67186f7f3c1643
x64
Run Ctrl+Shift+P (Cmd+Shift+P on Mac OS) >
Go: Locate Configured Go Tools
command.Share the Go related settings you have added/edited
Settings
"gopls": { "buildFlags": [ "-tags=mage" ] }, "go.testFlags": [ "-count=1" ], "go-template.patterns": [ "*.tpl", "*.go.tpl", "**/*.tpl" ], "go.toolsManagement.autoUpdate": true,
Briefly, the problem is that vscode reports a slew of errors even though the code in the repo builds and runs with no errors or warnings from
mage
,go vet
orgo build
.The problem appears to be that
vscode
(viagopls
) isn't honoring the//go:build mage
directive inmypkg/magefile.go
.The complaints seems to originate because magefiles want to have
package main
specified.I can make the
vscode
complaints go away by changing that topackage mypkg
, but then mage is no longer able to run.I've used mage in this manner prior to the advent of
go1.17
in large projects requiring extensive code generation without getting vscode complaints.Steps to reproduce the behavior:
I've created a minimal example that reproduces the problem in a public repo.
vscode
Problems tab.go vet
. Observe no problems reported.mage -v
The text was updated successfully, but these errors were encountered: