go/tools: add gopackagesdriver#2858
Conversation
This commit introduces the GOPACKAGESDRIVER for rules_go Signed-off-by: Steeve Morin <steeve@zen.ly>
Signed-off-by: Steeve Morin <steeve@zen.ly>
Those files are generated and will end up in the temporary mod cache, which isn't available later on. Signed-off-by: Steeve Morin <steeve@zen.ly>
Signed-off-by: Steeve Morin <steeve@zen.ly>
This handles some edges cases in which the import path last part is not the package name. Signed-off-by: Steeve Morin <steeve@zen.ly>
It's no use, and it can be a significant amount of files, so disable it. Signed-off-by: Steeve Morin <steeve@zen.ly>
The indexing by ID was never used except for iterating on all packages. The file index isn't used either since the whole graph is dumped anyway. Signed-off-by: Steeve Morin <steeve@zen.ly>
Needed when in a CGo environment for go list to work Signed-off-by: Steeve Morin <steeve@zen.ly>
Fetch the stdlib JSON from the deepest target, and cascade it upward so that transitions are applied properly. Also, this enables applying the aspect to a target that depends on a go_binary, such as a cc_binary with proper transition applied. Signed-off-by: Steeve Morin <steeve@zen.ly>
This is more correct. Signed-off-by: Steeve Morin <steeve@zen.ly>
This ensures that all possible packages are built Signed-off-by: Steeve Morin <steeve@zen.ly>
The key types have to be copied and paster so might has well remove the dependency altogether. In the process, rework how the LoadMode is passed. Signed-off-by: Steeve Morin <steeve@zen.ly>
Signed-off-by: Steeve Morin <steeve@zen.ly>
Since the packages driver is meant to be run via `bazel run`, it's simpler to leverage BUILD_WORKSPACE_DIRECTORY. It's one less parameter. Signed-off-by: Steeve Morin <steeve@zen.ly>
Signed-off-by: Steeve Morin <steeve@zen.ly>
|
Made some changes regarding UX, should be simpler now. |
Signed-off-by: Steeve Morin <steeve@zen.ly>
Will be easier when debugging. Signed-off-by: Steeve Morin <steeve@zen.ly>
Sometimes if the aspects explores nodes that won't have go_ rules, there is no inner stdlib_json to fetch. Signed-off-by: Steeve Morin <steeve@zen.ly>
Signed-off-by: Steeve Morin <steeve@zen.ly>
Now that https://github.com/bazelbuild/rules_go has a working prototype of a `GOPACKAGESDRIVER`, it may be time to revert that commit. The draft implementation is at bazel-contrib/rules_go#2858. Change-Id: Ia738e8be448d936f8a3b2b421d0a765f94bbff52 GitHub-Last-Rev: 0df6c91 GitHub-Pull-Request: #297 Reviewed-on: https://go-review.googlesource.com/c/tools/+/307169 Reviewed-by: Rebecca Stambler <rstambler@golang.org> Reviewed-by: Heschi Kreinick <heschi@google.com> Trust: Rebecca Stambler <rstambler@golang.org> Run-TryBot: Rebecca Stambler <rstambler@golang.org> gopls-CI: kokoro <noreply+kokoro@google.com> TryBot-Result: Go Bot <gobot@golang.org>
|
golang/tools#297 / https://go-review.googlesource.com/c/tools/+/307169 has been merged! |
|
The check failure seems transient and unrelated to the PR: Click to expand! |
Use special flags in order to speed up the bazel query Signed-off-by: Steeve Morin <steeve@zen.ly>
Signed-off-by: Steeve Morin <steeve@zen.ly>
Signed-off-by: Steeve Morin <steeve@zen.ly>
Signed-off-by: Steeve Morin <steeve@zen.ly>
simply print it and exit(1) Signed-off-by: Steeve Morin <steeve@zen.ly>
…cher Signed-off-by: Steeve Morin <steeve@zen.ly>
Since root packages are now properly computed, walk the package graph from them. This saves on JSON payload size a bit. Signed-off-by: Steeve Morin <steeve@zen.ly>
|
@jayconrod great review, I pushed most of the fixes |
|
My understanding is that this will fail if the project is library-only and doesn't contain any binary -- because of the bazel query. Any ideas? |
|
@pierreis it should work with a library too |
|
I am getting the following error -- which disappears as soon as I create a binary target: |
|
It looks like your GOPACKAGESDRIVER_TARGETS or _QUERY isn't matching anything. On the example I did make it match to go_binary, you should tweak it. |
|
Right, I did assume from the instructions that it had to be this query for some reason -- incorrectly it seems. Thanks! |
|
Is this waiting on anything else? |
|
I think this is ready to merge, but we're still sorting out a repo access issue. We'll probably do a minor release with this soon after. |
|
Is there any more work to do regarding error handling? |
|
Maybe it should return |
|
My biggest pain point with the package driver at this moment is that, when using it, I loose all autocompletion or error highlighting as soon as the project fails to compile for any reason. Fixing the issue without error highlighting is much more painful than it should be. |
|
What's the latest status of this PR? It'd be soooo nice to have this merged. Improvements could happen iteratively. |
|
@steeve thank you for the hard work on this one. I'm delighted to open the bug report floodgates. |
|
Thank you @steeve! This is amazing. I'll start preparing for a minor release this week or next week. |
|
Great!!! When using gopackagedriver with The workspace changes are caused by the Putting |
|
@yancl I experienced the same thing and realized when copying the . That seemed to also fix it for me :) |
|
@raymondpebble Thanks for your information. The problem disappeared after I upgraded the version of the The |
This makes non-Windows use the script bootstrap by default. It's been a couple releases without any reported issues, so it seems ready to become the default. Work towards bazel-contrib/rules_python#2156
This commit introduces an aspect based
GOPACKAGESDRIVERforrules_go.It is rather clunky, but all VSCode +
goplsfeatures work with it:It does not support static usage at the moment (for instance by running it inside an action for codegen tools), but it could with few additions.
Usage
The packages driver fundamentally works from the perspective of one or more targets. In order
for it to function properly, the targets must be specified in the packages driver configuration,
with environment variables.
These are:
GOPACKAGESDRIVER_BAZEL_TARGETS: specifies specific targets,//...works too (although this is not recommended without tag filters). It is possible to set targets to something other thango_rules, as long as thego_nodes are reachable via thedepsattribute in the graph. Such as whengo_binary(linkmode = "c-archive") -> cc_library.GOPACKAGESDRIVER_BAZEL_QUERY: runs abazel queryto select targets before, common one should bekind(go_binary, //...). This should work for most cases.GOPACKAGESDRIVER_BAZEL_TAG_FILTERS: makes use of the--build_tag_filtersoption to only build (filter) targets with certain tags. This is useful for when you want each target to define wether or not it should be part of the packages list. For instance, setGOPACKAGESDRIVER_BAZEL_TARGETS="//..."andGOPACKAGESDRIVER_BAZEL_TAG_FILTERS="completion"to only build targets with thecompletiontag in the whole workspace.In addition, those environment variables are optional:
GOPACKAGESDRIVER_BAZEL: bazel binary, defaults tobazelBUILD_WORKSPACE_DIRECTORY: directory of the bazel workspace (should be auto detected when using a launcher script because it invokesbazel run)1.
goplsYou'll need
gopls >= v0.6.10(released on Apr 13th 2021). If you are using VSCode, it should be automatic.2. Create a launcher script
Create a launcher script, say
tools/gopackagesdriver.sh:3. Sample VSCode configuration
In
.vscode/settings.jsonadd the following:Open VSCode, and after a while the packages should be properly detected after the build is done.
The same principles should apply for vim-go or any editor that uses
gopls.Limitations
Query patterns are ignored and the whole graph is returned each timepatterns are now usedRoot packages detection is probably wrongit should be better nowTechnical details
Aspects
Most of the work is done by one aspect that generates a minimum package JSON entry, and extracts/forwards the Stdlib JSON packages file from the deepest
go_target in the graph (ie once all configurations/transitions are applied).Each JSON file can contain one or more Go packages definition. Which are then loaded, their paths expanded to real absolute paths. Then, the package files are read to get the real package name and its imports list. Imports are then resolved to other packages in the graph.
stdlib packages
Because stdlib packages are not part of the bazel graph, the packages driver will open the
.gofiles of each package and scan its imports to then resolve all packages, including of course stdlib.The whole stdlib definition sits inside one JSON file, which is generated by the
stdliblistbuilder. It executesgo list builtin std runtime/cgoand then transforms its output to match the JSON packages.This list file is fetched from the deepest
go_target in the graph so that all configurations and transitions are applied.