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..2f2b56fc 100644 --- a/src/debugger.ts +++ b/src/debugger.ts @@ -113,7 +113,7 @@ async function main( let cliPath; try { - cliPath = await getMirrordBinary(); + cliPath = await getMirrordBinary(false); } catch (err) { // Get last active, that should work? cliPath = await getLastActiveMirrordPath(); 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()