Skip to content

Commit fd06b7f

Browse files
authored
fix: allow all environment variables to fallback prefix to HOMEBREW_ (#10050)
See the customer use-case in the code docs.
1 parent 252ec14 commit fd06b7f

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

cli/clibase/option.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,15 @@ func (optSet *OptionSet) ParseEnv(vs []EnvVar) error {
262262
}
263263

264264
envVal, ok := envs[opt.Env]
265+
if !ok {
266+
// Homebrew strips all environment variables that do not start with `HOMEBREW_`.
267+
// This prevented using brew to invoke the Coder agent, because the environment
268+
// variables to not get passed down.
269+
//
270+
// A customer wanted to use their custom tap inside a workspace, which was failing
271+
// because the agent lacked the environment variables to authenticate with Git.
272+
envVal, ok = envs[`HOMEBREW_`+opt.Env]
273+
}
265274
// Currently, empty values are treated as if the environment variable is
266275
// unset. This behavior is technically not correct as there is now no
267276
// way for a user to change a Default value to an empty string from

cli/clibase/option_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,26 @@ func TestOptionSet_ParseEnv(t *testing.T) {
206206
require.NoError(t, err)
207207
require.EqualValues(t, expected, actual.Value)
208208
})
209+
210+
t.Run("Homebrew", func(t *testing.T) {
211+
t.Parallel()
212+
213+
var agentToken clibase.String
214+
215+
os := clibase.OptionSet{
216+
clibase.Option{
217+
Name: "Agent Token",
218+
Value: &agentToken,
219+
Env: "AGENT_TOKEN",
220+
},
221+
}
222+
223+
err := os.ParseEnv([]clibase.EnvVar{
224+
{Name: "HOMEBREW_AGENT_TOKEN", Value: "foo"},
225+
})
226+
require.NoError(t, err)
227+
require.EqualValues(t, "foo", agentToken)
228+
})
209229
}
210230

211231
func TestOptionSet_JsonMarshal(t *testing.T) {

0 commit comments

Comments
 (0)