Skip to content

Commit a725c38

Browse files
committed
cue/load: support loading a CUE package whose directory ends with .cue
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í <[email protected]> Change-Id: I10bc05c1f821444bd913e6fc694b3194efb6da8b Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1204614 Unity-Result: CUE porcuepine <[email protected]> TryBot-Result: CUEcueckoo <[email protected]> Reviewed-by: Roger Peppe <[email protected]> Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1205927 Reviewed-by: Paul Jolly <[email protected]>
1 parent 971ce58 commit a725c38

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

Diff for: cue/load/loader_test.go

+15-1
Original file line numberDiff line numberDiff line change
@@ -636,7 +636,21 @@ display:.
636636
files:
637637
$CWD/testdata/testmod/test.cue
638638
imports:
639-
mod.test/test/sub: $CWD/testdata/testmod/sub/sub.cue`,
639+
mod.test/test/sub: $CWD/testdata/testmod/sub/sub.cue`}, {
640+
// This tests that we can load a CUE package by pointing Dir to it
641+
// even when the package's directory name ends with ".cue".
642+
name: "DirWithCUEFileExtension",
643+
cfg: &Config{
644+
Dir: filepath.Join(testdataDir, "testdir.cue"),
645+
},
646+
args: []string{"."},
647+
want: `path: mod.test/test/testdir.cue@v0:testdir
648+
module: mod.test/test@v0
649+
root: $CWD/testdata/testmod
650+
dir: $CWD/testdata/testmod/testdir.cue
651+
display:.
652+
files:
653+
$CWD/testdata/testmod/testdir.cue/test.cue`,
640654
}}
641655
tdtest.Run(t, testCases, func(t *tdtest.T, tc *loadTest) {
642656
pkgs := Instances(tc.args, tc.cfg)

Diff for: cue/load/testdata/testmod/testdir.cue/test.cue

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package testdir
2+
3+
"data from testdir"

Diff for: internal/mod/modimports/modimports.go

+4
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,10 @@ func PackageFiles(fsys fs.FS, dir string, pkgQualifier string) func(func(ModuleF
106106
if e.Name() == "cue.mod" {
107107
inModRoot = true
108108
}
109+
if e.IsDir() {
110+
// Directories are never package files, even when their filename ends with ".cue".
111+
continue
112+
}
109113
pkgName, cont := yieldPackageFile(fsys, path.Join(dir, e.Name()), selectPackage, yield)
110114
if !cont {
111115
return

0 commit comments

Comments
 (0)