Skip to content

Commit 0ed70ef

Browse files
committed
cmd/go: fix rebuild after installation of new Go release
The loading of zversion.go was expecting it to be in package runtime, but it moved to runtime/internal/sys. Worse, the load was not checking the error. Update the path, check the error, add a test. Fixes #14176. Change-Id: I203c40afe1448875581415d5e42c29f09b14545d Reviewed-on: https://go-review.googlesource.com/19180 Run-TryBot: Russ Cox <[email protected]> Reviewed-by: Brad Fitzpatrick <[email protected]> TryBot-Result: Gobot Gobot <[email protected]>
1 parent 182a9db commit 0ed70ef

File tree

2 files changed

+85
-10
lines changed

2 files changed

+85
-10
lines changed

src/cmd/go/go_test.go

Lines changed: 79 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -657,6 +657,9 @@ func TestGoBuildDashAInDevBranch(t *testing.T) {
657657
tg.setenv("TESTGO_IS_GO_RELEASE", "0")
658658
tg.run("build", "-v", "-a", "math")
659659
tg.grepStderr("runtime", "testgo build -a math in dev branch DID NOT build runtime, but should have")
660+
661+
// Everything is out of date. Rebuild to leave things in a better state.
662+
tg.run("install", "std")
660663
}
661664

