this repo has no description
0
fork

Configure Feed

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

cue/load: assume that Config.Registry is set when SkipImports is false

Now that the new modules implementation is always on by default,
any function which is only called when Config.SkipImports is false,
such as loadPackages or absDirFromImportPath1, can assume that
Config.Registry is set. Even if the user leaves it as nil, it takes
a default value derived from Config.Env.

Signed-off-by: Daniel Martí <mvdan@mvdan.cc>
Change-Id: I93b85fd9fa9cdad5e6d92d0cf8244490a1bb6679
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1205859
Reviewed-by: Roger Peppe <rogpeppe@gmail.com>
TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>

+35 -55
+34 -54
cue/load/import.go
··· 429 429 if isStdlibPackage(string(p)) { 430 430 return "", "", fmt.Errorf("standard library import path %q cannot be imported as a CUE package", p) 431 431 } 432 + if l.pkgs == nil { 433 + return "", "", fmt.Errorf("imports are unavailable because there is no cue.mod/module.cue file") 434 + } 432 435 // Extract the package name. 433 436 parts := module.ParseImportPath(string(p)) 434 437 unqualified := parts.Unqualified().String() 435 - if l.cfg.Registry != nil { 436 - if l.pkgs == nil { 437 - return "", "", fmt.Errorf("imports are unavailable because there is no cue.mod/module.cue file") 438 - } 439 - // TODO predicate registry-aware lookup on module.cue-declared CUE version? 438 + // TODO predicate registry-aware lookup on module.cue-declared CUE version? 440 439 441 - // Note: use the canonical form of the import path because 442 - // that's the form passed to [modpkgload.LoadPackages] 443 - // and hence it's available by that name via Pkg. 444 - pkg := l.pkgs.Pkg(parts.Canonical().String()) 445 - // TODO(mvdan): using "unqualified" for the errors below doesn't seem right, 446 - // should we not be using either the original path or the canonical path? 447 - // The unqualified import path should only be used for filepath.FromSlash further below. 448 - if pkg == nil { 449 - return "", "", fmt.Errorf("no dependency found for package %q", unqualified) 440 + // Note: use the canonical form of the import path because 441 + // that's the form passed to [modpkgload.LoadPackages] 442 + // and hence it's available by that name via Pkg. 443 + pkg := l.pkgs.Pkg(parts.Canonical().String()) 444 + // TODO(mvdan): using "unqualified" for the errors below doesn't seem right, 445 + // should we not be using either the original path or the canonical path? 446 + // The unqualified import path should only be used for filepath.FromSlash further below. 447 + if pkg == nil { 448 + return "", "", fmt.Errorf("no dependency found for package %q", unqualified) 449 + } 450 + if err := pkg.Error(); err != nil { 451 + return "", "", fmt.Errorf("cannot find package %q: %v", unqualified, err) 452 + } 453 + if mv := pkg.Mod(); mv.IsLocal() { 454 + // It's a local package that's present inside one or both of the gen, usr or pkg 455 + // directories. Even though modpkgload tells us exactly what those directories 456 + // are, the rest of the cue/load logic expects only a single directory for now, 457 + // so just use that. 458 + absDir = filepath.Join(GenPath(l.cfg.ModuleRoot), parts.Path) 459 + } else { 460 + locs := pkg.Locations() 461 + if len(locs) > 1 { 462 + return "", "", fmt.Errorf("package %q unexpectedly found in multiple locations", unqualified) 450 463 } 451 - if err := pkg.Error(); err != nil { 452 - return "", "", fmt.Errorf("cannot find package %q: %v", unqualified, err) 464 + if len(locs) == 0 { 465 + return "", "", fmt.Errorf("no location found for package %q", unqualified) 453 466 } 454 - if mv := pkg.Mod(); mv.IsLocal() { 455 - // It's a local package that's present inside one or both of the gen, usr or pkg 456 - // directories. Even though modpkgload tells us exactly what those directories 457 - // are, the rest of the cue/load logic expects only a single directory for now, 458 - // so just use that. 459 - absDir = filepath.Join(GenPath(l.cfg.ModuleRoot), parts.Path) 460 - } else { 461 - locs := pkg.Locations() 462 - if len(locs) > 1 { 463 - return "", "", fmt.Errorf("package %q unexpectedly found in multiple locations", unqualified) 464 - } 465 - if len(locs) == 0 { 466 - return "", "", fmt.Errorf("no location found for package %q", unqualified) 467 - } 468 - var err error 469 - absDir, err = absPathForSourceLoc(locs[0]) 470 - if err != nil { 471 - return "", "", fmt.Errorf("cannot determine source directory for package %q: %v", unqualified, err) 472 - } 467 + var err error 468 + absDir, err = absPathForSourceLoc(locs[0]) 469 + if err != nil { 470 + return "", "", fmt.Errorf("cannot determine source directory for package %q: %v", unqualified, err) 473 471 } 474 - return absDir, pkg.Mod().Path(), nil 475 472 } 476 - 477 - // Determine the directory without using the registry. 478 - 479 - sub := filepath.FromSlash(unqualified) 480 - switch hasPrefix := strings.HasPrefix(unqualified, l.cfg.Module); { 481 - case hasPrefix && len(sub) == len(l.cfg.Module): 482 - modPath = l.cfg.Module 483 - absDir = l.cfg.ModuleRoot 484 - 485 - case hasPrefix && unqualified[len(l.cfg.Module)] == '/': 486 - modPath = l.cfg.Module 487 - absDir = filepath.Join(l.cfg.ModuleRoot, sub[len(l.cfg.Module)+1:]) 488 - 489 - default: 490 - modPath = "local" 491 - absDir = filepath.Join(GenPath(l.cfg.ModuleRoot), sub) 492 - } 493 - return absDir, modPath, err 473 + return absDir, pkg.Mod().Path(), nil 494 474 } 495 475 496 476 func absPathForSourceLoc(loc module.SourceLoc) (string, error) {
+1 -1
cue/load/instances.go
··· 173 173 otherFiles []*build.File, 174 174 tg *tagger, 175 175 ) (*modpkgload.Packages, error) { 176 - if cfg.Registry == nil || cfg.modFile == nil || cfg.modFile.Module == "" { 176 + if cfg.modFile == nil || cfg.modFile.Module == "" { 177 177 return nil, nil 178 178 } 179 179 mainModPath := cfg.modFile.QualifiedModule()