github.com/powerman/golang-tools@v0.1.11-0.20220410185822-5ad214d8d803/go/internal/packagesdriver/sizes.go (about) 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. 6 package packagesdriver 7 8 import ( 9 "context" 10 "fmt" 11 "go/types" 12 "strings" 13 14 "github.com/powerman/golang-tools/internal/gocommand" 15 ) 16 17 var debug = false 18 19 func GetSizesGolist(ctx context.Context, inv gocommand.Invocation, gocmdRunner *gocommand.Runner) (types.Sizes, error) { 20 inv.Verb = "list" 21 inv.Args = []string{"-f", "{{context.GOARCH}} {{context.Compiler}}", "--", "unsafe"} 22 stdout, stderr, friendlyErr, rawErr := gocmdRunner.RunRaw(ctx, inv) 23 var goarch, compiler 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 envout, enverr := gocmdRunner.Run(ctx, inv) 31 if enverr != nil { 32 return nil, enverr 33 } 34 goarch = strings.TrimSpace(envout.String()) 35 compiler = "gc" 36 } else { 37 return nil, friendlyErr 38 } 39 } else { 40 fields := strings.Fields(stdout.String()) 41 if len(fields) < 2 { 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()) 44 } 45 goarch = fields[0] 46 compiler = fields[1] 47 } 48 return types.SizesFor(compiler, goarch), nil 49 }