662665
func TestGoBuildDashAInReleaseBranch(t *testing.T) {
@@ -672,11 +675,80 @@ func TestGoBuildDashAInReleaseBranch(t *testing.T) {
672675
tg.grepStderr("runtime", "testgo build -a math in release branch DID NOT build runtime, but should have")
673676

674677
// Now runtime.a is updated (newer mtime), so everything would look stale if not for being a release.
675-
//
676678
tg.run("build", "-v", "net/http")
677679
tg.grepStderrNot("strconv", "testgo build -v net/http in release branch with newer runtime.a DID build strconv but should not have")
678680
tg.grepStderrNot("golang.org/x/net/http2/hpack", "testgo build -v net/http in release branch with newer runtime.a DID build .../golang.org/x/net/http2/hpack but should not have")
679681
tg.grepStderrNot("net/http", "testgo build -v net/http in release branch with newer runtime.a DID build net/http but should not have")
682+
683+
// Everything is out of date. Rebuild to leave things in a better state.
684+
tg.run("install", "std")
685+
}
686+
687+
func TestNewReleaseRebuildsStalePackagesInGOPATH(t *testing.T) {
688+
if testing.Short() {
689+
t.Skip("don't rebuild the standard library in short mode")
690+
}
691+
692+
tg := testgo(t)
693+
defer tg.cleanup()
694+
695+
addNL := func(name string) (restore func()) {
696+
data, err := ioutil.ReadFile(name)
697+
if err != nil {
698+
t.Fatal(err)
699+
}
700+
old := data
701+
data = append(data, '\n')
702+
if err := ioutil.WriteFile(name, append(data, '\n'), 0666); err != nil {
703+
t.Fatal(err)
704+
}
705+
tg.sleep()
706+
return func() {
707+
if err := ioutil.WriteFile(name, old, 0666); err != nil {
708+
t.Fatal(err)
709+
}
710+
}
711+
}
712+
713+
tg.setenv("TESTGO_IS_GO_RELEASE", "1")
714+
715+
tg.tempFile("d1/src/p1/p1.go", `package p1`)
716+
tg.setenv("GOPATH", tg.path("d1"))
717+
tg.run("install", "-a", "p1")
718+
tg.wantNotStale("p1", "./testgo list claims p1 is stale, incorrectly")
719+
tg.sleep()
720+
721+
// Changing mtime and content of runtime/internal/sys/sys.go
722+
// should have no effect: we're in a release, which doesn't rebuild
723+
// for general mtime or content changes.
724+
sys := runtime.GOROOT() + "/src/runtime/internal/sys/sys.go"
725+
restore := addNL(sys)
726+
defer restore()
727+
tg.wantNotStale("p1", "./testgo list claims p1 is stale, incorrectly, after updating runtime/internal/sys/sys.go")
728+
restore()
729+
tg.wantNotStale("p1", "./testgo list claims p1 is stale, incorrectly, after restoring runtime/internal/sys/sys.go")
730+
731+
// But changing runtime/internal/sys/zversion.go should have an effect:
732+
// that's how we tell when we flip from one release to another.
733+
zversion := runtime.GOROOT() + "/src/runtime/internal/sys/zversion.go"
734+
restore = addNL(zversion)
735+
defer restore()
736+
tg.wantStale("p1", "./testgo list claims p1 is NOT stale, incorrectly, after changing to new release")
737+
restore()
738+
tg.wantNotStale("p1", "./testgo list claims p1 is stale, incorrectly, after changing back to old release")
739+
addNL(zversion)
740+
tg.wantStale("p1", "./testgo list claims p1 is NOT stale, incorrectly, after changing again to new release")
741+
tg.run("install", "p1")
742+
tg.wantNotStale("p1", "./testgo list claims p1 is stale after building with new release")
743+
744+
// Restore to "old" release.
745+
restore()
746+
tg.wantStale("p1", "./testgo list claims p1 is NOT stale, incorrectly, after changing to old release after new build")
747+
tg.run("install", "p1")
748+
tg.wantNotStale("p1", "./testgo list claims p1 is stale after building with old release")
749+
750+
// Everything is out of date. Rebuild to leave things in a better state.
751+
tg.run("install", "std")
680752
}
681753

682754
func TestGoListStandard(t *testing.T) {
@@ -756,8 +828,8 @@ func TestGoInstallRebuildsStalePackagesInOtherGOPATH(t *testing.T) {
756828
sep := string(filepath.ListSeparator)
757829
tg.setenv("GOPATH", tg.path("d1")+sep+tg.path("d2"))
758830
tg.run("install", "p1")
759-
tg.wantNotStale("p1", "./testgo list mypkg claims p1 is stale, incorrectly")
760-
tg.wantNotStale("p2", "./testgo list mypkg claims p2 is stale, incorrectly")
831+
tg.wantNotStale("p1", "./testgo list claims p1 is stale, incorrectly")
832+
tg.wantNotStale("p2", "./testgo list claims p2 is stale, incorrectly")
761833
tg.sleep()
762834
if f, err := os.OpenFile(tg.path("d2/src/p2/p2.go"), os.O_WRONLY|os.O_APPEND, 0); err != nil {
763835
t.Fatal(err)
@@ -766,12 +838,12 @@ func TestGoInstallRebuildsStalePackagesInOtherGOPATH(t *testing.T) {
766838
} else {
767839
tg.must(f.Close())
768840
}
769-
tg.wantStale("p2", "./testgo list mypkg claims p2 is NOT stale, incorrectly")
770-
tg.wantStale("p1", "./testgo list mypkg claims p1 is NOT stale, incorrectly")
841+
tg.wantStale("p2", "./testgo list claims p2 is NOT stale, incorrectly")
842+
tg.wantStale("p1", "./testgo list claims p1 is NOT stale, incorrectly")
771843

772844
tg.run("install", "p1")
773-
tg.wantNotStale("p2", "./testgo list mypkg claims p2 is stale after reinstall, incorrectly")
774-
tg.wantNotStale("p1", "./testgo list mypkg claims p1 is stale after reinstall, incorrectly")
845+
tg.wantNotStale("p2", "./testgo list claims p2 is stale after reinstall, incorrectly")
846+
tg.wantNotStale("p1", "./testgo list claims p1 is stale after reinstall, incorrectly")
775847
}
776848

777849
func TestGoInstallDetectsRemovedFiles(t *testing.T) {

src/cmd/go/pkg.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1542,11 +1542,14 @@ func computeBuildID(p *Package) {
15421542
fmt.Fprintf(h, "file %s\n", file)
15431543
}
15441544

1545-
// Include the content of runtime/zversion.go in the hash
1545+
// Include the content of runtime/internal/sys/zversion.go in the hash
15461546
// for package runtime. This will give package runtime a
15471547
// different build ID in each Go release.
1548-
if p.Standard && p.ImportPath == "runtime" {
1549-
data, _ := ioutil.ReadFile(filepath.Join(p.Dir, "zversion.go"))
1548+
if p.Standard && p.ImportPath == "runtime/internal/sys" {
1549+
data, err := ioutil.ReadFile(filepath.Join(p.Dir, "zversion.go"))
1550+
if err != nil {
1551+
fatalf("go: %s", err)
1552+
}
15501553
fmt.Fprintf(h, "zversion %q\n", string(data))
15511554
}
15521555

0 commit comments

Comments
 (0)