Commit Graph

53 Commits

Author SHA1 Message Date
Yasuhiro Matsumoto 4892ae6946 go/packages: use strings.EqualFold in sameFile to handle case-insensitive file systems
Name of file or URI must be checked ignorecase on Windows.

Change-Id: I61507b0aa95389c5e1f421f0702ba6ad17b79177
Reviewed-on: https://go-review.googlesource.com/c/160597
Run-TryBot: Rebecca Stambler <rstambler@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
2019-02-06 17:56:59 +00:00
Douglas Danger Manley 4e3518700c go/packages: stop parsing files if the context is canceled
`stamblerre/gocode`, which is the fork of `mdempsky/gocode` that supports Go 1.11 modules, uses `packages.Load` to load all of the Go code for, among other things, IDE autocomplete support.  A common aspect to IDE autocomplete is asking for suggestions very frequently (perhaps at every character typed).  Once the user has typed another character, the previous request for autocomplete is invalid and can be canceled.

`packages.Load` already stops its `go list` component if the context is canceled, but if the context is canceled afterward, then it will parse all of the files that it found.  This change stops the parsing of Go files once it detects that the context has been canceled.  When a file has not been processed due to cancelation, its error will be set to that of the context.

This change dramatically improves the performance of the `stamblerre/gocode` fork when requests have been canceled.

Change-Id: Iba8c1e08eefa59137559ac9108238bfe5ba4ac21
GitHub-Last-Rev: 11a2210c8ce2ed9db9462ddc3e9676476f49f937
GitHub-Pull-Request: golang/tools#72
Reviewed-on: https://go-review.googlesource.com/c/159259
Reviewed-by: Michael Matloob <matloob@golang.org>
Run-TryBot: Michael Matloob <matloob@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
2019-01-24 20:56:45 +00:00
Alan Donovan b4b6fe2cb8 go/{analysis,packages}: add TypesSizes
Many Analyzers need to measure the width of an integer and all today
use hacks. This change causes analysis.Pass to retain and expose the
type sizing function used during type checking.

This in turn requires go/packages to retain and expose the type sizing
function in Packages.TypesSizes, which addresses a longstanding need
among many of its clients.

Change-Id: Ia8362019bcde34c10cb4fbc38cfdfddcbef3eb5c
Reviewed-on: https://go-review.googlesource.com/c/158317
Reviewed-by: Michael Matloob <matloob@golang.org>
Run-TryBot: Michael Matloob <matloob@golang.org>
2019-01-17 19:41:23 +00:00
Dmitri Shuralyov 36f37f8f5c go/packages: rename Illtyped to IllTyped in docs
Fix the field name in docs to have the correct case, as seen at
https://godoc.org/golang.org/x/tools/go/packages#Package.IllTyped.

Change-Id: I3184c2fc5f9c4641a61dbd2af6a11309e9f284ea
Reviewed-on: https://go-review.googlesource.com/c/157797
Reviewed-by: Michael Matloob <matloob@golang.org>
2019-01-14 16:46:48 +00:00
Michael Matloob 59cd96f77e go/packages: add doc for the current state of overlays
Change-Id: Iba4ea587d7a14a1b930df122c925d1bc2f39eda8
Reviewed-on: https://go-review.googlesource.com/c/153678
Reviewed-by: Ian Cottrell <iancottrell@google.com>
2018-12-11 22:18:32 +00:00
Ian Cottrell 8bc39b9880 go/packages: an empty overlay does not force source mode
we check length of the map, and a non empty map forces source mode.

Change-Id: Id314ad87b96d9154198122586fd8672a70c5ca95
Reviewed-on: https://go-review.googlesource.com/c/153198
Run-TryBot: Ian Cottrell <iancottrell@google.com>
Reviewed-by: Michael Matloob <matloob@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
2018-12-07 18:38:36 +00:00
Michael Matloob 34bb05f9d8 go/packages: add support to overlays for unwritten files to existing packages
This support is not perfect, but should produce more accurate results than
the previous behavior, which is to silently drop those files.

Change-Id: Ia00c042bf303e9ec0fb1cbd579c0fccb29073de0
Reviewed-on: https://go-review.googlesource.com/c/151999
Run-TryBot: Michael Matloob <matloob@golang.org>
Reviewed-by: Ian Cottrell <iancottrell@google.com>
2018-11-30 21:54:54 +00:00
Daniel Martí d3a25d70bd go/packages: fix minor godoc typo
Spotted while reading some docs.

