Skip to content

Commit

Permalink
cue/load: support loading a CUE package whose directory ends with .cue
Browse files Browse the repository at this point in the history
When we load a CUE instance, we need to traverse the chain of parent
directories up to the module root to potentially load parent packages
with the same package name, if there are any. As such, we need to
parse any CUE files in parent directories.

If we are using cue/load to load a CUE package in a directory whose name
looks like a CUE file, this would work with older CUE versions like
v0.7.1, but it fails on newer ones like v0.11.0. This is because the new
logic brought in with CUE_EXPERIMENT=modules read and parsed all files
from parent directories with a ".cue" extension, regardless of whether
they were a directory or not.

The old code before CUE_EXPERIMENT=modules used IsDir to filter out
directories, presumably because package directory names such as
taxes.cue from https://github.com/tmm1/taxes.cue do happen.
Do the same in the new code, which is easy enough.

The test is added in the very same commit, and not in a previous one,
because the mere addition of the testdir.cue testdata directory
causes all other tests, which load parent directories, to also fail.

Fixes #3592.

Signed-off-by: Daniel Martí <mvdan@mvdan.cc>
Change-Id: I10bc05c1f821444bd913e6fc694b3194efb6da8b
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1204614
Unity-Result: CUE porcuepine <cue.porcuepine@gmail.com>
TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>
Reviewed-by: Roger Peppe <rogpeppe@gmail.com>
  • Loading branch information
mvdan committed Nov 25, 2024
1 parent 956a558 commit b54e767
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
16 changes: 15 additions & 1 deletion cue/load/loader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -636,7 +636,21 @@ display:.
files:
$CWD/testdata/testmod/test.cue
imports:
mod.test/test/sub: $CWD/testdata/testmod/sub/sub.cue`,
mod.test/test/sub: $CWD/testdata/testmod/sub/sub.cue`}, {
// This tests that we can load a CUE package by pointing Dir to it
// even when the package's directory name ends with ".cue".
name: "DirWithCUEFileExtension",
cfg: &Config{
Dir: filepath.Join(testdataDir, "testdir.cue"),
},
args: []string{"."},
want: `path: mod.test/test/testdir.cue@v0:testdir
module: mod.test/test@v0
root: $CWD/testdata/testmod
dir: $CWD/testdata/testmod/testdir.cue
display:.
files:
$CWD/testdata/testmod/testdir.cue/test.cue`,
}}
tdtest.Run(t, testCases, func(t *tdtest.T, tc *loadTest) {
pkgs := Instances(tc.args, tc.cfg)
Expand Down
3 changes: 3 additions & 0 deletions cue/load/testdata/testmod/testdir.cue/test.cue
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package testdir

"data from testdir"
4 changes: 4 additions & 0 deletions internal/mod/modimports/modimports.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,10 @@ func PackageFiles(fsys fs.FS, dir string, pkgQualifier string) func(func(ModuleF
if e.Name() == "cue.mod" {
inModRoot = true
}
if e.IsDir() {
// Directories are never package files, even when their filename ends with ".cue".
continue
}
pkgName, cont := yieldPackageFile(fsys, path.Join(dir, e.Name()), selectPackage, yield)
if !cont {
return
Expand Down

0 comments on commit b54e767

Please sign in to comment.