cmd/go/internal/modload: make EditBuildList report whether the build list was changed

For #36460

Change-Id: I8dd6e6f998a217a4287212815ce61209df6f007f
Reviewed-on: https://go-review.googlesource.com/c/go/+/296609
Trust: Bryan C. Mills <bcmills@google.com>
Run-TryBot: Bryan C. Mills <bcmills@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Michael Matloob <matloob@golang.org>
This commit is contained in:
Bryan C. Mills 2021-02-22 17:05:32 -05:00
parent b7f0fb6d9e
commit 2ceb79db52
3 changed files with 17 additions and 13 deletions

View file

@ -30,7 +30,6 @@ import (
"fmt"
"os"
"path/filepath"
"reflect"
"runtime"
"sort"
"strings"
@ -1635,7 +1634,8 @@ func (r *resolver) updateBuildList(ctx context.Context, additions []module.Versi
}
}
if err := modload.EditBuildList(ctx, additions, resolved); err != nil {
changed, err := modload.EditBuildList(ctx, additions, resolved)
if err != nil {
var constraint *modload.ConstraintError
if !errors.As(err, &constraint) {
base.Errorf("go get: %v", err)
@ -1654,12 +1654,11 @@ func (r *resolver) updateBuildList(ctx context.Context, additions []module.Versi
}
return false
}
buildList := modload.LoadAllModules(ctx)
if reflect.DeepEqual(r.buildList, buildList) {
if !changed {
return false
}
r.buildList = buildList
r.buildList = modload.LoadAllModules(ctx)
r.buildListVersion = make(map[string]string, len(r.buildList))
for _, m := range r.buildList {
r.buildListVersion[m.Path] = m.Version

View file

@ -12,6 +12,7 @@ import (
"context"
"fmt"
"os"
"reflect"
"strings"
"golang.org/x/mod/module"
@ -81,12 +82,12 @@ func Selected(path string) (version string) {
// 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 {
func EditBuildList(ctx context.Context, add, mustSelect []module.Version) (changed bool, err error) {
LoadModFile(ctx)
final, err := editBuildList(ctx, buildList, add, mustSelect)
if err != nil {
return err
return false, err
}
selected := make(map[string]module.Version, len(final))
@ -106,14 +107,18 @@ func EditBuildList(ctx context.Context, add, mustSelect []module.Version) error
}
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
changed := false
if !reflect.DeepEqual(buildList, final) {
buildList = final
changed = true
}
return changed, nil
}
// We overshot one or more of the modules in mustSelect, which means that
@ -136,7 +141,7 @@ func EditBuildList(ctx context.Context, add, mustSelect []module.Version) error
m, queue = queue[0], queue[1:]
required, err := reqs.Required(m)
if err != nil {
return err
return false, err
}
for _, r := range required {
if _, ok := reason[r]; !ok {
@ -164,7 +169,7 @@ func EditBuildList(ctx context.Context, add, mustSelect []module.Version) error
}
}
return &ConstraintError{
return false, &ConstraintError{
Conflicts: conflicts,
}
}

View file

@ -836,7 +836,7 @@ func installOutsideModule(ctx context.Context, args []string) {
// Since we are in NoRoot mode, the build list initially contains only
// the dummy command-line-arguments module. Add a requirement on the
// module that provides the packages named on the command line.
if err := modload.EditBuildList(ctx, nil, []module.Version{installMod}); err != nil {
if _, err := modload.EditBuildList(ctx, nil, []module.Version{installMod}); err != nil {
base.Fatalf("go install %s: %v", args[0], err)
}