Skip to content

Commit 45bcd34

Browse files
committed
internal/core/runtime: validate imports in package-less files too
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í <[email protected]> Change-Id: I0e7ad266db7bd839784439c2cd4d13df29256c45 Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1214605 Unity-Result: CUE porcuepine <[email protected]> TryBot-Result: CUEcueckoo <[email protected]> Reviewed-by: Matthew Sackman <[email protected]>
1 parent 21d4ba7 commit 45bcd34

File tree

10 files changed

+40
-56
lines changed

10 files changed

+40
-56
lines changed

cmd/cue/cmd/testdata/script/eval_import_bad.txtar

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
! exec cue export named.cue
66
cmpenv stderr named.stderr
77

8-
# TODO: this should report "redeclared as imported package" too; see https://cuelang.org/issue/1850
98
! exec cue export missing.cue
109
cmpenv stderr missing.stderr
1110

@@ -30,3 +29,6 @@ list: 123
3029
-- missing.stderr --
3130
builtin package "notinstd" undefined:
3231
./missing.cue:1:8
32+
list redeclared as imported package name
33+
previous declaration at ${WORK}${/}missing.cue:4:1:
34+
./missing.cue:2:8

cmd/cue/cmd/testdata/script/eval_import_unused.txtar

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,11 @@ cmp stderr named.stderr
77
! exec cue export renamed.cue
88
cmp stderr renamed.stderr
99

10-
# TODO: these should fail; see https://cuelang.org/issue/1850
10+
! exec cue export missing.cue
11+
cmp stderr missing.stderr
1112

12-
exec cue export missing.cue
13-
# ! exec cue export missing.cue
14-
# cmp stderr missing.stderr
15-
16-
exec cue export underscore.cue
17-
# ! exec cue export underscore.cue
18-
# cmp stderr underscore.stderr
13+
! exec cue export underscore.cue
14+
cmp stderr underscore.stderr
1915

2016
-- named.cue --
2117
package p
@@ -40,10 +36,14 @@ import "list"
4036

4137
out: 123
4238
-- missing.stderr --
39+
imported and not used: "list":
40+
./missing.cue:1:8
4341
-- underscore.cue --
4442
package _
4543

4644
import "list"
4745

4846
out: 123
4947
-- underscore.stderr --
48+
imported and not used: "list":
49+
./underscore.cue:3:8

cmd/cue/cmd/testdata/script/eval_resolve.txtar

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,8 @@ nodes: [{
7171
}]
7272

7373
-- check.cue --
74-
import "list"
75-
7674
#map: {for n in nodes {"\(n.name)": n}}
7775

7876
-- out-stderr --
7977
#map: reference "nodes" not found:
80-
./check.cue:3:17
78+
./check.cue:1:17

cue/testdata/benchmarks/issue2176.txtar

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ Disjuncts: 3820
1212
-- in.cue --
1313
import (
1414
"strings"
15-
"strconv"
1615
"list"
1716
)
1817

