Skip to content

Commit

Permalink
cmd/go: support the -overlay flag for go mod commands
Browse files Browse the repository at this point in the history
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 <matloob@golang.org>
Run-TryBot: Michael Matloob <matloob@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Bryan C. Mills <bcmills@google.com>
Reviewed-by: Jay Conrod <jayconrod@google.com>
  • Loading branch information
matloob committed Nov 20, 2020
1 parent c47eac7 commit 78e59bb
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/cmd/go/internal/base/flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"flag"

"cmd/go/internal/cfg"
"cmd/go/internal/fsys"
"cmd/go/internal/str"
)

Expand Down Expand Up @@ -66,4 +67,5 @@ func AddModFlag(flags *flag.FlagSet) {
func AddModCommonFlags(flags *flag.FlagSet) {
flags.BoolVar(&cfg.ModCacheRW, "modcacherw", false, "")
flags.StringVar(&cfg.ModFile, "modfile", "", "")
flags.StringVar(&fsys.OverlayFile, "overlay", "", "")
}
7 changes: 5 additions & 2 deletions src/cmd/go/internal/work/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,11 @@ func AddBuildFlags(cmd *base.Command, mask BuildFlagMask) {
}
if mask&OmitModCommonFlags == 0 {
base.AddModCommonFlags(&cmd.Flag)
} else {
// Add the overlay flag even when we don't add the rest of the mod common flags.
// This only affects 'go get' in GOPATH mode, but add the flag anyway for
// consistency.
cmd.Flag.StringVar(&fsys.OverlayFile, "overlay", "", "")
}
cmd.Flag.StringVar(&cfg.BuildContext.InstallSuffix, "installsuffix", "", "")
cmd.Flag.Var(&load.BuildLdflags, "ldflags", "")
Expand All @@ -279,8 +284,6 @@ func AddBuildFlags(cmd *base.Command, mask BuildFlagMask) {
cmd.Flag.BoolVar(&cfg.BuildTrimpath, "trimpath", false, "")
cmd.Flag.BoolVar(&cfg.BuildWork, "work", false, "")

cmd.Flag.StringVar(&fsys.OverlayFile, "overlay", "", "")

// Undocumented, unstable debugging flags.
cmd.Flag.StringVar(&cfg.DebugActiongraph, "debug-actiongraph", "", "")
cmd.Flag.StringVar(&cfg.DebugTrace, "debug-trace", "", "")
Expand Down
6 changes: 6 additions & 0 deletions src/cmd/go/testdata/script/mod_overlay.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,20 @@ cmp $WORK/overlay/get_doesnt_add_dep_go_mod $WORK/want_go_mod
cd $WORK/gopath/src/overlay-sum-used
! go get -d .
stderr 'SECURITY ERROR'
! go mod verify
stderr 'SECURITY ERROR'
go get -d -overlay overlay.json .
go mod verify -overlay overlay.json
# Overlaid go.sum is not rewritten.
# Copy an incomplete file to the overlay file, and expect an error
# attempting to update the file
cp incomplete-sum-file $WORK/overlay/overlay-sum-used-correct-sums
! go get -d -overlay overlay.json .
stderr 'overlaid files can''t be opened for write'
cmp incomplete-sum-file $WORK/overlay/overlay-sum-used-correct-sums
! go mod tidy -overlay overlay.json
stderr 'overlaid files can''t be opened for write'
cmp incomplete-sum-file $WORK/overlay/overlay-sum-used-correct-sums

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

0 comments on commit 78e59bb

Please sign in to comment.