Skip to content

Commit 098fe85

Browse files
committed
[gopls-release-branch.0.5] all: merge master into gopls-release-branch.0.5
23787c0 internal/lsp: fix race condition in diagnostics show message 6652d1f internal/gocommand: add a GoVersion function to avoid duplicating logic d105bfa internal/lsp: dynamically register semantic tokens 2cbe144 internal/lsp: return code lenses in a deterministic order eb9088f internal/lsp/source: remove workspace module from all experiments 2be66b6 cmd/toolstash: pass -S=2 when comparing toolchains c12dc66 gopls: regtest to demonstrate gopls confused by //line directives d49c4ed internal/lsp: do not allow non-Go files to be loaded in packages.load 226fd2f internal/lsp/fake: factor out working in a temp directory 9055098 internal/lsp/source: use 'gopls.' instead of 'gopls_' f5c826d internal/lsp: hold notifications until gopls is initialized b894a32 all: use explicit -mod, -modfile fields for gocommand.Invocation 0a3dccc internal/lsp/source: add a FileSource interface b2746f1 internal/lsp: fix nil pointer exception in initialization 6003fad internal/lsp/source: delete unused Snapshot.IsSaved a03874c internal/lsp: clean up unused parameters 2d76fa4 gopls/internal/regtest: delete OpenFileWithContent 593bd9b internal/lsp/cache: delay transitive invalidation in snapshot.clone e52f541 internal/lsp: fix analysis in all experiments, disable unusedparams eae18ab internal/lsp/cache: factor out guessPackagesForURI from snapshot.clone de28cda internal/lsp: remove staticcheck and gofumpt from all experiments a78bdc4 x/tools/go/analysis/passes/unsafeptr: report Header misuse 255fb2a x/tools/go/analysis/passes/unsafeptr: stricter reporting a2250d3 internal/lsp: add gopls semtok command and semantic token tests 7c89c52 x/tools/gopls: add a skipped test for renaming of package c901190 internal/lsp/source/genapijson: add 'gopls_' prefix to commands a5d9e45 internal/lsp/source: be consistent about command identifiers b29caf7 internal/lsp/source: include setting name in all settings errors 828e89d internal/lsp: fix env setting type 6e6f73a internal/lsp: fix mismatches between doc and settings 1610a49 internal/lsp/source/completion: use a minimum budget of 10ms for shallow completions d162461 go/analysis/passes/asmdecl: add support for ABI selector clauses 64a9e34 internal/imports: dedupe import paths for standard library packages c16b75f internal/imports: bump relevance for modules with higher major versions 0112737 gopls/internal/regtest: add a test for switching from modules to GOPATH d88ec18 go/analysis: add IgnoredFiles, check ignored files in buildtag check 67cabf8 go/loader: delete example_test.go 8c26973 gopls: add "go test" code action 2db1cd7 internal/lsp: extract mod errors in load and treat them like diagnostics ec925d8 gopls/internal/regtest: add a failing test for swig 5bd0538 internal/lsp: move the workspaceMode into the snapshot ed2f502 internal/lsp/semantic.go: generate semantic tokens 6e5c6d7 internal/lsp: handle nil pointer in semantic tokens fcf8212 internal/lsp/source: handle a possible nil pointer in call hierarchy 96877f2 internal/lsp, gopls: require a "gopls_" prefix on all commands 9ab7e51 internal/lsp: don't show blank identifiers in outline 7ddb464 internal/lsp: add "type " to hover messages for structs/interfaces/etc d01b322 internal/lsp/cache: extract goimports code 4c6f507 gopls/doc: use enum value docstrings, if they exist ffec978 internal/lsp: handle major versions above v0/v1 in workspace module mode 03e951c internal/lsp/source: add an experimental new cache key for packages 9df6960 internal/lsp: don't pass build flags to `go mod` commands 0806396 internal/lsp: don't show an error for "allExperiments" in (options).set d5f20aa gopls: add regtest mode for experiments, like workspace module tests 45061ab internal/lsp: add support for an "enable all experiments" settings a00137c internal/lsp: default to workspace module mode even with vendor dirs 454bc3d internal/lsp/source: show references to interface methods 23a3aa1 internal/lsp: improve handling of files not in views 567bb5a go/analysis/analysistest: fix issue in RunWithSuggestedFixes on a package with tests 90a82dd internal/lsp/cache: actually remove the view's modURI and sumURI 576e169 internal/lsp: fix hover for builtin error method (Error) 1ccce62 internal/lsp/cache: prevent version from including line break 9854aba internal/lsp: unconditionally create changedFiles map ecd9fd2 internal/lsp/source: ignore empty package names for completion 39ee293 gopls: add test for `go mod tidy` diagnostics in multiple modules 76a79a6 internal/lsp/source: factor out enum options pattern 7e5cd54 internal/lsp/source: improve documentation for symbolStyle 08f1973 gopls/internal/regtest: only run in singleton mode 2268388 internal/lsp: fix go.mod creation without experimental workspace module 8445f4f internal/lsp: add experimental support for multi-phase diagnostics 0d28ed0 gopls: add a test that mirrors govim's -mod=readonly test b5b8742 internal/lsp/source: refactor c.item to support deepSearch in all cases 4e032a7 internal/lsp: fix and add a test for non-workspace module mode dc8b4ba internal/lsp: fix two more staticcheck warnings af0a1b5 internal/lsp: fix staticcheck warning 41c411d internal/lsp/testdata: remove diagnostic from percent package 77e61d3 internal/lsp/source: remove unused Session method 1e3611d internal/lsp: remove logic for re-creating a view when a go.mod changes 43ebab8 gopls/doc: fix section rewriting e57f6d4 internal/lsp: move hasValidBuildConfiguration into the snapshot 2e5f0cf internal/lsp: remove all but one use of the view's modURI field a6f32d1 gopls/internal/regtest: allow cleanup to fail on windows a44386f internal/lsp/source/completion: use c.matcher for comments 66e72d0 internal/lsp: print the go version only once the view is created c8c0a1c internal/lsp/semantic.go: add the skeleton for supporting semantic tokens bf155c1 internal/lsp: use published diagnostics instead of reports in lsp_test bce87a7 internal/lsp/source/completion: remove unused contexts c43c25c internal/lsp/source: run deep completions before unimported completions f1e51e6 internal/lsp: stop using modURI as much as possible fc8742f cmd/bundle: expand test coverage to all modes 5272f30 gopls: fix various staticcheck errors ffa3839 cmd/bundle: use caller's module version via go/packages d7fc70a gopls/internal/regtest: always await InitialWorkspaceLoad a0ef9b6 internal/lsp: prepare for deletion of view.modURI 19e0367 internal/lsp/cache: use gopls.mod for the workspace module if it exists 50ab967 internal/lsp/source: add additional generated docs 42b62fc gopls/internal/regtest: fix data race when printing logs 5d1fdd8 internal/lsp: allow multiple go.mod files in a view e843550 internal/lsp: run marker tests in multi-module mode 4c77dbd internal/lsp: fix race condition caused by config in `go mod tidy` 34b80a0 gopls/doc: update and expand the nvim documentation 0eae6ac internal/lsp: add a test for gc annotation details code lens 8d73f17 internal/lsp: move package selection to before type checking Change-Id: If6de848f3d37deda28273b0b75dce2425fd1fe5d
2 parents 9647ced + 23787c0 commit 098fe85

File tree

148 files changed

+5735
-2510
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

148 files changed

+5735
-2510
lines changed

cmd/bundle/main.go

Lines changed: 50 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,9 @@
3333
// corresponding symbols.
3434
// Bundle also must write a package declaration in the output and must
3535
// choose a name to use in that declaration.
36+
// If the -pkg option is given, bundle uses that name.
37+
// Otherwise, the name of the destination package is used.
3638
// Build constraints for the generated file can be specified using the -tags option.
37-
// If the -package option is given, bundle uses that name.
38-
// Otherwise, if the -dst option is given, bundle uses the last
39-
// element of the destination import path.
40-
// Otherwise, by default bundle uses the package name found in the
41-
// package sources in the current directory.
4239
//
4340
// To avoid collisions, bundle inserts a prefix at the beginning of
4441
// every package-level const, func, type, and var identifier in src's code,
@@ -58,28 +55,19 @@
5855
// bundle -o zip.go archive/zip
5956
//
6057
// Bundle golang.org/x/net/http2 for inclusion in net/http,
61-
// prefixing all identifiers by "http2" instead of "http2_",
62-
// and rewriting the import "golang.org/x/net/http2/hpack"
63-
// to "internal/golang.org/x/net/http2/hpack", and also include
64-
// a "!nethttpomithttp2" build constraint:
58+
// prefixing all identifiers by "http2" instead of "http2_", and
59+
// including a "!nethttpomithttp2" build constraint:
6560
//
6661
// cd $GOROOT/src/net/http
67-
// bundle -o h2_bundle.go \
68-
// -prefix http2 \
69-
// -import golang.org/x/net/http2/hpack=internal/golang.org/x/net/http2/hpack \
70-
// -tags '!nethttpomithttp2' \
71-
// golang.org/x/net/http2
62+
// bundle -o h2_bundle.go -prefix http2 -tags '!nethttpomithttp2' golang.org/x/net/http2
7263
//
73-
// Two ways to update the http2 bundle:
64+
// Update the http2 bundle in net/http:
7465
//
7566
// go generate net/http
7667
//
77-
// cd $GOROOT/src/net/http
78-
// go generate
79-
//
80-
// Update both bundles, restricting ``go generate'' to running bundle commands:
68+
// Update all bundles in the standard library:
8169
//
82-
// go generate -run bundle cmd/dist net/http
70+
// go generate -run bundle std
8371
//
8472
package main
8573

