Skip to content

Commit 4e98188

Browse files
dmitshurgopherbot
authored andcommitted
internal/imports: use go/packages instead of cmd/api to compute symbols
The API of syscall/js package is generated by reusing cmd/api. That has stopped being a runnable command after an internal refactor in Go 1.20. Since cmd/api was never really supported or meant to be used outside of GOROOT, switch to using go/packages and compute the API more directly ourselves. Also use the same approach to generate the API of package unsafe (whose API is also not present in GOROOT/api files at this time) instead of a fixed list that needs manual maintenance. This adds Add and Slice that were added to package unsafe in Go 1.17. It also removes ArbitraryType, since that symbol isn't a part of package unsafe's API but used in its documentation—it seems like an oversight that it was added in CL 24463. This CL intentionally leaves out unsafe's SliceData, String, StringData that were added in Go 1.20, so I can test out the new relui workflow to send a CL that regenerates this package. Fixes golang/go#58245. For golang/go#38706. Change-Id: Ibe0d89bf0469691bd16e0d0f501e3762256f2239 Reviewed-on: https://go-review.googlesource.com/c/tools/+/464715 Reviewed-by: Robert Findley <[email protected]> Reviewed-by: Dmitri Shuralyov <[email protected]> Auto-Submit: Dmitri Shuralyov <[email protected]> TryBot-Result: Gopher Robot <[email protected]> gopls-CI: kokoro <[email protected]> Run-TryBot: Dmitri Shuralyov <[email protected]>
1 parent 4e8ff89 commit 4e98188

File tree

2 files changed

+24
-18
lines changed

2 files changed

+24
-18
lines changed

internal/imports/mkstdlib.go

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"bytes"
1616
"fmt"
1717
"go/format"
18+
"go/token"
1819
"io"
1920
"io/ioutil"
2021
"log"
@@ -25,7 +26,7 @@ import (
2526
"sort"
2627
"strings"
2728

28-
exec "golang.org/x/sys/execabs"
29+
"golang.org/x/tools/go/packages"
2930
)
3031

3132
func mustOpen(name string) io.Reader {
@@ -42,8 +43,6 @@ func api(base string) string {
4243

4344
var sym = regexp.MustCompile(`^pkg (\S+).*?, (?:var|func|type|const) ([A-Z]\w*)`)
4445

45-
var unsafeSyms = map[string]bool{"Alignof": true, "ArbitraryType": true, "Offsetof": true, "Pointer": true, "Sizeof": true}
46-
4746
func main() {
4847
var buf bytes.Buffer
4948
outf := func(format string, args ...interface{}) {
@@ -60,10 +59,13 @@ func main() {
6059
f := readAPI()
6160
sc := bufio.NewScanner(f)
6261

62+
// The APIs of the syscall/js and unsafe packages need to be computed explicitly,
63+
// because they're not included in the GOROOT/api/go1.*.txt files at this time.
6364
pkgs := map[string]map[string]bool{
64-
"unsafe": unsafeSyms,
65+
"syscall/js": syms("syscall/js", "GOOS=js", "GOARCH=wasm"),
66+
"unsafe": syms("unsafe"),
6567
}
66-
paths := []string{"unsafe"}
68+
paths := []string{"syscall/js", "unsafe"}
6769

6870
for sc.Scan() {
6971
l := sc.Text()
@@ -118,23 +120,26 @@ func readAPI() io.Reader {
118120
readers = append(readers, mustOpen(api(name)))
119121
}
120122
}
121-
// The API of the syscall/js package needs to be computed explicitly,
122-
// because it's not included in the GOROOT/api/go1.*.txt files at this time.
123-
readers = append(readers, syscallJSAPI())
124123
return io.MultiReader(readers...)
125124
}
126125

127-
// syscallJSAPI returns the API of the syscall/js package.
128-
// It's computed from the contents of $(go env GOROOT)/src/syscall/js.
129-
func syscallJSAPI() io.Reader {
130-
var exeSuffix string
131-
if runtime.GOOS == "windows" {
132-
exeSuffix = ".exe"
126+
// syms computes the exported symbols in the specified package.
127+
func syms(pkg string, extraEnv ...string) map[string]bool {
128+
var env []string
129+
if len(extraEnv) != 0 {
130+
env = append(os.Environ(), extraEnv...)
133131
}
134-
cmd := exec.Command("go"+exeSuffix, "run", "cmd/api", "-contexts", "js-wasm", "syscall/js")
135-
out, err := cmd.Output()
132+
pkgs, err := packages.Load(&packages.Config{Mode: packages.NeedTypes, Env: env}, pkg)
136133
if err != nil {
137134
log.Fatalln(err)
135+
} else if len(pkgs) != 1 {
136+
log.Fatalf("got %d packages, want one package %q", len(pkgs), pkg)
137+
}
138+
syms := make(map[string]bool)
139+
for _, name := range pkgs[0].Types.Scope().Names() {
140+
if token.IsExported(name) {
141+
syms[name] = true
142+
}
138143
}
139-
return bytes.NewReader(out)
144+
return syms
140145
}

internal/imports/zstdlib.go

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)