-
Notifications
You must be signed in to change notification settings - Fork 17.9k
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
proposal: x/exp: add a package to wrap 'go list -json' #38234
Comments
I think this is reasonable but we should have a discussion about it. Maybe we can schedule a video call to talk about it? I think the issues you raised about copying the package struct and errors are valid, and at the very least, we should make the definition of the struct that go list -json outputs public. The confusion about go list -e errors is also a problem, but I think that's a deeper problem that will only be fixed by fixing the go command. One more thing that we should be careful about: I'm worried that tools that don't need syntax and types might use the go list wrapper instead of go/packages, even when they don't need to. I'm not sure how we'd prevent that from happening. |
Sure! We have our monthly golang-tools catchup next week, so I'll propose a time then.
If we decide to only do this, I guess they could go in
It's one of the problems, but overall, running
A similar concern had come to mind, but I imagine it wouldn't be a big problem. Most people know |
We'll be discussing this on a video call at meet.google.com/btn-cryq-uwg, this Friday 10 April at 11:30 ET (15:30 UTC) |
I can't make the meeting, but I think go/packages should be the one true API. If it's not good enough, it should be made good enough. Bazel should not hold it back. We shouldn't have two APIs that do the same thing. |
@dominikh raised a similar point on Slack. I don't object to including the |
Summary
We have
go/packages
as a high-level API for loading Go packages. It's abstracted away fromgo list -json
, because it's also meant to support other build systems like Bazel.I propose that we add a lower-level, simple Go API to call
go list -json
andgo list -json -m
. Given that it's only meant to supportgo list
, it can show more information, and it can support listing modules too.Description
There are plenty of use cases where one can rightfully only support Go modules and GOPATH, and not care about other build systems. For example, a code generation tool that's only used by a project's developers, when they are happy with using modules alone. Or a piece of tooling internal to a company, if they are confident that they won't switch build systems anytime soon.
In those scenarios, using
go/packages
can be enough if one's needs are simple. For example, looking at the Go files contained in a number of packages. But, in the context of modules, it can be pretty easy to run into limitations for little reason, such as:All of these are contained by
go list -json
, but aren't exposed bygo/packages
, since they aren't common with other Go build systems. That makes sense, but it's a tradeoff that isn't right sometimes.One can call
go list -json
directly - for example, to obtain a package's directory alone. Given the structs that one can see ingo help list
:However, it quickly gets complicated if one needs multiple values at once, or if one wants to look at multiple packages in a single call. For example, these open questions aren't easy to answer, for anyone who isn't very familiar with the Go tool:
go help list
? How can I update them in the future without manual steps?-e
flag?-e
, can the Go command still return a non-zero status code?It's possible to make some assumptions or guesses here which might be OK for the user, but even then, the user will likely end up manually copying some code from
go help list
, and writing 20 to 40 lines of extra code withos/exec
andencoding/json
.Proposed solution
As explained before, I propose that we add a package with two new APIs - loading packages, and loading modules. The latter would only work in module mode, just like
go list -m
. I have a proof of concept implementation here, which I'm using in a couple of projects, and which I'd be happy to donate via a CL.The code isn't terribly complex or large, but I think it's easy to tell that exposing good errors isn't trivial.
I think
x/exp
would be a reasonable place for it to live for now, to give us some space to redesign the API later if we want. I haven't thought too hard about the package name itself, but one could imaginelist
followinggo list
, ormodules
, as an analogous topackages
given that long-term GOPATH will probably be deprecated/removed in favour of modules.Considered alternatives
cmd/go
into smaller packages, such as what one can find in https://godoc.org/golang.org/x/mod. I think that's a great initiative, and I hope that module is expanded. However, I struggle to imagine how we could ever fit an API like this inx/mod
:go list
? The rest of the module is "pure Go" without exec, as far as I can tellcmd/go/internal
, and it would mean that we no longer use the Go version that the machine has available.os/exec
, it probably wouldn't use any other part of thex/mod
module, and in my own mind would defeat the purpose of sharing the modulex/tools/modules
. However, I don't want to rush that decision, especially given how difficult it was to stabilizex/tools/packages
.CC @jayconrod @matloob @ianthehat @myitcv @leitzler
The text was updated successfully, but these errors were encountered: