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.
14 "golang.org/x/tools/internal/gocommand"
19 func GetSizesGolist(ctx context.Context, inv gocommand.Invocation, gocmdRunner *gocommand.Runner) (types.Sizes, error) {
21 inv.Args = []string{"-f", "{{context.GOARCH}} {{context.Compiler}}", "--", "unsafe"}
22 stdout, stderr, friendlyErr, rawErr := gocmdRunner.RunRaw(ctx, inv)
23 var goarch, compiler string
25 if strings.Contains(rawErr.Error(), "cannot find main module") {
26 // User's running outside of a module. All bets are off. Get GOARCH and guess compiler is gc.
27 // TODO(matloob): Is this a problem in practice?
29 inv.Args = []string{"GOARCH"}
30 envout, enverr := gocmdRunner.Run(ctx, inv)
34 goarch = strings.TrimSpace(envout.String())
37 return nil, friendlyErr
40 fields := strings.Fields(stdout.String())
42 return nil, fmt.Errorf("could not parse GOARCH and Go compiler in format \"<GOARCH> <compiler>\":\nstdout: <<%s>>\nstderr: <<%s>>",
43 stdout.String(), stderr.String())
48 return types.SizesFor(compiler, goarch), nil