Skip to content

Commit

Permalink
cue/load: move code before refactoring
Browse files Browse the repository at this point in the history
This step prepares for duplicating much of the
loader logic so that it can be modified for module
loading.

This CL only moves code, with no changes at
all other than just the code movement, so
the next CL will make any changes more clear.

Signed-off-by: Roger Peppe <rogpeppe@gmail.com>
Change-Id: I52060d3a2c1b15c931e1e2bf6fb430bc7d1a6d19
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/549521
Unity-Result: CUEcueckoo <cueckoo@cuelang.org>
TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>
Reviewed-by: Daniel Martí <mvdan@mvdan.cc>
  • Loading branch information
rogpeppe committed Feb 22, 2023
1 parent eff0108 commit 87b91c9
Show file tree
Hide file tree
Showing 8 changed files with 909 additions and 852 deletions.
118 changes: 6 additions & 112 deletions cue/load/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,8 @@
package load

import (
"fmt"
"io"
"os"
pathpkg "path"
"path/filepath"
"strings"

Expand Down Expand Up @@ -292,71 +290,6 @@ func (c *Config) stdin() io.Reader {
return c.Stdin
}

func (c *Config) newInstance(pos token.Pos, p importPath) *build.Instance {
dir, name, err := c.absDirFromImportPath(pos, p)
i := c.Context.NewInstance(dir, c.loadFunc)
i.Dir = dir
i.PkgName = name
i.DisplayPath = string(p)
i.ImportPath = string(p)
i.Root = c.ModuleRoot
i.Module = c.Module
i.Err = errors.Append(i.Err, err)

return i
}

// newRelInstance returns a build instance from the given
// relative import path.
func (c *Config) newRelInstance(pos token.Pos, path, pkgName string) *build.Instance {
if !isLocalImport(path) {
panic(fmt.Errorf("non-relative import path %q passed to newRelInstance", path))
}
fs := c.fileSystem

var err errors.Error
dir := path

p := c.Context.NewInstance(path, c.loadFunc)
p.PkgName = pkgName
p.DisplayPath = filepath.ToSlash(path)
// p.ImportPath = string(dir) // compute unique ID.
p.Root = c.ModuleRoot
p.Module = c.Module

dir = filepath.Join(c.Dir, filepath.FromSlash(path))

if path != cleanImport(path) {
err = errors.Append(err, c.loader.errPkgf(nil,
"non-canonical import path: %q should be %q", path, pathpkg.Clean(path)))
}

if importPath, e := c.importPathFromAbsDir(fsPath(dir), path); e != nil {
// Detect later to keep error messages consistent.
} else {
p.ImportPath = string(importPath)
}

p.Dir = dir

if fs.isAbsPath(path) || strings.HasPrefix(path, "/") {
err = errors.Append(err, errors.Newf(pos,
"absolute import path %q not allowed", path))
}
if err != nil {
p.Err = errors.Append(p.Err, err)
p.Incomplete = true
}

return p
}

func (c Config) newErrInstance(pos token.Pos, path importPath, err error) *build.Instance {
i := c.newInstance(pos, path)
i.Err = errors.Promote(err, "instance")
return i
}

func toImportPath(dir string) importPath {
return importPath(filepath.ToSlash(dir))
}
Expand All @@ -365,51 +298,6 @@ type importPath string

type fsPath string

func (c *Config) importPathFromAbsDir(absDir fsPath, key string) (importPath, errors.Error) {
if c.ModuleRoot == "" {
return "", errors.Newf(token.NoPos,
"cannot determine import path for %q (root undefined)", key)
}

dir := filepath.Clean(string(absDir))
if !strings.HasPrefix(dir, c.ModuleRoot) {
return "", errors.Newf(token.NoPos,
"cannot determine import path for %q (dir outside of root)", key)
}

pkg := filepath.ToSlash(dir[len(c.ModuleRoot):])
switch {
case strings.HasPrefix(pkg, "/cue.mod/"):
pkg = pkg[len("/cue.mod/"):]
if pkg == "" {
return "", errors.Newf(token.NoPos,
"invalid package %q (root of %s)", key, modDir)
}

// TODO(legacy): remove.
case strings.HasPrefix(pkg, "/pkg/"):
pkg = pkg[len("/pkg/"):]
if pkg == "" {
return "", errors.Newf(token.NoPos,
"invalid package %q (root of %s)", key, pkgDir)
}

case c.Module == "":
return "", errors.Newf(token.NoPos,
"cannot determine import path for %q (no module)", key)
default:
pkg = c.Module + pkg
}

name := c.Package
switch name {
case "_", "*":
name = ""
}

return addImportQualifier(importPath(pkg), name)
}

func addImportQualifier(pkg importPath, name string) (importPath, errors.Error) {
if name != "" {
s := string(pkg)
Expand Down Expand Up @@ -582,3 +470,9 @@ func (c Config) findRoot(absDir string) string {
abs = d
}
}

func (c Config) newErrInstance(pos token.Pos, path importPath, err error) *build.Instance {
i := c.newInstance(pos, path)
i.Err = errors.Promote(err, "instance")
return i
}
9 changes: 0 additions & 9 deletions cue/load/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,6 @@ func (p *PackageError) Position() token.Pos { return p.Pos }
func (p *PackageError) InputPositions() []token.Pos { return nil }
func (p *PackageError) Path() []string { return p.ImportStack }

func (l *loader) errPkgf(importPos []token.Pos, format string, args ...interface{}) *PackageError {
err := &PackageError{
ImportStack: l.stk.Copy(),
Message: errors.NewMessage(format, args),
}
err.fillPos(l.cfg.Dir, importPos)
return err
}

func (p *PackageError) fillPos(cwd string, positions []token.Pos) {
if len(positions) > 0 && !p.Pos.IsValid() {
p.Pos = positions[0]
Expand Down
Loading

0 comments on commit 87b91c9

Please sign in to comment.