@@ -379,32 +379,40 @@ func (l *loader) newInstance(pos token.Pos, p importPath) *build.Instance {
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.
393
403
parts := module .ParseImportPath (string (p ))
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,10 +421,10 @@ func (l *loader) absDirFromImportPath(pos token.Pos, p importPath) (absDir, name
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,15 +435,15 @@ func (l *loader) absDirFromImportPath(pos token.Pos, p importPath) (absDir, name
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
0 commit comments