@@ -115,17 +114,17 @@ Disjuncts: 1475
115114
length: (int){ int }
116115
position: (_|_){
117116
// [incomplete] #Datastream.marker.position.characters: error in call to strings.Split: non-concrete value string:
118-
// ./in.cue:14:21
119-
// ./in.cue:9:11
117+
// ./in.cue:13:21
118+
// ./in.cue:8:11
120119
let characters#1 = (_|_){
121120
// [incomplete] #Datastream.marker.position.characters: error in call to strings.Split: non-concrete value string:
122-
// ./in.cue:14:21
123-
// ./in.cue:9:11
121+
// ./in.cue:13:21
122+
// ./in.cue:8:11
124123
}
125124
let unique_combinations#2 = (_|_){
126125
// [incomplete] #Datastream.marker.position.characters: error in call to strings.Split: non-concrete value string:
127-
// ./in.cue:14:21
128-
// ./in.cue:9:11
126+
// ./in.cue:13:21
127+
// ./in.cue:8:11
129128
let combination#3multi = 〈import;list〉.Slice(〈3;let characters#1〉, ((〈1;i〉 - 〈4;length〉) + 1), (〈1;i〉 + 1))
130129
}
131130
}
@@ -140,8 +139,8 @@ Disjuncts: 1475
140139
length: (int){ int }
141140
position: (_|_){
142141
// [incomplete] datastream.marker.position.unique_combinations: non-concrete value int in operand to -:
143-
// ./in.cue:17:36
144-
// ./in.cue:12:11
142+
// ./in.cue:16:36
143+
// ./in.cue:11:11
145144
let characters#1 = (#list){
146145
0: (string){ "m" }
147146
1: (string){ "j" }
@@ -176,8 +175,8 @@ Disjuncts: 1475
176175
}
177176
let unique_combinations#2 = (_|_){
178177
// [incomplete] datastream.marker.position.unique_combinations: non-concrete value int in operand to -:
179-
// ./in.cue:17:36
180-
// ./in.cue:12:11
178+
// ./in.cue:16:36
179+
// ./in.cue:11:11
181180
let combination#3multi = 〈import;list〉.Slice(〈3;let characters#1〉, ((〈1;i〉 - 〈4;length〉) + 1), (〈1;i〉 + 1))
182181
}
183182
}
@@ -523,8 +522,8 @@ diff old new
523522
--- old
524523
+++ new
525524
@@ -12,13 +12,11 @@
526-
// ./in.cue:14:21
527-
// ./in.cue:9:11
525+
// ./in.cue:13:21
526+
// ./in.cue:8:11
528527
}
529528
- let unique_combinations#2multi = {
530529
- for i, _ in 〈1;let characters#1〉 if (〈0;i〉 >= (〈3;length〉 - 1)) {
@@ -535,8 +534,8 @@ diff old new
535534
- }
536535
+ let unique_combinations#2 = (_|_){
537536
+ // [incomplete] #Datastream.marker.position.characters: error in call to strings.Split: non-concrete value string:
538-
+ // ./in.cue:14:21
539-
+ // ./in.cue:9:11
537+
+ // ./in.cue:13:21
538+
+ // ./in.cue:8:11
540539
+ let combination#3multi = 〈import;list〉.Slice(〈3;let characters#1〉, ((〈1;i〉 - 〈4;length〉) + 1), (〈1;i〉 + 1))
541540
}
542541
}
@@ -554,8 +553,8 @@ diff old new
554553
- }
555554
+ let unique_combinations#2 = (_|_){
556555
+ // [incomplete] datastream.marker.position.unique_combinations: non-concrete value int in operand to -:
557-
+ // ./in.cue:17:36
558-
+ // ./in.cue:12:11
556+
+ // ./in.cue:16:36
557+
+ // ./in.cue:11:11
559558
+ let combination#3multi = 〈import;list〉.Slice(〈3;let characters#1〉, ((〈1;i〉 - 〈4;length〉) + 1), (〈1;i〉 + 1))
560559
}
561560
}
@@ -854,12 +853,12 @@ let unique_combinations: not marked as multi. Seems benign, but should be fixed.
854853
length: (int){ int }
855854
position: (_|_){
856855
// [incomplete] #Datastream.marker.position.characters: error in call to strings.Split: non-concrete value string:
857-
// ./in.cue:14:21
858-
// ./in.cue:9:11
856+
// ./in.cue:13:21
857+
// ./in.cue:8:11
859858
let characters#1 = (_|_){
860859
// [incomplete] #Datastream.marker.position.characters: error in call to strings.Split: non-concrete value string:
861-
// ./in.cue:14:21
862-
// ./in.cue:9:11
860+
// ./in.cue:13:21
861+
// ./in.cue:8:11
863862
}
864863
let unique_combinations#2multi = {
865864
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.
881880
length: (int){ int }
882881
position: (_|_){
883882
// [incomplete] datastream.marker.position.unique_combinations: non-concrete value int in operand to -:
884-
// ./in.cue:17:36
885-
// ./in.cue:12:11
883+
// ./in.cue:16:36
884+
// ./in.cue:11:11
886885
let characters#1 = (#list){
887886
0: (string){ "m" }
888887
1: (string){ "j" }

cue/testdata/builtins/default.txtar

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,4 @@
11
-- in.cue --
2-
import (
3-
"list"
4-
"strings"
5-
"text/template"
6-
)
7-
82
Len: len(*[1, 2, 3] | 0)
93
Close: close(*{} | 0)
104
And: and(*[1, 1] | int)

cue/testdata/comprehensions/pushdown.txtar

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -739,8 +739,6 @@ errorPropagation: {
739739
}
740740

741741
-- issue2131.cue --
742-
import "path"
743-
744742
issue2131: tests: {
745743
windows: {
746744
eg1: in: #"c:\d"#

cue/testdata/cycle/self.txtar

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ expr: error2: {
99
a: a + 1
1010
a: 3
1111
}
12-
list: error1: {
12+
listConcat: error1: {
1313
a: ["1", "2"]
1414
a: list.Concat([a, ["3"]])
1515
}
1616

17-
list: error2: {
17+
listConcat: error2: {
1818
a: list.Concat([["3"], a])
1919
a: ["1", "2"]
2020
}
@@ -309,7 +309,7 @@ Result:
309309
}
310310
}
311311
}
312-
list: (_|_){
312+
listConcat: (_|_){
313313
// [structural cycle]
314314
error1: (_|_){
315315
// [structural cycle]
@@ -723,7 +723,7 @@ Result:
723723
}
724724
}
725725
}
726-
list: (_|_){
726+
listConcat: (_|_){
727727
// [structural cycle]
728728
error1: (_|_){
729729
// [structural cycle]
@@ -1061,7 +1061,7 @@ Result:
10611061
a: 3
10621062
}
10631063
}
1064-
list: {
1064+
listConcat: {
10651065
error1: {
10661066
a: [
10671067
"1",
@@ -1075,7 +1075,7 @@ Result:
10751075
])
10761076
}
10771077
}
1078-
list: {
1078+
listConcat: {
10791079
error2: {
10801080
a: 〈import;list〉.Concat([
10811081
[

internal/core/export/testdata/main/num.txtar

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
-- in.cue --
2-
import "strings"
3-
42
floats: {
53
a: 3.0
64
b: float & 3.0

internal/core/export/testdata/selfcontained/splitcomprehension.txtar

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
// TODO: merge output structs.
44

55
-- in.cue --
6-
import "encoding/yaml"
7-
86
for key, value in { test: 1 } {
97
("a"): b: k1: key
108
("a"): b: v1: value

internal/core/runtime/resolve.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,6 @@ func (r *Runtime) ResolveFiles(p *build.Instance) (errs errors.Error) {
4343
}
4444
}
4545
for _, f := range p.Files {
46-
if f.PackageName() == "" {
47-
continue
48-
}
4946
err := resolveFile(idx, f, p, allFields)
5047
errs = errors.Append(errs, err)
5148
}

0 commit comments

Comments
 (0)