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: consider supporting script directories with multiple programs #41206

Open
stamblerre opened this issue Sep 3, 2020 · 6 comments
Labels
FeatureRequest gopls Issues related to the Go language server, gopls. Thinking Tools This label describes issues relating to any tools in the x/tools repository.

Comments

@stamblerre
Copy link
Contributor

Currently, gopls only supports directories with a single main, no matter whether you open a single file or a directory. For single files, we load the entire directory, no matter what. Some users create directories with multiple mains, which they run using go run file.go. Should gopls support such directories? If so, how should it work?

@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 Sep 3, 2020
@gopherbot gopherbot added this to the Unreleased milestone Sep 3, 2020
@bcmills
Copy link
Contributor

bcmills commented Sep 4, 2020

Most single files intended for use with go run should be tagged // +build ignore or similar, and they necessarily each declare a func main. Perhaps you could use unusual build constraints plus func main as a heuristic for which mode to use?

@myitcv
Copy link
Member

myitcv commented Sep 4, 2020

Previous discussion: #33595

@myitcv
Copy link
Member

myitcv commented Sep 4, 2020

Looking at the arguments against this, if someone can come up with a good enough name for the file that contains the build-ignore main package, they can use that name for a subdirectory ./internal/$NAME thereby obviating the need for any kind of single-file support in gopls.

The counter argument to this is that there is lots of preexisting code out there that does following the single-file approach.

@mvdan
Copy link
Member

mvdan commented Jan 3, 2021

Perhaps you could use unusual build constraints plus func main as a heuristic for which mode to use?

I think this would work reasonably well. I think it would have covered nearly all the times I've encountered gopls to error in this way.

they can use that name for a subdirectory ./internal/$NAME thereby obviating the need for any kind of single-file support in gopls.

While I see the point, I disagree. There's nothing wrong with build-tag-ignored one-off programs being part of an existing package. An extra directory with just one file will be more verbose, and an oddity that has never been needed in Go before. Coming up to all those users and telling them they need to change their workflow just to please gopls, and objectively for no other good reason, seems like it won't end well :)

@quasilyte
Copy link
Contributor

quasilyte commented Dec 7, 2021

I don't think this idiom is very common, but apart from the build ignore tag there is also a pattern where you put your "scripts" under a "_" prefixed folder, like _scripts.

This excludes Go files as per-file ignore would, but still keeps them usable with go run ./_scripts/gendocs.go.

I haven't checked it with the newer versions of gopls, but ignore tag previously turned off most gopls features for the file. Or maybe it's a VSCode integration (plugin) issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
FeatureRequest gopls Issues related to the Go language server, gopls. Thinking 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
@myitcv @mvdan @bcmills @stamblerre @quasilyte @gopherbot and others