Skip to content

Commit c3d7b38

Browse files
Make all SDKs consistent in support for COPILOT_CLI_PATH
1 parent 16a121c commit c3d7b38

5 files changed

Lines changed: 38 additions & 15 deletions

File tree

dotnet/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ new CopilotClient(CopilotClientOptions? options = null)
6767

6868
**Options:**
6969

70-
- `CliPath` - Path to CLI executable (default: "copilot" from PATH)
70+
- `CliPath` - Path to CLI executable (default: `COPILOT_CLI_PATH` env var, or bundled CLI)
7171
- `CliArgs` - Extra arguments prepended before SDK-managed flags
7272
- `CliUrl` - URL of existing CLI server to connect to (e.g., `"localhost:8080"`). When provided, the client will not spawn a CLI process.
7373
- `Port` - Server port (default: 0 for random)

dotnet/src/Client.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1064,8 +1064,12 @@ private async Task VerifyProtocolVersionAsync(Connection connection, Cancellatio
10641064

10651065
private static async Task<(Process Process, int? DetectedLocalhostTcpPort, StringBuilder StderrBuffer)> StartCliServerAsync(CopilotClientOptions options, ILogger logger, CancellationToken cancellationToken)
10661066
{
1067-
// Use explicit path or bundled CLI - no PATH fallback
1068-
var cliPath = options.CliPath ?? GetBundledCliPath(out var searchedPath)
1067+
// Use explicit path, COPILOT_CLI_PATH env var (from options.Environment or process env), or bundled CLI - no PATH fallback
1068+
var envCliPath = options.Environment is not null && options.Environment.TryGetValue("COPILOT_CLI_PATH", out var envValue) ? envValue
1069+
: System.Environment.GetEnvironmentVariable("COPILOT_CLI_PATH");
1070+
var cliPath = options.CliPath
1071+
?? envCliPath
1072+
?? GetBundledCliPath(out var searchedPath)
10691073
?? throw new InvalidOperationException($"Copilot CLI not found at '{searchedPath}'. Ensure the SDK NuGet package was restored correctly or provide an explicit CliPath.");
10701074
var args = new List<string>();
10711075

go/client.go

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -199,15 +199,29 @@ func NewClient(options *ClientOptions) *Client {
199199
opts.Env = os.Environ()
200200
}
201201

202-
// Check environment variable for CLI path
203-
if cliPath := os.Getenv("COPILOT_CLI_PATH"); cliPath != "" {
204-
opts.CLIPath = cliPath
202+
// Check effective environment for CLI path (only if not explicitly set via options)
203+
if opts.CLIPath == "" {
204+
if cliPath := getEnvValue(opts.Env, "COPILOT_CLI_PATH"); cliPath != "" {
205+
opts.CLIPath = cliPath
206+
}
205207
}
206208

207209
client.options = opts
208210
return client
209211
}
210212

213+
// getEnvValue looks up a key in an environment slice ([]string of "KEY=VALUE").
214+
// Returns the value if found, or empty string otherwise.
215+
func getEnvValue(env []string, key string) string {
216+
prefix := key + "="
217+
for i := len(env) - 1; i >= 0; i-- {
218+
if strings.HasPrefix(env[i], prefix) {
219+
return env[i][len(prefix):]
220+
}
221+
}
222+
return ""
223+
}
224+
211225
// parseCliUrl parses a CLI URL into host and port components.
212226
//
213227
// Supports formats: "host:port", "http://host:port", "https://host:port", or just "port".

python/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ CopilotClient(
125125

126126
**SubprocessConfig** — spawn a local CLI process:
127127

128-
- `cli_path` (str | None): Path to CLI executable (default: bundled binary)
128+
- `cli_path` (str | None): Path to CLI executable (default: `COPILOT_CLI_PATH` env var, or bundled binary)
129129
- `cli_args` (list[str]): Extra arguments for the CLI executable
130130
- `cwd` (str | None): Working directory for CLI process (default: current dir)
131131
- `use_stdio` (bool): Use stdio transport instead of TCP (default: True)

python/copilot/client.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -794,16 +794,21 @@ def __init__(
794794
else:
795795
self._actual_port = None
796796

797-
# Resolve CLI path: explicit > bundled binary
797+
# Resolve CLI path: explicit > COPILOT_CLI_PATH env var > bundled binary
798+
effective_env = config.env if config.env is not None else os.environ
798799
if config.cli_path is None:
799-
bundled_path = _get_bundled_cli_path()
800-
if bundled_path:
801-
config.cli_path = bundled_path
800+
env_cli_path = effective_env.get("COPILOT_CLI_PATH")
801+
if env_cli_path:
802+
config.cli_path = env_cli_path
802803
else:
803-
raise RuntimeError(
804-
"Copilot CLI not found. The bundled CLI binary is not available. "
805-
"Ensure you installed a platform-specific wheel, or provide cli_path."
806-
)
804+
bundled_path = _get_bundled_cli_path()
805+
if bundled_path:
806+
config.cli_path = bundled_path
807+
else:
808+
raise RuntimeError(
809+
"Copilot CLI not found. The bundled CLI binary is not available. "
810+
"Ensure you installed a platform-specific wheel, or provide cli_path."
811+
)
807812

808813
# Resolve use_logged_in_user default
809814
if config.use_logged_in_user is None:

0 commit comments

Comments
 (0)