go
go copied to clipboard
x/tools/gopls: better handling of “scripts” with //go:build ignore?
I have a directory with several Go “scripts”. That is, files each of which has a //go:build ingore
directive and its own main
. Currently, gopls
doesn't seem to handle this pattern very well, as when I add ignore
to the list of tags, gopls
is confused by several main
s in what it sees as a single package.
Is there a way to make gopls
treat such files as separate binaries?
Or, perhaps, there should be a separate marker build tag for these things, like //go:build run
?
I think we've talked about this in the past; it would require making 'ignore' special, but that seems fine to me. We have a long-standing issue to improve handling of build tags (#29202), that this will probably be prioritized behind.
Or, perhaps, there should be a separate marker build tag for these things, like //go:build run?
I'd rather just have gopls understand ignore
. There's plenty of code in the wild that uses this already. If this causes problems for anyone (because they use ignore
to define multi-file packages), we can make it configurable. But I would be surprised if that happened.
This is perhaps a medium-sized project, but would resolve some of the pain points around build tags. While it requires an understanding of the complicated way that gopls resolves packages, it does not require duplicating the package graph, or even signficant refactoring of how we track packages: we simply need to teach our package loader (snapshot.Load
) how to build synthetic packages for a special tag or sets of tags. In fact, supporting this for a single file only (i.e. not synthesizing multiple files into a package) is probably a small project.
I think it makes sense to try to land this for the next gopls release (v0.10.0).
This is perhaps a medium-sized project, but would resolve some of the pain points around build tags. While it requires an understanding of the complicated way that gopls resolves packages, it does not require duplicating the package graph, or even signficant refactoring of how we track packages: we simply need to teach our package loader (
snapshot.Load
) how to build synthetic packages for a special tag or sets of tags. In fact, supporting this for a single file only (i.e. not synthesizing multiple files into a package) is probably a small project.I think it makes sense to try to land this for the next gopls release (v0.10.0).
Hi. findleyr
How should I fix this quesion now?
https://github.com/golang/go/issues/54317
@wonderfate I replied on that issue, which I had not seen. I don't see a bug there: it looks like it is working as intended.
Meanwhile, let's please keep discussion here about adding support for //go:build ignore
.
@findleyr
PLS doesn't support //go:build ignore
and it miserable me
@wonderfate this issue is currently scheduled for the next gopls release, in September. There's always a chance it may miss that milestone, but barring some unforeseen technical obstacle it should get done soon.
We understand that gopls doesn't work for ignored files, which is why this is prioritized.
@findleyr Thank you for your answer. I look forward to it.
Change https://go.dev/cl/441877 mentions this issue: gopls/internal/lsp/cache: add support for loading script files
Oh boy, I can't wait to test this out :) Do we need to do anything special with gopls master for it to work?
@mvdan please hold off for a few minutes -- I'm about to cut v0.10.0-pre.1; I embarassingly left a print statement in that will be fixed by https://go.dev/cl/442782 (I was debugging why parser.PackageClauseOnly parsed some comments after the package clause...)
But to answer your question: there's a new configuration option "standaloneTags", which defaults to ["ignore"]. You can customize it if you use a a different set of tags to identify scripts. Otherwise it should just work.
Gotcha. I can definitely wait a few minutes :) I will be trying it out tomorrow.
Very interested to hear about anyone's experience with this feature, negative or positive:
go install golang.org/x/tools/[email protected]
@findleyr, just checked in a couple of projects. If there are lots (tens to hundreds) of go:build ignore
files in the same directory, gopls seems to struggle for some time, but after a while it does work as expected. Custom standalone tags work well too. Thanks!
Just tested in a small project of mine in vscode, works perfectly. Been watching this for a while, thanks for implementing it @findleyr!
I too confirm that it appears to work perfectly, on neovim :)
I also have a directory with simple go scripts. I've been looking now for few hours how to make VS Code ignore the
main redeclared in this block (see details)compilerDuplicateDecl
errors. I found now this but even after adding //go:build ignore
gopls seems to still complain... They do go away for a moment after restarting language server but then come back as I open each file...
@findleyr am I doing something wrong or my issue is something else?
@RafalSkolasinski sorry we don't always catch comments on closed issues.
I can't reproduce based on your description. Can you please file a new issue, following the issue template? For example, I would be interested to see your full version information (gopls and go command), as well as your settings. A reproducer would be ideal.