-
Notifications
You must be signed in to change notification settings - Fork 17.7k
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: improve handling for build tags #29202
Comments
Bitten by this also, lost an hour trying to figure out why after migrating to |
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:
|
This was brought up on Tuesday's golang-tools call. @christiankiely - the "editor level" 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:
We also need:
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 |
Another populate use case: when using wire, there are two copies of the initializers:
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 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
|
@evanphx I also found this issue. Now I develop the |
@zchee Ah! That makes a lot of sense, I'll start it with CGO_ENABLED=0 and see how it works. |
@evanphx |
@stamblerre |
I think we will still need special handling for build tags so that users don't need special configurations to get |
This CL leverages the new zero-config implementation to add support for dynamic build tags (golang/go#29202). While this CL is large, the gist of the change is relatively simple: - in bestView, check that the view GOOS/GOARCH actually matches the file - in defineView, loop through supported ports to find one that matches the file, and apply the necessary GOOS= GOARCH= env overlay - detect that views must be re-selected whenever a build constraint changes Everything else in the CL is supporting / refactoring around this minor adjustment to view selection. Notably, the logic to check whether a file matches a port (using go/build) required some care, because the go/build API is cumbersome and not particularly efficient. We therefore check ports as little as possible, and trim the file content that is passed into build.Context.MatchFile. Earlier attempts at this change were simpler, because they simply matched all available ports all the time, but this had significant cost (around a millisecond overhead added to every operation, including change processing). However, the good news is that with the logic as it is, I believe it is safe to support all available ports, because we only loop through this list when checking views, an infrequent operation. For golang/go#57979 For golang/go#29202 Change-Id: Ib654e18038dda74164b57d51b2d5274f91a1306d Reviewed-on: https://go-review.googlesource.com/c/tools/+/551897 TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: Alan Donovan <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Run-TryBot: Robert Findley <[email protected]>
Hi folks! I'm working on landing improvements to build tag support for [email protected]. 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 If you're interested in being an early tester, you can install the first prerelease:
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 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. |
@findleyr re:
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 ( //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 ( func TestRot13(t *testing.T) { ... } We also have a solution file ( //go:build solution
package cipher These files are in the same folder like this: cipher/
├── rot13.go
├── rot13_sol.go
└── rot13_test.go From the command line I can test that the template version fails because the % go test
--- FAIL: TestRot13 (0.00s)
rot13_test.go:33: rot13.Read("Go programming is fun."): (-want +got):
... And similarly, that the % go test -tags solution
PASS
ok dat520/lab1/gointro/cipher 0.280s This 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 However, I'm unaware of any solution to avoid the error:
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
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 |
@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. |
@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 ( 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. |
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 |
Yes, we do have common unconstrained file! |
@meling I think the problem is that we could perhaps make 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!) |
[email protected] 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:
(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. |
We have a number of tests that we do not appear to be running in CI. In addition, we are not running go vet with all suitable build tags resulting in some tests being skipped [1]. Combined, this has resulted in a number of tests that have not been fully updated to use context. While we will address the root cause elsewhere, we can fix the missed updates here. You can validate this fix by overriding GOFLAGS, as outlined in [1]. For example, from the root directory: GOFLAGS="-tags=acceptance" go vet ./... [1] golang/go#29202 Signed-off-by: Stephen Finucane <[email protected]>
We have a number of tests that we do not appear to be running in CI. In addition, we are not running go vet with all suitable build tags resulting in some tests being skipped [1]. Combined, this has resulted in a number of tests that have not been fully updated to use context. While we will address the root cause elsewhere, we can fix the missed updates here. You can validate this fix by overriding GOFLAGS, as outlined in [1]. For example, from the root directory: GOFLAGS="-tags=acceptance" go vet ./... [1] golang/go#29202 Signed-off-by: Stephen Finucane <[email protected]>
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.
The text was updated successfully, but these errors were encountered: