Skip to content

Commit cd694d8

Browse files
adonovangopherbot
authored andcommitted
go/packages: include "unsafe".GoFiles=["unsafe.go"]
This change causes the unsafe package's GoFiles list to include unsafe.go, and documents the field more precisely. This file is never compiled, but it is legal Go syntax, and serves as documentation. It is useful for client tools to know where to find it. Also, remove the corresponding workaround in gopls. Fixes golang/go#59929 Change-Id: I4ef9f4c16c5b5b74ee7a7c4d1f7eb3736f779b91 Reviewed-on: https://go-review.googlesource.com/c/tools/+/491375 Reviewed-by: Robert Findley <[email protected]> Run-TryBot: Alan Donovan <[email protected]> Auto-Submit: Alan Donovan <[email protected]> Reviewed-by: Michael Matloob <[email protected]> TryBot-Result: Gopher Robot <[email protected]>
1 parent 33c741d commit cd694d8

File tree

7 files changed

+20
-46
lines changed

7 files changed

+20
-46
lines changed

go/packages/golist.go

+6-8
Original file line numberDiff line numberDiff line change
@@ -625,7 +625,12 @@ func (state *golistState) createDriverResponse(words ...string) (*driverResponse
625625
}
626626

627627
if pkg.PkgPath == "unsafe" {
628-
pkg.GoFiles = nil // ignore fake unsafe.go file
628+
pkg.CompiledGoFiles = nil // ignore fake unsafe.go file (#59929)
629+
} else if len(pkg.CompiledGoFiles) == 0 {
630+
// Work around for pre-go.1.11 versions of go list.
631+
// TODO(matloob): they should be handled by the fallback.
632+
// Can we delete this?
633+
pkg.CompiledGoFiles = pkg.GoFiles
629634
}
630635

631636
// Assume go list emits only absolute paths for Dir.
@@ -663,13 +668,6 @@ func (state *golistState) createDriverResponse(words ...string) (*driverResponse
663668
response.Roots = append(response.Roots, pkg.ID)
664669
}
665670

666-
// Work around for pre-go.1.11 versions of go list.
667-
// TODO(matloob): they should be handled by the fallback.
668-
// Can we delete this?
669-
if len(pkg.CompiledGoFiles) == 0 {
670-
pkg.CompiledGoFiles = pkg.GoFiles
671-
}
672-
673671
// Temporary work-around for golang/go#39986. Parse filenames out of
674672
// error messages. This happens if there are unrecoverable syntax
675673
// errors in the source, so we can't match on a specific error message.

go/packages/packages.go

