From 5ba1c3f290ef8f3c373c560d3608325b07f44f7a Mon Sep 17 00:00:00 2001 From: "Bryan C. Mills" Date: Tue, 17 Nov 2020 23:16:30 -0500 Subject: [PATCH] cmd/go/internal/modload: remove SetBuildList For the last remaining call site (in cmd/go/internal/work, added for the new 'go install pkg@version' codepath in CL 254365), use EditBuildList instead. SetBuildList assumes that the caller has enough information to produce a complete, coherent build list. With lazy loading, producing a complete, coherent build list is no longer quite so trivial. In CL 263267, I rewrote the main caller of SetBuildList (the 'go get' command), and in the process added a more targeted modload hook (EditBuildList). That hook also suffices for 'go install pkg@version'. The resulting error messages are perhaps not as smooth as they ought to be, but if they are too awkward we should probably fix them for 'go get' too, and the commands can continue to share the edit hook. For #36460 Updates #40276 Change-Id: I698a9dcd2efe6378a4d91f21362880aa8e50001b Reviewed-on: https://go-review.googlesource.com/c/go/+/270980 Trust: Bryan C. Mills Run-TryBot: Bryan C. Mills TryBot-Result: Go Bot Reviewed-by: Jay Conrod --- src/cmd/go/internal/modload/buildlist.go | 9 +-------- src/cmd/go/internal/modload/mvs.go | 2 +- src/cmd/go/internal/work/build.go | 10 ++++++---- src/cmd/go/testdata/script/mod_install_pkg_version.txt | 2 +- 4 files changed, 9 insertions(+), 14 deletions(-) diff --git a/src/cmd/go/internal/modload/buildlist.go b/src/cmd/go/internal/modload/buildlist.go index 4aaaa8d2065..5b9984a4924 100644 --- a/src/cmd/go/internal/modload/buildlist.go +++ b/src/cmd/go/internal/modload/buildlist.go @@ -73,13 +73,6 @@ func Selected(path string) (version string) { return "" } -// SetBuildList sets the module build list. -// The caller is responsible for ensuring that the list is valid. -// SetBuildList does not retain a reference to the original list. -func SetBuildList(list []module.Version) { - buildList = append([]module.Version{}, list...) -} - // 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 @@ -222,7 +215,7 @@ type Conflict struct { } // ReloadBuildList resets the state of loaded packages, then loads and returns -// the build list set in SetBuildList. +// the build list set by EditBuildList. func ReloadBuildList() []module.Version { loaded = loadFromRoots(loaderParams{ PackageOpts: PackageOpts{ diff --git a/src/cmd/go/internal/modload/mvs.go b/src/cmd/go/internal/modload/mvs.go index 02b13cdd053..db57b3ec5fe 100644 --- a/src/cmd/go/internal/modload/mvs.go +++ b/src/cmd/go/internal/modload/mvs.go @@ -24,7 +24,7 @@ type mvsReqs struct { } // Reqs returns the current module requirement graph. -// Future calls to SetBuildList do not affect the operation +// Future calls to EditBuildList do not affect the operation // of the returned Reqs. func Reqs() mvs.Reqs { r := &mvsReqs{ diff --git a/src/cmd/go/internal/work/build.go b/src/cmd/go/internal/work/build.go index 7e26d4e6a37..0f91a86311d 100644 --- a/src/cmd/go/internal/work/build.go +++ b/src/cmd/go/internal/work/build.go @@ -789,10 +789,12 @@ func installOutsideModule(ctx context.Context, args []string) { base.Fatalf(directiveFmt, args[0], installMod, "exclude") } - // Initialize the build list using a dummy main module that requires the - // module providing the packages on the command line. - target := module.Version{Path: "go-install-target"} - modload.SetBuildList([]module.Version{target, installMod}) + // 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 { + base.Fatalf("go install %s: %v", args[0], err) + } // Load packages for all arguments. Ignore non-main packages. // Print a warning if an argument contains "..." and matches no main packages. diff --git a/src/cmd/go/testdata/script/mod_install_pkg_version.txt b/src/cmd/go/testdata/script/mod_install_pkg_version.txt index 93318b6659d..e4a76683518 100644 --- a/src/cmd/go/testdata/script/mod_install_pkg_version.txt +++ b/src/cmd/go/testdata/script/mod_install_pkg_version.txt @@ -159,7 +159,7 @@ cmp stderr exclude-err # 'go install pkg@version' should report an error if the module requires a # higher version of itself. ! go install example.com/cmd/a@v1.0.0-newerself -stderr '^go install: example.com/cmd@v1.0.0-newerself: module requires a higher version of itself \(v1.0.0\)$' +stderr '^go install example.com/cmd/a@v1.0.0-newerself: version constraints conflict:\n\texample.com/cmd@v1.0.0-newerself requires example.com/cmd@v1.0.0, but example.com/cmd@v1.0.0-newerself is requested$' # 'go install pkg@version' will only match a retracted version if it's