Skip to content

Commit

Permalink
cue/load: update for OCI modules
Browse files Browse the repository at this point in the history
Note: This is an experimental feature and is intended for internal-only
use for now.

Summary of changes:
- adapt cue/load/internal/registrytest to use OCI registry API;
- change cue/load to use new internal/mod/modfile package;
- change cue/load.TestLoad to use tdtest package for easier test
updating;
- change cue/load to support inferring major module version from
module.cue deps.
- remove now-unused packages inside cue/load/internal.

For #2330.

Signed-off-by: Roger Peppe <rogpeppe@gmail.com>
Change-Id: I7b6d71993c253e6525fc3a69216e87d814ba6b3e
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1168508
Reviewed-by: Daniel Martí <mvdan@mvdan.cc>
TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>
  • Loading branch information
rogpeppe authored and mvdan committed Sep 26, 2023
1 parent 3c368d3 commit b951b07
Show file tree
Hide file tree
Showing 29 changed files with 434 additions and 2,474 deletions.
4 changes: 2 additions & 2 deletions cmd/cue/cmd/testdata/script/eval_loaderr.txtar
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
! exec cue eval non-existing .
! exec cue eval nonexisting .
! stdout .
cmp stderr expect-stderr

-- expect-stderr --
cannot find package "non-existing"
cannot find package "nonexisting"
4 changes: 3 additions & 1 deletion cue/load/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"cuelang.org/go/cue/errors"
"cuelang.org/go/cue/token"
"cuelang.org/go/internal"
"cuelang.org/go/internal/mod/modfile"
)

const (
Expand Down Expand Up @@ -139,7 +140,7 @@ type Config struct {
// if no module file was present. If non-nil, then
// after calling Config.complete, modFile.Module will be
// equal to Module.
modFile *modFile
modFile *modfile.File

// Package defines the name of the package to be loaded. If this is not set,
// the package must be uniquely defined from its context. Special values:
Expand Down Expand Up @@ -364,6 +365,7 @@ func (c Config) complete() (cfg *Config, err error) {
} else if !filepath.IsAbs(c.ModuleRoot) {
c.ModuleRoot = filepath.Join(c.Dir, c.ModuleRoot)
}
//c.Registry = "registry.cue.works"
if c.Registry != "" {
u, err := url.Parse(c.Registry)
if err != nil {
Expand Down
30 changes: 23 additions & 7 deletions cue/load/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,20 @@
package load

import (
"context"
"fmt"
"os"
pathpkg "path"
"path/filepath"
"sort"
"strings"

"cuelang.org/go/cue/ast"
"cuelang.org/go/cue/build"
"cuelang.org/go/cue/errors"
"cuelang.org/go/cue/token"
"cuelang.org/go/internal/filetypes"
"cuelang.org/go/internal/mod/module"
)

// importPkg returns details about the CUE package named by the import path,
Expand Down Expand Up @@ -353,15 +356,25 @@ func (l *loader) absDirFromImportPath(pos token.Pos, p importPath) (absDir, name
p = p[:i]

default: // p[i] == '/'
name = string(p[i+1:])
mp, _, ok := module.SplitPathVersion(string(p))
if ok {
// import of the form: example.com/foo/bar@v1
if i := strings.LastIndex(mp, "/"); i >= 0 {
name = mp[i+1:]
}
} else {
name = string(p[i+1:])
}
}

// TODO: fully test that name is a valid identifier.
if name == "" {
err = errors.Newf(pos, "empty package name in import path %q", p)
} else if strings.IndexByte(name, '.') >= 0 {
err = errors.Newf(pos,
"cannot determine package name for %q (set explicitly with ':')", p)
} else if !ast.IsValidIdent(name) {
err = errors.Newf(pos,
"implied package identifier %q from import path %q is not valid", name, p)
}

// Determine the directory.
Expand All @@ -381,7 +394,7 @@ func (l *loader) absDirFromImportPath(pos token.Pos, p importPath) (absDir, name
absDir, err = l.externalPackageDir(p)
if err != nil {
// TODO why can't we use %w ?
return "", name, errors.Newf(token.NoPos, "cannot get directory for external module %q: %v", p, err)
return "", name, errors.Newf(token.NoPos, "cannot get directory for external module %q (registry %q): %v", p, l.cfg.Registry, err)
}
} else {
absDir = filepath.Join(GenPath(l.cfg.ModuleRoot), sub)
Expand All @@ -392,12 +405,15 @@ func (l *loader) absDirFromImportPath(pos token.Pos, p importPath) (absDir, name
}

func (l *loader) externalPackageDir(p importPath) (dir string, err error) {
m, subPath, ok := l.deps.lookup(p)
if !ok {
return "", fmt.Errorf("no dependency found for import path %q", p)
if l.deps == nil {
return "", fmt.Errorf("no dependency found for import path %q (no dependencies at all)", p)
}
m, subPath, err := l.deps.lookup(p)
if err != nil {
return "", err
}

dir, err = l.regClient.getModContents(m)
dir, err = l.regClient.getModContents(context.TODO(), m)
if err != nil {
return "", fmt.Errorf("cannot get contents for %v: %v", m, err)
}
Expand Down
13 changes: 11 additions & 2 deletions cue/load/import_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package load

import (
"fmt"
"os"
"path/filepath"
"reflect"
Expand All @@ -37,15 +38,23 @@ func getInst(pkg, cwd string) (*build.Instance, error) {
// all the way to the root of the git repository, causing Go's test caching
// to never kick in, as the .git directory almost always changes.
// Moreover, it's extra work that isn't useful to the tests.
c, _ := (&Config{ModuleRoot: cwd, Dir: cwd}).complete()
c, err := (&Config{ModuleRoot: cwd, Dir: cwd}).complete()
if err != nil {
return nil, fmt.Errorf("unexpected error on Config.complete: %v", err)
}
l := loader{cfg: c}
inst := l.newRelInstance(token.NoPos, pkg, c.Package)
p := l.importPkg(token.NoPos, inst)[0]
return p, p.Err
}

func TestEmptyImport(t *testing.T) {
c, _ := (&Config{}).complete()
c, err := (&Config{
ModuleRoot: ".",
}).complete()
if err != nil {
t.Fatal(err)
}
l := loader{cfg: c}
inst := l.newInstance(token.NoPos, "")
p := l.importPkg(token.NoPos, inst)[0]
Expand Down
7 changes: 6 additions & 1 deletion cue/load/instances.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package load
// - go/build

import (
"fmt"
"os"

"cuelang.org/go/cue/ast"
Expand Down Expand Up @@ -56,12 +57,16 @@ func Instances(args []string, c *Config) []*build.Instance {
if err != nil {
return []*build.Instance{c.newErrInstance(err)}
}
regClient = newRegistryClient(c.Registry, tmpDir)
regClient, err = newRegistryClient(c.Registry, tmpDir)
if err != nil {
return []*build.Instance{c.newErrInstance(fmt.Errorf("cannot make registry client: %v", err))}
}
deps1, err := resolveDependencies(c.modFile, regClient)
if err != nil {
return []*build.Instance{c.newErrInstance(err)}
}
deps = deps1

}
tg := newTagger(c)
l := newLoader(c, tg, deps, regClient)
Expand Down
103 changes: 0 additions & 103 deletions cue/load/internal/mvs/errors.go

This file was deleted.

Loading

0 comments on commit b951b07

Please sign in to comment.