x/tools/gopls: improve handling for build tags
stamblerre opened this issue ยท 83 comments
We need to handle build tags correctly in the LSP. If a user is on Linux, they should still see completions, diagnostics, etc. for files tagged with any other OS.
Bitten by this also, lost an hour trying to figure out why after migrating to gopls basic functionalities didn't work...no import-organize, no format, no nothing. Afterwards I found that it's was working properly for everything except for files with build tags as mentioned in #31286.
I'm wondering whether this is the right issue to become the umbrella issue for our long-running discussion about context?
This is killing me with integration tests that have build tags. Any workarounds known?
@sjdweb current workaround I'm using is to set the GOFLAGS environment variable. This is the configuration I'm using in VS Code:
"gopls.env": {
"GOFLAGS": "-tags=test"
},
This was brought up on Tuesday's golang-tools call.
@christiankiely - the "editor level" GOFLAGS approach your describe works in some cases, but not all. Because setting the flag in that way sets the flag for all modules and packages, i.e. for the gopls instance used by the editor.
In discussions we've had thus far it seems for a complete solution we need to be able to define the sets of valid build flags at various levels:
- Editor level (i.e. throughout editor)
- Module level
- Package (tree) level
- File level
We also need:
- the ability to specify, from within the editor, which build flags are in operation, because for a given file/package/module there might be multiple valid options
- the ability to then pass this choice on a per request basis to
gopls
The conclusion on Tuesday's call was broadly that there are other higher priority issues right now; add to that, a solution that doesn't involve LSP spec changes isn't exactly jumping out at us.
Bitten by this as well. I use build tags to distinguish between normal builds and debug builds. Now VSCode reports logError redeclared in this block error and the file is constantly marked as having errors.
Another populate use case: when using wire, there are two copies of the initializers:
- The template in a file with a build tag that never gets compiled
- The real one generated by wire with no build tags
gopls highlights them as duplicate definitions because it ignores build tags.
Is there a short term workaround in this simpler use case? Some way to make gopls ignore files with the wireinject build tag?
Thank you for sharing this info. We realize that this is an issue that affects a lot of people, so we're trying to come up with a viable solution. It's a complicated problem, but to start, I think we can try to disable diagnostics on files that would be excluded given your GOOS/GOARCH.
As a data point, I'm experiencing this today in any package that picks up os/user because it uses build tags to decide how to wire in OS user lookup. In my case, it's that analysis fails because cgoLookupHost isn't defined as files aren't being evaluated.
I understand that you'd like for gopls to allow for reading and analysis of files intended for a different GOOS that the one that the editor is on, but given how packages are structured, you'd having to perform analysis on the matrix of build tags. That seems.... time consuming at best. Plumbing through a default tag set seems like an easy way to at least get folks moving again.
I'm happy to look into how to do that if need be! Please just let me know.
Looks like it's also firing from net looking like:
/usr/local/Cellar/go/1.12/libexec/src/net/lookup_unix.go:81:24: undeclared name: cgoLookupHost
@evanphx I also found this issue.
The workaround is launch gopls use CGO_ENABLED=0 or something. Note that might be vscode can't that workaround AFAIK. Also not solved if set CGO_ENABLED=0 to env. Because it is late to place.
#29202 (comment)
Now I develop the client side for IDE written in Go, it's implemented set some env to launch gopls binary using set to exec.Command.Env. I did solve the use it.
Anyway, We should including CGO_ENABLED=0 to gopls send Build info.
@zchee Ah! That makes a lot of sense, I'll start it with CGO_ENABLED=0 and see how it works.
@evanphx
Also, I was send CL (but I think still not user friendly) for support build tags.
https://go-review.googlesource.com/c/tools/+/178782
It solved if your code needs any build tags such as purego, static.
@stamblerre
Can we close this issue by below CL?
I think we will still need special handling for build tags so that users don't need special configurations to get gopls features in files tagged for different build systems.
@stamblerre
I see, totally agreed.
@sjdweb current workaround I'm using is to set the GOFLAGS environment variable. This is the configuration I'm using in VS Code:
"gopls.env": { "GOFLAGS": "-tags=test" },
This incurs an Unknown configuration setting error in VS Code 1.36.1 with gopls v0.1.3, and instead I have to use the configuration below to make it work.
"go.toolsEnvVars": {
"GOFLAGS": "-tags=test"
},
I recently opened #33389 to get a better sense of the use cases for build tags. Please add your own use cases if they are not yet listed in this issue - it will help us better understand how to solve this issue.
+1
I'm having issues with VSCode flagging my different function implementations for different versions of go as duplications
RS: You probably already know this, gopls does not handle mutually exclusive build tags at all. I have one file with '// +build debug', and an almost identical file in the same folder with '// +build !debug'. gopls flagged every constant, variable and function as being redefined between the two files. Good luck with the fix.
#39005 might be of interest to the people following this thread. It's an attempt at defining a build-system-agnostic list of build configurations for a project, in a way that can be consumed by many tools like staticcheck and gopls.
For anyone having issues with wire + VSCode flagging auto generated function implementations as duplications, edit the project settings json and put this:
"go.buildTags": "wireinject"This solved for me. Have a downside that now your wire.go file will not have any code completion and validation, but you can just uncomment the build flag when developing to get those back.
in coc.nvim+coc-go, for wire inject, add:
"go.goplsOptions": {
"buildFlags": ["-tags=wireinject"]
},
Is there any timeline on releasing support for build tags in gopls, and consequently in vscode?
We will not add any new behavior to improve build tag handling before gopls/v1.0. For now, the best way to work with gopls and build tags is by using the configurations suggested above.
@stamblerre What's the exact property that needs to be set in settings.json (vscode) to switch build tags?
I would suggest the work-around described in this comment: #29202 (comment).
for a variable being overwritten based on build constraints, I tried every suggested config in this thread. it doesn't work.
here is the overwritten case:
- when edit a file, such as internal/lsp/lsprpc/lsprpc.go ,
- you see a statement in line 289, (in func connectToRemote): "network, address = autoNetworkAddress(goplsPath, inAddr)",
- now, to find the autoNetworkAddress func, you send a 'definition' query to the gopls server.
- in current implementation, 'definition' query will return the internal/lsp/lsprpc/autostart_default.go result. and the editor jump to that file directly. this implementation will mislead the developer. i checked the code several times and finally notice the 'build tags'
the propose is to return the internal/lsp/lsprpc/autostart_posix.go , the behavior of 'definition' query should consider the 'build tags', in this case, it is "// +build darwin dragonfly freebsd linux netbsd openbsd solaris".
any suggestion on this case? @stamblerre
To be able to edit windows-specific golang code ("xxx_windows.go") requiring windows-only golang packages on my OSX machine, I had to add this to my VSCode settings:
"go.toolsEnvVars": {
"GOOS": "windows"
}"gopls.env": {
"GOFLAGS": "-tags=test"
},
Where does this go? I tried settings.json and I get:
Unknown Configuration Setting
@Merrit: That's correct--the unknown configuration setting message is a warning, but it will still work.
I see. Am I misunderstanding the purpose then?
For example in one of my _linux.go files I am still getting No packages found for open file, unable to access functions from this file, etc.
If I add // +build windows everything works as expected.
Are you working on Windows / adding the Windows tag to the Linux file? OS-specific tags are detected based on your OS, so unless you set GOFLAGS to -tags=linux, you won't get editor features working for the _linux.go files.
Yes, currently working on Windows & having issues working with *_linux.go files. Not sure why I would want to be unable to reference or work on Linux files just because I am on Windows, or vice versa.
Setting this in settings.json seems to have it working, so far:
"gopls.env": {
"GOFLAGS": "-tags=linux"
},Thank you!
Spoke too soon.
Editor features work fine, but it then fails to run / debug.
undefined: funcFromLinuxFile
I tried adding "buildFlags": "-tags=linux" to launch.json, but that makes it try to compile the linux version which fails quite spectacularly. :)
Also tried "buildFlags": "-tags=test", "buildFlags": "-tags=!linux" and "buildFlags": "-tags '!linux test'".
Only solution so far is to just stop naming things with the *_os.go scheme, which I was given to understand was the preference for conditional compilation. FYI, naming the files featureLinux.go and leaving it be works just fine. Adding // +build linux to that same file breaks things the same as using the *_linux.go naming scheme.
Not sure why I would want to be unable to reference or work on Linux files just because I am on Windows, or vice versa.
This is a current limitation of gopls, and this issue is tracking improving that behavior.
I tried adding
"buildFlags": "-tags=linux"to launch.json, but that makes it try to compile the linux version which fails quite spectacularly. :)
You won't actually be able to run code meant for Linux on Windows--my suggestion was just to get editor features for development purposes.
Right, I don't want to run or compile anything meant for a different OS. Just mentioning what I had tried to resolve my issue.
Ideally what I would like is:
- Name files by OS to take advantage of conditional compilation, ie feature_windows.go & feature_linux.go
- Editor features still work, eg it recognizes the packages, doesn't complain about undeclared variables or functions, etc just because those variables/functions are in a feature_linux.go when I am on Windows, or vice versa.
- Still be able to use VSCode's run/debug features (for the code related to the OS I am on).
Currently I can only get 2 & 3 on my list by not naming files by OS nor using the // +build _____ -- which I understand means no conditional compilation.
Wanted to be able to use go generate in a directory to get a go run gen.go called and found gopls doesn't seem to work on the gen.go file because of a mix of build flags in other local files. Can I work around this by invoking vim on gen.go with an environment variable set to something for the benefit of gopls or is moving the gen.go into its own directory my best option?
gopls definition does work from a file that has an ignore build tag, but not if a plain go file in the same directory exists.
$ cat -n gen.go
1 // +build ignore
2
3 package main
4
5 import (
6 "fmt"
7 )
8
9 func main() {
10 fmt.Println("Hello world!")
11 }
$ gopls definition gen.go:10:10
gopls: /<snip>/gen.go:10:10: no packages returned: packages.Load error
$ rm config.go
$ gopls definition gen.go:10:10
/<snip>/go1.15.5/src/fmt/print.go:273:6-13: defined here as func fmt.Println<snip>The command line doesn't really give you much configurability, but you can set the GOFLAGS environment variable before running the gopls command. Otherwise, you can configure gopls.buildFlags or GOFLAGS as described in the examples above.
I am facing this issue as well, appending settings.json with "gopls.env": { "GOFLAGS": "-tags=debug" },does not resolve the issue for me.
In my case I have two files thermal.go and thermal_mock.go, with build tags // +build !debug and // +build debug respectively, so I can mock target HW specific functions.
My biggest issue is that Intellisense completely stopped working in the _mock version (as in, worked the other day, next day poof) , and the gopls.env in settings.json didn't help either.
Go 1.15.5, gopls v0.6.0
Any follow up on the issue?
Edit: Loss of intellisense can be temporarily fixed by removing the build tag instruction, saving and readding it.
In my case I have two files
thermal.goandthermal_mock.go, with build tags// +build !debugand// +build debugrespectively, so I can mock target HW specific functions.My biggest issue is that Intellisense completely stopped working in the
_mockversion (as in, worked the other day, next day poof) , and the gopls.env insettings.jsondidn't help either.
Would be happy to help investigate this, but please file a new issue with the following information: https://github.com/golang/tools/blob/master/gopls/doc/troubleshooting.md.
Note for VSCode settings.json in case it's helpful. A bit after upgrading gopls to 0.6.2 I noticed some No packages found for open file... errors. I was using GOFLAGS to list build tags.
"gopls": {
"env": {
"GOFLAGS": "-tags=<tag1>,<tag2>"
},
},
Switching to the go.buildFlags configuration below seemed to solve the problem. I no longer use GOFLAGS.
go.buildFlags: "<tag1>,<tag2>",
There might have also been a vscode-go release around the time I bumped my gopls version, but not sure.
The workarounds here didn't work for me.
I found
env GOOS="openbsd" code
Worked for me at the editor instance level
P.s. Irrelevant tip: if you also set HISTFILE and open a module folder after "code" then you can have dedicated terminal history and a project folder opened together.
Hello,
I'm facing this issue on linux using emacs. Have tried suggestions above and the one that is working is to delete and re-adding build tag line.
I also tried using VS Code with the same result.
I was finally able to get this working in VS Code by using the following and restarting VS Code.
{
"go.buildTags": "tag1,tag2"
}I think gopls shouldn't care about build constraints, or it only cares about the file I'm editing, which I call "constraint aware".
This and #49657 were the only relevant results for my internet searches, so, for historical reasons, my solution for emacs is to use setenv at the global scope and modify GOFLAGS. This isn't perfect but has worked for me:
(setenv "GOFLAGS" "-tags=linux,windows")Is there a plan to make something like this a permanent in gopls? Is there some problem with just specifying multiple platform tags by default? It hasn't seemed to cause any problems for me specifically. It doesn't make much sense to me that gopls would care about the platform, and simply failing for platform-tagged files breaks the entire workflow of developing cross-platform applications. This is almost certainly an undocumented bug in gopls, unless I missed some documentation some where (which I'd be happy if someone shared ๐).
@calebstewart That's insteresting. gopls heavily depends on go list and other go commands to parse go code, and by your setting -tags=linux,windows, the flag is passed to all the go commands gopls uses. If you see gopls and so go commands work with -tags=linux,windows, that's by accident, not intentional.
And, more puzzling thing is that AFAIK platform (os/arch) are special and go commands select them based on GOOS/GOARCH environment variables, not based on the -tags flag in most cases. So, I am not sure what's working and what's not working with specifying OSes using -tags.
for vscode users, update your PROJECT_ROOT/.vscode/settings.json like below may resolve this problem:
{
"go.toolsEnvVars": {
"GOOS": "js",
"GOARCH": "wasm"
}
}@hyangah interesting, it could have been some configuration specific to this project (it's a work project, and I'm not the one who configured it), then, but that seemed to be the best approximation of the suggestions I was seeing online. Admittedly, I'm not exactly sure why it worked for me either, but all I can say is that it did. ๐
I have a similar problem, I have separate files (config_windows.go, config_linux.go, config_darwin.go) for setting platform-specific configs, and by default, I don't get any code completion on Windows or darwin codes (I'm using VSCode on Linux) and it shows the following message:
No packages found for open file /home/amir/projects/Memor/memor/pkg/config/config_darwin.go: <nil>.
If this file contains build tags, try adding "-tags=<build tag>" to your gopls "buildFlags" configuration (see (https://github.com/golang/tools/blob/master/gopls/doc/settings.md#buildflags-string).
Otherwise, see the troubleshooting guidelines for help investigating (https://github.com/golang/tools/blob/master/gopls/doc/troubleshooting.md).go list
after adding the following tags I'll get a duplicate declaration error on these files because I'm declaring the same things in these 3 files.
"gopls": {
"build.buildFlags": ["-tags=linux,windows,darwin"],
}@ahmafi I can reproduce the same error messages from vscode/gopls with a trivially simple project using containing 3 files:
common.go
package main
import "fmt"
func common() {
fmt.Println("This is common")
}xmain.go
//go:build xmain
package main
import "fmt"
func main() {
fmt.Println("This is xmain")
common()
}ymain.go
//go:build ymain
package main
import "fmt"
func main() {
fmt.Println("This is ymain")
common()
}With no gopls build flags defined, I get the "no packages found" message. If I add the following to settings.json, I get the duplicate declaration error.
"gopls": {
"build.buildFlags": [
"xmain",
"ymain",
],
},There are no errors reported when I run go vet on the project and I'm able to build and run either variant, e.g. go build -tags xmain without errors.
@stamblerre This is a major limitation for one of my current projects. I'd love to see a solution or a viable workaround.
@Michael-F-Ellis you should be able to work on xmain OR ymain by setting just one of those two build tags, the same as you do when you run go build -tags xmain. As described in #49104 (comment), go vet and go build have the same limitations as gopls: only one set of build tags can be active at a given time.
The critical missing feature for gopls is the ability to edit using multiple sets of build tags simultaneously.
Bump
Hello,
Is this topic still in progress or should we not expect any improvement regarding this limitation?
Thanks,
Best regards
Hi all, this issue deserves an update (and sorry @alexgille for missing your inquiry above). Let me summarize the current state of things:
We know that support for multiple sets of build tags is one of the most desired features for gopls. Unfortunately, build tags pose a fundamental problem that we can't fix without changing many other things about gopls. Because several of gopls' algorithms rely on having an active graph of go/types.Packages in memory, and go/types relies on the canonical identity of its objects, gopls can't simply process multiple sets of build tags without maintaining two copies of the package graph (because ~everything will at some point in its transitive imports depend on a package for which there are multiple definitions based on build tags). The package graph drives most of gopls' memory usage, so maintaining N copies of the package graph corresponds to approximately an N-fold increase in memory (and often CPU). Given that memory usage is a huge pain point for our users, a solution that depends on throwing memory at the problem will probably not be viable for many users.
Even if that solution is acceptable for some users with small workspaces, it poses a significant burden on the code in an area where we have historically had a lot of bugs: package metadata tracking and package invalidation. I am currently trying to refactor that area of gopls and fix all the bugs. In gopls@v0.9.0 the representation of metadata was refactored, which contributed a piece of the performance improvements observed in that release. In gopls@v0.9.2, I'm trying to fix all the outstanding bugs we know about related to metadata. So here also, we are hesitant to throw more complexity at the problem.
Longer term, an even higher priority for us is to make gopls scale better and load faster. In order to achieve this, we will inevitably need to break our dependence on the package graph. The first step toward this is rewriting key algorithms to be based on data structures that can be incrementally updated at the package level. Once we do this, the problem of handling multiple sets of build tags becomes tractable.
So resolving this issue can't realistically happen soon, but is part of our longer terms goals for gopls.
In the meantime, I think we can still improve the status-quo. For example:
- we can make it easier to switch the set of active build tags (through better handling of configuration changes, code lenses on build constraints, etc.)
- we can resolve #49657, making it easier to work with packages defined by a single build tag (e.g.
ignoreormage).
For nvim-lspconfig users (with nvim-lsp-installer) following settings worked for me:
lspconfig.gopls.setup {
settings = {
gopls = {
env = {
GOFLAGS = "-tags=windows,linux,unittest"
}
}
}
}with files looking like
thing_test.go:
//go:build unittest
package thing_testand thing.go
package thingall lsp features work.
Cross-posting from golang/vscode-go#1799:
Is there an alternative without relying on setting env variable in VS code settings?
VS code doesn't support nesting settings so it's annoying when you mix go server code and go wasm code or more broadly different architectures.
Maybe a top-level comment in the file? This seems to have been mentioned at the begging of this thread: #29202 (comment), #29202 (comment)
(there are many messages, sorry if it has already been mentioned/answered)
@eric-burel as a workaround you can open projects in different VS Code instances. WASM client code can be placed somewhere in project dir and can have it's own workspace settings in .vscode/settings.json. I'm using it this way for many years already.
For emacs and eglot users:
(setq-default eglot-workspace-configuration
'((:gopls
(:ui.completion.usePlaceholders . t)
(:ui.diagnostic.staticcheck . t)
(:build.buildFlags . ["-tags" "sometag"]))))Has there been any update on this? Trying to build wasm plugins for my go program and this is obnoxious to deal with.
This problem is still there, att least a more friendly diagnose tip should be given.
@Torwalt
This still gives a compiler: TestFunc redeclared in this block (see details) [DuplicateDecl] , but I guess it's an improvement ๐
Also I can not get this to work with //go:build windows in one file and //go:build !windows in another. (I guess this makes sense because these can't both be true at the same time...)
You can also use this instead of the env, which I find a bit cleaner:
settings = {
gopls = {
buildFlags = { "-tags=linux,windows,darwin" },
},
},
I currently work in a repo that uses build tags for test files VS Code throws following (inaccurate) error about workspaces when a build flag is present. Setting the tags in the env variables sent to gopls does work around it, I just wanted to inform of another instance of this issue.
This file is within module ".", which is not included in your workspace.
To fix this problem, you can add a go.work file that uses this directory.
See the documentation for more information on setting up your workspace:
https://github.com/golang/tools/blob/master/gopls/doc/workspace.md.go list
@mxygem can you say more about how you're using build tags in test files? Which build tags, and how are you using them?
Omg @Torwalt thank you!!
Change https://go.dev/cl/551897 mentions this issue: gopls/internal/lsp/cache: proof of concept for dynamic build tag support
Hi folks! I'm working on landing improvements to build tag support for gopls@v0.15.0. Specifically, https://go.dev/cl/551897 added logic so that when you open a file constrained by GOOS or GOARCH, gopls will try to find a GOOS/GOARCH combination that includes that file, and will start tracking a build for the new configuration. This means that most gopls functionality will Just Work when, for example, you open up a *_darwin.go file while working on linux.
If you're interested in being an early tester, you can install the first prerelease:
go install golang.org/x/tools/gopls@v0.15.0-pre.1
In the workaround suggested above (#29202 (comment)), gopls loads only one build configuration, modified to include files for both linux and windows, but this will often lead to spurious build errors for duplicate declarations. Now that it supports multiplexing across multiple builds (#57979), gopls can finally support multiple GOOS/GOARCH combinations simultaneously. If you are testing out the prerelease, please remove any buildFlags configuration from your settings.
If you encounter problems, please let me know and revert to gopls@latest. This is a rather large change to the way gopls operates, and so will require some time to polish before it can be released. Among the potential new bugs, we may encounter new performance edge cases, or possibly conflicting diagnostics from different builds (though I have tried to mitigate both). Additionally, we may require additional UX improvements to make it clearer which build configuration is being used for a given file.
One other area where we could improve build tag support is by automatically handling go version constraints (e.g. //go:build !go1.20). After consulting with the team and Go command experts such as @bcmills and @matloob, we think it is too complex and potentially problematic to support dynamic builds at different Go versions. We also expect that this is an order of magnitude less important than GOOS/GOARCH support -- let us know if you disagree.
@findleyr re:
@mxygem can you say more about how you're using build tags in test files? Which build tags, and how are you using them?
I may have a similar scenario to what @mxygem described here.
I have a number of exercises for students, where I provide a template to ensure that our tests are compatible with a student submitted solution. Here is an example:
Template provided to students (rot13.go):
//go:build !solution // This line is actually removed before we provide it to students.
package cipher
type rot13Reader struct{ r io.Reader }
func (r rot13Reader) Read(p []byte) (n int, err error) {
return 0, nil
}Test provided to students (rot13_test.go):
func TestRot13(t *testing.T) { ... }We also have a solution file (rot13_sol.go) that we don't provide to students:
//go:build solution
package cipherThese files are in the same folder like this:
cipher/
โโโ rot13.go
โโโ rot13_sol.go
โโโ rot13_test.goFrom the command line I can test that the template version fails because the solution tag is not set:
% go test
--- FAIL: TestRot13 (0.00s)
rot13_test.go:33: rot13.Read("Go programming is fun."): (-want +got):
...And similarly, that the rot13_sol.go version passes the tests because the solution tag is set:
% go test -tags solution
PASS
ok dat520/lab1/gointro/cipher 0.280sThis approach is convenient because I can keep the template, test, and the solution in same folder.
With VSCode, I can do:
"gopls": {
"buildFlags": [
"-tags=solution"
]
},To enable compilation of the rot13_sol.go file, and switch to -tags=!solution to enable compilation of the rot13.go file.
However, I'm unaware of any solution to avoid the error:
No packages found for open file ... /lab1/gointro/cipher/rot13.go.
This file may be excluded due to its build tags; try adding "-tags=<build tag>" to your gopls "buildFlags" configuration
See the documentation for more information on working with build tags:
https://github.com/golang/tools/blob/master/gopls/doc/settings.md#buildflags-string.
Apologies for the long message, and the fact that I didn't take the time to read through the entire issue history. Anyway, I hope this can serve as another example of how tags are being used; I suspect others use it similarly.
@meling thanks for explaining your use-case. Indeed, there's no way to avoid the error you report in whichever file is excluded by the current configured build tags. In general, there's nothing we can do about this automatically, since it's not possible to guess the set(s) of build tags to use.
Theoretically, since we now support multiple builds per folder, we could accept something like
"gopls": {
"buildFlags": [
[],
["-tags=solution"]
]
},
I.e., overload the "buildFlags" setting to accept multiple sets of flags. I'd really like to avoid this if possible, because it complicates the configuration significantly. I wonder how many others would be interested in configurations like this.
@findleyr is there any point in using "gopls.BuildFlags" instead of "go.buildTags"?
@inliquid I believe "gopls.buildFlags" affects only gopls, whereas "build.buildFlags" affects all functionality (gopls, test, linter, debugger, etc.), which may or may not be desirable. "go.buildTags" is an older configuration mechanism that was generalized to "buildFlags".
@findleyr thanks for your reply.
For my "simple" use-case where there is a binary choice, would it be possible to recognize the tag of the current editor window and just remove the error from source files whose tag is the negation? And then switch if the active editor window changes to the negated tag.
It is not clear to me if dynamically "guessing" based on active editor window is possible.
@meling you're right, we could do that if there is only one build tag, though probably not anything more complicated. For example, we do have support for tags that are used to isolate scripts, such as //go:build ignore, via the "standaloneTags" configuration, and that looks for files with a build constraint consisting of a single tag.
@findleyr thanks for the pointer. Would it be possible to lift the restriction: Gopls considers a file to be a standalone main file if and only if it has package name "main" ?? Maybe then my use case would work. Or would it be preferable to support my use case via a separate configuration option?
Either way I appreciate the work you are doing to improve the plugin and gopls. Thank you so much!
@findleyr I am trying out the new gopls and it seems to work perfectly.
The only thing that I'm wondering is if I'm using go to definition on a function that is both declared in a file with a Windows and Linux build flag it goes to the Linux implementation (which makes sense, I'm on Linux).
Is it meant to show both definitions (similar to go to implementation) or is this expected behaviour?
@arnevm123 thanks for testing, and regarding your question this is expected behavior. For each open file there is one "default" build, which is first and foremost the default build on your system, followed by the first match from here. This ordering is somewhat arbitrary, though first class ports are preferred. We do need a fixed order for determinism.
I'd prefer if we could return all definitions, but unfortunately for technical reasons this is really intractable. Note that a file without any build constraints matches all ports. We can't feasibly return every single definition across any port, because this would be much more expensive for little value in the common case. The toolchain (go list and the type checker) only operates on one build context at a time, so to answer questions about multiple build contexts we need to do additional work for each. It is infeasible to do 50x the work (there are around 50 supported ports), so we'd have to try to be smarter by e.g. looking at ignored files in the same directory as the definition to see if there are other declarations of the same symbol. At that point our heuristic is liable to be flaky and/or buggy, and is tightly coupled to the go command (taking us even farther from supporting other build systems). Still, it may be worth pursuing at some point.
For now, I think we should keep it simple. We're going from gopls completely not working to mostly working, albeit with some surprises. Of course we'd greatly appreciate any alternative suggestions, or ways that we could make the current behavior more transparent.
Gopls considers a file to be a standalone main file if and only if it has package name "main" ?? Maybe then my use case would work.
I think there's a fundamental difference between scripts, for which there may be many in the same directory, each consisting of a single file, and your use case, for which you want to have at most two versions of your package in the directory. In your use case, I assume you want gopls to resolve packages declarations contained in other unconstrained files in the same directory (for example a common.go file that is shared by both the solution and !solution builds).
Gopls considers a file to be a standalone main file if and only if it has package name "main" ?? Maybe then my use case would work.
I think there's a fundamental difference between scripts, for which there may be many in the same directory, each consisting of a single file, and your use case, for which you want to have at most two versions of your package in the directory. In your use case, I assume you want gopls to resolve packages declarations contained in other unconstrained files in the same directory (for example a
common.gofile that is shared by both thesolutionand!solutionbuilds).
Yes, we do have common unconstrained file!
@meling I think the problem is that we could perhaps make //go:build solution work automatically by creating a new build with -tags=solution overlaid, but then users may be confused when opening a file tagged //go:build (solution || experiment) doesn't work automatically, and we don't want to get into the business of solving constraint equations. The special handling for //go:build ignore, is justifiable because it's an extremely common pattern for scripts.
If enough users want this, we can reconsider. But for now, for the same reasons as #29202 (comment), let's keep it simple.
(and thanks again for the feedback!)
gopls@v0.15.0 will be released soon (likely next week) containing the improvements discussed above. If you're interested to try it out, please install the latest prerelease, which is the release candidate:
go install golang.org/x/tools/gopls@v0.15.0-pre.4
(this differs from -pre.3 by just one minor CL)
With this release, I think working on multiple GOOS and GOARCH combinations is much better, though still not perfect. To follow up, I filed:
This is in addition to @meling's:
I think we're ready to close this issue in favor of the more specific follow-up items above. Please try out the release and let us know if you have feedback, either by commenting on or ๐'ing one of the issues above, or filing a new issue describing the improvement you'd like to see (or bug you observe). Thanks!
To clarify one point: the issue title "improve handling for build tags" could certainly be interpreted as also referring to better support for custom build tags, in addition to multiple GOOS/GOARCH. However, the issue description specifically mentions "completions, diagnostics, etc. for files tagged with any other OS", and I think we've achieved that in the v0.15.0 release.
I think better support for user-defined tags is covered by these two issues specifically:
Those issues both address ways that gopls could support working on multiple sets of user-defined tags. In #65089, it is proposed that gopls could automatically detect when there is one tag that separates files in a package. In #65757, it is proposed that users could explicitly configure multiple sets of tags.