-
-
Notifications
You must be signed in to change notification settings - Fork 666
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
go/tools: add gopackagesdriver #2858
Conversation
This commit introduces the GOPACKAGESDRIVER for rules_go Signed-off-by: Steeve Morin <[email protected]>
Signed-off-by: Steeve Morin <[email protected]>
Those files are generated and will end up in the temporary mod cache, which isn't available later on. Signed-off-by: Steeve Morin <[email protected]>
Signed-off-by: Steeve Morin <[email protected]>
This handles some edges cases in which the import path last part is not the package name. Signed-off-by: Steeve Morin <[email protected]>
It's no use, and it can be a significant amount of files, so disable it. Signed-off-by: Steeve Morin <[email protected]>
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 <[email protected]>
Needed when in a CGo environment for go list to work Signed-off-by: Steeve Morin <[email protected]>
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 <[email protected]>
This is more correct. Signed-off-by: Steeve Morin <[email protected]>
This ensures that all possible packages are built Signed-off-by: Steeve Morin <[email protected]>
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 <[email protected]>
Signed-off-by: Steeve Morin <[email protected]>
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 <[email protected]>
Signed-off-by: Steeve Morin <[email protected]>
Made some changes regarding UX, should be simpler now. |
Signed-off-by: Steeve Morin <[email protected]>
Will be easier when debugging. Signed-off-by: Steeve Morin <[email protected]>
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 <[email protected]>
Signed-off-by: Steeve Morin <[email protected]>
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 <[email protected]> Reviewed-by: Heschi Kreinick <[email protected]> Trust: Rebecca Stambler <[email protected]> Run-TryBot: Rebecca Stambler <[email protected]> gopls-CI: kokoro <[email protected]> TryBot-Result: Go Bot <[email protected]>
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 <[email protected]>
Signed-off-by: Steeve Morin <[email protected]>
Signed-off-by: Steeve Morin <[email protected]>
Cleaner than hasattr Signed-off-by: Steeve Morin <[email protected]>
Signed-off-by: Steeve Morin <[email protected]>
Signed-off-by: Steeve Morin <[email protected]>
simply print it and exit(1) Signed-off-by: Steeve Morin <[email protected]>
…cher Signed-off-by: Steeve Morin <[email protected]>
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 <[email protected]>
@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 commit introduces an aspect based
GOPACKAGESDRIVER
forrules_go
.It is rather clunky, but all VSCode +
gopls
features 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 thedeps
attribute in the graph. Such as whengo_binary(linkmode = "c-archive") -> cc_library
.GOPACKAGESDRIVER_BAZEL_QUERY
: runs abazel query
to 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_filters
option 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 thecompletion
tag in the whole workspace.In addition, those environment variables are optional:
GOPACKAGESDRIVER_BAZEL
: bazel binary, defaults tobazel
BUILD_WORKSPACE_DIRECTORY
: directory of the bazel workspace (should be auto detected when using a launcher script because it invokesbazel run
)1.
gopls
You'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.json
add 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
.go
files 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
stdliblist
builder. It executesgo list builtin std runtime/cgo
and 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.