Skip to content

Commit

Permalink
cue/load: treat all absDirFromImportPath errors as PackageErrors
Browse files Browse the repository at this point in the history
When `cue fmt` is formatting files, it explicitly checks for,
and ignores, `load.PackageError`. If we fail to load a package
in any way, we should ignore such a failure, so make
all errors returned by `absDirFromImportPath` (the primary
source of such errors), into a `PackageError`.

Signed-off-by: Roger Peppe <rogpeppe@gmail.com>
Change-Id: I3dafe2c7432342ebb96119b29d5b8796979a18d2
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1194851
TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>
Unity-Result: CUE porcuepine <cue.porcuepine@gmail.com>
Reviewed-by: Daniel Martí <mvdan@mvdan.cc>
  • Loading branch information
rogpeppe authored and mvdan committed May 16, 2024
1 parent 1ad8c52 commit fa64c62
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 14 deletions.
19 changes: 19 additions & 0 deletions cmd/cue/cmd/testdata/script/fmt_import_no_module.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# This tests that when there's an import and no current module,
# fmt will still work.

stdin a/x.cue
exec cue fmt -
cmp stdout a.cue-want

-- a/x.cue --
package x

import "mod.com/p"

x: p.p
-- a.cue-want --
package x

import "mod.com/p"

x: p.p
36 changes: 22 additions & 14 deletions cue/load/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -379,32 +379,40 @@ func (l *loader) newInstance(pos token.Pos, p importPath) *build.Instance {
//
// The returned directory may not exist.
func (l *loader) absDirFromImportPath(pos token.Pos, p importPath) (absDir, name string, err errors.Error) {
dir, name, err0 := l.absDirFromImportPath1(pos, p)
if err0 != nil {
// Any error trying to determine the package location
// is a PackageError.
err = l.errPkgf([]token.Pos{pos}, "%s", err0.Error())
}
return dir, name, err
}

func (l *loader) absDirFromImportPath1(pos token.Pos, p importPath) (absDir, name string, err error) {
if p == "" {
return "", "", errors.Newf(pos, "empty package name in import path %q", p)
return "", "", fmt.Errorf("empty package name in import path %q", p)
}
if l.cfg.ModuleRoot == "" {
return "", "", errors.Newf(pos, "cannot import %q (root undefined)", p)
return "", "", fmt.Errorf("cannot import %q (root undefined)", p)
}
if isStdlibPackage(string(p)) {
return "", "", errors.Newf(pos, "standard library import path %q cannot be imported as a CUE package", p)
return "", "", fmt.Errorf("standard library import path %q cannot be imported as a CUE package", p)
}
origp := p
// Extract the package name.
parts := module.ParseImportPath(string(p))
name = parts.Qualifier
p = importPath(parts.Unqualified().String())
if name == "" {
err = errors.Newf(pos, "empty package name in import path %q", p)
err = fmt.Errorf("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)
err = fmt.Errorf("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)
err = fmt.Errorf("implied package identifier %q from import path %q is not valid", name, p)
}
if l.cfg.Registry != nil {
if l.pkgs == nil {
return "", name, errors.Newf(pos, "imports are unavailable because there is no cue.mod/module.cue file")
return "", name, fmt.Errorf("imports are unavailable because there is no cue.mod/module.cue file")
}
// TODO predicate registry-aware lookup on module.cue-declared CUE version?

Expand All @@ -413,10 +421,10 @@ func (l *loader) absDirFromImportPath(pos token.Pos, p importPath) (absDir, name
// and hence it's available by that name via Pkg.
pkg := l.pkgs.Pkg(string(origp))
if pkg == nil {
return "", name, l.errPkgf([]token.Pos{pos}, "no dependency found for package %q", p)
return "", name, fmt.Errorf("no dependency found for package %q", p)
}
if err := pkg.Error(); err != nil {
return "", name, l.errPkgf([]token.Pos{pos}, "cannot find package %q: %v", p, err)
return "", name, fmt.Errorf("cannot find package %q: %v", p, err)
}
if mv := pkg.Mod(); mv.IsLocal() {
// It's a local package that's present inside one or both of the gen, usr or pkg
Expand All @@ -427,15 +435,15 @@ func (l *loader) absDirFromImportPath(pos token.Pos, p importPath) (absDir, name
} else {
locs := pkg.Locations()
if len(locs) > 1 {
return "", "", l.errPkgf([]token.Pos{pos}, "package %q unexpectedly found in multiple locations", p)
return "", "", fmt.Errorf("package %q unexpectedly found in multiple locations", p)
}
if len(locs) == 0 {
return "", "", l.errPkgf([]token.Pos{pos}, "no location found for package %q", p)
return "", "", fmt.Errorf("no location found for package %q", p)
}
var err error
absDir, err = absPathForSourceLoc(locs[0])
if err != nil {
return "", name, l.errPkgf([]token.Pos{pos}, "cannot determine source directory for package %q: %v", p, err)
return "", name, fmt.Errorf("cannot determine source directory for package %q: %v", p, err)
}
}
return absDir, name, nil
Expand Down

0 comments on commit fa64c62

Please sign in to comment.