Change-Id: I856c73b55f459fd980591ba5525d5170d035d79c
Reviewed-on: https://go-review.googlesource.com/c/148797
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2018-11-09 17:23:37 +00:00
Michael Matloob 34b416bd17 go/packages: change the driverResponse.Sizes to have type StdSizes
This will allow Sizes to be marshalled and unmarshalled. All the Sizes
we care about ane StdSizes anyways.

Change-Id: I79d1dcaebba32f7730de4375945e372eeefa78fe
Reviewed-on: https://go-review.googlesource.com/c/147978
Run-TryBot: Michael Matloob <matloob@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Alan Donovan <adonovan@google.com>
2018-11-07 21:56:32 +00:00
Michael Matloob 78dc5bac0c go/packages: determine sizes by calling go list
This is more reliable than looking in the environment.

Change-Id: I96c093b89faaece6b6256eefb4a4aac4d66b9cc9
Reviewed-on: https://go-review.googlesource.com/c/146757
Run-TryBot: Michael Matloob <matloob@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Alan Donovan <adonovan@google.com>
2018-11-05 23:00:42 +00:00
Ian Cottrell fc67db3d9a go/packages: fix crash
This is based on 147340 and has the same test, but with a different fix.
We now produce a nice error if there is an empty root, instead of
crashing, and the cause of empty roots in go list has been fixed.
The underlying call to go list is returning the same package more than
once, and we only fix the first entry in the root list, so the second
one got left blank.
The fix was to not add the second duplicate copy to the output of the
driver at all.

Change-Id: I9f1b2f0fd63635ba101cdd3c8a5108530e968ba9
Reviewed-on: https://go-review.googlesource.com/c/147440
Run-TryBot: Ian Cottrell <iancottrell@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Michael Matloob <matloob@golang.org>
2018-11-05 21:21:22 +00:00
Ian Cottrell 3a10b9bf0a go/packages: change so no results are sorted
We do insist that drivers are stable (not sorted, just stable) and that
refine is also stable, which allows us to promise a stable output.
I also changed refine so it returns the root set in the same order that
the original root id list was supplied, as this seems to be a strictly
better experience.

Change-Id: I8eb0bffd7547865d14a6c6f18646018b9af140bd
Reviewed-on: https://go-review.googlesource.com/c/145877
Run-TryBot: Ian Cottrell <iancottrell@google.com>
Run-TryBot: Michael Matloob <matloob@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Michael Matloob <matloob@golang.org>
2018-10-31 16:53:50 +00:00
Ian Cottrell 2d2de62981 go/packages: sort root list in Load
The builders are currently flaky failing because of a root order being
wrong.
This causes all root lists to be sorted before being returned, so the
order is always stable no matter which underlying driver is running.

Fixes golang/go#27594

Change-Id: I09db45c67ad00f23dfaec8e271acbd13fc338888
Reviewed-on: https://go-review.googlesource.com/c/143737
Run-TryBot: Ian Cottrell <iancottrell@google.com>
Reviewed-by: Alan Donovan <adonovan@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
2018-10-29 18:14:17 +00:00
Ian Cottrell 3bba456143 go/packages: don't use os.LookupEnv
Get the value of GOARCH from the config instead.
It should have been set to the same as the one from the environment by
default, but may have been overidden by the caller.

Change-Id: If9a6c0ae998c1c72ad2a68fe83c8bb9f5614a189
Reviewed-on: https://go-review.googlesource.com/c/142361
Run-TryBot: Ian Cottrell <iancottrell@google.com>
Run-TryBot: Michael Matloob <matloob@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Michael Matloob <matloob@golang.org>
2018-10-16 15:13:54 +00:00
Alan Donovan 87312bc3ed go/packages: use effective GOARCH to determine type size function
The typechecker's Sizes function is not currently set correctly.
The correct answer requires information known only to the build
system's query tool (go list, blaze query, etc), and we need to
add a mechanism for it to return this information.

In the meantime, we use this simple workaround: if the GOARCH
environment variable is set, we use that to determine sizes according
to the conventions of gc. Otherwise, we use the architecture for which
the application was compiled. Both could easily be incorrect, but this
is nonetheless progress.

This change should fix the tests of go/analysis/passes/shift,
which are currently broken for GOARCH=386 because the analysistest
driver uses go/packages, which ignores GOARCH.

