this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

cue/load: add package import cycle error

When two or more packages import each other resulting in a cycle,
cue/load used to cause an infinite loop.
Error instead, since we explicitly don't support import cycles,
per https://cuelang.org/docs/references/spec/#import-declarations:

It is illegal for a package to import itself, directly or indirectly

Fixes #849

Closes #2469 as merged as of commit ce733970.

Signed-off-by: Artem V. Navrotskiy <bozaro@yandex.ru>
Change-Id: I1928717b3df3c5e6c48eb4fa30966f26079e19c6
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1167597
TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>
Unity-Result: CUE porcuepine <cue.porcuepine@gmail.com>
Reviewed-by: Roger Peppe <rogpeppe@gmail.com>

authored by

Artem V. Navrotskiy and committed by
Daniel Martí
220b44e9 66ebe0ff

+48 -8
+13 -8
cue/load/import.go
··· 51 51 // _ anonymous files (which may be marked with _) 52 52 // * all packages 53 53 func (l *loader) importPkg(pos token.Pos, p *build.Instance) []*build.Instance { 54 + retErr := func(errs errors.Error) []*build.Instance { 55 + // XXX: move this loop to ReportError 56 + for _, err := range errors.Errors(errs) { 57 + p.ReportError(err) 58 + } 59 + return []*build.Instance{p} 60 + } 61 + 62 + for _, item := range l.stk { 63 + if item == p.ImportPath { 64 + return retErr(&PackageError{Message: errors.NewMessagef("package import cycle not allowed")}) 65 + } 66 + } 54 67 l.stk.Push(p.ImportPath) 55 68 defer l.stk.Pop() 56 69 ··· 58 71 ctxt := &cfg.fileSystem 59 72 60 73 if p.Err != nil { 61 - return []*build.Instance{p} 62 - } 63 - 64 - retErr := func(errs errors.Error) []*build.Instance { 65 - // XXX: move this loop to ReportError 66 - for _, err := range errors.Errors(errs) { 67 - p.ReportError(err) 68 - } 69 74 return []*build.Instance{p} 70 75 } 71 76
+17
cue/load/loader_test.go
··· 330 330 root: $CWD/testdata/testmod 331 331 dir: $CWD/testdata/testmod/tagsbad 332 332 display:./tagsbad`, 333 + }, { 334 + cfg: &Config{ 335 + Dir: testdataDir, 336 + }, 337 + args: args("./cycle"), 338 + want: ` 339 + err: import failed: import failed: import failed: package import cycle not allowed: 340 + $CWD/testdata/testmod/cycle/cycle.cue:3:8 341 + $CWD/testdata/testmod/cue.mod/pkg/mod.test/cycle/bar/bar.cue:3:8 342 + $CWD/testdata/testmod/cue.mod/pkg/mod.test/cycle/foo/foo.cue:3:8 343 + path: mod.test/test/cycle 344 + module: mod.test/test 345 + root: $CWD/testdata/testmod 346 + dir: $CWD/testdata/testmod/cycle 347 + display:./cycle 348 + files: 349 + $CWD/testdata/testmod/cycle/cycle.cue`, 333 350 }} 334 351 for i, tc := range testCases { 335 352 t.Run(strconv.Itoa(i)+"/"+strings.Join(tc.args, ":"), func(t *testing.T) {
+6
cue/load/testdata/testmod/cue.mod/pkg/mod.test/cycle/bar/bar.cue
··· 1 + package bar 2 + 3 + import "mod.test/cycle/foo" 4 + 5 + #Bar1: foo.#Foo1 + 8 6 + #Bar2: foo.#Foo2 + 16
+7
cue/load/testdata/testmod/cue.mod/pkg/mod.test/cycle/foo/foo.cue
··· 1 + package foo 2 + 3 + import "mod.test/cycle/bar" 4 + 5 + #Foo1: 1 6 + #Foo2: bar.#Bar1 + 2 7 + #Foo: bar.#Bar2 + 4
+5
cue/load/testdata/testmod/cycle/cycle.cue
··· 1 + package cycle 2 + 3 + import "mod.test/cycle/foo" 4 + 5 + Foo: foo.#Foo