From 00997cc0479fb5e65d4c58ef785c54e9ae95e59c Mon Sep 17 00:00:00 2001 From: Steven Masley Date: Fri, 1 May 2026 18:19:06 +0000 Subject: [PATCH 1/2] fix: allow empty environment variable values to override defaults Remove the long-standing hack that treated empty environment variable values as unset. This prevented users from clearing a default value by setting an env var to an empty string (e.g. CODER_LOGGING_HUMAN="" to disable human logging). The original comment noted this should have been removed in May 2023 after deployments had time to migrate. --- option.go | 10 +--------- option_test.go | 4 +++- 2 files changed, 4 insertions(+), 10 deletions(-) diff --git a/option.go b/option.go index 643a111..4ceac71 100644 --- a/option.go +++ b/option.go @@ -292,15 +292,7 @@ func (optSet *OptionSet) ParseEnv(vs []EnvVar) error { // because the agent lacked the environment variables to authenticate with Git. envVal, ok = envs[`HOMEBREW_`+opt.Env] } - // Currently, empty values are treated as if the environment variable is - // unset. This behavior is technically not correct as there is now no - // way for a user to change a Default value to an empty string from - // the environment. Unfortunately, we have old configuration files - // that rely on the faulty behavior. - // - // TODO: We should remove this hack in May 2023, when deployments - // have had months to migrate to the new behavior. - if !ok || envVal == "" { + if !ok { continue } diff --git a/option_test.go b/option_test.go index 19aaef4..3967b4e 100644 --- a/option_test.go +++ b/option_test.go @@ -152,7 +152,9 @@ func TestOptionSet_ParseEnv(t *testing.T) { err = os.ParseEnv(serpent.ParseEnviron([]string{"CODER_WORKSPACE_NAME="}, "CODER_")) require.NoError(t, err) - require.EqualValues(t, "defname", workspaceName) + // An explicitly empty environment variable should override the + // default value, allowing users to clear a default. + require.EqualValues(t, "", workspaceName) }) t.Run("StringSlice", func(t *testing.T) { From e9b06829ec563f79b757fd4e82ddf558b7075a4c Mon Sep 17 00:00:00 2001 From: Steven Masley Date: Fri, 1 May 2026 18:28:18 +0000 Subject: [PATCH 2/2] test: add ValueSource assertions and unset env var test Verify that EmptyValue sets ValueSource to ValueSourceEnv, and add a new EmptyValueUnset test confirming that a truly absent env var preserves the default. --- option_test.go | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/option_test.go b/option_test.go index 3967b4e..8b4d071 100644 --- a/option_test.go +++ b/option_test.go @@ -155,6 +155,32 @@ func TestOptionSet_ParseEnv(t *testing.T) { // An explicitly empty environment variable should override the // default value, allowing users to clear a default. require.EqualValues(t, "", workspaceName) + require.Equal(t, serpent.ValueSourceEnv, os[0].ValueSource) + }) + + t.Run("EmptyValueUnset", func(t *testing.T) { + t.Parallel() + + var workspaceName serpent.String + + os := serpent.OptionSet{ + serpent.Option{ + Name: "Workspace Name", + Value: &workspaceName, + Default: "defname", + Env: "WORKSPACE_NAME", + }, + } + + err := os.SetDefaults() + require.NoError(t, err) + + // An env var that is not present at all should preserve the + // default value. + err = os.ParseEnv(serpent.ParseEnviron([]string{}, "CODER_")) + require.NoError(t, err) + require.EqualValues(t, "defname", workspaceName) + require.Equal(t, serpent.ValueSourceDefault, os[0].ValueSource) }) t.Run("StringSlice", func(t *testing.T) {