Skip to content

Commit

Permalink
internal/mod/modpkgload: a package with no files is an error
Browse files Browse the repository at this point in the history
Currently if there are no files in a package, `modpkgload.LoadPackages`
is silent, but this should really be an error.

For #3155.

Signed-off-by: Roger Peppe <rogpeppe@gmail.com>
Change-Id: I8fb77ef69509f5e0a99300723c80a14763af3975
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1195281
Reviewed-by: Chief Cueckoo <chief.cueckoo@gmail.com>
Unity-Result: CUE porcuepine <cue.porcuepine@gmail.com>
TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>
Reviewed-by: Daniel Martí <mvdan@mvdan.cc>
  • Loading branch information
rogpeppe authored and mvdan committed May 28, 2024
1 parent b71b72d commit 6661949
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"
10 changes: 4 additions & 6 deletions cue/load/loader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"},
Expand Down
18 changes: 17 additions & 1 deletion internal/mod/modpkgload/pkgload.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions internal/mod/modpkgload/testdata/nocuefiles.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 6661949

Please sign in to comment.