diff --git a/cmd/cue/cmd/testdata/script/pkg_resolution_multiple_packages_no_matching_path_element.txtar b/cmd/cue/cmd/testdata/script/pkg_resolution_multiple_packages_no_matching_path_element.txtar index d687055eb58..91b4fb4a99d 100644 --- a/cmd/cue/cmd/testdata/script/pkg_resolution_multiple_packages_no_matching_path_element.txtar +++ b/cmd/cue/cmd/testdata/script/pkg_resolution_multiple_packages_no_matching_path_element.txtar @@ -3,10 +3,6 @@ # the final element of the import path. ! exec cue eval root.cue -# TODO: the error for this isn't quite right: it says: -# root.cue: package is root, want x -# but the evaluation should not care about the package in the root directory -# because we are not trying to evaluate that package. cmp stderr import_stderr.golden ! exec cue eval mod.com/x cmp stderr absolute_stderr.golden @@ -32,13 +28,7 @@ package z z: 5 -- import_stderr.golden -- -import failed: build constraints exclude all CUE files in mod.com/x: - root.cue: package is root, want x - x/y.cue: package is y, want x - x/z.cue: package is z, want x: +import failed: cannot find package "mod.com/x": no files in package directory with package name "x": ./root.cue:3:8 -- absolute_stderr.golden -- -build constraints exclude all CUE files in mod.com/x: - root.cue: package is root, want x - x/y.cue: package is y, want x - x/z.cue: package is z, want x +cannot find package "mod.com/x": no files in package directory with package name "x" diff --git a/cmd/cue/cmd/testdata/script/pkg_resolution_path_element_invalid_ident.txtar b/cmd/cue/cmd/testdata/script/pkg_resolution_path_element_invalid_ident.txtar index 34f318006c5..5887d0334c8 100644 --- a/cmd/cue/cmd/testdata/script/pkg_resolution_path_element_invalid_ident.txtar +++ b/cmd/cue/cmd/testdata/script/pkg_resolution_path_element_invalid_ident.txtar @@ -45,3 +45,4 @@ import failed: cannot determine package name for "mod.com/1x"; set it explicitly ./test2/root.cue:3:8 -- test2-abs-stderr.golden -- cannot determine package name for "mod.com/1x"; set it explicitly with ':' +cannot find package "mod.com/1x": cannot determine package name from import path "mod.com/1x" diff --git a/cmd/cue/cmd/testdata/script/pkg_resolution_single_package_not_matching_path_element.txtar b/cmd/cue/cmd/testdata/script/pkg_resolution_single_package_not_matching_path_element.txtar index edab823bc0c..66881137455 100644 --- a/cmd/cue/cmd/testdata/script/pkg_resolution_single_package_not_matching_path_element.txtar +++ b/cmd/cue/cmd/testdata/script/pkg_resolution_single_package_not_matching_path_element.txtar @@ -3,10 +3,6 @@ # that does not match the final element of the import path. ! exec cue eval root.cue -# TODO: the error for this isn't quite right: it says: -# root.cue: package is root, want x -# but the evaluation should not care about the package in the root directory -# because we are not trying to evaluate that package. cmp stderr import_stderr.golden ! exec cue eval mod.com/x cmp stderr absolute_stderr.golden @@ -28,11 +24,7 @@ package y y: 5 -- import_stderr.golden -- -import failed: build constraints exclude all CUE files in mod.com/x: - root.cue: package is root, want x - x/y.cue: package is y, want x: +import failed: cannot find package "mod.com/x": no files in package directory with package name "x": ./root.cue:3:8 -- absolute_stderr.golden -- -build constraints exclude all CUE files in mod.com/x: - root.cue: package is root, want x - x/y.cue: package is y, want x +cannot find package "mod.com/x": no files in package directory with package name "x" diff --git a/cue/load/loader_test.go b/cue/load/loader_test.go index 4bce058060e..fd830859248 100644 --- a/cue/load/loader_test.go +++ b/cue/load/loader_test.go @@ -182,15 +182,13 @@ imports: name: "NoPackageName", cfg: dirCfg, args: []string{"mod.test/test/hello:nonexist"}, - want: `err: build constraints exclude all CUE files in mod.test/test/hello:nonexist: - anon.cue: no package name - test.cue: package is test, want nonexist - hello/test.cue: package is test, want nonexist + want: `err: cannot find package "mod.test/test/hello": no files in package directory with package name "nonexist" path: mod.test/test/hello:nonexist module: mod.test/test@v0 root: $CWD/testdata/testmod -dir: $CWD/testdata/testmod/hello -display:mod.test/test/hello:nonexist`}, { +dir: "" +display:mod.test/test/hello:nonexist`, + }, { name: "ExplicitNonPackageFiles", cfg: dirCfg, args: []string{"./anon.cue", "./other/anon.cue"}, diff --git a/internal/mod/modpkgload/pkgload.go b/internal/mod/modpkgload/pkgload.go index 68f0831f6f6..c3490be785c 100644 --- a/internal/mod/modpkgload/pkgload.go +++ b/internal/mod/modpkgload/pkgload.go @@ -245,9 +245,21 @@ func (pkgs *Packages) load(ctx context.Context, pkg *Package) { pkgs.applyPkgFlags(ctx, pkg, PkgInAll) } pkgQual := module.ParseImportPath(pkg.path).Qualifier + if pkgQual == "" { + pkg.err = fmt.Errorf("cannot determine package name from import path %q", pkg.path) + return + } importsMap := make(map[string]bool) + foundPackageFile := false for _, loc := range pkg.locs { - imports, err := modimports.AllImports(modimports.PackageFiles(loc.FS, loc.Dir, pkgQual)) + // TODO(mvdan): using the PackageFiles iterator twice below + // causes twice as many io/fs operations on loc.FS. + pkgFileIter := modimports.PackageFiles(loc.FS, loc.Dir, pkgQual) + pkgFileIter(func(_ modimports.ModuleFile, err error) bool { + foundPackageFile = err == nil + return false + }) + imports, err := modimports.AllImports(pkgFileIter) if err != nil { pkg.err = fmt.Errorf("cannot get imports: %v", err) return @@ -256,6 +268,10 @@ func (pkgs *Packages) load(ctx context.Context, pkg *Package) { importsMap[imp] = true } } + if !foundPackageFile { + pkg.err = fmt.Errorf("no files in package directory with package name %q", pkgQual) + return + } imports := make([]string, 0, len(importsMap)) for imp := range importsMap { imports = append(imports, imp) diff --git a/internal/mod/modpkgload/testdata/nocuefiles.txtar b/internal/mod/modpkgload/testdata/nocuefiles.txtar index b3fe10a82d1..0d24092a325 100644 --- a/internal/mod/modpkgload/testdata/nocuefiles.txtar +++ b/internal/mod/modpkgload/testdata/nocuefiles.txtar @@ -12,9 +12,9 @@ main.test@v0:main imports: example.com/blah@v0 example.com/blah@v0 - flags: inAll,isRoot,fromRoot,importsLoaded - mod: example.com@v0.0.1 - location: _registry/example.com_v0.0.1/blah + flags: inAll,isRoot,fromRoot + error: no files in package directory with package name "blah" + missing: false -- main.cue -- package main import "example.com/blah@v0"