+3
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,9 @@ type Package struct {
308308
TypeErrors []types.Error
309309

310310
// GoFiles lists the absolute file paths of the package's Go source files.
311+
// It may include files that should not be compiled, for example because
312+
// they contain non-matching build tags, are documentary pseudo-files such as
313+
// unsafe/unsafe.go or builtin/builtin.go, or are subject to cgo preprocessing.
311314
GoFiles []string
312315

313316
// CompiledGoFiles lists the absolute file paths of the package's source

go/packages/packages_test.go

+9-5
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ func testLoadImportsGraph(t *testing.T, exporter packagestest.Exporter) {
217217
id string
218218
wantName string
219219
wantKind string
220-
wantSrcs string
220+
wantSrcs string // = {Go,Other,Embed}Files
221221
wantIgnored string
222222
}{
223223
{"golang.org/fake/a", "a", "package", "a.go", ""},
@@ -227,7 +227,7 @@ func testLoadImportsGraph(t *testing.T, exporter packagestest.Exporter) {
227227
{"container/list", "list", "package", "list.go", ""},
228228
{"golang.org/fake/subdir/d", "d", "package", "d.go", ""},
229229
{"golang.org/fake/subdir/d.test", "main", "command", "0.go", ""},
230-
{"unsafe", "unsafe", "package", "", ""},
230+
{"unsafe", "unsafe", "package", "unsafe.go", ""},
231231
} {
232232
p, ok := all[test.id]
233233
if !ok {
@@ -250,10 +250,10 @@ func testLoadImportsGraph(t *testing.T, exporter packagestest.Exporter) {
250250
}
251251

252252
if srcs := strings.Join(srcs(p), " "); srcs != test.wantSrcs {
253-
t.Errorf("%s.Srcs = [%s], want [%s]", test.id, srcs, test.wantSrcs)
253+
t.Errorf("%s.{Go,Other,Embed}Files = [%s], want [%s]", test.id, srcs, test.wantSrcs)
254254
}
255255
if ignored := strings.Join(cleanPaths(p.IgnoredFiles), " "); ignored != test.wantIgnored {
256-
t.Errorf("%s.Srcs = [%s], want [%s]", test.id, ignored, test.wantIgnored)
256+
t.Errorf("%s.IgnoredFiles = [%s], want [%s]", test.id, ignored, test.wantIgnored)
257257
}
258258
}
259259

@@ -2788,7 +2788,11 @@ func errorMessages(errors []packages.Error) []string {
27882788
}
27892789

27902790
func srcs(p *packages.Package) []string {
2791-
return cleanPaths(append(append(p.GoFiles[:len(p.GoFiles):len(p.GoFiles)], p.OtherFiles...), p.EmbedFiles...))
2791+
var files []string
2792+
files = append(files, p.GoFiles...)
2793+
files = append(files, p.OtherFiles...)
2794+
files = append(files, p.EmbedFiles...)
2795+
return cleanPaths(files)
27922796
}
27932797

27942798
// cleanPaths attempts to reduce path names to stable forms

gopls/internal/lsp/cache/load.go

-26
Original file line numberDiff line numberDiff line change
@@ -159,32 +159,6 @@ func (s *snapshot) load(ctx context.Context, allowNetwork bool, scopes ...loadSc
159159
return bug.Errorf("internal error: go/packages returned multiple packages for standalone file")
160160
}
161161

162-
// Workaround for a bug (?) that has been in go/packages since
163-
// the outset: Package("unsafe").GoFiles=[], whereas it should
164-
// include unsafe/unsafe.go. Derive it from builtins.go.
165-
//
166-
// This workaround relies on the fact that we always add both
167-
// builtins and unsafe to the set of scopes in the workspace load.
168-
//
169-
// TODO(adonovan): fix upstream in go/packages.
170-
// (Does this need a proposal? Arguably not.)
171-
{
172-
var builtin, unsafe *packages.Package
173-
for _, pkg := range pkgs {
174-
switch pkg.ID {
175-
case "unsafe":
176-
unsafe = pkg
177-
case "builtin":
178-
builtin = pkg
179-
}
180-
}
181-
if builtin != nil && unsafe != nil && len(builtin.GoFiles) == 1 {
182-
unsafe.GoFiles = []string{
183-
filepath.Join(filepath.Dir(builtin.GoFiles[0]), "../unsafe/unsafe.go"),
184-
}
185-
}
186-
}
187-
188162
moduleErrs := make(map[string][]packages.Error) // module path -> errors
189163
filterFunc := s.view.filterFunc()
190164
newMetadata := make(map[PackageID]*source.Metadata)

gopls/internal/lsp/cache/view.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -862,10 +862,8 @@ func (s *snapshot) loadWorkspace(ctx context.Context, firstAttempt bool) (loadEr
862862
// If we're loading anything, ensure we also load builtin,
863863
// since it provides fake definitions (and documentation)
864864
// for types like int that are used everywhere.
865-
// ("unsafe" is also needed since its sole GoFiles is
866-
// derived from that of "builtin" via a workaround in load.)
867865
if len(scopes) > 0 {
868-
scopes = append(scopes, packageLoadScope("builtin"), packageLoadScope("unsafe"))
866+
scopes = append(scopes, packageLoadScope("builtin"))
869867
}
870868
loadErr = s.load(ctx, true, scopes...)
871869

gopls/internal/regtest/misc/references_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ func _() {
104104

105105
func TestDefsRefsBuiltins(t *testing.T) {
106106
testenv.NeedsGo1Point(t, 17) // for unsafe.{Add,Slice}
107-
// TODO(adonovan): add unsafe.SliceData,String,StringData} in later go versions.
107+
// TODO(adonovan): add unsafe.{SliceData,String,StringData} in later go versions.
108108
const files = `
109109
-- go.mod --
110110
module example.com

gopls/internal/regtest/misc/workspace_symbol_test.go

-3
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,6 @@ package exclude
3232
const K2 = "exclude.go"
3333
`
3434

35-
// NB: the name K was chosen to avoid spurious
36-
// matches in the always-present "unsafe" package.
3735
Run(t, files, func(t *testing.T, env *Env) {
3836
env.OpenFile("a.go")
3937
checkSymbols(env, "K", "K1")
@@ -73,7 +71,6 @@ const (
7371
"Fooex", // shorter than Fooest, FooBar, lexically before Fooey
7472
"Fooey", // shorter than Fooest, Foobar
7573
"Fooest",
76-
"unsafe.Offsetof", // a very fuzzy match
7774
)
7875
})
7976
}

0 commit comments

Comments
 (0)