Skip to content

Commit d97b0c3

Browse files
uhthomasmvdan
authored andcommitted
cmd/cue: fix incorrect position in yaml import
Position information of the CUE AST is represented with bytes, whereas the YAML decoder represents position information with characters. The mismatch could result in incorrect and strange positions in the CUE AST from decoded YAML files. The simplest thing to do is to change the YAML decoder to represent position information as bytes. Other solutions may be complex or computationally expensive. I do not see the character index used in any meaningful way when decoding, so I believe this to be a safe change. In addition, it seems position information for decoded YAML documents may have been off by one, which has also been addressed. Position information for sequence end tokens were also invalid (or missing) and is now correctly set to the starting position of the sequence end event. The Kubernetes tutorial files have been updated to match the changes as a result of the corrected position information. CUE_LONG=true CUE_UPDATE=true go test ./doc/tutorial/kubernetes Fixes #826 Change-Id: Ie139708698296660b2396bc696e7b753a004a132 Signed-off-by: Thomas Way <[email protected]> Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1172017 TryBot-Result: CUEcueckoo <[email protected]> Reviewed-by: Daniel Martí <[email protected]>
1 parent 68e9d5d commit d97b0c3

File tree

11 files changed

+43
-29
lines changed

11 files changed

+43
-29
lines changed

cmd/cue/cmd/testdata/script/def_jsonschema.txtar

+2-2
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,10 @@ age: twenty
7373

7474
-- expect-stderr2 --
7575
age: conflicting values "twenty" and int (mismatched types string and int):
76-
./data.yaml:1:7
76+
./data.yaml:1:6
7777
./schema.json:18:7
7878
-- expect-stderr3 --
7979
age: conflicting values "twenty" and int (mismatched types string and int):
80-
./data.yaml:1:7
80+
./data.yaml:1:6
8181
./schema.json:18:7
8282
-- cue.mod --
+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
exec cue import x.yaml
2+
cmp x.cue expect.cue
3+
4+
-- x.yaml --
5+
# Importing YAML with multi-byte characters as CUE resulted in bad formatting
6+
# since the YAML decoder would count columns by characters and we count by bytes.
7+
x1:
8+
# ああああああああああああああああああ
9+
description: ああ
10+
type: string
11+
-- expect.cue --
12+
// Importing YAML with multi-byte characters as CUE resulted in bad formatting
13+
// since the YAML decoder would count columns by characters and we count by bytes.
14+
x1: {
15+
// ああああああああああああああああああ
16+
description: "ああ"
17+
type: "string"
18+
}

cmd/cue/cmd/testdata/script/vet_data.txtar

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ languages:
2323
-- vet-stderr --
2424
languages.1.name: invalid value "dutch" (out of bound =~"^\\p{Lu}"):
2525
./schema.cue:3:8
26-
./data.yaml:5:12
26+
./data.yaml:5:11
2727
-- export-stderr --
2828
languages.1.name: invalid value "dutch" (out of bound =~"^\\p{Lu}"):
2929
./schema.cue:3:8
30-
./data.yaml:5:12
30+
./data.yaml:5:11

cmd/cue/cmd/testdata/script/vet_embed.txtar

+3-3
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ e: 2
3232

3333
-- expect-foo --
3434
c: field not allowed:
35-
./foo.yaml:2:2
35+
./foo.yaml:2:1
3636
./schema.cue:1:1
3737
./schema.cue:3:7
3838
./schema.cue:7:1
@@ -41,10 +41,10 @@ d: field not allowed:
4141
./schema.cue:1:1
4242
./schema.cue:3:7
4343
./schema.cue:7:1
44-
./stream.yaml:2:2
44+
./stream.yaml:2:1
4545
-- expect-stream --
4646
d: field not allowed:
4747
./schema.cue:1:1
4848
./schema.cue:3:7
4949
./schema.cue:7:1
50-
./stream.yaml:2:2
50+
./stream.yaml:2:1

doc/tutorial/basics/0_intro/47_validation.txtar

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,4 @@ languages:
2828
-- expect-stderr --
2929
languages.1.name: invalid value "dutch" (out of bound =~"^\\p{Lu}"):
3030
./schema.cue:3:8
31-
./data.yaml:5:12
31+
./data.yaml:5:11

doc/tutorial/kubernetes/quick/services/infra/etcd/kube.cue

+1-3
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,7 @@ statefulSet: etcd: spec: {
2020
labelSelector: matchExpressions: [{
2121
key: "app"
2222
operator: "In"
23-
values: [
24-
"etcd",
25-
]
23+
values: ["etcd"]
2624
}]
2725
topologyKey: "kubernetes.io/hostname"
2826
}]

doc/tutorial/kubernetes/quick/services/infra/events/kube.cue

