Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions go/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ func NewClient(options *ClientOptions) *Client {
if options.LogLevel != "" {
opts.LogLevel = options.LogLevel
}
if len(options.Env) > 0 {
if options.Env != nil {
opts.Env = options.Env
}
if options.AutoStart != nil {
Expand All @@ -161,6 +161,11 @@ func NewClient(options *ClientOptions) *Client {
}
}

// Default Env to current environment if not set
if opts.Env == nil {
opts.Env = os.Environ()
}

// Check environment variable for CLI path
if cliPath := os.Getenv("COPILOT_CLI_PATH"); cliPath != "" {
opts.CLIPath = cliPath
Expand Down Expand Up @@ -1086,12 +1091,8 @@ func (c *Client) startCLIServer() error {
c.process.Dir = c.options.Cwd
}

// Set environment if specified, adding auth token if needed
if len(c.options.Env) > 0 {
c.process.Env = c.options.Env
} else {
c.process.Env = os.Environ()
}
// Add auth token if needed.
c.process.Env = c.options.Env
if c.options.GithubToken != "" {
c.process.Env = append(c.process.Env, "COPILOT_SDK_AUTH_TOKEN="+c.options.GithubToken)
}
Expand Down
48 changes: 48 additions & 0 deletions go/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package copilot
import (
"os"
"path/filepath"
"reflect"
"regexp"
"testing"
)
Expand Down Expand Up @@ -314,6 +315,53 @@ func TestClient_AuthOptions(t *testing.T) {
})
}

func TestClient_EnvOptions(t *testing.T) {
t.Run("should store custom environment variables", func(t *testing.T) {
client := NewClient(&ClientOptions{
Env: []string{"FOO=bar", "BAZ=qux"},
})

if len(client.options.Env) != 2 {
t.Errorf("Expected 2 environment variables, got %d", len(client.options.Env))
}
if client.options.Env[0] != "FOO=bar" {
t.Errorf("Expected first env var to be 'FOO=bar', got %q", client.options.Env[0])
}
if client.options.Env[1] != "BAZ=qux" {
t.Errorf("Expected second env var to be 'BAZ=qux', got %q", client.options.Env[1])
}
})

t.Run("should default to inherit from current process", func(t *testing.T) {
client := NewClient(&ClientOptions{})

if want := os.Environ(); !reflect.DeepEqual(client.options.Env, want) {
t.Errorf("Expected Env to be %v, got %v", want, client.options.Env)
}
})

t.Run("should default to inherit from current process with nil options", func(t *testing.T) {
client := NewClient(nil)

if want := os.Environ(); !reflect.DeepEqual(client.options.Env, want) {
t.Errorf("Expected Env to be %v, got %v", want, client.options.Env)
}
})

t.Run("should allow empty environment", func(t *testing.T) {
client := NewClient(&ClientOptions{
Env: []string{},
})

if client.options.Env == nil {
t.Error("Expected Env to be non-nil empty slice")
}
if len(client.options.Env) != 0 {
t.Errorf("Expected 0 environment variables, got %d", len(client.options.Env))
}
})
}

func findCLIPathForTest() string {
abs, _ := filepath.Abs("../nodejs/node_modules/@github/copilot/index.js")
if fileExistsForTest(abs) {
Expand Down
6 changes: 5 additions & 1 deletion go/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,11 @@ type ClientOptions struct {
// AutoRestart automatically restarts the CLI server if it crashes (default: true).
// Use Bool(false) to disable.
AutoRestart *bool
// Env is the environment variables for the CLI process (default: inherits from current process)
// Env is the environment variables for the CLI process (default: inherits from current process).
// Each entry is of the form "key=value".
// If Env is nil, the new process uses the current process's environment.
// If Env contains duplicate environment keys, only the last value in the
// slice for each duplicate key is used.
Env []string
// GithubToken is the GitHub token to use for authentication.
// When provided, the token is passed to the CLI server via environment variable.
Expand Down