aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/go/internal/modload/buildlist.go
blob: 45f220a6ee6946b510a6bb67cb06b21971d31576 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
// Copyright 2018 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package modload

import (
	"cmd/go/internal/base"
	"cmd/go/internal/cfg"
	"cmd/go/internal/imports"
	"cmd/go/internal/mvs"
	"context"
	"fmt"
	"os"
	"strings"

	"golang.org/x/mod/module"
)

// buildList is the list of modules to use for building packages.
// It is initialized by calling LoadPackages or ImportFromFiles,
// each of which uses loaded.load.
//
// Ideally, exactly ONE of those functions would be called,
// and exactly once. Most of the time, that's true.
// During "go get" it may not be. TODO(rsc): Figure out if
// that restriction can be established, or else document why not.
//
var buildList []module.Version

// additionalExplicitRequirements is a list of modules paths for which
// WriteGoMod should record explicit requirements, even if they would be
// selected without those requirements. Each path must also appear in buildList.
var additionalExplicitRequirements []string

// capVersionSlice returns s with its cap reduced to its length.
func capVersionSlice(s []module.Version) []module.Version {
	return s[:len(s):len(s)]
}

// LoadAllModules loads and returns the list of modules matching the "all"
// module pattern, starting with the Target module and in a deterministic
// (stable) order, without loading any packages.
//
// Modules are loaded automatically (and lazily) in LoadPackages:
// LoadAllModules need only be called if LoadPackages is not,
// typically in commands that care about modules but no particular package.
//
// The caller must not modify the returned list, but may append to it.
func LoadAllModules(ctx context.Context) []module.Version {
	LoadModFile(ctx)
	ReloadBuildList()
	WriteGoMod()
	return capVersionSlice(buildList)
}

// Selected returns the selected version of the module with the given path, or
// the empty string if the given module has no selected version
// (either because it is not required or because it is the Target module).
func Selected(path string) (version string) {
	if path == Target.Path {
		return ""
	}
	for _, m := range buildList {
		if m.Path == path {
			return m.Version
		}
	}
	return ""
}

// EditBuildList edits the global build list by first adding every module in add
// to the existing build list, then adjusting versions (and adding or removing
// requirements as needed) until every module in mustSelect is selected at the
// given version.
//
// (Note that the newly-added modules might not be selected in the resulting
// build list: they could be lower than existing requirements or conflict with
// versions in mustSelect.)
//
// If the versions listed in mustSelect are mutually incompatible (due to one of
// the listed modules requiring a higher version of another), EditBuildList
// returns a *ConstraintError and leaves the build list in its previous state.
func EditBuildList(ctx context.Context, add, mustSelect []module.Version) error {
	var upgraded = capVersionSlice(buildList)
	if len(add) > 0 {
		// First, upgrade the build list with any additions.
		// In theory we could just append the additions to the build list and let
		// mvs.Downgrade take care of resolving the upgrades too, but the
		// diagnostics from Upgrade are currently much better in case of errors.
		var err error
		upgraded, err = mvs.Upgrade(Target, &mvsReqs{buildList: upgraded}, add...)
		if err != nil {
			return err
		}
	}

	downgraded, err := mvs.Downgrade(Target, &mvsReqs{buildList: append(upgraded, mustSelect...)}, mustSelect...)
	if err != nil {
		return err
	}

	final, err := mvs.Upgrade(Target, &mvsReqs{buildList: downgraded}, mustSelect...)
	if err != nil {
		return err
	}

	selected := make(map[string]module.Version, len(final))
	for _, m := range final {
		selected[m.Path] = m
	}
	inconsistent := false
	for _, m := range mustSelect {
		s, ok := selected[m.Path]
		if !ok {
			if m.Version != "none" {
				panic(fmt.Sprintf("internal error: mvs.BuildList lost %v", m))
			}
			continue
		}
		if s.Version != m.Version {
			inconsistent = true
			break
		}
	}

	if !inconsistent {
		buildList = final
		additionalExplicitRequirements = make([]string, 0, len(mustSelect))
		for _, m := range mustSelect {
			if m.Version != "none" {
				additionalExplicitRequirements = append(additionalExplicitRequirements, m.Path)
			}
		}
		return nil
	}

	// We overshot one or more of the modules in mustSelected, which means that
	// Downgrade removed something in mustSelect because it conflicted with
	// something else in mustSelect.
	//
	// Walk the requirement graph to find the conflict.
	//
	// TODO(bcmills): Ideally, mvs.Downgrade (or a replacement for it) would do
	// this directly.

	reqs := &mvsReqs{buildList: final}
	reason := map[module.Version]module.Version{}
	for _, m := range mustSelect {
		reason[m] = m
	}
	queue := mustSelect[:len(mustSelect):len(mustSelect)]
	for len(queue) > 0 {
		var m module.Version
		m, queue = queue[0], queue[1:]
		required, err := reqs.Required(m)
		if err != nil {
			return err
		}
		for _, r := range required {
			if _, ok := reason[r]; !ok {
				reason[r] = reason[m]
				queue = append(queue, r)
			}
		}
	}

	var conflicts []Conflict
	for _, m := range mustSelect {
		s, ok := selected[m.Path]
		if !ok {
			if m.Version != "none" {
				panic(fmt.Sprintf("internal error: mvs.BuildList lost %v", m))
			}
			continue
		}
		if s.Version != m.Version {
			conflicts = append(conflicts, Conflict{
				Source:     reason[s],
				Dep:        s,
				Constraint: m,
			})
		}
	}

	return &ConstraintError{
		Conflicts: conflicts,
	}
}