Change-Id: Iabe3211ad513a9a94eadd6d8f4b2068f7abdd053
Reviewed-on: https://go-review.googlesource.com/c/141757
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2018-10-12 12:58:16 +00:00
Michael Matloob 2f1727f1b3 go/packages: add basic support for overlays
This allows users of go/packages to replace the contents of already
existing files, to support use-cases such as unsaved files in editors.

BREAKING CHANGE: This CL changes the signature of the function provided
to Config.ParseFile.

Change-Id: I6ce50336060832679e9f64f8d201b44651772e0b
Reviewed-on: https://go-review.googlesource.com/c/139798
Reviewed-by: Alan Donovan <adonovan@google.com>
2018-10-08 20:38:53 +00:00
Rebecca Stambler 8e930c1793 go/packages: change ParserError to ParseError
Correct minor typo from
https://go-review.googlesource.com/c/tools/+/139317/3.

Change-Id: I76b661fbe136914b903011990e48c74563115ae6
Reviewed-on: https://go-review.googlesource.com/139318
Reviewed-by: Michael Matloob <matloob@golang.org>
2018-10-03 17:47:30 +00:00
Rebecca Stambler 9599141856 go/packages: add ErrorKind field to differentiate error sources
Some applications (for example, diagnostics shown to a user in an
editor) may want to distinguish between errors generated by
the driver, parser, and type-checker. The Error struct did not have any
mechanism for doing this, so add an ErrorKind field and set it in
appendError.

Change-Id: If347163225d1e3a567e98610e9ba8a0930e4659c
Reviewed-on: https://go-review.googlesource.com/139317
Run-TryBot: Rebecca Stambler <rstambler@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Alan Donovan <adonovan@google.com>
2018-10-03 17:20:41 +00:00
Bryan C. Mills 84988e2dba go/packages: do not error out for patterns that match no packages
The documentation for Load says:
“Load returns an error if any of the patterns was invalid as defined
by the underlying build system. It may return an empty list of
packages without an error, for instance for an empty expansion of a
valid wildcard. Errors associated with a particular package are
recorded in the corresponding Package's Errors list, and do not cause
Load to return an error.”

Therefore, it should not be an error for a pattern to match no
packages. If the pattern is a literal package path that does not
exist, we should prefer to return a *Package for it with an error in
the Errors field.

Change-Id: Iaecfb920097e3b520e763bd52c0e326d2e7a4861
Reviewed-on: https://go-review.googlesource.com/137075
Run-TryBot: Bryan C. Mills <bcmills@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Michael Matloob <matloob@golang.org>
2018-09-28 12:33:17 +00:00
Michael Matloob 0aa4b8830f go/packages: remove the Config.Error hook
Errors relating to a particular package (as opposed to the Load
operation as a whole) are now only recorded in the Errors
field of the corresponding Package.
Clients are responsible for printing or otherwise handling errors.
This is a breaking API change.

The PrintErrors function prints all accumulated errors,
dependencies first, and is provided for convenience.

PrintErrors is based on Visit, another helper function, which
visits each Package in an import graph.

Also:
- add an Example documenting typical use.
- update clients to handle errors explicitly.

Depends on https://go-review.googlesource.com/c/tools/+/130576

Change-Id: I39407ab7f46dae2f0dd0fdde21080e172e1258aa
Reviewed-on: https://go-review.googlesource.com/131015
Reviewed-by: Michael Matloob <matloob@golang.org>
2018-09-04 20:52:37 +00:00
Michael Matloob f1c1faf65a go/packages: coerce all errors to a single type
Rather than document the range of possible error types, requiring
clumsy client code to extract position information, we now expose a
single concrete type for all errors. Position information (in
standardized string form) is technically optional, but we should
strive for 100%, fixing gaps as they arise.

This change enables us to unify the Package and JSON structs in a
follow-up.

Question: should we eliminate the Config.Error hook and be silent by default?
Pro:
+ most clients suppress it.
Con:
- clients that want to print errors (e.g. vet-like tools) would have
  to traverse the entire import graph to find them.
- silence is not the most fail-safe behavior.

