Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/build/lib/copilot.ts
13379 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import * as fs from 'fs';
7
import * as path from 'path';
8
9
/**
10
* The platforms that @github/copilot ships platform-specific packages for.
11
* These are the `@github/copilot-{platform}` optional dependency packages.
12
*/
13
export const copilotPlatforms = [
14
'darwin-arm64', 'darwin-x64',
15
'linux-arm64', 'linux-x64',
16
'win32-arm64', 'win32-x64',
17
];
18
19
/**
20
* Converts VS Code build platform/arch to the values that Node.js reports
21
* at runtime via `process.platform` and `process.arch`.
22
*
23
* The copilot SDK's `loadNativeModule` looks up native binaries under
24
* `prebuilds/${process.platform}-${process.arch}/`, so the directory names
25
* must match these runtime values exactly.
26
*/
27
function toNodePlatformArch(platform: string, arch: string): { nodePlatform: string; nodeArch: string } {
28
// alpine is musl-linux; Node still reports process.platform === 'linux'
29
let nodePlatform = platform === 'alpine' ? 'linux' : platform;
30
let nodeArch = arch;
31
32
if (arch === 'armhf') {
33
// VS Code build uses 'armhf'; Node reports process.arch === 'arm'
34
nodeArch = 'arm';
35
} else if (arch === 'alpine') {
36
// Legacy: { platform: 'linux', arch: 'alpine' } means alpine-x64
37
nodePlatform = 'linux';
38
nodeArch = 'x64';
39
}
40
41
return { nodePlatform, nodeArch };
42
}
43
44
/**
45
* Returns a glob filter that strips @github/copilot platform packages
46
* for architectures other than the build target.
47
*
48
* For platforms the copilot SDK doesn't natively support (e.g. alpine, armhf),
49
* ALL platform packages are stripped - that's fine because the copilot CLI SDK
50
* resolves `node-pty` from the embedder (VS Code) first via `hostRequire`,
51
* falling back to its bundled copy only if the embedder can't provide it.
52
*/
53
export function getCopilotExcludeFilter(platform: string, arch: string): string[] {
54
const { nodePlatform, nodeArch } = toNodePlatformArch(platform, arch);
55
const targetPlatformArch = `${nodePlatform}-${nodeArch}`;
56
const nonTargetPlatforms = copilotPlatforms.filter(p => p !== targetPlatformArch);
57
58
// Strip wrong-architecture @github/copilot-{platform} packages.
59
// All copilot prebuilds are stripped by .moduleignore; the copilot CLI SDK
60
// resolves `node-pty` from VS Code's own node_modules via `hostRequire`.
61
const excludes = nonTargetPlatforms.map(p => `!**/node_modules/@github/copilot-${p}/**`);
62
63
return ['**', ...excludes];
64
}
65
66
/**
67
* Materializes the copilot CLI ripgrep shim directly inside the built-in copilot extension.
68
*
69
* This is used when copilot is shipped as a built-in extension so startup does
70
* not need to create the shim at runtime. The destination layout matches the
71
* runtime shim logic in the copilot extension:
72
* - ripgrep: node_modules/@github/copilot/sdk/ripgrep/bin/{platform-arch}
73
* - marker: node_modules/@github/copilot/shims.txt
74
*
75
* Note: `node-pty` is no longer shimmed. The copilot CLI SDK resolves
76
* `node-pty` from the embedder (VS Code) via `hostRequire` and falls back to
77
* its bundled copy only if that fails.
78
*
79
* Failures throw to fail the build because built-in packaging must guarantee
80
* this artifact is present.
81
*/
82
export function prepareBuiltInCopilotRipgrepShim(platform: string, arch: string, builtInCopilotExtensionDir: string, appNodeModulesDir: string): void {
83
const { nodePlatform, nodeArch } = toNodePlatformArch(platform, arch);
84
const platformArch = `${nodePlatform}-${nodeArch}`;
85
86
const extensionNodeModules = path.join(builtInCopilotExtensionDir, 'node_modules');
87
const copilotBase = path.join(extensionNodeModules, '@github', 'copilot');
88
const copilotSdkBase = path.join(copilotBase, 'sdk');
89
if (!fs.existsSync(copilotSdkBase)) {
90
throw new Error(`[prepareBuiltInCopilotRipgrepShim] Copilot SDK directory not found at ${copilotSdkBase}`);
91
}
92
93
const ripgrepSource = path.join(appNodeModulesDir, '@vscode', 'ripgrep', 'bin');
94
if (!fs.existsSync(ripgrepSource)) {
95
throw new Error(`[prepareBuiltInCopilotRipgrepShim] ripgrep source not found at ${ripgrepSource}`);
96
}
97
98
const ripgrepDest = path.join(copilotSdkBase, 'ripgrep', 'bin', platformArch);
99
const shimMarkerPath = path.join(copilotBase, 'shims.txt');
100
101
try {
102
fs.mkdirSync(ripgrepDest, { recursive: true });
103
fs.cpSync(ripgrepSource, ripgrepDest, { recursive: true });
104
105
fs.writeFileSync(shimMarkerPath, 'Shims created successfully');
106
console.log(`[prepareBuiltInCopilotRipgrepShim] Materialized ripgrep shim for ${platformArch} in ${builtInCopilotExtensionDir}`);
107
} catch (err) {
108
throw new Error(`[prepareBuiltInCopilotRipgrepShim] Failed to materialize ripgrep shim for ${platformArch}: ${err}`);
109
}
110
}
111
112