this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

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 <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>

authored by

Roger Peppe and committed by
Daniel Martí
fa64c628 1ad8c520

+41 -14
+19
cmd/cue/cmd/testdata/script/fmt_import_no_module.txtar
··· 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
+22 -14
cue/load/import.go
··· 379 379 // 380 380 // The returned directory may not exist. 381 381 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) { 382 392 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) 384 394 } 385 395 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) 387 397 } 388 398 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) 390 400 } 391 401 origp := p 392 402 // Extract the package name. ··· 394 404 name = parts.Qualifier 395 405 p = importPath(parts.Unqualified().String()) 396 406 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) 398 408 } 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) 401 410 } 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) 404 412 } 405 413 if l.cfg.Registry != nil { 406 414 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") 408 416 } 409 417 // TODO predicate registry-aware lookup on module.cue-declared CUE version? 410 418 ··· 413 421 // and hence it's available by that name via Pkg. 414 422 pkg := l.pkgs.Pkg(string(origp)) 415 423 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) 417 425 } 418 426 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) 420 428 } 421 429 if mv := pkg.Mod(); mv.IsLocal() { 422 430 // It's a local package that's present inside one or both of the gen, usr or pkg ··· 427 435 } else { 428 436 locs := pkg.Locations() 429 437 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) 431 439 } 432 440 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) 434 442 } 435 443 var err error 436 444 absDir, err = absPathForSourceLoc(locs[0]) 437 445 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) 439 447 } 440 448 } 441 449 return absDir, name, nil