From f811f5b7fa0f4be9c77ac630e1a77b9706a50922 Mon Sep 17 00:00:00 2001 From: Aviram Hassan Date: Sun, 10 Dec 2023 12:19:33 +0200 Subject: [PATCH] Fix error when loading into Windows VSCode and improve binary fetching (#89) * Fix error when loading into Windows VSCode and improve binary fetching code * ff * remove public ip check * don't run CI for push other than main * fix merge queue --- .github/workflows/ci.yaml | 6 +- .github/workflows/reusable_e2e.yaml | 8 --- changelog.d/+binary-download.fixed.md | 1 + src/binaryManager.ts | 94 ++++++++++++++------------- src/debugger.ts | 40 ++---------- src/extension.ts | 2 +- 6 files changed, 59 insertions(+), 92 deletions(-) create mode 100644 changelog.d/+binary-download.fixed.md diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 1ca087bd..3ff66977 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -4,9 +4,11 @@ env: on: workflow_dispatch: push: - branches-ignore: [staging-squash-merge.tmp] + branches: + - main + - 'gh-readonly-queue/**' pull_request: - branches: [main, staging, trying] + branches: [main] types: [opened, synchronize, reopened, ready_for_review] # Cancel previous runs on the same PR. diff --git a/.github/workflows/reusable_e2e.yaml b/.github/workflows/reusable_e2e.yaml index c5339d77..c26f8765 100644 --- a/.github/workflows/reusable_e2e.yaml +++ b/.github/workflows/reusable_e2e.yaml @@ -21,14 +21,6 @@ jobs: env: CI_BUILD_PLUGIN: "true" steps: - - name: Public IP - id: ip - uses: haythem/public-ip@v1.3 - - name: Print Public IP - run: | - echo ${{ steps.ip.outputs.ipv4 }} - echo ${{ steps.ip.outputs.ipv6 }} - - name: Remove unnecessary files run: | sudo rm -rf /usr/share/dotnet diff --git a/changelog.d/+binary-download.fixed.md b/changelog.d/+binary-download.fixed.md new file mode 100644 index 00000000..b095a63b --- /dev/null +++ b/changelog.d/+binary-download.fixed.md @@ -0,0 +1 @@ +Fix error when loading into Windows VSCode and improve binary fetching code \ No newline at end of file diff --git a/src/binaryManager.ts b/src/binaryManager.ts index 813b5058..3aa490c4 100644 --- a/src/binaryManager.ts +++ b/src/binaryManager.ts @@ -24,7 +24,7 @@ function getExtensionMirrordPath(): Uri { * @param version If specified, then the version of the binary is checked and matched path is returned. * @returns Path to mirrord binary or null if not found */ -export async function getLocalMirrordBinary(version?: string): Promise { +export async function getLocalMirrordBinary(version: string | null): Promise { try { const mirrordPath = await which("mirrord"); if (version) { @@ -61,7 +61,7 @@ export async function getLocalMirrordBinary(version?: string): Promise { +async function getConfiguredMirrordBinary(background: boolean, latestVersion: string | null): Promise { const configured = workspace.getConfiguration().get("mirrord.binaryPath"); if (!configured) { return null; @@ -80,12 +80,9 @@ async function getConfiguredMirrordBinary(): Promise { return null; } - let latestVersion; - try { - latestVersion = await getLatestSupportedVersion(); - } catch (err) { + if (latestVersion === null) { new NotificationBuilder() - .withMessage(`failed to check latest supported version of mirrord binary, binary specified in settings may be outdated: ${err}`) + .withMessage(`failed to check latest supported version of mirrord binary, binary specified in settings may be outdated`) .warning(); return configured; } @@ -112,17 +109,23 @@ async function getConfiguredMirrordBinary(): Promise { * to mirrord binary auto-update will prompt the user to reload the window. * @returns Path to mirrord binary */ -export async function getMirrordBinary(): Promise { - const configured = await getConfiguredMirrordBinary(); +export async function getMirrordBinary(background: boolean): Promise { + let latestVersion; + let wantedVersion = null; + + try { + latestVersion = await getLatestSupportedVersion(background); + } catch (err) { + latestVersion = null; + } + + const configured = await getConfiguredMirrordBinary(background, latestVersion); if (configured) { vscode.window.showInformationMessage(`Using mirrord binary specified in settings: ${configured}`); return configured; } - let foundLocal = await getLocalMirrordBinary(); - const latestVersion = await getLatestSupportedVersion(); - const autoUpdateConfigured = vscode.workspace.getConfiguration().get("mirrord.autoUpdate"); // values for `mirrord.autoUpdate` can be: @@ -131,56 +134,57 @@ export async function getMirrordBinary(): Promise { // - string: version to download // example: "mirrord.autoUpdate": "3.70.1" or "mirrord.autoUpdate": false or "mirrord.autoUpdate": true - const extensionPath = getExtensionMirrordPath().fsPath; - // check the type can be only null, string or boolean if (typeof autoUpdateConfigured !== 'boolean' && typeof autoUpdateConfigured !== 'string') { vscode.window.showErrorMessage(`Invalid autoUpdate setting ${autoUpdateConfigured}: must be a boolean or a string`); - return extensionPath; + return null; } if (typeof autoUpdateConfigured === 'string') { - if (semver.valid(autoUpdateConfigured)) { - const localMirrordBinary = await getLocalMirrordBinary(autoUpdateConfigured); - if (localMirrordBinary) { - return localMirrordBinary; - } - // donot block and download binary, instead download in background - await downloadMirrordBinary(getExtensionMirrordPath(), autoUpdateConfigured); - return extensionPath; - } else { + if (!semver.valid(autoUpdateConfigured)) { vscode.window.showErrorMessage(`Invalid version format ${autoUpdateConfigured}: must follow semver format`); + return null; } + wantedVersion = autoUpdateConfigured; + } else if (autoUpdateConfigured === true) { + wantedVersion = latestVersion; + } else { + // any version will do + wantedVersion = null; } - if (autoUpdateConfigured === true) { - const localMirrordBinary = await getLocalMirrordBinary(latestVersion); - if (localMirrordBinary) { - return localMirrordBinary; + let foundLocal = await getLocalMirrordBinary(wantedVersion); + + if (foundLocal) { + vscode.window.showInformationMessage(`Using mirrord binary found in path: ${foundLocal} of version ${wantedVersion}`); + return foundLocal; + } + + if (!wantedVersion) { + let anyVersion = await getLocalMirrordBinary(null); + if (anyVersion) { + vscode.window.showInformationMessage(`Version check not available/allowed and no wanted version set. Using mirrord binary found in path: ${anyVersion}`); + return anyVersion; } - await downloadMirrordBinary(getExtensionMirrordPath(), latestVersion); - return extensionPath; + vscode.window.showErrorMessage(`Failed to find mirrord binary in path and failed to check latest supported version of mirrord binary to download`); + return null; + } + + // now wantedVersion is true, meaning we haven't found it yet locally so we have to download it{ + if (background) { + downloadMirrordBinary(getExtensionMirrordPath(), wantedVersion); } else { - if (foundLocal) { - return foundLocal; - } - return extensionPath; + await downloadMirrordBinary(getExtensionMirrordPath(), wantedVersion); } + + return await getLocalMirrordBinary(wantedVersion); } /** * * @returns The latest supported version of mirrord for current extension version */ -async function getLatestSupportedVersion(): Promise { - // commented out logic to avoid checking every X seconds - // uncomment if hits performance or too annoying - // let lastChecked = globalContext.globalState.get('binaryLastChecked', 0); - // let lastBinaryVersion = globalContext.globalState.get('lastBinaryVersion', ''); - - // if (lastBinaryVersion && lastChecked > Date.now() - binaryCheckInterval) { - // return lastBinaryVersion; - // } +async function getLatestSupportedVersion(background: boolean): Promise { let version; // send test for test runs if ((globalContext.extensionMode === ExtensionMode.Development) || (process.env.CI_BUILD_PLUGIN === "true")) { @@ -189,12 +193,10 @@ async function getLatestSupportedVersion(): Promise { version = globalContext.extension.packageJSON.version; } const response = await axios.get(mirrordBinaryEndpoint, { - "params": { "source": 1, "version": version, "platform": platform() }, + "params": { "source": 1, "version": version, "platform": platform(), "background": background }, timeout: 2000, }); - // globalContext.globalState.update('binaryLastChecked', Date.now()); - // globalContext.globalState.update('lastBinaryVersion', response.data); return response.data as string; } diff --git a/src/debugger.ts b/src/debugger.ts index 4b8c3ad7..b2e50b83 100644 --- a/src/debugger.ts +++ b/src/debugger.ts @@ -3,7 +3,7 @@ import { globalContext } from './extension'; import { isTargetSet, MirrordConfigManager } from './config'; import { LAST_TARGET_KEY, MirrordAPI, mirrordFailure, MirrordExecution } from './api'; import { updateTelemetries } from './versionCheck'; -import { getLocalMirrordBinary, getMirrordBinary } from './binaryManager'; +import { getMirrordBinary } from './binaryManager'; import { platform } from 'node:os'; import { NotificationBuilder } from './notification'; @@ -66,24 +66,6 @@ function changeConfigForSip(config: vscode.DebugConfiguration, executableFieldNa } - -async function getLastActiveMirrordPath(): Promise { - const binaryPath = globalContext.globalState.get('binaryPath', null); - if (!binaryPath) { - return null; - } - try { - await vscode.workspace.fs.stat(binaryPath); - return binaryPath; - } catch (e) { - return null; - } -} - -function setLastActiveMirrordPath(path: string) { - globalContext.globalState.update('binaryPath', path); -} - /** * Entrypoint for the vscode extension, called from `resolveDebugConfigurationWithSubstitutedVariables`. */ @@ -110,24 +92,12 @@ async function main( updateTelemetries(); //TODO: add progress bar maybe ? - let cliPath; + let cliPath = await getMirrordBinary(false); - try { - cliPath = await getMirrordBinary(); - } catch (err) { - // Get last active, that should work? - cliPath = await getLastActiveMirrordPath(); - - // Well try any mirrord we can try :\ - if (!cliPath) { - cliPath = await getLocalMirrordBinary(); - if (!cliPath) { - mirrordFailure(`Couldn't download mirrord binaries or find local one in path ${err}.`); - return null; - } - } + if (!cliPath) { + mirrordFailure(`Couldn't download mirrord binaries or find local one in path`); + return null; } - setLastActiveMirrordPath(cliPath); let mirrordApi = new MirrordAPI(cliPath); diff --git a/src/extension.ts b/src/extension.ts index 3fc07442..4a97d3b3 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -14,7 +14,7 @@ export async function activate(context: vscode.ExtensionContext) { vscode.debug.registerDebugConfigurationProvider('*', new ConfigurationProvider(), 2); // start mirrord binary updates, so that we avoid downloading mid session - await getMirrordBinary(); + await getMirrordBinary(true); new MirrordStatus(vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Left, 0)) .register()