Change-Id: Ie92b9fb7641ceda429f00928474b650d1dfadedd
Reviewed-on: https://go-review.googlesource.com/130576
Reviewed-by: Michael Matloob <matloob@golang.org>
2018-09-04 20:04:35 +00:00
Todd Neal 7ca1327549 go/packages: fix docs
Change-Id: Ife879995fd1a8f23821efefe6970a0e907f2fb12
Reviewed-on: https://go-review.googlesource.com/132600
Run-TryBot: Todd Neal <todd@tneal.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Kevin Burke <kev@inburke.com>
2018-08-31 21:12:45 +00:00
Jay Conrod ff6c8c104a go/packages: initialize config.Env with os.Environ when unset
This makes Load use the value of GOPACKAGESDRIVER in the current
process's environment if the loader's environment wasn't explicitly
set.

Note that if GOPACKAGESDRIVER is not set, we'll still search PATH in
the current process, regardless of whether PATH is set in the loader's
environment. This has not changed.

Change-Id: I3081a6cb0876a21ccd4d87b4b8d082aa47911b75
Reviewed-on: https://go-review.googlesource.com/131016
Reviewed-by: Michael Matloob <matloob@golang.org>
Reviewed-by: Alan Donovan <adonovan@google.com>
2018-08-23 20:10:42 +00:00
Jay Conrod 5fad05c818 go/packages: rename config.Flags to BuildFlags
This change adds the -buildflag argument to go/packages/gopackages,
which may be passed repeatedly to set config.BuildFlags. I felt -flag
was too vague.

config.Flags is renamed to config.BuildFlags for consistency, and
packages.findExternalDriver now passes -buildflag instead of -flag to
drivers. This will break existing drivers.

Change-Id: Iaed58026373a46e137a236ee9a652eb3a9433ee3
Reviewed-on: https://go-review.googlesource.com/130136
Reviewed-by: Alan Donovan <adonovan@google.com>
2018-08-21 16:42:13 +00:00
Alan Donovan 7d1dc99761 go/packages: smooth API documentation for release
Change-Id: Ia3d06a320228831115eb0e0e99d45a84a812da03
Reviewed-on: https://go-review.googlesource.com/129636
Reviewed-by: Ian Cottrell <iancottrell@google.com>
2018-08-17 17:07:05 +00:00
Ian Cottrell 641913b2bd go/packages: reduce the number of export data files loaded
In ModeTypes, we only need the roots, in ModeSyntax we need the direct
dependancies.
We now do exactly what we need rather than the entire set of transitive
dependancies.
Without this change tools operating on source of a deep dependancy tree are far
to slow.

Change-Id: I3ddbe660709184758ad33ab7cdea860244433ce6
Reviewed-on: https://go-review.googlesource.com/129355
Run-TryBot: Ian Cottrell <iancottrell@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Alan Donovan <adonovan@google.com>
2018-08-17 16:41:42 +00:00
Alan Donovan 9de1900191 go/packages: opt: ignore function bodies in non-initial packages
This saves 35% of wall time in a command such as

 $ gopackages -mode=syntax golang.org/x/tools/go/packages/gopackages fmt

that would otherwise typecheck every function in each package that
depends on fmt. It also applies in -mode=types when source was loaded
as a fall back for missing export data due to an error in a lower package.

Also, remove Config.TypeChecker field now that not a single of its
subfields is passed through go/packages to the type checker.
(The Sizes function is logically a result, not an input, of loading,
though we have yet to implement it properly.)

Change-Id: I472d21b34fc5e2832f7353e82992a67a06e4e4cc
Reviewed-on: https://go-review.googlesource.com/129497
Reviewed-by: Ian Cottrell <iancottrell@google.com>
2018-08-16 00:26:32 +00:00
Ian Cottrell 7883bde2a5 go/packages: minor fixes
This is fixes for review comments made on cl/128120

Change-Id: Ie6382d6ec91db2bbdece564a83ae028be4e2e269
Reviewed-on: https://go-review.googlesource.com/129000
Run-TryBot: Ian Cottrell <iancottrell@google.com>
Reviewed-by: Alan Donovan <adonovan@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
2018-08-10 19:50:59 +00:00
Ian Cottrell af5e78811a go/packages: use Package as the raw form
This deletes the raw and golist packatges, instead using the main
Package structure as the serialzied form between the build tool and the
loader.
This requires a few new fields on Package for information that we used
to hide, and some extra json tags on Package.
It also required Package to have custom JSON marshalling methods.

