Skip to content

Commit 6e4daa9

Browse files
authored
[go] Honor empty ClientOptions.Env (#297)
* honor empty ClientOptions.Env * fic nil options
1 parent 82dbb15 commit 6e4daa9

3 files changed

Lines changed: 61 additions & 8 deletions

File tree

go/client.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ func NewClient(options *ClientOptions) *Client {
145145
if options.LogLevel != "" {
146146
opts.LogLevel = options.LogLevel
147147
}
148-
if len(options.Env) > 0 {
148+
if options.Env != nil {
149149
opts.Env = options.Env
150150
}
151151
if options.UseStdio != nil {
@@ -165,6 +165,11 @@ func NewClient(options *ClientOptions) *Client {
165165
}
166166
}
167167

168+
// Default Env to current environment if not set
169+
if opts.Env == nil {
170+
opts.Env = os.Environ()
171+
}
172+
168173
// Check environment variable for CLI path
169174
if cliPath := os.Getenv("COPILOT_CLI_PATH"); cliPath != "" {
170175
opts.CLIPath = cliPath
@@ -1090,12 +1095,8 @@ func (c *Client) startCLIServer() error {
10901095
c.process.Dir = c.options.Cwd
10911096
}
10921097

1093-
// Set environment if specified, adding auth token if needed
1094-
if len(c.options.Env) > 0 {
1095-
c.process.Env = c.options.Env
1096-
} else {
1097-
c.process.Env = os.Environ()
1098-
}
1098+
// Add auth token if needed.
1099+
c.process.Env = c.options.Env
10991100
if c.options.GithubToken != "" {
11001101
c.process.Env = append(c.process.Env, "COPILOT_SDK_AUTH_TOKEN="+c.options.GithubToken)
11011102
}

go/client_test.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package copilot
33
import (
44
"os"
55
"path/filepath"
6+
"reflect"
67
"regexp"
78
"testing"
89
)
@@ -334,6 +335,53 @@ func TestClient_AuthOptions(t *testing.T) {
334335
})
335336
}
336337

338+
func TestClient_EnvOptions(t *testing.T) {
339+
t.Run("should store custom environment variables", func(t *testing.T) {
340+
client := NewClient(&ClientOptions{
341+
Env: []string{"FOO=bar", "BAZ=qux"},
342+
})
343+
344+
if len(client.options.Env) != 2 {
345+
t.Errorf("Expected 2 environment variables, got %d", len(client.options.Env))
346+
}
347+
if client.options.Env[0] != "FOO=bar" {
348+
t.Errorf("Expected first env var to be 'FOO=bar', got %q", client.options.Env[0])
349+
}
350+
if client.options.Env[1] != "BAZ=qux" {
351+
t.Errorf("Expected second env var to be 'BAZ=qux', got %q", client.options.Env[1])
352+
}
353+
})
354+
355+
t.Run("should default to inherit from current process", func(t *testing.T) {
356+
client := NewClient(&ClientOptions{})
357+
358+
if want := os.Environ(); !reflect.DeepEqual(client.options.Env, want) {
359+
t.Errorf("Expected Env to be %v, got %v", want, client.options.Env)
360+
}
361+
})
362+
363+
t.Run("should default to inherit from current process with nil options", func(t *testing.T) {
364+
client := NewClient(nil)
365+
366+
if want := os.Environ(); !reflect.DeepEqual(client.options.Env, want) {
367+
t.Errorf("Expected Env to be %v, got %v", want, client.options.Env)
368+
}
369+
})
370+
371+
t.Run("should allow empty environment", func(t *testing.T) {
372+
client := NewClient(&ClientOptions{
373+
Env: []string{},
374+
})
375+
376+
if client.options.Env == nil {
377+
t.Error("Expected Env to be non-nil empty slice")
378+
}
379+
if len(client.options.Env) != 0 {
380+
t.Errorf("Expected 0 environment variables, got %d", len(client.options.Env))
381+
}
382+
})
383+
}
384+
337385
func findCLIPathForTest() string {
338386
abs, _ := filepath.Abs("../nodejs/node_modules/@github/copilot/index.js")
339387
if fileExistsForTest(abs) {

go/types.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,11 @@ type ClientOptions struct {
3434
// AutoRestart automatically restarts the CLI server if it crashes (default: true).
3535
// Use Bool(false) to disable.
3636
AutoRestart *bool
37-
// Env is the environment variables for the CLI process (default: inherits from current process)
37+
// Env is the environment variables for the CLI process (default: inherits from current process).
38+
// Each entry is of the form "key=value".
39+
// If Env is nil, the new process uses the current process's environment.
40+
// If Env contains duplicate environment keys, only the last value in the
41+
// slice for each duplicate key is used.
3842
Env []string
3943
// GithubToken is the GitHub token to use for authentication.
4044
// When provided, the token is passed to the CLI server via environment variable.

0 commit comments

Comments
 (0)