Skip to content
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: better handling of “scripts” with //go:build ignore? #49657

Closed
ainar-g opened this issue Nov 18, 2021 · 18 comments
Closed

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

ainar-g opened this issue Nov 18, 2021 · 18 comments
Assignees
Labels
FeatureRequest FrozenDueToAge gopls Issues related to the Go language server, gopls. Tools This label describes issues relating to any tools in the x/tools repository.
Milestone

Comments

@ainar-g
Copy link
Contributor

ainar-g commented Nov 18, 2021

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?

@gopherbot gopherbot added Tools This label describes issues relating to any tools in the x/tools repository. gopls Issues related to the Go language server, gopls. labels Nov 18, 2021
@gopherbot gopherbot added this to the Unreleased milestone Nov 18, 2021
@findleyr
Copy link
Contributor

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
Copy link
Contributor

findleyr commented Aug 4, 2022

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

@gaoyusongcn

This comment was marked as off-topic.

@findleyr

This comment was marked as off-topic.

@gaoyusongcn
Copy link

@findleyr

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

image

@findleyr
Copy link
Contributor

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

@gaoyusongcn
Copy link

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

@gopherbot
Copy link

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

@mvdan
Copy link
Member

mvdan commented Oct 13, 2022

Oh boy, I can't wait to test this out :) Do we need to do anything special with gopls master for it to work?

@findleyr
Copy link
Contributor

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

@mvdan
Copy link
Member

mvdan commented Oct 13, 2022

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

@findleyr
Copy link
Contributor

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

go install golang.org/x/tools/gopls@v0.10.0-pre.1

@ainar-g
Copy link
Contributor Author

ainar-g commented Oct 14, 2022

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

@CHATALOT1
Copy link

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

@mvdan
Copy link
Member

mvdan commented Oct 14, 2022

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

@RafalSkolasinski
Copy link

RafalSkolasinski commented Nov 10, 2022

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
Copy link

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

@findleyr
Copy link
Contributor

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

agl pushed a commit to google/boringssl that referenced this issue Jan 31, 2023
gopls currently litters our project with a sea of red, because it
assumes Go files are part of a package, but we have a lot of standalone
Go scripts. (If there are C files in the same directory as the script,
it gets upset about cgo. If there are multiple standalone scripts in the
same directory, it gets uspet about duplicate files.)

Per golang/go#49657 and
https://github.com/golang/tools/blob/master/gopls/doc/settings.md#standalonetags-string,
the convention seems to be a go:build ignore tag. Newer versions of
gopls run in a "standalone" mode, so we still get all the nice LSP
features.

As part of this, I had to align the license header comments from /*
block comments */ to // line comments. Go build constraints can only be
preceded by blank lines and line comments. Block comments apparently
aren't allowed. (See https://pkg.go.dev/cmd/go#hdr-Build_constraints.)
If I leave the file unconverted, go fmt will immediately move the
comment to above the license block.

Change-Id: I47c69255522e9aae2bdb97a6e83fcc6ce0cf29d5
Reviewed-on: https://boringssl-review.googlesource.com/c/boringssl/+/56525
Commit-Queue: Adam Langley <agl@google.com>
Reviewed-by: Adam Langley <agl@google.com>
Auto-Submit: David Benjamin <davidben@google.com>
skmcgrail pushed a commit to skmcgrail/aws-lc that referenced this issue Mar 2, 2023
gopls currently litters our project with a sea of red, because it
assumes Go files are part of a package, but we have a lot of standalone
Go scripts. (If there are C files in the same directory as the script,
it gets upset about cgo. If there are multiple standalone scripts in the
same directory, it gets uspet about duplicate files.)

Per golang/go#49657 and
https://github.com/golang/tools/blob/master/gopls/doc/settings.md#standalonetags-string,
the convention seems to be a go:build ignore tag. Newer versions of
gopls run in a "standalone" mode, so we still get all the nice LSP
features.

As part of this, I had to align the license header comments from /*
block comments */ to // line comments. Go build constraints can only be
preceded by blank lines and line comments. Block comments apparently
aren't allowed. (See https://pkg.go.dev/cmd/go#hdr-Build_constraints.)
If I leave the file unconverted, go fmt will immediately move the
comment to above the license block.

