Skip to content

Commit 363fb4b

Browse files
author
Bryan C. Mills
committed
cmd/go/internal/modload: consolidate buildList and associated functions into one file
Change-Id: I310c37c7f0ce5581f07cf6e27d1f6361d03b92ef Reviewed-on: https://go-review.googlesource.com/c/go/+/244077 Run-TryBot: Bryan C. Mills <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Michael Matloob <[email protected]> Reviewed-by: Jay Conrod <[email protected]>
1 parent d27ebc7 commit 363fb4b

File tree

3 files changed

+121
-103
lines changed

3 files changed

+121
-103
lines changed

src/cmd/go/internal/modget/get.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -628,6 +628,10 @@ func runGet(ctx context.Context, cmd *base.Command, args []string) {
628628
if err != nil {
629629
base.Fatalf("go: %v", err)
630630
}
631+
632+
// TODO(bcmills) What should happen here under lazy loading?
633+
// Downgrading may intentionally violate the lazy-loading invariants.
634+
631635
modload.SetBuildList(buildList)
632636
modload.ReloadBuildList() // note: does not update go.mod
633637
base.ExitIfErrors()
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
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 modload
6+
7+
import (
8+
"cmd/go/internal/base"
9+
"cmd/go/internal/cfg"
10+
"cmd/go/internal/imports"
11+
"cmd/go/internal/mvs"
12+
"context"
13+
"fmt"
14+
"os"
15+
16+
"golang.org/x/mod/module"
17+
)
18+
19+
// buildList is the list of modules to use for building packages.
20+
// It is initialized by calling ImportPaths, ImportFromFiles,
21+
// LoadALL, or LoadBuildList, each of which uses loaded.load.
22+
//
23+
// Ideally, exactly ONE of those functions would be called,
24+
// and exactly once. Most of the time, that's true.
25+
// During "go get" it may not be. TODO(rsc): Figure out if
26+
// that restriction can be established, or else document why not.
27+
//
28+
var buildList []module.Version
29+
30+
// LoadBuildList loads and returns the build list from go.mod.
31+
// The loading of the build list happens automatically in ImportPaths:
32+
// LoadBuildList need only be called if ImportPaths is not
33+
// (typically in commands that care about the module but
34+
// no particular package).
35+
func LoadBuildList(ctx context.Context) []module.Version {
36+
InitMod(ctx)
37+
ReloadBuildList()
38+
WriteGoMod()
39+
return buildList
40+
}
41+
42+
// ReloadBuildList resets the state of loaded packages, then loads and returns
43+
// the build list set in SetBuildList.
44+
func ReloadBuildList() []module.Version {
45+
loaded = loadFromRoots(loaderParams{
46+
tags: imports.Tags(),
47+
listRoots: func() []string { return nil },
48+
allClosesOverTests: index.allPatternClosesOverTests(), // but doesn't matter because the root list is empty.
49+
})
50+
return buildList
51+
}
52+
53+
// BuildList returns the module build list,
54+
// typically constructed by a previous call to
55+
// LoadBuildList or ImportPaths.
56+
// The caller must not modify the returned list.
57+
func BuildList() []module.Version {
58+
return buildList
59+
}
60+
61+
// SetBuildList sets the module build list.
62+
// The caller is responsible for ensuring that the list is valid.
63+
// SetBuildList does not retain a reference to the original list.
64+
func SetBuildList(list []module.Version) {
65+
buildList = append([]module.Version{}, list...)
66+
}
67+
68+
// TidyBuildList trims the build list to the minimal requirements needed to
69+
// retain the same versions of all packages from the preceding Load* or
70+
// ImportPaths* call.
71+
func TidyBuildList() {
72+
used := map[module.Version]bool{Target: true}
73+
for _, pkg := range loaded.pkgs {
74+
used[pkg.mod] = true
75+
}
76+
77+
keep := []module.Version{Target}
78+
var direct []string
79+
for _, m := range buildList[1:] {
80+
if used[m] {
81+
keep = append(keep, m)
82+
if loaded.direct[m.Path] {
83+
direct = append(direct, m.Path)
84+
}
85+
} else if cfg.BuildV {
86+
if _, ok := index.require[m]; ok {
87+
fmt.Fprintf(os.Stderr, "unused %s\n", m.Path)
88+
}
89+
}
90+
}
91+
92+
min, err := mvs.Req(Target, direct, &mvsReqs{buildList: keep})
93+
if err != nil {
94+
base.Fatalf("go: %v", err)
95+
}
96+
buildList = append([]module.Version{Target}, min...)
97+
}
98+
99+
// checkMultiplePaths verifies that a given module path is used as itself
100+
// or as a replacement for another module, but not both at the same time.
101+
//
102+
// (See https://golang.org/issue/26607 and https://golang.org/issue/34650.)
103+
func checkMultiplePaths() {
104+
firstPath := make(map[module.Version]string, len(buildList))
105+
for _, mod := range buildList {
106+
src := mod
107+
if rep := Replacement(mod); rep.Path != "" {
108+
src = rep
109+
}
110+
if prev, ok := firstPath[src]; !ok {
111+
firstPath[src] = mod.Path
112+
} else if prev != mod.Path {
113+
base.Errorf("go: %s@%s used for two different module paths (%s and %s)", src.Path, src.Version, prev, mod.Path)
114+
}
115+
}
116+
base.ExitIfErrors()
117+
}

src/cmd/go/internal/modload/load.go

Lines changed: 0 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -118,22 +118,10 @@ import (
118118
"cmd/go/internal/par"
119119
"cmd/go/internal/search"
120120
"cmd/go/internal/str"
121-
"cmd/go/internal/trace"
122121

123122
"golang.org/x/mod/module"
124123
)
125124

126-
// buildList is the list of modules to use for building packages.
127-
// It is initialized by calling ImportPaths, ImportFromFiles,
128-
// LoadALL, or LoadBuildList, each of which uses loaded.load.
129-
//
130-
// Ideally, exactly ONE of those functions would be called,
131-
// and exactly once. Most of the time, that's true.
132-
// During "go get" it may not be. TODO(rsc): Figure out if
133-
// that restriction can be established, or else document why not.
134-
//
135-
var buildList []module.Version
136-
137125
// loaded is the most recently-used package loader.
138126
// It holds details about individual packages.
139127
var loaded *loader
@@ -250,26 +238,6 @@ func ImportPathsQuiet(ctx context.Context, patterns []string, tags map[string]bo
250238
return matches
251239
}
252240

253-
// checkMultiplePaths verifies that a given module path is used as itself
254-
// or as a replacement for another module, but not both at the same time.
255-
//
256-
// (See https://golang.org/issue/26607 and https://golang.org/issue/34650.)
257-
func checkMultiplePaths() {
258-
firstPath := make(map[module.Version]string, len(buildList))
259-
for _, mod := range buildList {
260-
src := mod
261-
if rep := Replacement(mod); rep.Path != "" {
262-
src = rep
263-
}
264-
if prev, ok := firstPath[src]; !ok {
265-
firstPath[src] = mod.Path
266-
} else if prev != mod.Path {
267-
base.Errorf("go: %s@%s used for two different module paths (%s and %s)", src.Path, src.Version, prev, mod.Path)
268-
}
269-
}
270-
base.ExitIfErrors()
271-
}
272-
273241
// matchLocalDirs is like m.MatchDirs, but tries to avoid scanning directories
274242
// outside of the standard library and active modules.
275243
func matchLocalDirs(m *search.Match) {
@@ -481,31 +449,6 @@ func DirImportPath(dir string) string {
481449
return "."
482450
}
483451

484-
// LoadBuildList loads and returns the build list from go.mod.
485-
// The loading of the build list happens automatically in ImportPaths:
486-
// LoadBuildList need only be called if ImportPaths is not
487-
// (typically in commands that care about the module but
488-
// no particular package).
489-
func LoadBuildList(ctx context.Context) []module.Version {
490-
ctx, span := trace.StartSpan(ctx, "LoadBuildList")
491-
defer span.Done()
492-
InitMod(ctx)
493-
ReloadBuildList()
494-
WriteGoMod()
495-
return buildList
496-
}
497-
498-
// ReloadBuildList resets the state of loaded packages, then loads and returns
499-
// the build list set in SetBuildList.
500-
func ReloadBuildList() []module.Version {
501-
loaded = loadFromRoots(loaderParams{
502-
tags: imports.Tags(),
503-
listRoots: func() []string { return nil },
504-
allClosesOverTests: index.allPatternClosesOverTests(), // but doesn't matter because the root list is empty.
505-
})
506-
return buildList
507-
}
508-
509452
// LoadALL returns the set of all packages in the current module
510453
// and their dependencies in any other modules, without filtering
511454
// due to build tags, except "+build ignore".
@@ -571,52 +514,6 @@ func TargetPackages(ctx context.Context, pattern string) *search.Match {
571514
return m
572515
}
573516

574-
// BuildList returns the module build list,
575-
// typically constructed by a previous call to
576-
// LoadBuildList or ImportPaths.
577-
// The caller must not modify the returned list.
578-
func BuildList() []module.Version {
579-
return buildList
580-
}
581-
582-
// SetBuildList sets the module build list.
583-
// The caller is responsible for ensuring that the list is valid.
584-
// SetBuildList does not retain a reference to the original list.
585-
func SetBuildList(list []module.Version) {
586-
buildList = append([]module.Version{}, list...)
587-
}
588-
589-
// TidyBuildList trims the build list to the minimal requirements needed to
590-
// retain the same versions of all packages from the preceding Load* or
591-
// ImportPaths* call.
592-
func TidyBuildList() {
593-
used := map[module.Version]bool{Target: true}
594-
for _, pkg := range loaded.pkgs {
595-
used[pkg.mod] = true
596-
}
597-
598-
keep := []module.Version{Target}
599-
var direct []string
600-
for _, m := range buildList[1:] {
601-
if used[m] {
602-
keep = append(keep, m)
603-
if loaded.direct[m.Path] {
604-
direct = append(direct, m.Path)
605-
}
606-
} else if cfg.BuildV {
607-
if _, ok := index.require[m]; ok {
608-
fmt.Fprintf(os.Stderr, "unused %s\n", m.Path)
609-
}
610-
}
611-
}
612-
613-
min, err := mvs.Req(Target, direct, &mvsReqs{buildList: keep})
614-
if err != nil {
615-
base.Fatalf("go: %v", err)
616-
}
617-
buildList = append([]module.Version{Target}, min...)
618-
}
619-
620517
// ImportMap returns the actual package import path
621518
// for an import path found in source code.
622519
// If the given import path does not appear in the source code

0 commit comments

Comments
 (0)