Change-Id: Ib72171bc93e2b494b97f4b0266504acaa9b3f647
Reviewed-on: https://go-review.googlesource.com/128120
Reviewed-by: Michael Matloob <matloob@golang.org>
Run-TryBot: Michael Matloob <matloob@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
2018-08-10 14:45:52 +00:00
Michael Matloob aaa76ee82d go/packages: cleanup style
This change addresses some style issues pointed out by rsc in
golang.org/cl/126177. It doesn't fix the comment on listfunc
or the comment on the processing of the containing directories.
Those will be handled in a separate cl.

Change-Id: I2094ad1e5e2ee181a646466675f4d993bd69ff9b
Reviewed-on: https://go-review.googlesource.com/128897
Run-TryBot: Michael Matloob <matloob@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Russ Cox <rsc@golang.org>
Reviewed-by: Alan Donovan <adonovan@google.com>
2018-08-09 21:03:15 +00:00
Alan Donovan 2e5c1e6f75 go/packages: make LoadFiles the zero mode
Also:
- make a nil *Config equivalent to new(Config).
- add a test for nil *Config.
- document that Load may return an empty list of packages.

Change-Id: I642133abe18553ca8c7f46b7bd2709a03eda0b28
Reviewed-on: https://go-review.googlesource.com/128875
Reviewed-by: Michael Matloob <matloob@golang.org>
2018-08-09 18:40:53 +00:00
Suzy Mueller 2fda359797 go/packages: use export data to load types info
Use export data in LoadTypes mode, when it is available to get the type information.

The lock can be moved back to loadFromExportData, as each package is
loaded in topological order, and all calls to loadFromExportData for a
particular package occur when that package is being loaded.

Fixes golang/go#26834

Change-Id: Ib6c28eb8642a473cc100d54d0aac7b90644d5d22
Reviewed-on: https://go-review.googlesource.com/128365
Reviewed-by: Michael Matloob <matloob@golang.org>
Reviewed-by: Alan Donovan <adonovan@google.com>
2018-08-09 18:26:40 +00:00
Alan Donovan 201986631b go/packages: initialise Fset if mode >= Types
...as it is required not just for Syntax processing but also
when loading export data.

Clarify comments surrounding Types, Fset.
Remove bogus comment re: Dir.

Change-Id: I8d234bc311d73cbed4fd271ef883ebba22ba2708
Reviewed-on: https://go-review.googlesource.com/128595
Run-TryBot: Michael Matloob <matloob@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Michael Matloob <matloob@golang.org>
2018-08-08 18:17:01 +00:00
Ian Cottrell 3c07937fe1 go/packages: enable an external source of package information
This allows an external binary (not go list) to be the source of package
information.
It uses a binary called gopackagesraw if present in the PATH.
The binary can be overriden by specifying the GOPACKAGESRAW
environment variable.
The command must accept the -test -deps -export and -flags, and take a
list of package patterns to match. It then returns a raw.Results followed by
the matching raw.Package structs in json format on stdout.

Change-Id: I836014d837a284999ded0c1157b8e6dac058ba69
Reviewed-on: https://go-review.googlesource.com/125938
Run-TryBot: Ian Cottrell <iancottrell@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Michael Matloob <matloob@golang.org>
2018-08-03 18:01:56 +00:00
Michael Matloob 82515cf895 go/packages: fix race detected by race detector
Diamonds in the dependency graph could cause the same package
to be processed by two separate gorotines, causing a potential
race between checking if the package's Types field is set, and
setting the field.

adonovan's comment suggested this might be unnecessary. We'll
ask him about this once he returns from his vacation.

Change-Id: I27511b6e44bb1b6a04ea294206fd8ed4ea60f56f
Reviewed-on: https://go-review.googlesource.com/127636
Run-TryBot: Michael Matloob <matloob@golang.org>
Reviewed-by: Ian Cottrell <iancottrell@google.com>
Reviewed-by: Suzy Mueller <suzmue@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
2018-08-02 19:53:59 +00:00
Michael Matloob 4a42e0a488 go/packages/golist: pass in -compiled to go list
This is mostly straightforward, except that go list -e -compiled
will now return errors if a package can't build. This is a bug.
We need to skip the errors test until that's fixed.

For now, don't try to run go list with no arguments because it will
fail. So when all arguments are contains, we will check for empty
patterns and skip running go list.

Change-Id: I7c92b1bf9448e5dbea22f9ade9fb9429945e3719
Reviewed-on: https://go-review.googlesource.com/127339
Run-TryBot: Michael Matloob <matloob@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Cottrell <iancottrell@google.com>
2018-08-02 18:51:39 +00:00
Ian Cottrell 681bc0c94c go/packages: fix the documentation
It got out of sync after the round of API design changes, this brings it
back in line.
Also fix the behavior of passing nil as the config to match the
expected/documented behavior.

