Skip to content

Commit b1c39aa

Browse files
committed
gopls/internal/cache: use a named bool type for allowNetwork
As suggested on CL 626715, using a named bool clarifies call sites. Change-Id: Ie36f406dcca382c7edc277895826265ae9040ee2 Reviewed-on: https://go-review.googlesource.com/c/tools/+/628235 LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Alan Donovan <[email protected]>
1 parent c043599 commit b1c39aa

File tree

8 files changed

+27
-20
lines changed

8 files changed

+27
-20
lines changed

gopls/internal/cache/load.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ var errNoPackages = errors.New("no packages returned")
4343
// errors associated with specific modules.
4444
//
4545
// If scopes contains a file scope there must be exactly one scope.
46-
func (s *Snapshot) load(ctx context.Context, allowNetwork bool, scopes ...loadScope) (err error) {
46+
func (s *Snapshot) load(ctx context.Context, allowNetwork AllowNetwork, scopes ...loadScope) (err error) {
4747
if ctx.Err() != nil {
4848
// Check context cancellation before incrementing id below: a load on a
4949
// cancelled context should be a no-op.
@@ -363,7 +363,7 @@ func (m *moduleErrorMap) Error() string {
363363
// multiple modules in one config, so buildOverlay needs to filter overlays by
364364
// module.
365365
// TODO(rfindley): ^^ is this still true?
366-
func (s *Snapshot) config(ctx context.Context, allowNetwork bool) *packages.Config {
366+
func (s *Snapshot) config(ctx context.Context, allowNetwork AllowNetwork) *packages.Config {
367367
cfg := &packages.Config{
368368
Context: ctx,
369369
Dir: s.view.root.Path(),

gopls/internal/cache/mod.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ func modWhyImpl(ctx context.Context, snapshot *Snapshot, fh file.Handle) (map[st
250250
for _, req := range pm.File.Require {
251251
args = append(args, req.Mod.Path)
252252
}
253-
inv, cleanupInvocation, err := snapshot.GoCommandInvocation(false, fh.URI().DirPath(), "mod", args)
253+
inv, cleanupInvocation, err := snapshot.GoCommandInvocation(NoNetwork, fh.URI().DirPath(), "mod", args)
254254
if err != nil {
255255
return nil, err
256256
}

gopls/internal/cache/mod_tidy.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ func modTidyImpl(ctx context.Context, snapshot *Snapshot, pm *ParsedModule) (*Ti
108108
defer cleanup()
109109

110110
args := []string{"tidy", "-modfile=" + filepath.Join(tempDir, "go.mod")}
111-
inv, cleanupInvocation, err := snapshot.GoCommandInvocation(false, pm.URI.DirPath(), "mod", args, "GOWORK=off")
111+
inv, cleanupInvocation, err := snapshot.GoCommandInvocation(NoNetwork, pm.URI.DirPath(), "mod", args, "GOWORK=off")
112112
if err != nil {
113113
return nil, err
114114
}

gopls/internal/cache/snapshot.go

+14-7
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,7 @@ func (s *Snapshot) RunGoModUpdateCommands(ctx context.Context, modURI protocol.D
375375
// TODO(rfindley): we must use ModFlag and ModFile here (rather than simply
376376
// setting Args), because without knowing the verb, we can't know whether
377377
// ModFlag is appropriate. Refactor so that args can be set by the caller.
378-
inv, cleanupInvocation, err := s.GoCommandInvocation(true, modURI.DirPath(), "", nil, "GOWORK=off")
378+
inv, cleanupInvocation, err := s.GoCommandInvocation(NetworkOK, modURI.DirPath(), "", nil, "GOWORK=off")
379379
if err != nil {
380380
return nil, nil, err
381381
}
@@ -449,6 +449,15 @@ func TempModDir(ctx context.Context, fs file.Source, modURI protocol.DocumentURI
449449
return dir, cleanup, nil
450450
}
451451

452+
// AllowNetwork determines whether Go commands are permitted to use the
453+
// network. (Controlled via GOPROXY=off.)
454+
type AllowNetwork bool
455+
456+
const (
457+
NoNetwork AllowNetwork = false
458+
NetworkOK AllowNetwork = true
459+
)
460+
452461
// GoCommandInvocation populates inv with configuration for running go commands
453462
// on the snapshot.
454463
//
@@ -459,10 +468,8 @@ func TempModDir(ctx context.Context, fs file.Source, modURI protocol.DocumentURI
459468
// additional refactoring is still required: the responsibility for Env and
460469
// BuildFlags should be more clearly expressed in the API.
461470
//
462-
// If allowNetwork is set, do not set GOPROXY=off.
463-
//
464-
// TODO(rfindley): use a named boolean for allowNetwork.
465-
func (s *Snapshot) GoCommandInvocation(allowNetwork bool, dir, verb string, args []string, env ...string) (_ *gocommand.Invocation, cleanup func(), _ error) {
471+
// If allowNetwork is NoNetwork, set GOPROXY=off.
472+
func (s *Snapshot) GoCommandInvocation(allowNetwork AllowNetwork, dir, verb string, args []string, env ...string) (_ *gocommand.Invocation, cleanup func(), _ error) {
466473
inv := &gocommand.Invocation{
467474
Verb: verb,
468475
Args: args,
@@ -687,7 +694,7 @@ func (s *Snapshot) MetadataForFile(ctx context.Context, uri protocol.DocumentURI
687694
// - ...but uri is not unloadable
688695
if (shouldLoad || len(ids) == 0) && !unloadable {
689696
scope := fileLoadScope(uri)
690-
err := s.load(ctx, false, scope)
697+
err := s.load(ctx, NoNetwork, scope)
691698

692699
//
693700
// Return the context error here as the current operation is no longer
@@ -1254,7 +1261,7 @@ func (s *Snapshot) reloadWorkspace(ctx context.Context) {
12541261
scopes = []loadScope{viewLoadScope{}}
12551262
}
12561263

1257-
err := s.load(ctx, false, scopes...)
1264+
err := s.load(ctx, NoNetwork, scopes...)
12581265

12591266
// Unless the context was canceled, set "shouldLoad" to false for all
12601267
// of the metadata we attempted to load.

gopls/internal/cache/view.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -688,7 +688,7 @@ func (s *Snapshot) initialize(ctx context.Context, firstAttempt bool) {
688688
if len(scopes) > 0 {
689689
scopes = append(scopes, packageLoadScope("builtin"))
690690
}
691-
loadErr := s.load(ctx, true, scopes...)
691+
loadErr := s.load(ctx, NetworkOK, scopes...)
692692

693693
// A failure is retryable if it may have been due to context cancellation,
694694
// and this is not the initial workspace load (firstAttempt==true).

gopls/internal/golang/assembly.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import (
3030
// - cross-link jumps and block labels, like github.com/aclements/objbrowse.
3131
func AssemblyHTML(ctx context.Context, snapshot *cache.Snapshot, pkg *cache.Package, symbol string, web Web) ([]byte, error) {
3232
// Compile the package with -S, and capture its stderr stream.
33-
inv, cleanupInvocation, err := snapshot.GoCommandInvocation(false, pkg.Metadata().CompiledGoFiles[0].DirPath(), "build", []string{"-gcflags=-S", "."})
33+
inv, cleanupInvocation, err := snapshot.GoCommandInvocation(cache.NoNetwork, pkg.Metadata().CompiledGoFiles[0].DirPath(), "build", []string{"-gcflags=-S", "."})
3434
if err != nil {
3535
return nil, err // e.g. failed to write overlays (rare)
3636
}

gopls/internal/golang/gc_annotations.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ func GCOptimizationDetails(ctx context.Context, snapshot *cache.Snapshot, mp *me
5656
if !strings.HasPrefix(outDir, "/") {
5757
outDirURI = protocol.DocumentURI(strings.Replace(string(outDirURI), "file:///", "file://", 1))
5858
}
59-
inv, cleanupInvocation, err := snapshot.GoCommandInvocation(false, pkgDir, "build", []string{
59+
inv, cleanupInvocation, err := snapshot.GoCommandInvocation(cache.NoNetwork, pkgDir, "build", []string{
6060
fmt.Sprintf("-gcflags=-json=0,%s", outDirURI),
6161
fmt.Sprintf("-o=%s", tmpFile.Name()),
6262
".",

gopls/internal/server/command.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -589,7 +589,7 @@ func (c *commandHandler) Vendor(ctx context.Context, args command.URIArg) error
589589
// modules.txt in-place. In that case we could theoretically allow this
590590
// command to run concurrently.
591591
stderr := new(bytes.Buffer)
592-
inv, cleanupInvocation, err := deps.snapshot.GoCommandInvocation(true, args.URI.DirPath(), "mod", []string{"vendor"})
592+
inv, cleanupInvocation, err := deps.snapshot.GoCommandInvocation(cache.NetworkOK, args.URI.DirPath(), "mod", []string{"vendor"})
593593
if err != nil {
594594
return err
595595
}
@@ -751,7 +751,7 @@ func (c *commandHandler) runTests(ctx context.Context, snapshot *cache.Snapshot,
751751
var failedTests int
752752
for _, funcName := range tests {
753753
args := []string{pkgPath, "-v", "-count=1", fmt.Sprintf("-run=^%s$", regexp.QuoteMeta(funcName))}
754-
inv, cleanupInvocation, err := snapshot.GoCommandInvocation(false, uri.DirPath(), "test", args)
754+
inv, cleanupInvocation, err := snapshot.GoCommandInvocation(cache.NoNetwork, uri.DirPath(), "test", args)
755755
if err != nil {
756756
return err
757757
}
@@ -767,7 +767,7 @@ func (c *commandHandler) runTests(ctx context.Context, snapshot *cache.Snapshot,
767767
// Run `go test -run=^$ -bench Func` on each test.
768768
var failedBenchmarks int
769769
for _, funcName := range benchmarks {
770-
inv, cleanupInvocation, err := snapshot.GoCommandInvocation(false, uri.DirPath(), "test", []string{
770+
inv, cleanupInvocation, err := snapshot.GoCommandInvocation(cache.NoNetwork, uri.DirPath(), "test", []string{
771771
pkgPath, "-v", "-run=^$", fmt.Sprintf("-bench=^%s$", regexp.QuoteMeta(funcName)),
772772
})
773773
if err != nil {
@@ -828,7 +828,7 @@ func (c *commandHandler) Generate(ctx context.Context, args command.GenerateArgs
828828
if args.Recursive {
829829
pattern = "./..."
830830
}
831-
inv, cleanupInvocation, err := deps.snapshot.GoCommandInvocation(true, args.Dir.Path(), "generate", []string{"-x", pattern})
831+
inv, cleanupInvocation, err := deps.snapshot.GoCommandInvocation(cache.NetworkOK, args.Dir.Path(), "generate", []string{"-x", pattern})
832832
if err != nil {
833833
return err
834834
}
@@ -857,7 +857,7 @@ func (c *commandHandler) GoGetPackage(ctx context.Context, args command.GoGetPac
857857
}
858858
defer cleanupModDir()
859859

860-
inv, cleanupInvocation, err := snapshot.GoCommandInvocation(true, modURI.DirPath(), "list",
860+
inv, cleanupInvocation, err := snapshot.GoCommandInvocation(cache.NetworkOK, modURI.DirPath(), "list",
861861
[]string{"-f", "{{.Module.Path}}@{{.Module.Version}}", "-mod=mod", "-modfile=" + filepath.Join(tempDir, "go.mod"), args.Pkg},
862862
"GOWORK=off",
863863
)
@@ -991,7 +991,7 @@ func addModuleRequire(invoke func(...string) (*bytes.Buffer, error), args []stri
991991
// TODO(rfindley): inline.
992992
func (s *server) getUpgrades(ctx context.Context, snapshot *cache.Snapshot, uri protocol.DocumentURI, modules []string) (map[string]string, error) {
993993
args := append([]string{"-mod=readonly", "-m", "-u", "-json"}, modules...)
994-
inv, cleanup, err := snapshot.GoCommandInvocation(true, uri.DirPath(), "list", args)
994+
inv, cleanup, err := snapshot.GoCommandInvocation(cache.NetworkOK, uri.DirPath(), "list", args)
995995
if err != nil {
996996
return nil, err
997997
}

0 commit comments

Comments
 (0)