From 45bcd349e00769ed288352dcd406152d12dfbf0b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Mart=C3=AD?= Date: Wed, 7 May 2025 23:40:26 +0100 Subject: [PATCH] internal/core/runtime: validate imports in package-less files too MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Back in 2021, the patch https://cue-review.googlesource.com/c/cue/+/8925 fixed the bug at https://cuelang.org/issue/736 where a `cue eval` of CUE files belonging to a package could resolve references declared in files with an anonymous package. However, the fix went too far; it also removed the use of resolveFile for CUE files belonging to anonymous packages. resolveFile turns out to be responsible for reporting various errors relating to imports, such as them being unused, unknown in std, or redeclaring fields. Since none of that relates to resolving references between CUE files, tweak the logic so that resolveFile runs for anonymous package files. Given that the original patch from 2021 added tests that still pass, and we just added test that lock in the behavior for import errors on files with named and anonymous package clauses, this seems fine. The logic around "resolve files" should be cleaned up in the future, which is already tracked with TODOs in the runtime and compile packages. Fixes #1850. Signed-off-by: Daniel Martí Change-Id: I0e7ad266db7bd839784439c2cd4d13df29256c45 Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1214605 Unity-Result: CUE porcuepine TryBot-Result: CUEcueckoo Reviewed-by: Matthew Sackman --- .../cmd/testdata/script/eval_import_bad.txtar | 4 +- .../testdata/script/eval_import_unused.txtar | 16 +++---- .../cmd/testdata/script/eval_resolve.txtar | 4 +- cue/testdata/benchmarks/issue2176.txtar | 45 +++++++++---------- cue/testdata/builtins/default.txtar | 6 --- cue/testdata/comprehensions/pushdown.txtar | 2 - cue/testdata/cycle/self.txtar | 12 ++--- internal/core/export/testdata/main/num.txtar | 2 - .../selfcontained/splitcomprehension.txtar | 2 - internal/core/runtime/resolve.go | 3 -- 10 files changed, 40 insertions(+), 56 deletions(-) diff --git a/cmd/cue/cmd/testdata/script/eval_import_bad.txtar b/cmd/cue/cmd/testdata/script/eval_import_bad.txtar index 5de22f2de..0b58f8bda 100644 --- a/cmd/cue/cmd/testdata/script/eval_import_bad.txtar +++ b/cmd/cue/cmd/testdata/script/eval_import_bad.txtar @@ -5,7 +5,6 @@ ! exec cue export named.cue cmpenv stderr named.stderr -# TODO: this should report "redeclared as imported package" too; see https://cuelang.org/issue/1850 ! exec cue export missing.cue cmpenv stderr missing.stderr @@ -30,3 +29,6 @@ list: 123 -- missing.stderr -- builtin package "notinstd" undefined: ./missing.cue:1:8 +list redeclared as imported package name + previous declaration at ${WORK}${/}missing.cue:4:1: + ./missing.cue:2:8 diff --git a/cmd/cue/cmd/testdata/script/eval_import_unused.txtar b/cmd/cue/cmd/testdata/script/eval_import_unused.txtar index 8b829f87b..61602e7ca 100644 --- a/cmd/cue/cmd/testdata/script/eval_import_unused.txtar +++ b/cmd/cue/cmd/testdata/script/eval_import_unused.txtar @@ -7,15 +7,11 @@ cmp stderr named.stderr ! exec cue export renamed.cue cmp stderr renamed.stderr -# TODO: these should fail; see https://cuelang.org/issue/1850 +! exec cue export missing.cue +cmp stderr missing.stderr -exec cue export missing.cue -# ! exec cue export missing.cue -# cmp stderr missing.stderr - -exec cue export underscore.cue -# ! exec cue export underscore.cue -# cmp stderr underscore.stderr +! exec cue export underscore.cue +cmp stderr underscore.stderr -- named.cue -- package p @@ -40,6 +36,8 @@ import "list" out: 123 -- missing.stderr -- +imported and not used: "list": + ./missing.cue:1:8 -- underscore.cue -- package _ @@ -47,3 +45,5 @@ import "list" out: 123 -- underscore.stderr -- +imported and not used: "list": + ./underscore.cue:3:8 diff --git a/cmd/cue/cmd/testdata/script/eval_resolve.txtar b/cmd/cue/cmd/testdata/script/eval_resolve.txtar index a2b14d20d..78af9bc64 100644 --- a/cmd/cue/cmd/testdata/script/eval_resolve.txtar +++ b/cmd/cue/cmd/testdata/script/eval_resolve.txtar @@ -71,10 +71,8 @@ nodes: [{ }] -- check.cue -- -import "list" - #map: {for n in nodes {"\(n.name)": n}} -- out-stderr -- #map: reference "nodes" not found: - ./check.cue:3:17 + ./check.cue:1:17 diff --git a/cue/testdata/benchmarks/issue2176.txtar b/cue/testdata/benchmarks/issue2176.txtar index ed56590ec..835a49cf8 100644 --- a/cue/testdata/benchmarks/issue2176.txtar +++ b/cue/testdata/benchmarks/issue2176.txtar @@ -12,7 +12,6 @@ Disjuncts: 3820 -- in.cue -- import ( "strings" - "strconv" "list" ) @@ -115,17 +114,17 @@ Disjuncts: 1475 length: (int){ int } position: (_|_){ // [incomplete] #Datastream.marker.position.characters: error in call to strings.Split: non-concrete value string: - // ./in.cue:14:21 - // ./in.cue:9:11 + // ./in.cue:13:21 + // ./in.cue:8:11 let characters#1 = (_|_){ // [incomplete] #Datastream.marker.position.characters: error in call to strings.Split: non-concrete value string: - // ./in.cue:14:21 - // ./in.cue:9:11 + // ./in.cue:13:21 + // ./in.cue:8:11 } let unique_combinations#2 = (_|_){ // [incomplete] #Datastream.marker.position.characters: error in call to strings.Split: non-concrete value string: - // ./in.cue:14:21 - // ./in.cue:9:11 + // ./in.cue:13:21 + // ./in.cue:8:11 let combination#3multi = 〈import;list〉.Slice(〈3;let characters#1〉, ((〈1;i〉 - 〈4;length〉) + 1), (〈1;i〉 + 1)) } } @@ -140,8 +139,8 @@ Disjuncts: 1475 length: (int){ int } position: (_|_){ // [incomplete] datastream.marker.position.unique_combinations: non-concrete value int in operand to -: - // ./in.cue:17:36 - // ./in.cue:12:11 + // ./in.cue:16:36 + // ./in.cue:11:11 let characters#1 = (#list){ 0: (string){ "m" } 1: (string){ "j" } @@ -176,8 +175,8 @@ Disjuncts: 1475 } let unique_combinations#2 = (_|_){ // [incomplete] datastream.marker.position.unique_combinations: non-concrete value int in operand to -: - // ./in.cue:17:36 - // ./in.cue:12:11 + // ./in.cue:16:36 + // ./in.cue:11:11 let combination#3multi = 〈import;list〉.Slice(〈3;let characters#1〉, ((〈1;i〉 - 〈4;length〉) + 1), (〈1;i〉 + 1)) } } @@ -523,8 +522,8 @@ diff old new --- old +++ new @@ -12,13 +12,11 @@ - // ./in.cue:14:21 - // ./in.cue:9:11 + // ./in.cue:13:21 + // ./in.cue:8:11 } - let unique_combinations#2multi = { - for i, _ in 〈1;let characters#1〉 if (〈0;i〉 >= (〈3;length〉 - 1)) { @@ -535,8 +534,8 @@ diff old new - } + let unique_combinations#2 = (_|_){ + // [incomplete] #Datastream.marker.position.characters: error in call to strings.Split: non-concrete value string: -+ // ./in.cue:14:21 -+ // ./in.cue:9:11 ++ // ./in.cue:13:21 ++ // ./in.cue:8:11 + let combination#3multi = 〈import;list〉.Slice(〈3;let characters#1〉, ((〈1;i〉 - 〈4;length〉) + 1), (〈1;i〉 + 1)) } } @@ -554,8 +553,8 @@ diff old new - } + let unique_combinations#2 = (_|_){ + // [incomplete] datastream.marker.position.unique_combinations: non-concrete value int in operand to -: -+ // ./in.cue:17:36 -+ // ./in.cue:12:11 ++ // ./in.cue:16:36 ++ // ./in.cue:11:11 + let combination#3multi = 〈import;list〉.Slice(〈3;let characters#1〉, ((〈1;i〉 - 〈4;length〉) + 1), (〈1;i〉 + 1)) } } @@ -854,12 +853,12 @@ let unique_combinations: not marked as multi. Seems benign, but should be fixed. length: (int){ int } position: (_|_){ // [incomplete] #Datastream.marker.position.characters: error in call to strings.Split: non-concrete value string: - // ./in.cue:14:21 - // ./in.cue:9:11 + // ./in.cue:13:21 + // ./in.cue:8:11 let characters#1 = (_|_){ // [incomplete] #Datastream.marker.position.characters: error in call to strings.Split: non-concrete value string: - // ./in.cue:14:21 - // ./in.cue:9:11 + // ./in.cue:13:21 + // ./in.cue:8:11 } let unique_combinations#2multi = { for i, _ in 〈1;let characters#1〉 if (〈0;i〉 >= (〈3;length〉 - 1)) { @@ -881,8 +880,8 @@ let unique_combinations: not marked as multi. Seems benign, but should be fixed. length: (int){ int } position: (_|_){ // [incomplete] datastream.marker.position.unique_combinations: non-concrete value int in operand to -: - // ./in.cue:17:36 - // ./in.cue:12:11 + // ./in.cue:16:36 + // ./in.cue:11:11 let characters#1 = (#list){ 0: (string){ "m" } 1: (string){ "j" } diff --git a/cue/testdata/builtins/default.txtar b/cue/testdata/builtins/default.txtar index 13801a7fa..2b21d544d 100644 --- a/cue/testdata/builtins/default.txtar +++ b/cue/testdata/builtins/default.txtar @@ -1,10 +1,4 @@ -- in.cue -- -import ( - "list" - "strings" - "text/template" -) - Len: len(*[1, 2, 3] | 0) Close: close(*{} | 0) And: and(*[1, 1] | int) diff --git a/cue/testdata/comprehensions/pushdown.txtar b/cue/testdata/comprehensions/pushdown.txtar index af9ec7043..7e77febea 100644 --- a/cue/testdata/comprehensions/pushdown.txtar +++ b/cue/testdata/comprehensions/pushdown.txtar @@ -739,8 +739,6 @@ errorPropagation: { } -- issue2131.cue -- -import "path" - issue2131: tests: { windows: { eg1: in: #"c:\d"# diff --git a/cue/testdata/cycle/self.txtar b/cue/testdata/cycle/self.txtar index 669471edc..204272f68 100644 --- a/cue/testdata/cycle/self.txtar +++ b/cue/testdata/cycle/self.txtar @@ -9,12 +9,12 @@ expr: error2: { a: a + 1 a: 3 } -list: error1: { +listConcat: error1: { a: ["1", "2"] a: list.Concat([a, ["3"]]) } -list: error2: { +listConcat: error2: { a: list.Concat([["3"], a]) a: ["1", "2"] } @@ -309,7 +309,7 @@ Result: } } } - list: (_|_){ + listConcat: (_|_){ // [structural cycle] error1: (_|_){ // [structural cycle] @@ -723,7 +723,7 @@ Result: } } } - list: (_|_){ + listConcat: (_|_){ // [structural cycle] error1: (_|_){ // [structural cycle] @@ -1061,7 +1061,7 @@ Result: a: 3 } } - list: { + listConcat: { error1: { a: [ "1", @@ -1075,7 +1075,7 @@ Result: ]) } } - list: { + listConcat: { error2: { a: 〈import;list〉.Concat([ [ diff --git a/internal/core/export/testdata/main/num.txtar b/internal/core/export/testdata/main/num.txtar index c0b459970..9bd9c0c0b 100644 --- a/internal/core/export/testdata/main/num.txtar +++ b/internal/core/export/testdata/main/num.txtar @@ -1,6 +1,4 @@ -- in.cue -- -import "strings" - floats: { a: 3.0 b: float & 3.0 diff --git a/internal/core/export/testdata/selfcontained/splitcomprehension.txtar b/internal/core/export/testdata/selfcontained/splitcomprehension.txtar index a2dd6ad8d..de06cb314 100644 --- a/internal/core/export/testdata/selfcontained/splitcomprehension.txtar +++ b/internal/core/export/testdata/selfcontained/splitcomprehension.txtar @@ -3,8 +3,6 @@ // TODO: merge output structs. -- in.cue -- -import "encoding/yaml" - for key, value in { test: 1 } { ("a"): b: k1: key ("a"): b: v1: value diff --git a/internal/core/runtime/resolve.go b/internal/core/runtime/resolve.go index 843245864..af9998bde 100644 --- a/internal/core/runtime/resolve.go +++ b/internal/core/runtime/resolve.go @@ -43,9 +43,6 @@ func (r *Runtime) ResolveFiles(p *build.Instance) (errs errors.Error) { } } for _, f := range p.Files { - if f.PackageName() == "" { - continue - } err := resolveFile(idx, f, p, allFields) errs = errors.Append(errs, err) } -- 2.51.2