-
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/go/packages: expose Deps field of a package #38953
Comments
/cc @matloob |
What do you mean when you say that packages implicitly depend on the runtime package? Naturally everything implicitly depends on the runtime for memory allocation and such, but those functions don't come in by a normal package dependency. Nothing should depend on the runtime package unless they explicitly import it, afaik. |
I'm building a compiler, not a regular Go tool. Therefore I need to know all dependencies, including the runtime (which is an implicit dependency of all packages). I use it to get the list of files I need to compile to produce an executable. For example, if have the following Go file: package main
func main() {
println("hello world!")
} I get the following JSON for {
"Name": "main",
"GoFiles": [
"hello.go"
],
"Deps": [
"internal/bytealg",
"internal/cpu",
"runtime",
"runtime/internal/atomic",
"runtime/internal/math",
"runtime/internal/sys",
"unsafe"
]
} Note that while this package has no explicit imports, it still depends on the runtime (and all packages the runtime imports). All these other packages are also given as output of the |
I see. That seems like more of a build system/implementation detail than information about the package, so I don't know if it's a fit for |
I think this might be too low-level for go/packages, but on the other hand, I think this solidly falls within the use cases of go/packages. Can your tool always add package runtime as an argument to go/packages and then determine the set of Deps by collecting the transitive set of Imports and adding on runtime's Imports? If you can or there's a similar path to getting the information you need, we should go that route. |
I tried adding both import paths (the package to build and the runtime package), and while that works in many cases it fails for getting dependencies for single Go files. This works: packages.Load(cfg, "runtime", "some/package/path") This doesn't: packages.Load(cfg, "runtime", "some/file.go") So it looks like there are two workarounds: either do two queries (what I'm doing now), or call |
I see. Is doing two queries a burden (performance- or other-wise) for your tool? That's the right way to do it. Unfortunately because of how go list works you can't mix sources ad-hoc packages with normal packages, and the go/packages query interface is the same as go lists, with some additions. |
I haven't measured it but it's probably not a problem. Consistency might be a problem however (when edits are ongoing during the query). Also, it feels a bit unclean to me to run the query twice for what - in my view - should be one query.
That seems like a strange statement to me. The |
Adding @dominikh @ianthehat @mvdan for their opinions. go/packages isn't meant to expose all the information that Since the data in the By the way, there's a discussion here: #38445 about adding other fields to the Packages struct. It might be worth examining how they fit in. |
Given that @aykevl do you intend to support build systems other than |
Okay, understood. I agree that my 'tool' (actually a Go compiler) isn't exactly what the packages package was intended to support. The
Not right now, but directly calling |
Okay. I'll close this issue if that's okay. |
I have been working on Go module support in TinyGo, here: tinygo-org/tinygo#941
For getting the list of packages, I'm currently using the x/tools/go/packages package. Unfortunately it doesn't provide a way to get the full list of (recursive) dependencies of a package, in particular the runtime package (which appears to be implicitly added by the Go toolchain). Therefore I need to run the packages query twice: once to get the dependencies of the main package, and once to get the dependencies of the runtime package.
What version of Go are you using (
go version
)?(but this is not related to any particular Go version)
Does this issue reproduce with the latest release?
I'm right now using the tools module at golang/tools@b320d3a0f5a2 but I can't find a
Deps
field on https://godoc.org/golang.org/x/tools/go/packages#Package either.What did you do?
Right now I'm using
Package.Imports
and obtaining a list of packages by going through all imports recursively. Unfortunately this doesn't include the runtime package, unless it is explicitly imported.What did you expect to see?
I was hoping for a field like this:
And perhaps a way to specify that I want this field to be filled (unfortunately
NeedDeps
is already in use, perhaps useNeedRecursiveDeps
instead?).What did you see instead?
No way to get the packages in the
Deps
field returned bygo list -json
.The text was updated successfully, but these errors were encountered: