Skip to content

Commit c7a6706

Browse files
committed
fix: treat empty env as defaults
1 parent 62a64d5 commit c7a6706

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

cli/clibase/option.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,12 @@ func (s *OptionSet) ParseEnv(globalPrefix string, environ []string) error {
107107
}
108108

109109
envVal, ok := envs[opt.Env]
110-
if !ok {
110+
// Currently, empty values are treated as if the environment variable is
111+
// unset. This behavior is technically not correct as there is now no
112+
// way for a user to change a Default value to an empty string from
113+
// the environment. Unfortunately, we have old configuration files
114+
// that rely on the faulty behavior.
115+
if !ok || envVal == "" {
111116
continue
112117
}
113118

cli/clibase/option_test.go

+22
Original file line numberDiff line numberDiff line change
@@ -91,4 +91,26 @@ func TestOptionSet_ParseEnv(t *testing.T) {
9191
require.NoError(t, err)
9292
require.EqualValues(t, "foo", workspaceName)
9393
})
94+
95+
t.Run("EmptyValue", func(t *testing.T) {
96+
t.Parallel()
97+
98+
var workspaceName clibase.String
99+
100+
os := clibase.OptionSet{
101+
clibase.Option{
102+
Name: "Workspace Name",
103+
Value: &workspaceName,
104+
Default: "defname",
105+
Env: "WORKSPACE_NAME",
106+
},
107+
}
108+
109+
err := os.SetDefaults()
110+
require.NoError(t, err)
111+
112+
err = os.ParseEnv("CODER_", []string{"CODER_WORKSPACE_NAME="})
113+
require.NoError(t, err)
114+
require.EqualValues(t, "defname", workspaceName)
115+
})
94116
}

0 commit comments

Comments
 (0)