Skip to content

Commit

Permalink
internal/mod/modpkgload: support packages without major versions
Browse files Browse the repository at this point in the history
When there's no major version in a package import
path, try to find one from the requirements.

Signed-off-by: Roger Peppe <[email protected]>
Change-Id: Iceee9a0589d5b23748c07ef64c2815a853ae0041
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1173323
Reviewed-by: Daniel Martí <[email protected]>
Unity-Result: CUE porcuepine <[email protected]>
TryBot-Result: CUEcueckoo <[email protected]>
  • Loading branch information
rogpeppe committed Dec 8, 2023
1 parent 7afd472 commit 3cfb6e2
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 4 deletions.
12 changes: 9 additions & 3 deletions internal/mod/modpkgload/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ func (pkgs *Packages) importFromModules(ctx context.Context, pkgPath string) (m
// take it from requirements; if not present there, search for latest
// major version and use that. What should we do when there are
// several major versions present in the go.mod file?
pkgPathOnly, pkgVersion, ok := module.SplitPathVersion(pkgPath)
if !ok {
return failf("all imports must currently be qualified with a major version")
pkgPathOnly, pkgVersion, hasMajor := module.SplitPathVersion(pkgPath)
if !hasMajor {
pkgPathOnly = pkgPath
}
if filepath.IsAbs(pkgPathOnly) || path.IsAbs(pkgPathOnly) {
return failf("%q is not a package path", pkgPath)
Expand Down Expand Up @@ -82,6 +82,12 @@ func (pkgs *Packages) importFromModules(ctx context.Context, pkgPath string) (m
v string
ok bool
)
pkgVersion := pkgVersion
if !hasMajor {
if pkgVersion, _ = pkgs.requirements.DefaultMajorVersion(prefix); pkgVersion == "" {
continue
}
}
prefixPath := prefix + "@" + pkgVersion
if mg == nil {
v, ok = pkgs.requirements.RootSelected(prefixPath)
Expand Down
8 changes: 7 additions & 1 deletion internal/mod/modpkgload/pkgload_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,13 @@ func TestLoadPackages(t *testing.T) {

initialRequirementsStr := strings.Fields(readTestFile("initial-requirements"))
mainModulePath, moduleVersions := initialRequirementsStr[0], mapSlice(initialRequirementsStr[1:], module.MustParseVersion)
initialRequirements := modrequirements.NewRequirements(mainModulePath, reg, moduleVersions, nil)
defaultMajorVersions := make(map[string]string)
for _, f := range strings.Fields(readTestFile("default-major-versions")) {
p, v, ok := strings.Cut(f, "@")
qt.Assert(t, qt.IsTrue(ok))
defaultMajorVersions[p] = v
}
initialRequirements := modrequirements.NewRequirements(mainModulePath, reg, moduleVersions, defaultMajorVersions)

rootPackages := strings.Fields(readTestFile("root-packages"))
want := readTestFile("want")
Expand Down
2 changes: 2 additions & 0 deletions internal/mod/modpkgload/testdata/simple.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
main.test@v0
-- test0/root-packages --
main.test@v0
-- test0/default-major-versions --
-- test0/want --
main.test@v0
flags: inAll,isRoot,fromRoot,importsLoaded
Expand All @@ -18,6 +19,7 @@ main.test@v0
[email protected]
-- test1/root-packages --
main.test@v0
-- test1/default-major-versions --
-- test1/want --
main.test@v0
flags: inAll,isRoot,fromRoot,importsLoaded
Expand Down
37 changes: 37 additions & 0 deletions internal/mod/modpkgload/testdata/withdefaultmajorversions.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
-- test0/initial-requirements --
main.test@v0 [email protected]
-- test0/root-packages --
main.test@v0
example.com/blah
-- test0/default-major-versions --
example.com@v0
-- test0/want --
main.test@v0
flags: inAll,isRoot,fromRoot,importsLoaded
mod: main.test@v0
location: .
imports:
example.com/blah
example.com/blah
flags: inAll,isRoot,fromRoot,importsLoaded
mod: [email protected]
location: _registry/example.com_v0.0.1/blah
imports:
foo.com/bar/hello/goodbye@v0
foo.com/bar/hello/goodbye@v0
flags: inAll,isRoot,fromRoot
error: cannot fetch foo.com/bar/[email protected]: module foo.com/bar/[email protected] not found at _registry/foo.com_bar_hello_v0.2.3
missing: false
-- main.cue --
package main
import "example.com/blah"

-- _registry/example.com_v0.0.1/cue.mod/module.cue --
module: "example.com@v0"
deps: {
"foo.com/bar/hello@v0": v: "v0.2.3"
"bar.com@v0": v: "v0.5.0"
}
-- _registry/example.com_v0.0.1/blah/blah.cue --
package blah
import _ "foo.com/bar/hello/goodbye@v0"

0 comments on commit 3cfb6e2

Please sign in to comment.