go icon indicating copy to clipboard operation
go copied to clipboard

x/tools/gopls: better handling of “scripts” with //go:build ignore?

Open ainar-g opened this issue 3 years ago • 4 comments

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 mains 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?

ainar-g avatar Nov 18 '21 14:11 ainar-g

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.

findleyr avatar Nov 18 '21 14:11 findleyr

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

findleyr avatar Aug 04 '22 20:08 findleyr

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

gaoyusongcn avatar Aug 06 '22 17:08 gaoyusongcn

@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 avatar Aug 08 '22 21:08 findleyr

@findleyr

PLS doesn't support //go:build ignore and it miserable me

image

gaoyusongcn avatar Aug 14 '22 19:08 gaoyusongcn

@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 avatar Aug 14 '22 20:08 findleyr

@findleyr Thank you for your answer. I look forward to it.

gaoyusongcn avatar Aug 14 '22 20:08 gaoyusongcn

Change https://go.dev/cl/441877 mentions this issue: gopls/internal/lsp/cache: add support for loading script files

gopherbot avatar Oct 11 '22 20:10 gopherbot

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 avatar Oct 13 '22 17:10 mvdan

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

findleyr avatar Oct 13 '22 17:10 findleyr

Gotcha. I can definitely wait a few minutes :) I will be trying it out tomorrow.

mvdan avatar Oct 13 '22 17:10 mvdan

Very interested to hear about anyone's experience with this feature, negative or positive:

go install golang.org/x/tools/[email protected]

findleyr avatar Oct 14 '22 13:10 findleyr

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

ainar-g avatar Oct 14 '22 13:10 ainar-g

Just tested in a small project of mine in vscode, works perfectly. Been watching this for a while, thanks for implementing it @findleyr!

CHATALOT1 avatar Oct 14 '22 13:10 CHATALOT1

I too confirm that it appears to work perfectly, on neovim :)

mvdan avatar Oct 14 '22 15:10 mvdan

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

RafalSkolasinski avatar Nov 10 '22 00:11 RafalSkolasinski

@findleyr am I doing something wrong or my issue is something else?

RafalSkolasinski avatar Nov 10 '22 09:11 RafalSkolasinski

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

findleyr avatar Nov 14 '22 14:11 findleyr