Skip to content

Commit d21c7b7

Browse files
marwan-at-workBryan C. Mills
authored and
Bryan C. Mills
committed
cmd/go: disallow go.sum updates in -mod=readonly
When running go build with the flag -mod=readonly, it fails the build if go.sum files requires updating. This ensures that CI/CD systems get a complete go.sum file so that they'd never hit a notary, assuming the CI/CD system passes the above flag. I am not familiar with the entire codebase but I assume goSum.dirty will always be true if go.sum has any missing lines. Fixes #30667 Change-Id: I767d3b594055d8c10048f4c68e6687c94bb0545c Reviewed-on: https://go-review.googlesource.com/c/go/+/166237 Reviewed-by: Bryan C. Mills <[email protected]> Run-TryBot: Bryan C. Mills <[email protected]> TryBot-Result: Gobot Gobot <[email protected]>
1 parent 19966e9 commit d21c7b7

File tree

4 files changed

+46
-4
lines changed

4 files changed

+46
-4
lines changed

src/cmd/go/internal/modfetch/fetch.go

+3
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,9 @@ func WriteGoSum() {
509509
// Don't bother opening the go.sum file if we don't have anything to add.
510510
return
511511
}
512+
if cfg.BuildMod == "readonly" {
513+
base.Fatalf("go: updates to go.sum needed, disabled by -mod=readonly")
514+
}
512515

513516
// We want to avoid races between creating the lockfile and deleting it, but
514517
// we also don't want to leave a permanent lockfile in the user's repository.

src/cmd/go/internal/modload/init.go

+7-4
Original file line numberDiff line numberDiff line change
@@ -665,18 +665,21 @@ func WriteGoMod() {
665665
base.Fatalf("go: %v", err)
666666
}
667667

668+
dirty := !bytes.Equal(new, modFileData)
669+
if dirty && cfg.BuildMod == "readonly" {
670+
// If we're about to fail due to -mod=readonly,
671+
// prefer to report a dirty go.mod over a dirty go.sum
672+
base.Fatalf("go: updates to go.mod needed, disabled by -mod=readonly")
673+
}
668674
// Always update go.sum, even if we didn't change go.mod: we may have
669675
// downloaded modules that we didn't have before.
670676
modfetch.WriteGoSum()
671677

672-
if bytes.Equal(new, modFileData) {
678+
if !dirty {
673679
// We don't need to modify go.mod from what we read previously.
674680
// Ignore any intervening edits.
675681
return
676682
}
677-
if cfg.BuildMod == "readonly" {
678-
base.Fatalf("go: updates to go.mod needed, disabled by -mod=readonly")
679-
}
680683

681684
unlock := modfetch.SideLock()
682685
defer unlock()

src/cmd/go/testdata/script/mod_file_proxy.txt

+7
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,10 @@ require rsc.io/quote v1.5.1
2323
-- $WORK/x/x.go --
2424
package x
2525
import _ "rsc.io/quote"
26+
-- $WORK/x/go.sum --
27+
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c h1:pvCbr/wm8HzDD3fVywevekufpn6tCGPY3spdHeZJEsw=
28+
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
29+
rsc.io/quote v1.5.1 h1:ZE3OgnVGrhXtFkGw90HwW992ZRqcdli/33DLqEYsoxA=
30+
rsc.io/quote v1.5.1/go.mod h1:LzX7hefJvL54yjefDEDHNONDjII0t9xZLPXsUe+TKr0=
31+
rsc.io/sampler v1.3.0 h1:HLGR/BgEtI3r0uymSP/nl2uPLsUnNJX8toRyhfpBTII=
32+
rsc.io/sampler v1.3.0/go.mod h1:T1hPZKmBbMNahiBKFy5HrXp6adAjACjK9JXDnKaTXpA=
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Test that go.sum does not get updated when -mod=readonly flag is set
2+
env GO111MODULE=on
3+
4+
go get rsc.io/quote
5+
go mod tidy
6+
7+
# go.sum != dirty; -mod=readonly
8+
go build -mod=readonly
9+
10+
# dirty up go.sum by removing it.
11+
rm go.sum
12+
13+
# go.sum == dirty; -mod=readonly
14+
! go build -mod=readonly
15+
16+
stderr 'go: updates to go.sum needed, disabled by -mod=readonly'
17+
18+
-- go.mod --
19+
module m
20+
21+
-- main.go --
22+
23+
package main
24+
25+
import "rsc.io/quote"
26+
27+
func main() {
28+
println(quote.Hello())
29+
}

0 commit comments

Comments
 (0)