Skip to content

Commit ee780d4

Browse files
author
Jay Conrod
committed
cmd/go: clarify error when package is removed in a module
If no module in the build list provides an imported package, we try to upgrade to the "@latest" version. If there is a requirement on a version of the module which is newer than the "@latest" version (e.g., a prerelease or pseudoversion), we cannot upgrade further. We previously reported "looping trying to add package" when we saw the package in "@latest" but it was removed later. The meaning of this is unclear for users, so with this change, we explain the package was removed. Fixes golang#30394 Change-Id: I1b7fec2c37e762fb600e66ee8a4df4aeaf13e67a Reviewed-on: https://go-review.googlesource.com/c/go/+/169720 Run-TryBot: Jay Conrod <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Bryan C. Mills <[email protected]>
1 parent d8f60ee commit ee780d4

6 files changed

Lines changed: 63 additions & 1 deletion

File tree

src/cmd/go/internal/modload/import.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ import (
2828
type ImportMissingError struct {
2929
ImportPath string
3030
Module module.Version
31+
32+
// newMissingVersion is set to a newer version of Module if one is present
33+
// in the build list. When set, we can't automatically upgrade.
34+
newMissingVersion string
3135
}
3236

3337
func (e *ImportMissingError) Error() string {
@@ -189,7 +193,18 @@ func Import(path string) (m module.Version, dir string, err error) {
189193
}
190194
return module.Version{}, "", &ImportMissingError{ImportPath: path}
191195
}
192-
return m, "", &ImportMissingError{ImportPath: path, Module: m}
196+
newMissingVersion := ""
197+
for _, bm := range buildList {
198+
if bm.Path == m.Path && semver.Compare(bm.Version, m.Version) > 0 {
199+
// This typically happens when a package is present at the "@latest"
200+
// version (e.g., v1.0.0) of a module, but we have a newer version
201+
// of the same module in the build list (e.g., v1.0.1-beta), and
202+
// the package is not present there.
203+
newMissingVersion = bm.Version
204+
break
205+
}
206+
}
207+
return m, "", &ImportMissingError{ImportPath: path, Module: m, newMissingVersion: newMissingVersion}
193208
}
194209

195210
// maybeInModule reports whether, syntactically,

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -547,6 +547,9 @@ func (ld *loader) load(roots func() []string) {
547547
}
548548
for _, pkg := range ld.pkgs {
549549
if err, ok := pkg.err.(*ImportMissingError); ok && err.Module.Path != "" {
550+
if err.newMissingVersion != "" {
551+
base.Fatalf("go: %s: package provided by %s at latest version %s but not at required version %s", pkg.stackText(), err.Module.Path, err.Module.Version, err.newMissingVersion)
552+
}
550553
if added[pkg.path] {
551554
base.Fatalf("go: %s: looping trying to add package", pkg.stackText())
552555
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
The deprecated package is present in this version (which is @latest) but
2+
is deleted in a newer prerelease version.
3+
4+
-- .mod --
5+
module example.com/missingpkg
6+
-- .info --
7+
{"Version":"v1.0.0"}
8+
-- lib.go --
9+
package lib
10+
-- deprecated/deprecated.go --
11+
package deprecated
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
The deprecated package is deleted in this version.
2+
3+
-- .mod --
4+
module example.com/missingpkg
5+
-- .info --
6+
{"Version":"v1.0.1-beta"}
7+
-- lib.go --
8+
package lib
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
This module requires example.com/missingpkg at a prerelease version, which
2+
is newer than @latest.
3+
4+
-- .mod --
5+
module example.com/usemissingpre
6+
7+
require example.com/missingpkg v1.0.1-beta
8+
-- .info --
9+
{"Version":"v1.0.0"}
10+
-- use.go --
11+
package use
12+
13+
import _ "example.com/missingpkg"
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
env GO111MODULE=on
2+
3+
! go list use.go
4+
stderr 'import "example.com/missingpkg/deprecated": package provided by example.com/missingpkg at latest version v1.0.0 but not at required version v1.0.1-beta'
5+
6+
-- use.go --
7+
package use
8+
9+
import (
10+
_ "example.com/missingpkg/deprecated"
11+
_ "example.com/usemissingpre"
12+
)

0 commit comments

Comments
 (0)