forked from ko-build/ko
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontext.go
183 lines (153 loc) · 4.83 KB
/
context.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/*
Copyright 2021 Google LLC All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package build
import (
"bytes"
"context"
"encoding/json"
"fmt"
gb "go/build"
"io"
"log"
"os/exec"
"strings"
"golang.org/x/tools/go/packages"
)
const (
gorootWarningTemplate = `NOTICE!
-----------------------------------------------------------------
ko and go have mismatched GOROOT:
go/build.Default.GOROOT = %q
$(go env GOROOT) = %q
Inferring GOROOT=%q
Run this to remove this warning:
export GOROOT=$(go env GOROOT)
For more information see:
https://github.com/google/ko/issues/106
-----------------------------------------------------------------
`
)
// https://golang.org/pkg/cmd/go/internal/modinfo/#ModulePublic
type modules struct {
main *modInfo
deps map[string]*modInfo
}
type modInfo struct {
Path string
Dir string
Main bool
}
type buildContext interface {
importPackage(path string, srcDir string) (*gb.Package, error)
moduleInfo(ctx context.Context) (*modules, error)
qualifyLocalImport(importpath string) (string, error)
}
type goBuildContext struct {
// Not a pointer, we need a copy of the go/build.Context. Otherwise, we'll
// end up with race conditions for parallel image builds.
bc gb.Context
}
// newBuildContext creates a new buildContext, which wraps a go/build.Context.
func newBuildContext(ctx context.Context, dir string) (buildContext, error) {
g := &goBuildContext{
bc: gb.Default,
}
g.bc.Dir = dir
// If $(go env GOROOT) successfully returns a non-empty string that differs from
// the default build context GOROOT, print a warning and use $(go env GOROOT).
goroot, err := getGoroot(ctx, dir)
if err != nil {
// On error, print the output and set goroot to "" to avoid using it later.
log.Printf("Unexpected error running \"go env GOROOT\": %v\n%v", err, goroot)
goroot = ""
} else if goroot == "" {
log.Printf(`Unexpected: $(go env GOROOT) == ""`)
}
if goroot != "" && g.bc.GOROOT != goroot {
log.Printf(gorootWarningTemplate, g.bc.GOROOT, goroot, goroot)
g.bc.GOROOT = goroot
}
return g, nil
}
// importPackage wraps go/build.Context Import()
func (g *goBuildContext) importPackage(path string, srcDir string) (*gb.Package, error) {
return g.bc.Import(path, srcDir, gb.ImportComment)
}
// moduleInfo returns the module path and module root directory for a project
// using go modules, otherwise returns nil.
//
// Related: https://github.com/golang/go/issues/26504
func (g *goBuildContext) moduleInfo(ctx context.Context) (*modules, error) {
modules := modules{
deps: make(map[string]*modInfo),
}
// TODO we read all the output as a single byte array - it may
// be possible & more efficient to stream it
cmd := exec.CommandContext(ctx, "go", "list", "-mod=readonly", "-json", "-m", "all")
cmd.Dir = g.bc.Dir
output, err := cmd.Output()
if err != nil {
return nil, nil
}
dec := json.NewDecoder(bytes.NewReader(output))
for {
var info modInfo
err := dec.Decode(&info)
if err == io.EOF {
// all done
break
}
modules.deps[info.Path] = &info
if info.Main {
modules.main = &info
}
if err != nil {
return nil, fmt.Errorf("error reading module data %w", err)
}
}
if modules.main == nil {
return nil, fmt.Errorf("couldn't find main module")
}
return &modules, nil
}
func (g *goBuildContext) qualifyLocalImport(importpath string) (string, error) {
cfg := &packages.Config{
Mode: packages.NeedName,
Dir: g.bc.Dir,
}
pkgs, err := packages.Load(cfg, importpath)
if err != nil {
return "", err
}
if len(pkgs) != 1 {
return "", fmt.Errorf("found %d local packages, expected 1", len(pkgs))
}
return pkgs[0].PkgPath, nil
}
// getGoroot shells out to `go env GOROOT` to determine
// the GOROOT for the installed version of go so that we
// can set it in our buildContext. By default, the GOROOT
// of our buildContext is set to the GOROOT at install
// time for `ko`, which means that we break when certain
// package managers update go or when using a pre-built
// `ko` binary that expects a different GOROOT.
//
// See https://github.com/google/ko/issues/106
func getGoroot(ctx context.Context, dir string) (string, error) {
cmd := exec.CommandContext(ctx, "go", "env", "GOROOT")
// It may not necessary to set the command working directory here,
// but it helps keep everything consistent.
cmd.Dir = dir
output, err := cmd.Output()
return strings.TrimSpace(string(output)), err
}