Skip to content

Commit 99266e0

Browse files
committed
cmd/go/internal/vgo: add -getmode=local, -getmode=vendor build flag
The new build flag -getmode=mode changes the way that vgo resolves references to modules outside the target module. The default (no -getmode flag) is to download and cache as usual. With -getmode=local, the local cache is still consulted for modules, but no network lookups are permitted. With -getmode==vendor, only the vendor directory is used, bypassing both the network and the local cache. In this mode most of the interesting vgo behavior is inaccessible: there is no world beyond the current module and its vendor directory. I'm not thrilled with the name -getmode but don't have a better name yet. It will probably change before the release - this CL is about implementing the behavior. Fixes golang/go#25073. Change-Id: Ic5273f9f2d0263b49540401c4f554190702df099 Reviewed-on: https://go-review.googlesource.com/118316 Reviewed-by: Bryan C. Mills <[email protected]>
1 parent 48532fc commit 99266e0

File tree

8 files changed

+81
-2
lines changed

8 files changed

+81
-2
lines changed

vendor/cmd/go/internal/cfg/cfg.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ var (
2222
BuildA bool // -a flag
2323
BuildBuildmode string // -buildmode flag
2424
BuildContext = build.Default
25+
BuildGetmode string // -getmode flag
2526
BuildI bool // -i flag
2627
BuildLinkshared bool // -linkshared flag
2728
BuildMSan bool // -msan flag

vendor/cmd/go/internal/modfetch/repo.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@ package modfetch
66

77
import (
88
"errors"
9+
"fmt"
910
pathpkg "path"
1011
"sort"
1112
"strings"
1213
"time"
1314

15+
"cmd/go/internal/cfg"
1416
"cmd/go/internal/modfetch/bitbucket"
1517
"cmd/go/internal/modfetch/codehost"
1618
"cmd/go/internal/modfetch/github"
@@ -60,6 +62,9 @@ type RevInfo struct {
6062

6163
// Lookup returns the module with the given module path.
6264
func Lookup(path string) (Repo, error) {
65+
if cfg.BuildGetmode != "" {
66+
return nil, fmt.Errorf("module lookup disabled by -getmode=%s", cfg.BuildGetmode)
67+
}
6368
if proxyURL != "" {
6469
return lookupProxy(path)
6570
}

vendor/cmd/go/internal/vgo/help.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright 2018 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package vgo
6+
7+
import "cmd/go/internal/base"
8+
9+
var HelpModule = &base.Command{
10+
UsageLine: "module",
11+
Short: "using versioned modules",
12+
Long: `
13+
TODO: write whole thing
14+
15+
TODO: mention -getmode=vendor, -getmode=local
16+
`,
17+
}

vendor/cmd/go/internal/vgo/load.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,13 @@ func (ld *loader) importDir(path string) string {
260260
}
261261
}
262262

263+
if cfg.BuildGetmode == "vendor" {
264+
// Using -getmode=vendor, everything the module needs
265+
// (beyond the current module and standard library)
266+
// must be in the module's vendor directory.
267+
return filepath.Join(ModRoot, "vendor", path)
268+
}
269+
263270
var mod1 module.Version
264271
var dir1 string
265272
for _, mod := range buildList {

vendor/cmd/go/internal/work/build.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ and test commands:
8888
arguments to pass on each gccgo compiler/linker invocation.
8989
-gcflags '[pattern=]arg list'
9090
arguments to pass on each go tool compile invocation.
91+
-getmode mode
92+
module download mode to use. See 'go help module' for more.
9193
-installsuffix suffix
9294
a suffix to use in the name of the package installation directory,
9395
in order to keep output separate from default builds.
@@ -220,6 +222,7 @@ func AddBuildFlags(cmd *base.Command) {
220222
cmd.Flag.StringVar(&cfg.BuildBuildmode, "buildmode", "default", "")
221223
cmd.Flag.Var(&load.BuildGcflags, "gcflags", "")
222224
cmd.Flag.Var(&load.BuildGccgoflags, "gccgoflags", "")
225+
cmd.Flag.StringVar(&cfg.BuildGetmode, "getmode", "", "")
223226
cmd.Flag.StringVar(&cfg.BuildContext.InstallSuffix, "installsuffix", "", "")
224227
cmd.Flag.Var(&load.BuildLdflags, "ldflags", "")
225228
cmd.Flag.BoolVar(&cfg.BuildLinkshared, "linkshared", false, "")

vendor/cmd/go/internal/work/init.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ package work
99
import (
1010
"cmd/go/internal/base"
1111
"cmd/go/internal/cfg"
12+
"cmd/go/internal/vgo"
1213
"flag"
1314
"fmt"
1415
"os"
@@ -224,4 +225,16 @@ func buildModeInit() {
224225
cfg.BuildContext.InstallSuffix += codegenArg[1:]
225226
}
226227
}
228+
229+
switch cfg.BuildGetmode {
230+
case "":
231+
// ok
232+
case "local", "vendor":
233+
// ok but check for vgo
234+
if !vgo.Enabled() {
235+
base.Fatalf("build flag -getmode=%s only valid when using modules", cfg.BuildGetmode)
236+
}
237+
default:
238+
base.Fatalf("-getmode=%s not supported (can be '', 'local', or 'vendor')", cfg.BuildGetmode)
239+
}
227240
}

vendor/cmd/go/main.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ func init() {
6464
help.HelpFileType,
6565
help.HelpGopath,
6666
help.HelpImportPath,
67+
vgo.HelpModule,
6768
help.HelpPackages,
6869
test.HelpTestflag,
6970
test.HelpTestfunc,
@@ -123,8 +124,9 @@ func Main() {
123124
os.Exit(2)
124125
}
125126

127+
vgo.Init()
126128
if !vgo.MustBeVgo {
127-
if vgo.Init(); vgo.Enabled() {
129+
if vgo.Enabled() {
128130
// Didn't do this above, so do it now.
129131
*get.CmdGet = *vgo.CmdGet
130132
}

vendor/cmd/go/vgo_test.go

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,11 +227,22 @@ func TestVgoVendor(t *testing.T) {
227227

228228
wd, _ := os.Getwd()
229229
tg.cd(filepath.Join(wd, "testdata/vendormod"))
230+
defer tg.must(os.RemoveAll(filepath.Join(wd, "testdata/vendormod/vendor")))
231+
230232
tg.run("-vgo", "list", "-m")
231233
tg.grepStdout(`^x`, "expected to see module x")
232234
tg.grepStdout(`=> ./x`, "expected to see replacement for module x")
233235
tg.grepStdout(`^w`, "expected to see module w")
234236

237+
tg.must(os.RemoveAll(filepath.Join(wd, "testdata/vendormod/vendor")))
238+
if !testing.Short() {
239+
tg.run("-vgo", "build")
240+
tg.runFail("-vgo", "build", "-getmode=vendor")
241+
}
242+
243+
tg.run("-vgo", "list", "-f={{.Dir}}", "x")
244+
tg.grepStdout(`vendormod[/\\]x$`, "expected x in vendormod/x")
245+
235246
tg.run("-vgo", "vendor", "-v")
236247
tg.grepStderr(`^# x v1.0.0 => ./x`, "expected to see module x with replacement")
237248
tg.grepStderr(`^x`, "expected to see package x")
@@ -241,7 +252,27 @@ func TestVgoVendor(t *testing.T) {
241252
tg.grepStderr(`^z`, "expected to see package z")
242253
tg.grepStderrNot(`w`, "expected NOT to see unused module w")
243254

244-
tg.must(os.RemoveAll(filepath.Join(wd, "testdata/vendormod/vendor")))
255+
tg.run("-vgo", "list", "-f={{.Dir}}", "x")
256+
tg.grepStdout(`vendormod[/\\]x$`, "expected x in vendormod/x")
257+
258+
tg.run("-vgo", "list", "-getmode=vendor", "-f={{.Dir}}", "x")
259+
tg.grepStdout(`vendormod[/\\]vendor[/\\]x$`, "expected x in vendormod/vendor/x in -get=vendor mode")
260+
261+
tg.run("-vgo", "list", "-f={{.Dir}}", "w")
262+
tg.grepStdout(`vendormod[/\\]w$`, "expected w in vendormod/w")
263+
tg.runFail("-vgo", "list", "-getmode=vendor", "-f={{.Dir}}", "w")
264+
tg.grepStderr(`vendormod[/\\]vendor[/\\]w`, "want error about vendormod/vendor/w not existing")
265+
266+
tg.run("-vgo", "list", "-getmode=local", "-f={{.Dir}}", "w")
267+
tg.grepStdout(`vendormod[/\\]w`, "expected w in vendormod/w")
268+
269+
tg.runFail("-vgo", "list", "-getmode=local", "-f={{.Dir}}", "newpkg")
270+
tg.grepStderr(`module lookup disabled by -getmode=local`, "expected -getmode=local to avoid network")
271+
272+
if !testing.Short() {
273+
tg.run("-vgo", "build")
274+
tg.run("-vgo", "build", "-getmode=vendor")
275+
}
245276
}
246277

247278
func TestFillGoMod(t *testing.T) {

0 commit comments

Comments
 (0)