Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Bundling for Python
  • Loading branch information
SteveSandersonMS committed Feb 5, 2026
commit 059c53bcb438c27c69a78442be12ec5bf11574dc
10 changes: 8 additions & 2 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -153,12 +153,18 @@ jobs:
- uses: actions/setup-python@v6
with:
python-version: "3.12"
- uses: actions/setup-node@v6
with:
node-version: "22.x"
- name: Set up uv
uses: astral-sh/setup-uv@v7
- name: Install Node.js dependencies (for CLI version)
working-directory: ./nodejs
run: npm ci --ignore-scripts
- name: Set version
run: sed -i "s/^version = .*/version = \"${{ needs.version.outputs.version }}\"/" pyproject.toml
- name: Build package
run: uv build
- name: Build platform wheels
run: node scripts/build-wheels.mjs --output-dir dist
- name: Upload artifact
uses: actions/upload-artifact@v6
with:
Expand Down
12 changes: 8 additions & 4 deletions .github/workflows/python-sdk-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ on:
- 'test/**'
- 'nodejs/package.json'
- '.github/workflows/python-sdk-tests.yml'
- '.github/actions/setup-copilot/**'
- '!**/*.md'
- '!**/LICENSE*'
- '!**/.gitignore'
Expand Down Expand Up @@ -42,11 +41,13 @@ jobs:
working-directory: ./python
steps:
- uses: actions/checkout@v6.0.2
- uses: ./.github/actions/setup-copilot
id: setup-copilot
- uses: actions/setup-python@v6
with:
python-version: "3.12"
- uses: actions/setup-node@v6
with:
cache: "npm"
cache-dependency-path: "./nodejs/package-lock.json"

- name: Set up uv
uses: astral-sh/setup-uv@v7
Expand All @@ -56,6 +57,10 @@ jobs:
- name: Install Python dev dependencies
run: uv sync --locked --all-extras --dev

- name: Install Node.js dependencies (for CLI in tests)
working-directory: ./nodejs
run: npm ci --ignore-scripts

- name: Run ruff format check
run: uv run ruff format --check .

Expand All @@ -76,5 +81,4 @@ jobs:
- name: Run Python SDK tests
env:
COPILOT_HMAC_KEY: ${{ secrets.COPILOT_DEVELOPER_CLI_INTEGRATION_HMAC_KEY }}
COPILOT_CLI_PATH: ${{ steps.setup-copilot.outputs.cli-path }}
run: uv run pytest -v -s
7 changes: 7 additions & 0 deletions python/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -162,3 +162,10 @@ cython_debug/
# Ruff and ty cache
.ruff_cache/
.ty_cache/

# Build script caches
.cli-cache/
.build-temp/

# Bundled CLI binary (only in platform wheels, not in repo)
copilot/bin/
48 changes: 38 additions & 10 deletions python/copilot/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@
import inspect
import os
import re
import shutil
import subprocess
import sys
import threading
from dataclasses import asdict, is_dataclass
from pathlib import Path
from typing import Any, Callable, Optional, cast

from .generated.session_events import session_event_from_dict
Expand Down Expand Up @@ -48,6 +49,26 @@
)


def _get_bundled_cli_path() -> Optional[str]:
"""Get the path to the bundled CLI binary, if available."""
# The binary is bundled in copilot/bin/ within the package
bin_dir = Path(__file__).parent / "bin"
if not bin_dir.exists():
return None

# Determine binary name based on platform
if sys.platform == "win32":
binary_name = "copilot.exe"
else:
binary_name = "copilot"

binary_path = bin_dir / binary_name
if binary_path.exists():
return str(binary_path)

return None


class CopilotClient:
"""
Main client for interacting with the Copilot CLI.
Expand Down Expand Up @@ -130,8 +151,18 @@ def __init__(self, options: Optional[CopilotClientOptions] = None):
else:
self._actual_port = None

# Check environment variable for CLI path
default_cli_path = os.environ.get("COPILOT_CLI_PATH", "copilot")
# Determine CLI path: explicit option > bundled binary
if opts.get("cli_path"):
default_cli_path = opts["cli_path"]
else:
bundled_path = _get_bundled_cli_path()
if bundled_path:
default_cli_path = bundled_path
else:
raise RuntimeError(
"Copilot CLI not found. The bundled CLI binary is not available. "
"Ensure you installed a platform-specific wheel, or provide cli_path."
)

# Default use_logged_in_user to False when github_token is provided
github_token = opts.get("github_token")
Expand All @@ -140,7 +171,7 @@ def __init__(self, options: Optional[CopilotClientOptions] = None):
use_logged_in_user = False if github_token else True

self.options: CopilotClientOptions = {
"cli_path": opts.get("cli_path", default_cli_path),
"cli_path": default_cli_path,
"cwd": opts.get("cwd", os.getcwd()),
"port": opts.get("port", 0),
"use_stdio": False if opts.get("cli_url") else opts.get("use_stdio", True),
Expand Down Expand Up @@ -1037,12 +1068,9 @@ async def _start_cli_server(self) -> None:
"""
cli_path = self.options["cli_path"]

# Resolve the full path on Windows (handles .cmd/.bat files)
# On Windows, subprocess.Popen doesn't use PATHEXT to resolve extensions,
# so we need to use shutil.which() to find the actual executable
resolved_path = shutil.which(cli_path)
if resolved_path:
cli_path = resolved_path
# Verify CLI exists
if not os.path.exists(cli_path):
raise RuntimeError(f"Copilot CLI not found at {cli_path}")

args = ["--headless", "--log-level", self.options["log_level"]]

Expand Down
22 changes: 6 additions & 16 deletions python/e2e/testharness/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,20 @@
from .proxy import CapiProxy


def get_cli_path() -> str:
"""Get CLI path from environment or try to find it. Raises if not found."""
# Check environment variable first
cli_path = os.environ.get("COPILOT_CLI_PATH")
if cli_path and os.path.exists(cli_path):
return cli_path

def get_cli_path_for_tests() -> str:
"""Get CLI path for E2E tests. Uses node_modules CLI during development."""
# Look for CLI in sibling nodejs directory's node_modules
base_path = Path(__file__).parents[3] # equivalent to: path.parent.parent.parent.parent
base_path = Path(__file__).parents[3]
full_path = base_path / "nodejs" / "node_modules" / "@github" / "copilot" / "index.js"
if full_path.exists():
return str(full_path.resolve())

raise RuntimeError(
"CLI not found. Set COPILOT_CLI_PATH or run 'npm install' in the nodejs directory."
"CLI not found for tests. Run 'npm install' in the nodejs directory."
)


CLI_PATH = get_cli_path()
CLI_PATH = get_cli_path_for_tests()
SNAPSHOTS_DIR = Path(__file__).parents[3] / "test" / "snapshots"


Expand All @@ -51,12 +46,7 @@ def __init__(self):

async def setup(self):
"""Set up the test context with a shared client."""
cli_path = get_cli_path()
if not cli_path or not os.path.exists(cli_path):
raise RuntimeError(
f"CLI not found at {cli_path}. Run 'npm install' in the nodejs directory first."
)
self.cli_path = cli_path
self.cli_path = get_cli_path_for_tests()

self.home_dir = tempfile.mkdtemp(prefix="copilot-test-config-")
self.work_dir = tempfile.mkdtemp(prefix="copilot-test-work-")
Expand Down
Loading
Loading