Skip to content

Commit 8e7bc15

Browse files
committed
compiler needs to link vendored packages
1 parent f19201d commit 8e7bc15

File tree

2 files changed

+103
-7
lines changed

2 files changed

+103
-7
lines changed

builder_go110.go

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -159,13 +159,32 @@ func Build(bin string) error {
159159
return err
160160
}
161161

162+
// godog package may be vendored and may need importmap
163+
vendored := maybeVendoredGodog()
164+
162165
// compile godog testmain package archive
163166
// we do not depend on CGO so a lot of checks are not necessary
164-
cfg := filepath.Join(testdir, "importcfg.link")
167+
linkerCfg := filepath.Join(testdir, "importcfg.link")
168+
compilerCfg := linkerCfg
169+
if vendored != nil {
170+
data, err := ioutil.ReadFile(linkerCfg)
171+
if err != nil {
172+
return err
173+
}
174+
175+
data = append(data, []byte(fmt.Sprintf("importmap %s=%s\n", godogImportPath, vendored.ImportPath))...)
176+
compilerCfg = filepath.Join(testdir, "importcfg")
177+
178+
err = ioutil.WriteFile(compilerCfg, data, 0644)
179+
if err != nil {
180+
return err
181+
}
182+
}
183+
165184
testMainPkgOut := filepath.Join(testdir, "main.a")
166185
args := []string{
167186
"-o", testMainPkgOut,
168-
"-importcfg", cfg,
187+
"-importcfg", compilerCfg,
169188
"-p", "main",
170189
"-complete",
171190
}
@@ -181,7 +200,7 @@ func Build(bin string) error {
181200
// link test suite executable
182201
args = []string{
183202
"-o", bin,
184-
"-importcfg", cfg,
203+
"-importcfg", linkerCfg,
185204
"-buildmode=exe",
186205
}
187206
args = append(args, testMainPkgOut)
@@ -199,6 +218,26 @@ func Build(bin string) error {
199218
return nil
200219
}
201220

221+
func maybeVendoredGodog() *build.Package {
222+
dir, err := filepath.Abs(".")
223+
if err != nil {
224+
return nil
225+
}
226+
227+
for _, gopath := range gopaths {
228+
gopath = filepath.Join(gopath, "src")
229+
for strings.HasPrefix(dir, gopath) && dir != gopath {
230+
pkg, err := build.ImportDir(filepath.Join(dir, "vendor", godogImportPath), 0)
231+
if err != nil {
232+
dir = filepath.Dir(dir)
233+
continue
234+
}
235+
return pkg
236+
}
237+
}
238+
return nil
239+
}
240+
202241
func importPackage(dir string) *build.Package {
203242
pkg, _ := build.ImportDir(dir, 0)
204243

builder_test.go

Lines changed: 61 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"os"
88
"os/exec"
99
"path/filepath"
10+
"strings"
1011
"testing"
1112
)
1213

@@ -140,6 +141,17 @@ func buildTestCommand(t *testing.T, args ...string) *exec.Cmd {
140141
return exec.Command(bin, args...)
141142
}
142143

144+
func envVarsWithoutGopath() []string {
145+
var env []string
146+
for _, def := range os.Environ() {
147+
if strings.Index(def, "GOPATH=") == 0 {
148+
continue
149+
}
150+
env = append(env, def)
151+
}
152+
return env
153+
}
154+
143155
func TestGodogBuildWithSourceNotInGoPath(t *testing.T) {
144156
dir := filepath.Join(os.TempDir(), "godogs")
145157
err := buildTestPackage(dir, map[string]string{
@@ -298,8 +310,8 @@ func TestGodogBuildWithinGopath(t *testing.T) {
298310
}
299311
}
300312

301-
func TestGodogBuildWithVendoredGodog(t *testing.T) {
302-
gopath := filepath.Join(os.TempDir(), "_gp")
313+
func TestGodogBuildWithVendoredGodogAndMod(t *testing.T) {
314+
gopath := filepath.Join(os.TempDir(), "_gpc")
303315
dir := filepath.Join(gopath, "src", "godogs")
304316
err := buildTestPackage(dir, map[string]string{
305317
"godogs.feature": builderFeatureFile,
@@ -338,8 +350,53 @@ func TestGodogBuildWithVendoredGodog(t *testing.T) {
338350
var stdout, stderr bytes.Buffer
339351
cmd.Stdout = &stdout
340352
cmd.Stderr = &stderr
341-
cmd.Env = os.Environ()
342-
cmd.Env = append(cmd.Env, "GOPATH="+gopath)
353+
cmd.Env = append(envVarsWithoutGopath(), "GOPATH="+gopath)
354+
355+
if err := cmd.Run(); err != nil {
356+
t.Log(stdout.String())
357+
t.Log(stderr.String())
358+
t.Fatal(err)
359+
}
360+
}
361+
362+
func TestGodogBuildWithVendoredGodogWithoutModule(t *testing.T) {
363+
gopath := filepath.Join(os.TempDir(), "_gp")
364+
dir := filepath.Join(gopath, "src", "godogs")
365+
err := buildTestPackage(dir, map[string]string{
366+
"godogs.feature": builderFeatureFile,
367+
})
368+
if err != nil {
369+
os.RemoveAll(gopath)
370+
t.Fatal(err)
371+
}
372+
defer os.RemoveAll(gopath)
373+
374+
pkg := filepath.Join(dir, "vendor", "github.com", "DATA-DOG")
375+
if err := os.MkdirAll(pkg, 0755); err != nil {
376+
t.Fatal(err)
377+
}
378+
379+
prevDir, err := os.Getwd()
380+
if err != nil {
381+
t.Fatal(err)
382+
}
383+
384+
// symlink godog package
385+
if err := os.Symlink(prevDir, filepath.Join(pkg, "godog")); err != nil {
386+
t.Fatal(err)
387+
}
388+
389+
if err := os.Chdir(dir); err != nil {
390+
t.Fatal(err)
391+
}
392+
defer os.Chdir(prevDir)
393+
394+
cmd := buildTestCommand(t, "godogs.feature")
395+
396+
var stdout, stderr bytes.Buffer
397+
cmd.Stdout = &stdout
398+
cmd.Stderr = &stderr
399+
cmd.Env = append(envVarsWithoutGopath(), "GOPATH="+gopath)
343400

344401
if err := cmd.Run(); err != nil {
345402
t.Log(stdout.String())

0 commit comments

Comments
 (0)