GoPLS Viewer

Home|gopls/go/internal/packagesdriver/sizes.go
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.
4
5// Package packagesdriver fetches type sizes for go/packages and go/analysis.
6package packagesdriver
7
8import (
9    "context"
10    "fmt"
11    "go/types"
12    "strings"
13
14    "golang.org/x/tools/internal/gocommand"
15)
16
17var debug = false
18
19func GetSizesGolist(ctx context.Contextinv gocommand.InvocationgocmdRunner *gocommand.Runner) (types.Sizeserror) {
20    inv.Verb = "list"
21    inv.Args = []string{"-f""{{context.GOARCH}} {{context.Compiler}}""--""unsafe"}
22    stdoutstderrfriendlyErrrawErr := gocmdRunner.RunRaw(ctxinv)
23    var goarchcompiler string
24    if rawErr != nil {
25        if rawErrMsg := rawErr.Error(); strings.Contains(rawErrMsg"cannot find main module") || strings.Contains(rawErrMsg"go.mod file not found") {
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?
28            inv.Verb = "env"
29            inv.Args = []string{"GOARCH"}
30            envoutenverr := gocmdRunner.Run(ctxinv)
31            if enverr != nil {
32                return nilenverr
33            }
34            goarch = strings.TrimSpace(envout.String())
35            compiler = "gc"
36        } else {
37            return nilfriendlyErr
38        }
39    } else {
40        fields := strings.Fields(stdout.String())
41        if len(fields) < 2 {
42            return nilfmt.Errorf("could not parse GOARCH and Go compiler in format \"<GOARCH> <compiler>\":\nstdout: <<%s>>\nstderr: <<%s>>",
43                stdout.String(), stderr.String())
44        }
45        goarch = fields[0]
46        compiler = fields[1]
47    }
48    return types.SizesFor(compilergoarch), nil
49}
50
MembersX
GetSizesGolist.ctx
GetSizesGolist.stdout
GetSizesGolist.BlockStmt.BlockStmt.enverr
context
fmt
types
gocommand
GetSizesGolist.inv
GetSizesGolist.gocmdRunner
GetSizesGolist.rawErr
GetSizesGolist.goarch
GetSizesGolist.BlockStmt.rawErrMsg
GetSizesGolist.BlockStmt.BlockStmt.envout
strings
GetSizesGolist.stderr
GetSizesGolist.compiler
debug
GetSizesGolist
GetSizesGolist.friendlyErr
GetSizesGolist.BlockStmt.fields
Members
X