Change-Id: I8b51f5bd29ef9338ddb920fb872d857f180f9c84
Reviewed-on: https://go-review.googlesource.com/127255
Reviewed-by: Michael Matloob <matloob@golang.org>
2018-08-02 15:09:54 +00:00
Ian Cottrell 8149a73649 go/packages: extract the raw API to its own package
We want to make it public to enable external sources of information
(other than go list), but we don't want to clutter the main
go/packages API with things that are intended for implementers only.
We also want to move the two API's from experimental to stable at
different times.
We also need to split the API because the golist implementation has to depend on
the raw structs, and then the main packages.Load has to depend on the golist
implementation.

The golist functionality is now in it's own package, and provides a Load method
to return the raw structures. This can be re-used by more complex raw providers.

Change-Id: I767af216b0d1ad9391e613cbf24eb2db5f503369
Reviewed-on: https://go-review.googlesource.com/126718
Run-TryBot: Ian Cottrell <iancottrell@google.com>
Reviewed-by: Michael Matloob <matloob@golang.org>
2018-08-01 17:44:00 +00:00
Ian Cottrell 7ebc57ba19 go/packages: remove DepOnly from the raw package
All the other members of raw package are stable for any given package,
DepOnly relates to the query patterns, not thepackages.
Instead the raw functions now return the set of roots matched

Other minor changes included:
rawConfig.ExtraFlags -> rawConfig.Flags
delete rawConfig.Context

Change-Id: I3a729fa4557043f65da386eed94ab46fdfacb8ad
Reviewed-on: https://go-review.googlesource.com/127035
Reviewed-by: Michael Matloob <matloob@golang.org>
2018-08-01 17:19:07 +00:00
Ian Cottrell ade7853a2a go/packages: allow Load to use source if no export data is possible
We ask the underlying tool to produce export data if needed, but if it
cant we should fall back to source rather than just failing.

Change-Id: I376d41b4c8ad4d3c1aef3ca4ab4e3d0466b83b56
Reviewed-on: https://go-review.googlesource.com/126875
Run-TryBot: Ian Cottrell <iancottrell@google.com>
Reviewed-by: Michael Matloob <matloob@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
2018-07-31 16:59:11 +00:00
Suzy Mueller 8cc4e8a6f4 go/packages: add Flags to Config
Add the Flags field to the Config struct in packages to provide a way
for users to pass along additional information to the underlying query
tool.

Since users that need Flags will already know something about the build
system they are using (and flags will vary depending on the underlying
build system), they can pass through the flags that they need for that
build system.

For example, build tags should be passed through using the Flags field in go build,
using "-tags=".

Change-Id: Ia65bf0d003db2f6d9aaad6cd09c602f4bc5bf3e3
Reviewed-on: https://go-review.googlesource.com/125302
Reviewed-by: Michael Matloob <matloob@golang.org>
Reviewed-by: Ian Cottrell <iancottrell@google.com>
2018-07-27 19:51:41 +00:00
Michael Matloob 9dbe528bbb go/packages: add support for 'contains:' words
If a 'word' provided to go/packages' Load function starts with contains,
go/packages will interpret that word as the package containing the given
file.

For example:
   packages.Load(config, "contains:/usr/local/go/src/fmt/format.go")
would load the fmt package from the Go installation at /usr/local/go.

This implementation uses "go list ." in the directory the file is
contained in to find the package, but this won't work in the module
cache. We plan to add support to go list directly to help find the
containing package. Then, because we won't need to change directory,
go list will have knowledge of the correct vgo root module, and will
be able to surface correct results.

Change-Id: I6bff62447c12f13dae5e4c0c65f729d9f271c388
Reviewed-on: https://go-review.googlesource.com/126177
Run-TryBot: Michael Matloob <matloob@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Cottrell <iancottrell@google.com>
2018-07-27 19:42:20 +00:00
Michael Matloob ceb18cec46 go/packages: switch fallback implementation to use go list
The go/loader based implementation was too different. Instead
Just call go list twice to get all the dependencies. This immediately
enables more functionality.

