Skip to content

Commit 78e59bb

Browse files
committed
cmd/go: support the -overlay flag for go mod commands
Move the declaration of the -overlay flag to base.AddModCommonFlags, where other flags that are needed for go mod commands and for builds are declared. The flag's already initialized in modload.Init so there's no additional work needed to be done to support it in the go mod commands. For #39958 Change-Id: I70725d620cc69cb820f6ed923d626f4fe041b1c5 Reviewed-on: https://go-review.googlesource.com/c/go/+/272126 Trust: Michael Matloob <[email protected]> Run-TryBot: Michael Matloob <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Bryan C. Mills <[email protected]> Reviewed-by: Jay Conrod <[email protected]>
1 parent c47eac7 commit 78e59bb

File tree

3 files changed

+13
-2
lines changed

3 files changed

+13
-2
lines changed

src/cmd/go/internal/base/flag.go

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"flag"
99

1010
"cmd/go/internal/cfg"
11+
"cmd/go/internal/fsys"
1112
"cmd/go/internal/str"
1213
)
1314

@@ -66,4 +67,5 @@ func AddModFlag(flags *flag.FlagSet) {
6667
func AddModCommonFlags(flags *flag.FlagSet) {
6768
flags.BoolVar(&cfg.ModCacheRW, "modcacherw", false, "")
6869
flags.StringVar(&cfg.ModFile, "modfile", "", "")
70+
flags.StringVar(&fsys.OverlayFile, "overlay", "", "")
6971
}

src/cmd/go/internal/work/build.go

+5-2
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,11 @@ func AddBuildFlags(cmd *base.Command, mask BuildFlagMask) {
267267
}
268268
if mask&OmitModCommonFlags == 0 {
269269
base.AddModCommonFlags(&cmd.Flag)
270+
} else {
271+
// Add the overlay flag even when we don't add the rest of the mod common flags.
272+
// This only affects 'go get' in GOPATH mode, but add the flag anyway for
273+
// consistency.
274+
cmd.Flag.StringVar(&fsys.OverlayFile, "overlay", "", "")
270275
}
271276
cmd.Flag.StringVar(&cfg.BuildContext.InstallSuffix, "installsuffix", "", "")
272277
cmd.Flag.Var(&load.BuildLdflags, "ldflags", "")
@@ -279,8 +284,6 @@ func AddBuildFlags(cmd *base.Command, mask BuildFlagMask) {
279284
cmd.Flag.BoolVar(&cfg.BuildTrimpath, "trimpath", false, "")
280285
cmd.Flag.BoolVar(&cfg.BuildWork, "work", false, "")
281286

282-
cmd.Flag.StringVar(&fsys.OverlayFile, "overlay", "", "")
283-
284287
// Undocumented, unstable debugging flags.
285288
cmd.Flag.StringVar(&cfg.DebugActiongraph, "debug-actiongraph", "", "")
286289
cmd.Flag.StringVar(&cfg.DebugTrace, "debug-trace", "", "")

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

+6
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,20 @@ cmp $WORK/overlay/get_doesnt_add_dep_go_mod $WORK/want_go_mod
3232
cd $WORK/gopath/src/overlay-sum-used
3333
! go get -d .
3434
stderr 'SECURITY ERROR'
35+
! go mod verify
36+
stderr 'SECURITY ERROR'
3537
go get -d -overlay overlay.json .
38+
go mod verify -overlay overlay.json
3639
# Overlaid go.sum is not rewritten.
3740
# Copy an incomplete file to the overlay file, and expect an error
3841
# attempting to update the file
3942
cp incomplete-sum-file $WORK/overlay/overlay-sum-used-correct-sums
4043
! go get -d -overlay overlay.json .
4144
stderr 'overlaid files can''t be opened for write'
4245
cmp incomplete-sum-file $WORK/overlay/overlay-sum-used-correct-sums
46+
! go mod tidy -overlay overlay.json
47+
stderr 'overlaid files can''t be opened for write'
48+
cmp incomplete-sum-file $WORK/overlay/overlay-sum-used-correct-sums
4349

4450
# -overlay works with -modfile.
4551
# There's an empty go.mod file in the directory, and the file alternate.mod is

0 commit comments

Comments
 (0)