Skip to content

Commit fa64c62

Browse files
rogpeppemvdan
authored andcommitted
cue/load: treat all absDirFromImportPath errors as PackageErrors
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 <[email protected]> Change-Id: I3dafe2c7432342ebb96119b29d5b8796979a18d2 Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1194851 TryBot-Result: CUEcueckoo <[email protected]> Unity-Result: CUE porcuepine <[email protected]> Reviewed-by: Daniel Martí <[email protected]>
1 parent 1ad8c52 commit fa64c62

File tree

2 files changed

+41
-14
lines changed

2 files changed

+41
-14
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# This tests that when there's an import and no current module,
2+
# fmt will still work.
3+
4+
stdin a/x.cue
5+
exec cue fmt -
6+
cmp stdout a.cue-want
7+
8+
-- a/x.cue --
9+
package x
10+
11+
import "mod.com/p"
12+
13+
x: p.p
14+
-- a.cue-want --
15+
package x
16+
17+
import "mod.com/p"
18+
19+
x: p.p

cue/load/import.go

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -379,32 +379,40 @@ func (l *loader) newInstance(pos token.Pos, p importPath) *build.Instance {
379379
//
380380
// The returned directory may not exist.
381381
func (l *loader) absDirFromImportPath(pos token.Pos, p importPath) (absDir, name string, err errors.Error) {
382+
dir, name, err0 := l.absDirFromImportPath1(pos, p)
383+
if err0 != nil {
384+
// Any error trying to determine the package location
385+
// is a PackageError.
386+
err = l.errPkgf([]token.Pos{pos}, "%s", err0.Error())
387+
}
388+
return dir, name, err
389+
}
390+
391+
func (l *loader) absDirFromImportPath1(pos token.Pos, p importPath) (absDir, name string, err error) {
382392
if p == "" {
383-
return "", "", errors.Newf(pos, "empty package name in import path %q", p)
393+
return "", "", fmt.Errorf("empty package name in import path %q", p)
384394
}
385395
if l.cfg.ModuleRoot == "" {
386-
return "", "", errors.Newf(pos, "cannot import %q (root undefined)", p)
396+
return "", "", fmt.Errorf("cannot import %q (root undefined)", p)
387397
}
388398
if isStdlibPackage(string(p)) {
389-
return "", "", errors.Newf(pos, "standard library import path %q cannot be imported as a CUE package", p)
399+
return "", "", fmt.Errorf("standard library import path %q cannot be imported as a CUE package", p)
390400
}
391401
origp := p
392402
// Extract the package name.
393403
parts := module.ParseImportPath(string(p))
394404
name = parts.Qualifier
395405
p = importPath(parts.Unqualified().String())
396406
if name == "" {
397-
err = errors.Newf(pos, "empty package name in import path %q", p)
407+
err = fmt.Errorf("empty package name in import path %q", p)
398408
} else if strings.IndexByte(name, '.') >= 0 {
399-
err = errors.Newf(pos,
400-
"cannot determine package name for %q (set explicitly with ':')", p)
409+
err = fmt.Errorf("cannot determine package name for %q (set explicitly with ':')", p)
401410
} else if !ast.IsValidIdent(name) {
402-
err = errors.Newf(pos,
403-
"implied package identifier %q from import path %q is not valid", name, p)
411+
err = fmt.Errorf("implied package identifier %q from import path %q is not valid", name, p)
404412
}
405413
if l.cfg.Registry != nil {
406414
if l.pkgs == nil {
407-
return "", name, errors.Newf(pos, "imports are unavailable because there is no cue.mod/module.cue file")
415+
return "", name, fmt.Errorf("imports are unavailable because there is no cue.mod/module.cue file")
408416
}
409417
// TODO predicate registry-aware lookup on module.cue-declared CUE version?
410418

@@ -413,10 +421,10 @@ func (l *loader) absDirFromImportPath(pos token.Pos, p importPath) (absDir, name
413421
// and hence it's available by that name via Pkg.
414422
pkg := l.pkgs.Pkg(string(origp))
415423
if pkg == nil {
416-
return "", name, l.errPkgf([]token.Pos{pos}, "no dependency found for package %q", p)
424+
return "", name, fmt.Errorf("no dependency found for package %q", p)
417425
}
418426
if err := pkg.Error(); err != nil {
419-
return "", name, l.errPkgf([]token.Pos{pos}, "cannot find package %q: %v", p, err)
427+
return "", name, fmt.Errorf("cannot find package %q: %v", p, err)
420428
}
421429
if mv := pkg.Mod(); mv.IsLocal() {
422430
// It's a local package that's present inside one or both of the gen, usr or pkg
@@ -427,15 +435,15 @@ func (l *loader) absDirFromImportPath(pos token.Pos, p importPath) (absDir, name
427435
} else {
428436
locs := pkg.Locations()
429437
if len(locs) > 1 {
430-
return "", "", l.errPkgf([]token.Pos{pos}, "package %q unexpectedly found in multiple locations", p)
438+
return "", "", fmt.Errorf("package %q unexpectedly found in multiple locations", p)
431439
}
432440
if len(locs) == 0 {
433-
return "", "", l.errPkgf([]token.Pos{pos}, "no location found for package %q", p)
441+
return "", "", fmt.Errorf("no location found for package %q", p)
434442
}
435443
var err error
436444
absDir, err = absPathForSourceLoc(locs[0])
437445
if err != nil {
438-
return "", name, l.errPkgf([]token.Pos{pos}, "cannot determine source directory for package %q: %v", p, err)
446+
return "", name, fmt.Errorf("cannot determine source directory for package %q: %v", p, err)
439447
}
440448
}
441449
return absDir, name, nil

0 commit comments

Comments
 (0)