-
Notifications
You must be signed in to change notification settings - Fork 0
Bundle GitHub Copilot CLI for zero-config installation #14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
Copilot
wants to merge
8
commits into
main
Choose a base branch
from
copilot/bundle-copilot-cli-installation
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
66a0ca8
Initial plan
Copilot ce24f4c
Implement CLI bundling with automatic detection
Copilot 060f493
Add CLI info display and improved error handling
Copilot e19a226
Address code review feedback: rename function, extract CliInfo type, …
Copilot 68215bf
Fix command injection vulnerability by using execFileSync
Copilot 2b2f4d5
Fix remaining command injection vulnerability and improve test clarity
Copilot 128db98
Optimize CLI location caching to avoid redundant file system operations
Copilot eaa681b
Address PR review feedback: preserve error stack, add timeout, fix te…
Copilot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| import { describe, it, expect } from 'vitest'; | ||
| import { locateCopilotCli } from './cli-locator.js'; | ||
|
|
||
| describe('cli-locator', () => { | ||
| it('should return null or valid CLI info', () => { | ||
| const location = locateCopilotCli(); | ||
|
|
||
| // Location may be null if CLI is not available (e.g., CI with --omit=optional) | ||
| if (location) { | ||
| expect(location.path).toBeTruthy(); | ||
| expect(['bundled', 'system']).toContain(location.source); | ||
| expect(location.version).toBeTruthy(); | ||
| } else { | ||
| // If no CLI is found, location should be null | ||
| expect(location).toBeNull(); | ||
| } | ||
| }); | ||
|
|
||
| it('should return valid structure when CLI is found', () => { | ||
| const location = locateCopilotCli(); | ||
|
|
||
| // Only validate structure if a CLI was found | ||
| if (location) { | ||
| expect(location).toHaveProperty('path'); | ||
| expect(location).toHaveProperty('version'); | ||
| expect(location).toHaveProperty('source'); | ||
| expect(location.source).toMatch(/^(bundled|system)$/); | ||
| } | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,158 @@ | ||
| import { existsSync } from 'node:fs'; | ||
| import { join, dirname } from 'node:path'; | ||
| import { fileURLToPath } from 'node:url'; | ||
| import { execFileSync } from 'node:child_process'; | ||
|
|
||
| export interface CliInfo { | ||
| path: string; | ||
| version: string; | ||
| source: 'bundled' | 'system'; | ||
| } | ||
|
|
||
| /** | ||
| * Locate the bundled Copilot CLI binary. | ||
| * Returns the path if found, otherwise null. | ||
| */ | ||
| function findBundledCli(): string | null { | ||
| try { | ||
| // Try to resolve the platform-specific package | ||
| const platform = process.platform; | ||
| const arch = process.arch; | ||
| const packageName = `@github/copilot-${platform}-${arch}`; | ||
|
|
||
| // Attempt to resolve via import.meta.resolve | ||
| try { | ||
| const resolved = import.meta.resolve(packageName); | ||
| const packagePath = fileURLToPath(resolved); | ||
| const binaryDir = dirname(packagePath); | ||
| const binaryName = platform === 'win32' ? 'copilot.exe' : 'copilot'; | ||
| const binaryPath = join(binaryDir, binaryName); | ||
|
|
||
| if (existsSync(binaryPath)) { | ||
| return binaryPath; | ||
| } | ||
| } catch { | ||
| // If import.meta.resolve fails, try manual path construction | ||
| } | ||
|
|
||
| // Fallback: Construct path from this file's location | ||
| // Assuming this file is in src/utils/ and node_modules is at repo root | ||
| const currentFile = fileURLToPath(import.meta.url); | ||
| const repoRoot = join(dirname(currentFile), '..', '..'); | ||
|
|
||
| // Try node_modules location | ||
| const nodeModulesPath = join( | ||
| repoRoot, | ||
| 'node_modules', | ||
| '@github', | ||
| `copilot-${platform}-${arch}`, | ||
| platform === 'win32' ? 'copilot.exe' : 'copilot' | ||
| ); | ||
|
|
||
| if (existsSync(nodeModulesPath)) { | ||
| return nodeModulesPath; | ||
| } | ||
|
|
||
| // Try prebuilds location (legacy structure) | ||
| const prebuildsPath = join( | ||
| repoRoot, | ||
| 'node_modules', | ||
| '@github', | ||
| 'copilot', | ||
| 'prebuilds', | ||
| `${platform}-${arch}`, | ||
| platform === 'win32' ? 'copilot.exe' : 'copilot' | ||
| ); | ||
|
|
||
| if (existsSync(prebuildsPath)) { | ||
| return prebuildsPath; | ||
| } | ||
| } catch { | ||
| // Ignore errors | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
|
|
||
| /** | ||
| * Find the system-installed Copilot CLI. | ||
| * Returns the path if found, otherwise null. | ||
| */ | ||
| function findSystemCli(): string | null { | ||
| try { | ||
| const executable = process.platform === 'win32' ? 'where' : 'which'; | ||
| const result = execFileSync(executable, ['copilot'], { | ||
| encoding: 'utf-8', | ||
| // Prevent PATH lookups from hanging indefinitely | ||
| timeout: 2000, | ||
| }); | ||
| const path = result.trim().split('\n')[0]; | ||
|
|
||
| if (path && existsSync(path)) { | ||
| return path; | ||
| } | ||
| } catch (error) { | ||
| // On timeout or lookup errors, treat as "CLI not in PATH" | ||
| return null; | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
|
|
||
| /** | ||
| * Get the version of a CLI binary. | ||
| */ | ||
| function getCliVersion(cliPath: string): string { | ||
| try { | ||
| const result = execFileSync(cliPath, ['--version'], { | ||
| encoding: 'utf-8', | ||
| timeout: 5000, | ||
| }); | ||
|
|
||
| // Parse version from output (e.g., "GitHub Copilot CLI 0.0.403") | ||
| const match = result.match(/(\d+\.\d+\.\d+)/); | ||
| return match ? match[1] : 'unknown'; | ||
| } catch { | ||
| return 'unknown'; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Locate the Copilot CLI binary, checking bundled first, then system. | ||
| * Returns null if no CLI is found. | ||
| */ | ||
| export function locateCopilotCli(): CliInfo | null { | ||
| // Try bundled CLI first | ||
| const bundledPath = findBundledCli(); | ||
| if (bundledPath) { | ||
| const version = getCliVersion(bundledPath); | ||
| return { path: bundledPath, version, source: 'bundled' }; | ||
| } | ||
|
|
||
| // Fallback to system CLI | ||
| const systemPath = findSystemCli(); | ||
| if (systemPath) { | ||
| const version = getCliVersion(systemPath); | ||
| return { path: systemPath, version, source: 'system' }; | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
|
|
||
| /** | ||
| * Check if the CLI binary is executable. | ||
| * Returns true if the binary can be executed, false otherwise. | ||
| * Note: This does NOT verify authentication status. | ||
| */ | ||
| export function checkCliExecutable(cliPath: string): boolean { | ||
| try { | ||
| // Run a simple command to verify the binary is executable | ||
| execFileSync(cliPath, ['--help'], { | ||
| encoding: 'utf-8', | ||
| timeout: 5000, | ||
| }); | ||
| return true; | ||
| } catch { | ||
| return false; | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The rethrown error from
c.start()drops the original error object/stack, which makes diagnosing CLI startup failures harder. Consider preserving the original error ascause(or otherwise propagating the original stack) while still adding the extra troubleshooting context.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in eaa681b - now using
{ cause: err }to preserve the original error object and stack trace while adding troubleshooting context.