Skip to content

Commit

Permalink
Fix error when loading into Windows VSCode and improve binary fetching (
Browse files Browse the repository at this point in the history
metalbear-co#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
  • Loading branch information
aviramha authored Dec 10, 2023
1 parent 66caadc commit f811f5b
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 92 deletions.
6 changes: 4 additions & 2 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
8 changes: 0 additions & 8 deletions .github/workflows/reusable_e2e.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,6 @@ jobs:
env:
CI_BUILD_PLUGIN: "true"
steps:
- name: Public IP
id: ip
uses: haythem/[email protected]
- 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
Expand Down
1 change: 1 addition & 0 deletions changelog.d/+binary-download.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix error when loading into Windows VSCode and improve binary fetching code
94 changes: 48 additions & 46 deletions src/binaryManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string | null> {
export async function getLocalMirrordBinary(version: string | null): Promise<string | null> {
try {
const mirrordPath = await which("mirrord");
if (version) {
Expand Down Expand Up @@ -61,7 +61,7 @@ export async function getLocalMirrordBinary(version?: string): Promise<string |
return null;
}

async function getConfiguredMirrordBinary(): Promise<string | null> {
async function getConfiguredMirrordBinary(background: boolean, latestVersion: string | null): Promise<string | null> {
const configured = workspace.getConfiguration().get<string | null>("mirrord.binaryPath");
if (!configured) {
return null;
Expand All @@ -80,12 +80,9 @@ async function getConfiguredMirrordBinary(): Promise<string | null> {
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;
}
Expand All @@ -112,17 +109,23 @@ async function getConfiguredMirrordBinary(): Promise<string | null> {
* to mirrord binary auto-update will prompt the user to reload the window.
* @returns Path to mirrord binary
*/
export async function getMirrordBinary(): Promise<string> {
const configured = await getConfiguredMirrordBinary();
export async function getMirrordBinary(background: boolean): Promise<string | null> {
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:
Expand All @@ -131,56 +134,57 @@ export async function getMirrordBinary(): Promise<string> {
// - 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<string> {
// 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<string> {
let version;
// send test for test runs
if ((globalContext.extensionMode === ExtensionMode.Development) || (process.env.CI_BUILD_PLUGIN === "true")) {
Expand All @@ -189,12 +193,10 @@ async function getLatestSupportedVersion(): Promise<string> {
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;
}

Expand Down
40 changes: 5 additions & 35 deletions src/debugger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -66,24 +66,6 @@ function changeConfigForSip(config: vscode.DebugConfiguration, executableFieldNa
}



async function getLastActiveMirrordPath(): Promise<string | null> {
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`.
*/
Expand All @@ -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);

Expand Down
2 changes: 1 addition & 1 deletion src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit f811f5b

Please sign in to comment.