From 4a5d9735c8fba4a608c5c87a1ad564b2b1748dd0 Mon Sep 17 00:00:00 2001 From: Stephan Renatus Date: Mon, 11 Aug 2025 20:08:37 +0200 Subject: [PATCH] config: accept env vars set to "", discern from unset Fixes #7831 to a certain extent. Signed-off-by: Stephan Renatus --- internal/config/config.go | 3 ++- internal/config/config_test.go | 16 +++++++++++++++- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/internal/config/config.go b/internal/config/config.go index 7b92a95b7f..53dfc6d6cb 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -138,7 +138,8 @@ func subEnvVars(s string) string { // Lookup the variable in the environment. We do not // play by bash rules: if its undefined we'll keep it // as-is, it could be replaced somewhere down the line. - if lu := os.Getenv(varName); lu != "" { + // If it's set to "", we'll return that. + if lu, ok := os.LookupEnv(varName); ok { return lu } return s diff --git a/internal/config/config_test.go b/internal/config/config_test.go index e97bb19f75..2e883c1792 100644 --- a/internal/config/config_test.go +++ b/internal/config/config_test.go @@ -96,7 +96,7 @@ func TestSubEnvVarsVarsSubMissingEnvVar(t *testing.T) { envKey := setTestEnvVar(t, "var1", "foo") configYaml := fmt.Sprintf("field1: '${%s}'", envKey) - // Remove the env var and expect the system to sub in "" + // Remove the env var and expect the system to keep the key as-is os.Unsetenv(envKey) expected := configYaml // untouched @@ -118,6 +118,20 @@ func TestSubEnvVarsVarsSubEmptyVarName(t *testing.T) { } } +func TestSubEnvVarsVarsSubEmptyEnvVar(t *testing.T) { + envKey := setTestEnvVar(t, "var1", "foo") + configYaml := fmt.Sprintf("field1: '${%s}'", envKey) + + t.Setenv(envKey, "") + expected := "field1: ''" + + actual := subEnvVars(configYaml) + + if actual != expected { + t.Errorf("Expected: '%s'\nActual: '%s'", expected, actual) + } +} + func TestMergeValuesNoOverride(t *testing.T) { dest := map[string]any{} src := map[string]any{