From 2751ddeb2dd8150a0d7de6c5b65e275e1aa0e155 Mon Sep 17 00:00:00 2001 From: Nate Sesti Date: Thu, 5 Oct 2023 13:26:54 -0700 Subject: [PATCH] feat: :green_heart: ship with binary --- .github/workflows/preview.yaml | 9 + extension/src/activation/environmentSetup.ts | 180 +++++++++++-------- 2 files changed, 114 insertions(+), 75 deletions(-) diff --git a/.github/workflows/preview.yaml b/.github/workflows/preview.yaml index fd1792ee8..374990b74 100644 --- a/.github/workflows/preview.yaml +++ b/.github/workflows/preview.yaml @@ -72,6 +72,15 @@ jobs: run: | chmod 777 dist/run + # 1.5 Place the binary in extension/exe directory + - name: Make sure extension/exe directory exists + run: | + mkdir -p extension/exe + + - name: Copy binary to extension + run: | + cp dist/run extension/exe/run + # 2. Install npm dependencies - name: Use Node.js 19.0.0 uses: actions/setup-node@v3 diff --git a/extension/src/activation/environmentSetup.ts b/extension/src/activation/environmentSetup.ts index 96b3f8eb8..61c97f7db 100644 --- a/extension/src/activation/environmentSetup.ts +++ b/extension/src/activation/environmentSetup.ts @@ -198,22 +198,79 @@ export async function downloadFromS3( }); } -export async function startContinuePythonServer(redownload: boolean = true) { - // Check vscode settings - const manuallyRunningServer = - vscode.workspace - .getConfiguration("continue") - .get("manuallyRunningServer") || false; - const serverUrl = getContinueServerUrl(); - if ( - (serverUrl !== "http://localhost:65432" && - serverUrl !== "http://127.0.0.1:65432") || - manuallyRunningServer - ) { - console.log("Continue server is being run manually, skipping start"); - return; - } +function includedBinaryPath(): string { + const extensionPath = getExtensionUri().fsPath; + return path.join( + extensionPath, + "exe", + `run${os.platform() === "win32" ? ".exe" : ""}` + ); +} +function runExecutable(path: string) { + console.log("---- Starting Continue server ----"); + let attempts = 0; + let maxAttempts = 5; + let delay = 1000; // Delay between each attempt in milliseconds + + const spawnChild = () => { + const retry = (e: any) => { + attempts++; + console.log(`Error caught: ${e}.\n\nRetrying attempt ${attempts}...`); + setTimeout(spawnChild, delay); + }; + try { + // NodeJS bug requires not using detached on Windows, otherwise windowsHide is ineffective + // Otherwise, detach is preferable + const windowsSettings = { + windowsHide: true, + }; + const macLinuxSettings = { + detached: true, + stdio: "ignore", + }; + const settings: any = + os.platform() === "win32" ? windowsSettings : macLinuxSettings; + // Spawn the server + const child = spawn(path, settings); + + // Either unref to avoid zombie process, or listen to events because you can + if (os.platform() === "win32") { + child.stdout.on("data", (data: any) => { + console.log(`stdout: ${data}`); + }); + child.stderr.on("data", (data: any) => { + console.log(`stderr: ${data}`); + }); + child.on("error", (err: any) => { + if (attempts < maxAttempts) { + retry(err); + } else { + console.error("Failed to start subprocess.", err); + } + }); + child.on("exit", (code: any, signal: any) => { + console.log("Subprocess exited with code", code, signal); + }); + child.on("close", (code: any, signal: any) => { + console.log("Subprocess closed with code", code, signal); + }); + } else { + child.unref(); + } + } catch (e: any) { + if (attempts < maxAttempts) { + retry(e); + } else { + throw e; + } + } + }; + + spawnChild(); +} + +async function setupWithS3Download(redownload: boolean, serverUrl: string) { // Check if server is already running if (redownload && (await checkOrKillRunningServer(serverUrl))) { console.log("Continue server already running"); @@ -353,67 +410,40 @@ export async function startContinuePythonServer(redownload: boolean = true) { } // Run the executable - console.log("---- Starting Continue server ----"); - let attempts = 0; - let maxAttempts = 5; - let delay = 1000; // Delay between each attempt in milliseconds - - const spawnChild = () => { - const retry = (e: any) => { - attempts++; - console.log(`Error caught: ${e}.\n\nRetrying attempt ${attempts}...`); - setTimeout(spawnChild, delay); - }; - try { - // NodeJS bug requires not using detached on Windows, otherwise windowsHide is ineffective - // Otherwise, detach is preferable - const windowsSettings = { - windowsHide: true, - }; - const macLinuxSettings = { - detached: true, - stdio: "ignore", - }; - const settings: any = - os.platform() === "win32" ? windowsSettings : macLinuxSettings; - // Spawn the server - const child = spawn(destination, settings); - - // Either unref to avoid zombie process, or listen to events because you can - if (os.platform() === "win32") { - child.stdout.on("data", (data: any) => { - console.log(`stdout: ${data}`); - }); - child.stderr.on("data", (data: any) => { - console.log(`stderr: ${data}`); - }); - child.on("error", (err: any) => { - if (attempts < maxAttempts) { - retry(err); - } else { - console.error("Failed to start subprocess.", err); - } - }); - child.on("exit", (code: any, signal: any) => { - console.log("Subprocess exited with code", code, signal); - }); - child.on("close", (code: any, signal: any) => { - console.log("Subprocess closed with code", code, signal); - }); - } else { - child.unref(); - } - } catch (e: any) { - if (attempts < maxAttempts) { - retry(e); - } else { - throw e; - } - } - }; - - spawnChild(); + runExecutable(destination); // Write the current version of vscode extension to a file called server_version.txt fs.writeFileSync(serverVersionPath(), getExtensionVersion()); } + +export async function startContinuePythonServer(redownload: boolean = true) { + // Check vscode settings for whether server is being run manually + const manuallyRunningServer = + vscode.workspace + .getConfiguration("continue") + .get("manuallyRunningServer") || false; + const serverUrl = getContinueServerUrl(); + if ( + (serverUrl !== "http://localhost:65432" && + serverUrl !== "http://127.0.0.1:65432") || + manuallyRunningServer + ) { + console.log("Continue server is being run manually, skipping start"); + return; + } + + // Check if on Apple Silicon, or if the exe folder is empty - have to download binary from S3 + const isAppleSilicon = os.platform() === "darwin" && os.arch() === "arm64"; + // if (isAppleSilicon) { + // await setupWithS3Download(redownload, serverUrl); + // } + + if (!fs.existsSync(includedBinaryPath())) { + throw new Error( + `Continue server binary not found at ${includedBinaryPath()}` + ); + } + + // Otherwise, use the binary installed with the extension + runExecutable(includedBinaryPath()); +}