@@ -88,28 +76,24 @@ import (
8876
"flag"
8977
"fmt"
9078
"go/ast"
91-
"go/build"
9279
"go/format"
93-
"go/parser"
9480
"go/printer"
9581
"go/token"
9682
"go/types"
9783
"io/ioutil"
9884
"log"
9985
"os"
100-
"path"
10186
"strconv"
10287
"strings"
10388

104-
"golang.org/x/tools/go/loader"
89+
"golang.org/x/tools/go/packages"
10590
)
10691

10792
var (
10893
outputFile = flag.String("o", "", "write output to `file` (default standard output)")
109-
dstPath = flag.String("dst", "", "set destination import `path` (default taken from current directory)")
110-
pkgName = flag.String("pkg", "", "set destination package `name` (default taken from current directory)")
94+
dstPath = flag.String("dst", ".", "set destination import `path`")
95+
pkgName = flag.String("pkg", "", "set destination package `name`")
11196
prefix = flag.String("prefix", "&_", "set bundled identifier prefix to `p` (default is \"&_\", where & stands for the original name)")
112-
underscore = flag.Bool("underscore", false, "rewrite golang.org/x/* to internal/x/* imports; temporary workaround for golang.org/issue/16333")
11397
buildTags = flag.String("tags", "", "the build constraints to be inserted into the generated file")
11498

11599
importMap = map[string]string{}
@@ -148,23 +132,19 @@ func main() {
148132
os.Exit(2)
149133
}
150134

151-
if *dstPath != "" {
152-
if *pkgName == "" {
153-
*pkgName = path.Base(*dstPath)
154-
}
155-
} else {
156-
wd, _ := os.Getwd()
157-
pkg, err := build.ImportDir(wd, 0)
158-
if err != nil {
159-
log.Fatalf("cannot find package in current directory: %v", err)
160-
}
161-
*dstPath = pkg.ImportPath
162-
if *pkgName == "" {
163-
*pkgName = pkg.Name
164-
}
135+
cfg := &packages.Config{Mode: packages.NeedName}
136+
pkgs, err := packages.Load(cfg, *dstPath)
137+
if err != nil {
138+
log.Fatalf("cannot load destination package: %v", err)
139+
}
140+
if packages.PrintErrors(pkgs) > 0 || len(pkgs) != 1 {
141+
log.Fatalf("failed to load destination package")
142+
}
143+
if *pkgName == "" {
144+
*pkgName = pkgs[0].Name
165145
}
166146

167-
code, err := bundle(args[0], *dstPath, *pkgName, *prefix, *buildTags)
147+
code, err := bundle(args[0], pkgs[0].PkgPath, *pkgName, *prefix, *buildTags)
168148
if err != nil {
169149
log.Fatal(err)
170150
}
@@ -191,24 +171,30 @@ func isStandardImportPath(path string) bool {
191171
return !strings.Contains(elem, ".")
192172
}
193173

194-
var ctxt = &build.Default
174+
var testingOnlyPackagesConfig *packages.Config
195175

196176
func bundle(src, dst, dstpkg, prefix, buildTags string) ([]byte, error) {
197177
// Load the initial package.
198-
conf := loader.Config{ParserMode: parser.ParseComments, Build: ctxt}
199-
conf.TypeCheckFuncBodies = func(p string) bool { return p == src }
200-
conf.Import(src)
201-
202-
lprog, err := conf.Load()
178+
cfg := &packages.Config{}
179+
if testingOnlyPackagesConfig != nil {
180+
*cfg = *testingOnlyPackagesConfig
181+
} else {
182+
// Bypass default vendor mode, as we need a package not available in the
183+
// std module vendor folder.
184+
cfg.Env = append(os.Environ(), "GOFLAGS=-mod=mod")
185+
}
186+
cfg.Mode = packages.NeedTypes | packages.NeedSyntax | packages.NeedTypesInfo
187+
pkgs, err := packages.Load(cfg, src)
203188
if err != nil {
204189
return nil, err
205190
}
191+
if packages.PrintErrors(pkgs) > 0 || len(pkgs) != 1 {
192+
return nil, fmt.Errorf("failed to load source package")
193+
}
194+
pkg := pkgs[0]
206195

207-
// Because there was a single Import call and Load succeeded,
208-
// InitialPackages is guaranteed to hold the sole requested package.
209-
info := lprog.InitialPackages()[0]
210196
if strings.Contains(prefix, "&") {
211-
prefix = strings.Replace(prefix, "&", info.Files[0].Name.Name, -1)
197+
prefix = strings.Replace(prefix, "&", pkg.Syntax[0].Name.Name, -1)
212198
}
213199

214200
objsToUpdate := make(map[types.Object]bool)
@@ -223,9 +209,9 @@ func bundle(src, dst, dstpkg, prefix, buildTags string) ([]byte, error) {
223209
// var s struct {T}
224210
// print(s.T) // ...this must change too
225211
if _, ok := from.(*types.TypeName); ok {
226-
for id, obj := range info.Uses {
212+
for id, obj := range pkg.TypesInfo.Uses {
227213
if obj == from {
228-
if field := info.Defs[id]; field != nil {
214+
if field := pkg.TypesInfo.Defs[id]; field != nil {
229215
rename(field)
230216
}
231217
}
@@ -235,7 +221,7 @@ func bundle(src, dst, dstpkg, prefix, buildTags string) ([]byte, error) {
235221
}
236222

237223
// Rename each package-level object.
238-
scope := info.Pkg.Scope()
224+
scope := pkg.Types.Scope()
239225
for _, name := range scope.Names() {
240226
rename(scope.Lookup(name))
241227
}
@@ -254,7 +240,7 @@ func bundle(src, dst, dstpkg, prefix, buildTags string) ([]byte, error) {
254240
fmt.Fprintf(&out, "\n")
255241

256242
// Concatenate package comments from all files...
257-
for _, f := range info.Files {
243+
for _, f := range pkg.Syntax {
258244
if doc := f.Doc.Text(); strings.TrimSpace(doc) != "" {
259245
for _, line := range strings.Split(doc, "\n") {
260246
fmt.Fprintf(&out, "// %s\n", line)
@@ -283,11 +269,11 @@ func bundle(src, dst, dstpkg, prefix, buildTags string) ([]byte, error) {
283269
// to deduplicate instances of the same import name and path.
284270
var pkgStd = make(map[string]bool)
285271
var pkgExt = make(map[string]bool)
286-
for _, f := range info.Files {
272+
for _, f := range pkg.Syntax {
287273
for _, imp := range f.Imports {
288274
path, err := strconv.Unquote(imp.Path.Value)
289275
if err != nil {
290-
log.Fatalf("invalid import path string: %v", err) // Shouldn't happen here since conf.Load succeeded.
276+
log.Fatalf("invalid import path string: %v", err) // Shouldn't happen here since packages.Load succeeded.
291277
}
292278
if path == dst {
293279
continue
@@ -304,9 +290,6 @@ func bundle(src, dst, dstpkg, prefix, buildTags string) ([]byte, error) {
304290
if isStandardImportPath(path) {
305291
pkgStd[spec] = true
306292
} else {
307-
if *underscore {
308-
spec = strings.Replace(spec, "golang.org/x/", "internal/x/", 1)
309-
}
310293
pkgExt[spec] = true
311294
}
312295
}
@@ -326,14 +309,14 @@ func bundle(src, dst, dstpkg, prefix, buildTags string) ([]byte, error) {
326309
fmt.Fprint(&out, ")\n\n")
327310

328311
// Modify and print each file.
329-
for _, f := range info.Files {
312+
for _, f := range pkg.Syntax {
330313
// Update renamed identifiers.
331-
for id, obj := range info.Defs {
314+
for id, obj := range pkg.TypesInfo.Defs {
332315
if objsToUpdate[obj] {
333316
id.Name = prefix + obj.Name()
334317
}
335318
}
336-
for id, obj := range info.Uses {
319+
for id, obj := range pkg.TypesInfo.Uses {
337320
if objsToUpdate[obj] {
338321
id.Name = prefix + obj.Name()
339322
}
@@ -345,7 +328,7 @@ func bundle(src, dst, dstpkg, prefix, buildTags string) ([]byte, error) {
345328
ast.Inspect(f, func(n ast.Node) bool {
346329
if sel, ok := n.(*ast.SelectorExpr); ok {
347330
if id, ok := sel.X.(*ast.Ident); ok {
348-
if obj, ok := info.Uses[id].(*types.PkgName); ok {
331+
if obj, ok := pkg.TypesInfo.Uses[id].(*types.PkgName); ok {
349332
if obj.Imported().Path() == dst {
350333
id.Name = "@@@"
351334
}
@@ -379,12 +362,12 @@ func bundle(src, dst, dstpkg, prefix, buildTags string) ([]byte, error) {
379362
printComments(&out, f.Comments, last, beg)
380363

381364
buf.Reset()
382-
format.Node(&buf, lprog.Fset, &printer.CommentedNode{Node: decl, Comments: f.Comments})
365+
format.Node(&buf, pkg.Fset, &printer.CommentedNode{Node: decl, Comments: f.Comments})
383366
// Remove each "@@@." in the output.
384367
// TODO(adonovan): not hygienic.
385368
out.Write(bytes.Replace(buf.Bytes(), []byte("@@@."), nil, -1))
386369

387-
last = printSameLineComment(&out, f.Comments, lprog.Fset, end)
370+
last = printSameLineComment(&out, f.Comments, pkg.Fset, end)
388371

389372
out.WriteString("\n\n")
390373
}

cmd/bundle/main_test.go

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

5-
// +build go1.9
6-
75
package main
86

97
import (
@@ -14,10 +12,11 @@ import (
1412
"runtime"
1513
"testing"
1614

17-
"golang.org/x/tools/go/buildutil"
15+
"golang.org/x/tools/go/packages/packagestest"
1816
)
1917

20-
func TestBundle(t *testing.T) {
18+
func TestBundle(t *testing.T) { packagestest.TestAll(t, testBundle) }
19+
func testBundle(t *testing.T, x packagestest.Exporter) {
2120
load := func(name string) string {
2221
data, err := ioutil.ReadFile(name)
2322
if err != nil {
@@ -26,25 +25,31 @@ func TestBundle(t *testing.T) {
2625
return string(data)
2726
}
2827

29-
ctxt = buildutil.FakeContext(map[string]map[string]string{
30-
"initial": {
31-
"a.go": load("testdata/src/initial/a.go"),
32-
"b.go": load("testdata/src/initial/b.go"),
33-
"c.go": load("testdata/src/initial/c.go"),
34-
},
35-
"domain.name/importdecl": {
36-
"p.go": load("testdata/src/domain.name/importdecl/p.go"),
28+
e := packagestest.Export(t, x, []packagestest.Module{
29+
{
30+
Name: "initial",
31+
Files: map[string]interface{}{
32+
"a.go": load("testdata/src/initial/a.go"),
33+
"b.go": load("testdata/src/initial/b.go"),
34+
"c.go": load("testdata/src/initial/c.go"),
35+
},
3736
},
38-
"fmt": {
39-
"print.go": `package fmt; func Println(...interface{})`,
37+
{
38+
Name: "domain.name/importdecl",
39+
Files: map[string]interface{}{
40+
"p.go": load("testdata/src/domain.name/importdecl/p.go"),
41+
},
4042
},
4143
})
44+
defer e.Cleanup()
45+
testingOnlyPackagesConfig = e.Config
4246

4347
os.Args = os.Args[:1] // avoid e.g. -test=short in the output
4448
out, err := bundle("initial", "github.com/dest", "dest", "prefix", "tag")
4549
if err != nil {
4650
t.Fatal(err)
4751
}
52+
4853
if got, want := string(out), load("testdata/out.golden"); got != want {
4954
t.Errorf("-- got --\n%s\n-- want --\n%s\n-- diff --", got, want)
5055

cmd/toolstash/main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
// copy of an assembler or compiler and check that they produce identical
4646
// object files. If not, toolstash reports the mismatch and exits with a failure status.
4747
// As part of reporting the mismatch, toolstash reinvokes the command with
48-
// the -S flag and identifies the first divergence in the assembly output.
48+
// the -S=2 flag and identifies the first divergence in the assembly output.
4949
// If the command is a Go compiler, toolstash also determines whether the
5050
// difference is triggered by optimization passes.
5151
// On failure, toolstash leaves additional information in files named
@@ -275,7 +275,7 @@ func compareTool() {
275275
return
276276
}
277277

278-
extra := "-S"
278+
extra := "-S=2"
279279
switch {
280280
default:
281281
log.Fatalf("unknown tool %s", tool)

go/analysis/analysis.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -95,12 +95,13 @@ type Pass struct {
9595
Analyzer *Analyzer // the identity of the current analyzer
9696

9797
// syntax and type information
98-
Fset *token.FileSet // file position information
99-
Files []*ast.File // the abstract syntax tree of each file
100-
OtherFiles []string // names of non-Go files of this package
101-
Pkg *types.Package // type information about the package
102-
TypesInfo *types.Info // type information about the syntax trees
103-
TypesSizes types.Sizes // function for computing sizes of types
98+
Fset *token.FileSet // file position information
99+
Files []*ast.File // the abstract syntax tree of each file
100+
OtherFiles []string // names of non-Go files of this package
101+
IgnoredFiles []string // names of ignored source files in this package
102+
Pkg *types.Package // type information about the package
103+
TypesInfo *types.Info // type information about the syntax trees
104+
TypesSizes types.Sizes // function for computing sizes of types
104105

105106
// Report reports a Diagnostic, a finding about a specific location
106107
// in the analyzed source code such as a potential mistake.

0 commit comments

Comments
 (0)