Skip to content

Commit 33945a2

Browse files
committed
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í <[email protected]> Change-Id: I93b85fd9fa9cdad5e6d92d0cf8244490a1bb6679 Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1205859 Reviewed-by: Roger Peppe <[email protected]> TryBot-Result: CUEcueckoo <[email protected]>
1 parent c8033e9 commit 33945a2

File tree

2 files changed

+36
-56
lines changed

2 files changed

+36
-56
lines changed

cue/load/import.go

+35-55
Original file line numberDiff line numberDiff line change
@@ -429,68 +429,48 @@ func (l *loader) absDirFromImportPath1(pos token.Pos, p importPath) (absDir stri
429429
if isStdlibPackage(string(p)) {
430430
return "", "", fmt.Errorf("standard library import path %q cannot be imported as a CUE package", p)
431431
}
432+
if l.pkgs == nil {
433+
return "", "", fmt.Errorf("imports are unavailable because there is no cue.mod/module.cue file")
434+
}
432435
// Extract the package name.
433436
parts := module.ParseImportPath(string(p))
434437
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?
440-
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)
438+
// TODO predicate registry-aware lookup on module.cue-declared CUE version?
439+
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)
450463
}
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)
453466
}
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)
473471
}
474-
return absDir, pkg.Mod().Path(), nil
475-
}
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)
492472
}
493-
return absDir, modPath, err
473+
return absDir, pkg.Mod().Path(), nil
494474
}
495475

496476
func absPathForSourceLoc(loc module.SourceLoc) (string, error) {

cue/load/instances.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ func loadPackages(
173173
otherFiles []*build.File,
174174
tg *tagger,
175175
) (*modpkgload.Packages, error) {
176-
if cfg.Registry == nil || cfg.modFile == nil || cfg.modFile.Module == "" {
176+
if cfg.modFile == nil || cfg.modFile.Module == "" {
177177
return nil, nil
178178
}
179179
mainModPath := cfg.modFile.QualifiedModule()

0 commit comments

Comments
 (0)