// A ConstraintError describes inconsistent constraints in EditBuildList
type ConstraintError struct {
	// Conflict lists the source of the conflict for each version in mustSelect
	// that could not be selected due to the requirements of some other version in
	// mustSelect.
	Conflicts []Conflict
}

func (e *ConstraintError) Error() string {
	b := new(strings.Builder)
	b.WriteString("version constraints conflict:")
	for _, c := range e.Conflicts {
		fmt.Fprintf(b, "\n\t%v requires %v, but %v is requested", c.Source, c.Dep, c.Constraint)
	}
	return b.String()
}

// A Conflict documents that Source requires Dep, which conflicts with Constraint.
// (That is, Dep has the same module path as Constraint but a higher version.)
type Conflict struct {
	Source     module.Version
	Dep        module.Version
	Constraint module.Version
}

// ReloadBuildList resets the state of loaded packages, then loads and returns
// the build list set by EditBuildList.
func ReloadBuildList() []module.Version {
	loaded = loadFromRoots(loaderParams{
		PackageOpts: PackageOpts{
			Tags: imports.Tags(),
		},
		listRoots:          func() []string { return nil },
		allClosesOverTests: index.allPatternClosesOverTests(), // but doesn't matter because the root list is empty.
	})
	return capVersionSlice(buildList)
}

// TidyBuildList trims the build list to the minimal requirements needed to
// retain the same versions of all packages from the preceding call to
// LoadPackages.
func TidyBuildList() {
	used := map[module.Version]bool{Target: true}
	for _, pkg := range loaded.pkgs {
		used[pkg.mod] = true
	}

	keep := []module.Version{Target}
	var direct []string
	for _, m := range buildList[1:] {
		if used[m] {
			keep = append(keep, m)
			if loaded.direct[m.Path] {
				direct = append(direct, m.Path)
			}
		} else if cfg.BuildV {
			if _, ok := index.require[m]; ok {
				fmt.Fprintf(os.Stderr, "unused %s\n", m.Path)
			}
		}
	}

	min, err := mvs.Req(Target, direct, &mvsReqs{buildList: keep})
	if err != nil {
		base.Fatalf("go: %v", err)
	}
	buildList = append([]module.Version{Target}, min...)
}

// checkMultiplePaths verifies that a given module path is used as itself
// or as a replacement for another module, but not both at the same time.
//
// (See https://golang.org/issue/26607 and https://golang.org/issue/34650.)
func checkMultiplePaths() {
	firstPath := make(map[module.Version]string, len(buildList))
	for _, mod := range buildList {
		src := mod
		if rep := Replacement(mod); rep.Path != "" {
			src = rep
		}
		if prev, ok := firstPath[src]; !ok {
			firstPath[src] = mod.Path
		} else if prev != mod.Path {
			base.Errorf("go: %s@%s used for two different module paths (%s and %s)", src.Path, src.Version, prev, mod.Path)
		}
	}
	base.ExitIfErrors()
}