From e02bc9eca55d5fc66221bc0aeeaaa77410603914 Mon Sep 17 00:00:00 2001 From: bpizzi Date: Fri, 27 Jul 2018 10:10:14 +0200 Subject: [PATCH] Fixed missing f.Close() in writeConfig() Defering can cause trouble because we're writing to the file outside the function where the defering is registered, calling f.Sync() ensures that bytes are flushed to disk even is Close() is called too soon. --- viper.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/viper.go b/viper.go index 363a6f3..79c334e 100644 --- a/viper.go +++ b/viper.go @@ -1378,7 +1378,13 @@ func (v *Viper) writeConfig(filename string, force bool) error { if err != nil { return err } - return v.marshalWriter(f, configType) + defer f.Close() + + if err := v.marshalWriter(f, configType); err != nil { + return err + } + + return f.Sync() } // Unmarshal a Reader into a map.