Change-Id: I47c69255522e9aae2bdb97a6e83fcc6ce0cf29d5
Reviewed-on: https://boringssl-review.googlesource.com/c/boringssl/+/56525
Commit-Queue: Adam Langley <agl@google.com>
Reviewed-by: Adam Langley <agl@google.com>
Auto-Submit: David Benjamin <davidben@google.com>
(cherry picked from commit 54b04fdc21d540a6e24f9ddb7ddc3e583518e24f)
skmcgrail pushed a commit to skmcgrail/aws-lc that referenced this issue Mar 2, 2023
gopls currently litters our project with a sea of red, because it
assumes Go files are part of a package, but we have a lot of standalone
Go scripts. (If there are C files in the same directory as the script,
it gets upset about cgo. If there are multiple standalone scripts in the
same directory, it gets uspet about duplicate files.)

Per golang/go#49657 and
https://github.com/golang/tools/blob/master/gopls/doc/settings.md#standalonetags-string,
the convention seems to be a go:build ignore tag. Newer versions of
gopls run in a "standalone" mode, so we still get all the nice LSP
features.

As part of this, I had to align the license header comments from /*
block comments */ to // line comments. Go build constraints can only be
preceded by blank lines and line comments. Block comments apparently
aren't allowed. (See https://pkg.go.dev/cmd/go#hdr-Build_constraints.)
If I leave the file unconverted, go fmt will immediately move the
comment to above the license block.

Change-Id: I47c69255522e9aae2bdb97a6e83fcc6ce0cf29d5
Reviewed-on: https://boringssl-review.googlesource.com/c/boringssl/+/56525
Commit-Queue: Adam Langley <agl@google.com>
Reviewed-by: Adam Langley <agl@google.com>
Auto-Submit: David Benjamin <davidben@google.com>
(cherry picked from commit 54b04fdc21d540a6e24f9ddb7ddc3e583518e24f)
skmcgrail pushed a commit to skmcgrail/aws-lc that referenced this issue Mar 3, 2023
gopls currently litters our project with a sea of red, because it
assumes Go files are part of a package, but we have a lot of standalone
Go scripts. (If there are C files in the same directory as the script,
it gets upset about cgo. If there are multiple standalone scripts in the
same directory, it gets uspet about duplicate files.)

Per golang/go#49657 and
https://github.com/golang/tools/blob/master/gopls/doc/settings.md#standalonetags-string,
the convention seems to be a go:build ignore tag. Newer versions of
gopls run in a "standalone" mode, so we still get all the nice LSP
features.

As part of this, I had to align the license header comments from /*
block comments */ to // line comments. Go build constraints can only be
preceded by blank lines and line comments. Block comments apparently
aren't allowed. (See https://pkg.go.dev/cmd/go#hdr-Build_constraints.)
If I leave the file unconverted, go fmt will immediately move the
comment to above the license block.

Change-Id: I47c69255522e9aae2bdb97a6e83fcc6ce0cf29d5
Reviewed-on: https://boringssl-review.googlesource.com/c/boringssl/+/56525
Commit-Queue: Adam Langley <agl@google.com>
Reviewed-by: Adam Langley <agl@google.com>
Auto-Submit: David Benjamin <davidben@google.com>
(cherry picked from commit 54b04fdc21d540a6e24f9ddb7ddc3e583518e24f)
skmcgrail pushed a commit to skmcgrail/aws-lc that referenced this issue Mar 4, 2023
gopls currently litters our project with a sea of red, because it
assumes Go files are part of a package, but we have a lot of standalone
Go scripts. (If there are C files in the same directory as the script,
it gets upset about cgo. If there are multiple standalone scripts in the
same directory, it gets uspet about duplicate files.)

Per golang/go#49657 and
https://github.com/golang/tools/blob/master/gopls/doc/settings.md#standalonetags-string,
the convention seems to be a go:build ignore tag. Newer versions of
gopls run in a "standalone" mode, so we still get all the nice LSP
features.

As part of this, I had to align the license header comments from /*
block comments */ to // line comments. Go build constraints can only be
preceded by blank lines and line comments. Block comments apparently
aren't allowed. (See https://pkg.go.dev/cmd/go#hdr-Build_constraints.)
If I leave the file unconverted, go fmt will immediately move the
comment to above the license block.

Change-Id: I47c69255522e9aae2bdb97a6e83fcc6ce0cf29d5
Reviewed-on: https://boringssl-review.googlesource.com/c/boringssl/+/56525
Commit-Queue: Adam Langley <agl@google.com>
Reviewed-by: Adam Langley <agl@google.com>
Auto-Submit: David Benjamin <davidben@google.com>
(cherry picked from commit 54b04fdc21d540a6e24f9ddb7ddc3e583518e24f)
@golang golang locked and limited conversation to collaborators Nov 14, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FeatureRequest FrozenDueToAge gopls Issues related to the Go language server, gopls. Tools This label describes issues relating to any tools in the x/tools repository.
Projects
None yet
Development

No branches or pull requests

7 participants