Skip to content

Commit 45971c6

Browse files
committed
cmd/go: fix disallow of p/vendor/x during vendor experiment
The percolation of errors upward in the load process could drop errors, meaning that a build tree could, depending on the processing order, import the same directory as both "p/vendor/x" and as "x". That's not supposed to be allowed. But then, worse, the build would generate two jobs for building that directory, which would use the same work space and overwrite each other's files, leading to very strange failures. Two fixes: 1. Fix the propagation of errors upward (prefer errors over success). 2. Check explicitly for duplicated packages before starting a build. New test for golang#1. Since golang#2 can't happen, tested golang#2 by hand after reverting fix for golang#1. Fixes golang#11913. Change-Id: I6d2fc65f93b8fb5f3b263ace8d5f68d803a2ae5c Reviewed-on: https://go-review.googlesource.com/13022 Reviewed-by: Ian Lance Taylor <[email protected]>
1 parent c5dff72 commit 45971c6

File tree

6 files changed

+43
-5
lines changed

6 files changed

+43
-5
lines changed

src/cmd/go/pkg.go

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -900,11 +900,10 @@ func (p *Package) load(stk *importStack, bp *build.Package, err error) *Package
900900
deps[path] = p1
901901
imports = append(imports, p1)
902902
for _, dep := range p1.deps {
903-
// Do not overwrite entries installed by direct import
904-
// just above this loop. Those have stricter constraints
905-
// about internal and vendor visibility and may contain
906-
// errors that we need to preserve.
907-
if deps[dep.ImportPath] == nil {
903+
// The same import path could produce an error or not,
904+
// depending on what tries to import it.
905+
// Prefer to record entries with errors, so we can report them.
906+
if deps[dep.ImportPath] == nil || dep.Error != nil {
908907
deps[dep.ImportPath] = dep
909908
}
910909
}
@@ -1612,6 +1611,23 @@ func packagesForBuild(args []string) []*Package {
16121611
}
16131612
}
16141613
exitIfErrors()
1614+
1615+
// Check for duplicate loads of the same package.
1616+
// That should be impossible, but if it does happen then
1617+
// we end up trying to build the same package twice,
1618+
// usually in parallel overwriting the same files,
1619+
// which doesn't work very well.
1620+
seen := map[string]bool{}
1621+
reported := map[string]bool{}
1622+
for _, pkg := range packageList(pkgs) {
1623+
if seen[pkg.ImportPath] && !reported[pkg.ImportPath] {
1624+
reported[pkg.ImportPath] = true
1625+
errorf("internal error: duplicate loads of %s", pkg.ImportPath)
1626+
}
1627+
seen[pkg.ImportPath] = true
1628+
}
1629+
exitIfErrors()
1630+
16151631
return pkgs
16161632
}
16171633

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package p
2+
3+
import (
4+
_ "q/y"
5+
_ "q/z"
6+
)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package x
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package y
2+
3+
import _ "x"
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package z
2+
3+
import _ "q/vendor/x"

src/cmd/go/vendor_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,3 +186,12 @@ func TestVendorGetUpdate(t *testing.T) {
186186
tg.run("get", "github.com/rsc/go-get-issue-11864")
187187
tg.run("get", "-u", "github.com/rsc/go-get-issue-11864")
188188
}
189+
190+
func TestVendorCache(t *testing.T) {
191+
tg := testgo(t)
192+
defer tg.cleanup()
193+
tg.setenv("GOPATH", filepath.Join(tg.pwd(), "testdata/testvendor"))
194+
tg.setenv("GO15VENDOREXPERIMENT", "1")
195+
tg.runFail("build", "p")
196+
tg.grepStderr("must be imported as x", "did not fail to build p")
197+
}

0 commit comments

Comments
 (0)