From 1fec74ba37fe139099ca41770a99b5508c2790b2 Mon Sep 17 00:00:00 2001 From: Ti Chi Robot Date: Mon, 15 Apr 2024 17:10:38 +0800 Subject: [PATCH] config: return error when `toml.DecodeFile` failed. (#51828) (#52577) close pingcap/tidb#51399 --- config/BUILD.bazel | 2 +- config/config.go | 5 ++++- config/config_test.go | 22 ++++++++++++++++++++++ 3 files changed, 27 insertions(+), 2 deletions(-) diff --git a/config/BUILD.bazel b/config/BUILD.bazel index db2e165331bca..299e001e2cf90 100644 --- a/config/BUILD.bazel +++ b/config/BUILD.bazel @@ -37,7 +37,7 @@ go_test( data = glob(["**"]), embed = [":config"], flaky = True, - shard_count = 23, + shard_count = 24, deps = [ "//testkit/testsetup", "//util/logutil", diff --git a/config/config.go b/config/config.go index 63c181d51675e..508dbe27fca60 100644 --- a/config/config.go +++ b/config/config.go @@ -1237,13 +1237,16 @@ func (c *Config) RemovedVariableCheck(confFile string) error { // Load loads config options from a toml file. func (c *Config) Load(confFile string) error { metaData, err := toml.DecodeFile(confFile, c) + if err != nil { + return err + } if c.TokenLimit == 0 { c.TokenLimit = 1000 } // If any items in confFile file are not mapped into the Config struct, issue // an error and stop the server from starting. undecoded := metaData.Undecoded() - if len(undecoded) > 0 && err == nil { + if len(undecoded) > 0 { var undecodedItems []string for _, item := range undecoded { undecodedItems = append(undecodedItems, item.String()) diff --git a/config/config_test.go b/config/config_test.go index 5523165b935d9..42307f3e37280 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -1329,3 +1329,25 @@ func TestAutoScalerConfig(t *testing.T) { conf.UseAutoScaler = false }) } + +func TestInvalidConfigWithDeprecatedConfig(t *testing.T) { + tmpDir := t.TempDir() + configFile := filepath.Join(tmpDir, "config.toml") + + f, err := os.Create(configFile) + require.NoError(t, err) + + _, err = f.WriteString(` +[log] +slow-threshold = 1000 +[performance] +enforce-mpp = 1 + `) + require.NoError(t, err) + require.NoError(t, f.Sync()) + + var conf Config + err = conf.Load(configFile) + require.Error(t, err) + require.Equal(t, err.Error(), "toml: line 5 (last key \"performance.enforce-mpp\"): incompatible types: TOML value has type int64; destination has type boolean") +}