Skip to content

Commit 2d5049e

Browse files
committed
yaml: deprecate non-strict YAML
Having duplicated keys is now prohibited, but having unsupported keys is still allowed (with deprecation warning) Signed-off-by: Akihiro Suda <[email protected]>
1 parent 7a74ca9 commit 2d5049e

File tree

4 files changed

+33
-18
lines changed

4 files changed

+33
-18
lines changed

go.mod

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ require (
1515
github.com/docker/go-units v0.5.0
1616
github.com/elastic/go-libaudit/v2 v2.3.2
1717
github.com/foxcpp/go-mockdns v1.0.0
18+
github.com/goccy/go-yaml v1.9.5
1819
github.com/google/go-cmp v0.5.8
1920
github.com/gorilla/mux v1.8.0
2021
github.com/hashicorp/go-multierror v1.1.1
@@ -31,7 +32,6 @@ require (
3132
github.com/xorcare/pointer v1.2.1
3233
github.com/yalue/native_endian v1.0.2
3334
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab
34-
gopkg.in/yaml.v2 v2.4.0
3535
gotest.tools/v3 v3.3.0
3636
)
3737

@@ -46,11 +46,9 @@ require (
4646
github.com/inconshreveable/mousetrap v1.0.0 // indirect
4747
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 // indirect
4848
github.com/kr/fs v0.1.0 // indirect
49-
github.com/kr/text v0.2.0 // indirect
5049
github.com/mattn/go-colorable v0.1.12 // indirect
5150
github.com/mattn/go-runewidth v0.0.12 // indirect
5251
github.com/mgutz/ansi v0.0.0-20170206155736-9520e82c474b // indirect
53-
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e // indirect
5452
github.com/pkg/sftp v1.13.4 // indirect
5553
github.com/rivo/uniseg v0.2.0 // indirect
5654
github.com/spf13/pflag v1.0.5 // indirect
@@ -67,7 +65,6 @@ require (
6765
google.golang.org/genproto v0.0.0-20211208223120-3a66f561d7aa // indirect
6866
google.golang.org/grpc v1.43.0 // indirect
6967
google.golang.org/protobuf v1.27.1 // indirect
70-
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f // indirect
7168
gopkg.in/djherbis/times.v1 v1.2.0 // indirect
7269
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 // indirect
7370
)

go.sum

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ github.com/containerd/continuity v0.3.0/go.mod h1:wJEAIwKOm/pBZuBd0JmeTvnLquTB1A
3434
github.com/coreos/go-semver v0.3.0 h1:wkHLiw0WNATZnSG7epLsujiMCgPAc9xhjJ4tgnAxmfM=
3535
github.com/coreos/go-semver v0.3.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3EedlOD2RNk=
3636
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
37-
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
3837
github.com/creack/pty v1.1.17 h1:QeVUsEDNrLBW4tMgZHvxy18sKtr6VI492kBhUfhDJNI=
3938
github.com/creack/pty v1.1.17/go.mod h1:MOBLtS5ELjhRRrroQr9kyvTxUAFNvYEK993ew/Vr4O4=
4039
github.com/cyphar/filepath-securejoin v0.2.3 h1:YX6ebbZCZP7VkM3scTTokDgBL2TY741X51MTk3ycuNI=
@@ -69,6 +68,15 @@ github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4
6968
github.com/fsnotify/fsnotify v1.5.1 h1:mZcQUHVQUQWoPXXtuf9yuEXKudkV2sx1E06UadKWpgI=
7069
github.com/fsnotify/fsnotify v1.5.1/go.mod h1:T3375wBYaZdLLcVNkcVbzGHY7f1l/uK5T5Ai1i3InKU=
7170
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
71+
github.com/go-playground/assert/v2 v2.0.1/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
72+
github.com/go-playground/locales v0.13.0 h1:HyWk6mgj5qFqCT5fjGBuRArbVDfE4hi8+e8ceBS/t7Q=
73+
github.com/go-playground/locales v0.13.0/go.mod h1:taPMhCMXrRLJO55olJkUXHZBHCxTMfnGwq/HNwmWNS8=
74+
github.com/go-playground/universal-translator v0.17.0 h1:icxd5fm+REJzpZx7ZfpaD876Lmtgy7VtROAbHHXk8no=
75+
github.com/go-playground/universal-translator v0.17.0/go.mod h1:UkSxE5sNxxRwHyU+Scu5vgOQjsIJAF8j9muTVoKLVtA=
76+
github.com/go-playground/validator/v10 v10.4.1 h1:pH2c5ADXtd66mxoE0Zm9SUhxE20r7aM3F26W0hOn+GE=
77+
github.com/go-playground/validator/v10 v10.4.1/go.mod h1:nlOn6nFhuKACm19sB/8EGNn9GlaMV7XkbRSipzJ0Ii4=
78+
github.com/goccy/go-yaml v1.9.5 h1:Eh/+3uk9kLxG4koCX6lRMAPS1OaMSAi+FJcya0INdB0=
79+
github.com/goccy/go-yaml v1.9.5/go.mod h1:U/jl18uSupI5rdI2jmuCswEA2htH9eXfferR3KfscvA=
7280
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
7381
github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
7482
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
@@ -120,8 +128,8 @@ github.com/kr/fs v0.1.0/go.mod h1:FFnZGqtBN9Gxj7eW1uZ42v5BccTP0vu6NEaFoC2HwRg=
120128
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
121129
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
122130
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
123-
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
124-
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
131+
github.com/leodido/go-urn v1.2.0 h1:hpXL4XnriNwQ/ABnpepYM/1vCLWNDfUNts8dX3xTG6Y=
132+
github.com/leodido/go-urn v1.2.0/go.mod h1:+8+nEpDfqqsY+g338gtMEUOtuK+4dEMhiQEgxpxOKII=
125133
github.com/lima-vm/sshocker v0.3.0 h1:4W7AFfwqkhPwBIOtwfnxxggdS3/bs6JkTYT1RfrXwfQ=
126134
github.com/lima-vm/sshocker v0.3.0/go.mod h1:LtQ68MCRh2MPgAczFNyElrOObNR1lsb31YCeFGgeLyc=
127135
github.com/mattn/go-colorable v0.1.2/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=
@@ -144,8 +152,6 @@ github.com/mibk/dupl v1.0.0/go.mod h1:pCr4pNxxIbFGvtyCOi0c7LVjmV6duhKWV+ex5vh38M
144152
github.com/miekg/dns v1.1.25/go.mod h1:bPDLeHnStXmXAq1m/Ch/hvfNHr14JKNPMBo3VZKjuso=
145153
github.com/miekg/dns v1.1.50 h1:DQUfb9uc6smULcREF09Uc+/Gd46YWqJd5DbpPE9xkcA=
146154
github.com/miekg/dns v1.1.50/go.mod h1:e3IlAVfNqAllflbibAZEWOXOQ+Ynzk/dDozDxY7XnME=
147-
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e h1:fD57ERR4JtEqsWbfPhv4DMiApHyliiK5xCTNVSPiaAs=
148-
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
149155
github.com/norouter/norouter v0.6.4 h1:a4XBFacd5tZmdP2xgXV8ojsuCKYdJHGNt/X3YHdHhH8=
150156
github.com/norouter/norouter v0.6.4/go.mod h1:p+KsaLwHDNs33CUtyQBezKbMYyBeVo0EQEFVGdT66ms=
151157
github.com/nxadm/tail v1.4.8 h1:nPr65rt6Y5JFSKQO7qToXr7pePgD6Gwiw05lkbyAQTE=
@@ -177,6 +183,7 @@ github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An
177183
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
178184
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
179185
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
186+
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
180187
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
181188
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
182189
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
@@ -331,8 +338,6 @@ google.golang.org/protobuf v1.27.1 h1:SnqbnDw1V7RiZcXPx5MEeqPv2s79L9i7BJUlG/+Rur
331338
google.golang.org/protobuf v1.27.1/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
332339
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
333340
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
334-
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f h1:BLraFXnmrev5lT+xlilqcH8XK9/i0At2xKjWk4p6zsU=
335-
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
336341
gopkg.in/djherbis/times.v1 v1.2.0 h1:UCvDKl1L/fmBygl2Y7hubXCnY7t4Yj46ZrBFNUipFbM=
337342
gopkg.in/djherbis/times.v1 v1.2.0/go.mod h1:AQlg6unIsrsCEdQYhTzERy542dz6SFdQFZFv6mUY0P8=
338343
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=

pkg/limayaml/load.go

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,35 @@ package limayaml
22

33
import (
44
"errors"
5+
"fmt"
56
"os"
67
"path/filepath"
78

9+
"github.com/goccy/go-yaml"
810
"github.com/lima-vm/lima/pkg/store/dirnames"
911
"github.com/lima-vm/lima/pkg/store/filenames"
1012
"github.com/sirupsen/logrus"
11-
"gopkg.in/yaml.v2"
1213
)
1314

15+
func unmarshalYAML(data []byte, v interface{}, comment string) error {
16+
if err := yaml.UnmarshalWithOptions(data, v, yaml.DisallowDuplicateKey()); err != nil {
17+
return fmt.Errorf("failed to unmarshal YAML (%s): %w", comment, err)
18+
}
19+
if err := yaml.UnmarshalWithOptions(data, v, yaml.Strict()); err != nil {
20+
logrus.WithField("comment", comment).WithError(err).Warn("Non-strict YAML is deprecated and will be unsupported in a future version of Lima")
21+
// Non-strict YAML is known to be used by Rancher Desktop:
22+
// https://github.com/rancher-sandbox/rancher-desktop/blob/c7ea7508a0191634adf16f4675f64c73198e8d37/src/backend/lima.ts#L114-L117
23+
}
24+
return nil
25+
}
26+
1427
// Load loads the yaml and fulfills unspecified fields with the default values.
1528
//
1629
// Load does not validate. Use Validate for validation.
1730
func Load(b []byte, filePath string) (*LimaYAML, error) {
1831
var y, d, o LimaYAML
1932

20-
if err := yaml.Unmarshal(b, &y); err != nil {
33+
if err := unmarshalYAML(b, &y, fmt.Sprintf("main file %q", filePath)); err != nil {
2134
return nil, err
2235
}
2336
configDir, err := dirnames.LimaConfigDir()
@@ -29,7 +42,7 @@ func Load(b []byte, filePath string) (*LimaYAML, error) {
2942
bytes, err := os.ReadFile(defaultPath)
3043
if err == nil {
3144
logrus.Debugf("Mixing %q into %q", defaultPath, filePath)
32-
if err := yaml.Unmarshal(bytes, &d); err != nil {
45+
if err := unmarshalYAML(bytes, &d, fmt.Sprintf("default file %q", defaultPath)); err != nil {
3346
return nil, err
3447
}
3548
} else if !errors.Is(err, os.ErrNotExist) {
@@ -40,7 +53,7 @@ func Load(b []byte, filePath string) (*LimaYAML, error) {
4053
bytes, err = os.ReadFile(overridePath)
4154
if err == nil {
4255
logrus.Debugf("Mixing %q into %q", overridePath, filePath)
43-
if err := yaml.Unmarshal(bytes, &o); err != nil {
56+
if err := unmarshalYAML(bytes, &o, fmt.Sprintf("override file %q", overridePath)); err != nil {
4457
return nil, err
4558
}
4659
} else if !errors.Is(err, os.ErrNotExist) {

pkg/networks/config.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,17 @@ import (
99
"runtime"
1010
"sync"
1111

12+
"github.com/goccy/go-yaml"
1213
"github.com/lima-vm/lima/pkg/store/dirnames"
1314
"github.com/lima-vm/lima/pkg/store/filenames"
14-
"gopkg.in/yaml.v2"
1515
)
1616

1717
//go:embed networks.yaml
1818
var defaultConfig []byte
1919

2020
func DefaultConfig() (NetworksConfig, error) {
2121
var config NetworksConfig
22-
err := yaml.Unmarshal(defaultConfig, &config)
22+
err := yaml.UnmarshalWithOptions(defaultConfig, &config, yaml.Strict())
2323
return config, err
2424
}
2525

@@ -66,7 +66,7 @@ func loadCache() {
6666
if cache.err != nil {
6767
return
6868
}
69-
cache.err = yaml.Unmarshal(b, &cache.config)
69+
cache.err = yaml.UnmarshalWithOptions(b, &cache.config, yaml.Strict())
7070
if cache.err != nil {
7171
cache.err = fmt.Errorf("cannot parse %q: %w", configFile, cache.err)
7272
}

0 commit comments

Comments
 (0)