Change-Id: I22883492e996133bc61664314318c5e0a9cc10dc
Reviewed-on: https://go-review.googlesource.com/125939
Reviewed-by: Suzy Mueller <suzmue@golang.org>
Reviewed-by: Ian Cottrell <iancottrell@google.com>
2018-07-26 16:53:35 +00:00
Ian Cottrell b1e4acd68a go/packages: alter the internal api to go list
This separates the go list specific behavior from the generalised go/packages
loading behaviour, to enable alternate build system back ends.

Change-Id: I07e7649f8f2afc7470a3ee3d0d743cbbcc6f713e
Reviewed-on: https://go-review.googlesource.com/125715
Run-TryBot: Ian Cottrell <iancottrell@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Michael Matloob <matloob@golang.org>
2018-07-25 20:44:53 +00:00
Ian Cottrell 14e571052b go/packages: fix internal go list function signatures
After a discussion in cl/125535 this is a proposal to clean up some
internal function signatures that were becomming unwieldy.

Change-Id: I37a9e525ab18dface9ea9fb0b0ec68ac942ee632
Reviewed-on: https://go-review.googlesource.com/125659
Reviewed-by: Michael Matloob <matloob@golang.org>
2018-07-25 19:03:22 +00:00
Michael Matloob 526516e9c4 go/packages: support pre go1.10.4 using go/loader
If go list fails because it doesn't support the new flags added
for Go 1.11 (which will be also released in Go 1.10.4) try again
using the Loader to approximate the packages requested.

This implementation is incomplete. It will never support test packages
because of the two phase test loading process of the Loader. It
also doesn't reliably have access to export data so it will
always do an upgraded whole-program query. But we'll try to get
the best level of support we can for the go/packages interface
given the limitations of the loader.

Once Go 1.12 is released, we'll delete this support. By then, most
Go users should have at least switched to Go 1.10.4.

Change-Id: I5248e20980032695a86b052caa9ff368ecf7b142
Reviewed-on: https://go-review.googlesource.com/125616
Reviewed-by: Ian Cottrell <iancottrell@google.com>
2018-07-25 00:20:46 +00:00
Ian Cottrell 214274eeeb go/packages: changes to the API per CL 125305
Collapse the main entry points to the single Load function.
Make public the Mode enumeration in the Config.
Add a lowest level mode that does not do the import graph.
Remove PkgPath.
Remove the DisableCgo option.
Rename some fields of Package:
  Srcs -> GoFiles
  OtherSrcs -> OtherFiles
  Type -> Types
  Files -> Syntax
  Info -> TypesInfo
delete the All function

Change-Id: I54e5dc3ca5cb0b7e33fe6aa6f70cc66e18ea8cac
Reviewed-on: https://go-review.googlesource.com/125535
Run-TryBot: Ian Cottrell <iancottrell@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Russ Cox <rsc@golang.org>
Reviewed-by: Michael Matloob <matloob@golang.org>
2018-07-25 00:10:29 +00:00
Ian Cottrell ded554d068 go/packages: splitting the scratch fields out of Package
This makes Package a struct we can re-use in other places/algorithms.

Change-Id: I2e095a624bf44223319ba1ea3d60deb727b38f81
Reviewed-on: https://go-review.googlesource.com/124855
Run-TryBot: Ian Cottrell <iancottrell@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Michael Matloob <matloob@golang.org>
2018-07-23 20:42:46 +00:00
Russ Cox 96ee42108a go/packages: revise documentation
I made a pass through the documentation trying to simplify
and make it more accessible to readers who are unfamiliar
with all the ins and outs of the implementation.

I also added TODOs about things I don't understand
and names that we should think about changing.

Change-Id: I633316bd407f3360eb8a683bc8d85fba93ca381e
Reviewed-on: https://go-review.googlesource.com/125305
Run-TryBot: Russ Cox <rsc@golang.org>
Reviewed-by: Michael Matloob <matloob@golang.org>
2018-07-23 18:30:23 +00:00
Michael Matloob 54f9f5980a go/packages: remove Package.IsTest field
We have not yet settled on the meaning of Package.IsTest, and more
deeply which packages are test and which are non test packages.

This change will remove Package.IsTest in the meantime to avoid
confusion. We may later add a boolean or some other way to distinguish
test and non-test packages.

Change-Id: If6b128f7914009fdd42b8bc3de3bff73c8f006cd
Reviewed-on: https://go-review.googlesource.com/124388
Reviewed-by: Russ Cox <rsc@golang.org>
2018-07-19 17:37:31 +00:00