1 // Copyright 2018 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
5 // Package packagesdriver fetches type sizes for go/packages and go/analysis.
17 "golang.org/x/tools/internal/gocommand"
22 func GetSizes(ctx context.Context, buildFlags, env []string, gocmdRunner *gocommand.Runner, dir string) (types.Sizes, error) {
23 // TODO(matloob): Clean this up. This code is mostly a copy of packages.findExternalDriver.
24 const toolPrefix = "GOPACKAGESDRIVER="
26 for _, env := range env {
27 if val := strings.TrimPrefix(env, toolPrefix); val != env {
34 tool, err = exec.LookPath("gopackagesdriver")
36 // We did not find the driver, so use "go list".
42 inv := gocommand.Invocation{
43 BuildFlags: buildFlags,
47 return GetSizesGolist(ctx, inv, gocmdRunner)
50 req, err := json.Marshal(struct {
51 Command string `json:"command"`
52 Env []string `json:"env"`
53 BuildFlags []string `json:"build_flags"`
57 BuildFlags: buildFlags,
60 return nil, fmt.Errorf("failed to encode message to driver tool: %v", err)
63 buf := new(bytes.Buffer)
64 cmd := exec.CommandContext(ctx, tool)
67 cmd.Stdin = bytes.NewReader(req)
69 cmd.Stderr = new(bytes.Buffer)
70 if err := cmd.Run(); err != nil {
71 return nil, fmt.Errorf("%v: %v: %s", tool, err, cmd.Stderr)
74 // Sizes, if not nil, is the types.Sizes to use when type checking.
77 if err := json.Unmarshal(buf.Bytes(), &response); err != nil {
80 return response.Sizes, nil
83 func GetSizesGolist(ctx context.Context, inv gocommand.Invocation, gocmdRunner *gocommand.Runner) (types.Sizes, error) {
85 inv.Args = []string{"-f", "{{context.GOARCH}} {{context.Compiler}}", "--", "unsafe"}
86 stdout, stderr, friendlyErr, rawErr := gocmdRunner.RunRaw(ctx, inv)
87 var goarch, compiler string
89 if strings.Contains(rawErr.Error(), "cannot find main module") {
90 // User's running outside of a module. All bets are off. Get GOARCH and guess compiler is gc.
91 // TODO(matloob): Is this a problem in practice?
93 inv.Args = []string{"GOARCH"}
94 envout, enverr := gocmdRunner.Run(ctx, inv)
98 goarch = strings.TrimSpace(envout.String())
101 return nil, friendlyErr
104 fields := strings.Fields(stdout.String())
106 return nil, fmt.Errorf("could not parse GOARCH and Go compiler in format \"<GOARCH> <compiler>\":\nstdout: <<%s>>\nstderr: <<%s>>",
107 stdout.String(), stderr.String())
112 return types.SizesFor(compiler, goarch), nil