+1-3
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,7 @@ deployment: events: spec: {
1515
labelSelector: matchExpressions: [{
1616
key: "app"
1717
operator: "In"
18-
values: [
19-
"events",
20-
]
18+
values: ["events"]
2119
}]
2220
topologyKey: "kubernetes.io/hostname"
2321
}]

doc/tutorial/kubernetes/quick/services/mon/prometheus/configmap.cue

+2-6
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,11 @@ configMap: prometheus: {
5151
"prometheus.yml": yaml656e63.Marshal(_cue_prometheus_yml)
5252
let _cue_prometheus_yml = {
5353
global: scrape_interval: "15s"
54-
rule_files: [
55-
"/etc/prometheus/alert.rules",
56-
]
54+
rule_files: ["/etc/prometheus/alert.rules"]
5755
alerting: alertmanagers: [{
5856
scheme: "http"
5957
static_configs: [{
60-
targets: [
61-
"alertmanager:9093",
62-
]
58+
targets: ["alertmanager:9093"]
6359
}]
6460
}]
6561
scrape_configs: [{

internal/third_party/yaml/decode.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,8 @@ func (p *parser) sequence() *node {
236236
}
237237
if len(n.children) > 0 {
238238
n.endPos = n.children[len(n.children)-1].endPos
239+
} else {
240+
n.endPos = p.event.start_mark
239241
}
240242
p.expect(yaml_SEQUENCE_END_EVENT)
241243
return n
@@ -362,7 +364,7 @@ func (d *decoder) attachLineComment(m yaml_mark_t, pos int8, expr ast.Node) {
362364
}
363365

364366
func (d *decoder) pos(m yaml_mark_t) token.Pos {
365-
pos := d.p.info.Pos(m.index+1, token.NoRelPos)
367+
pos := d.absPos(m)
366368

367369
if d.forceNewline {
368370
d.forceNewline = false
@@ -390,7 +392,7 @@ func (d *decoder) pos(m yaml_mark_t) token.Pos {
390392
}
391393

392394
func (d *decoder) absPos(m yaml_mark_t) token.Pos {
393-
return d.p.info.Pos(m.index+1, token.NoRelPos)
395+
return d.p.info.Pos(m.index, token.NoRelPos)
394396
}
395397

396398
func (d *decoder) start(n *node) token.Pos {

internal/third_party/yaml/scannerc.go

+7-5
Original file line numberDiff line numberDiff line change
@@ -489,10 +489,11 @@ func cache(parser *yaml_parser_t, length int) bool {
489489

490490
// Advance the buffer pointer.
491491
func skip(parser *yaml_parser_t) {
492-
parser.mark.index++
492+
w := width(parser.buffer[parser.buffer_pos])
493+
parser.mark.index += w
493494
parser.mark.column++
494495
parser.unread--
495-
parser.buffer_pos += width(parser.buffer[parser.buffer_pos])
496+
parser.buffer_pos += w
496497
}
497498

498499
func skip_line(parser *yaml_parser_t) {
@@ -503,11 +504,12 @@ func skip_line(parser *yaml_parser_t) {
503504
parser.unread -= 2
504505
parser.buffer_pos += 2
505506
} else if is_break(parser.buffer, parser.buffer_pos) {
506-
parser.mark.index++
507+
w := width(parser.buffer[parser.buffer_pos])
508+
parser.mark.index += w
507509
parser.mark.column = 0
508510
parser.mark.line++
509511
parser.unread--
510-
parser.buffer_pos += width(parser.buffer[parser.buffer_pos])
512+
parser.buffer_pos += w
511513
}
512514
}
513515

@@ -528,7 +530,7 @@ func read(parser *yaml_parser_t, s []byte) []byte {
528530
s = append(s, parser.buffer[parser.buffer_pos:parser.buffer_pos+w]...)
529531
parser.buffer_pos += w
530532
}
531-
parser.mark.index++
533+
parser.mark.index += w
532534
parser.mark.column++
533535
parser.unread--
534536
return s

pkg/encoding/yaml/testdata/gen.txtar

+2-2
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,14 @@ Errors:
3232
t1: error in call to encoding/yaml.Validate: invalid value 4 (out of bound <3):
3333
./in.cue:3:5
3434
./in.cue:3:49
35-
yaml.Validate:3:5
35+
yaml.Validate:3:4
3636
t3: error in call to encoding/yaml.Validate: incomplete value int:
3737
./in.cue:5:5
3838
./in.cue:5:56
3939
t4: error in call to encoding/yaml.ValidatePartial: invalid value 4 (out of bound <3):
4040
./in.cue:6:5
4141
./in.cue:6:49
42-
yaml.ValidatePartial:3:5
42+
yaml.ValidatePartial:3:4
4343

4444
Result:
4545
t1: _|_ // t1: error in call to encoding/yaml.Validate: a: invalid value 4 (out of bound <3)

0 commit comments

Comments
 (0)